= Azure ServiceBus Component
:doctitle: Azure ServiceBus
:shortname: azure-servicebus
:artifactid: camel-azure-servicebus
:description: Send and receive messages to/from Azure Service Bus.
:since: 3.12
:supportlevel: Stable
:tabs-sync-option:
:component-header: Both producer and consumer are supported
//Manually maintained attributes
:group: Azure
:camel-spring-boot-name: azure-servicebus

*Since Camel {since}*

*{component-header}*

The azure-servicebus component that integrates https://docs.microsoft.com/en-us/azure/service-bus-messaging/service-bus-messaging-overview[Azure ServiceBus]. Azure ServiceBus is a fully managed enterprise
integration message broker. Service Bus can decouple applications and services. Service Bus offers a reliable and secure platform for asynchronous transfer of data and state. Data is transferred between different applications and
 services using messages.

Prerequisites

You must have a valid Windows Azure Storage account. More information is available at
https://docs.microsoft.com/azure/[Azure Documentation Portal].

[source,xml]
------------------------------------------------------------
<dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-azure-servicebus</artifactId>
    <version>x.x.x</version>
    <!-- use the same version as your Camel core version -->
</dependency>
------------------------------------------------------------


// component-configure options: START

// component-configure options: END

// component options: START
include::partial$component-configure-options.adoc[]
include::partial$component-endpoint-options.adoc[]
// component options: END

// endpoint options: START

// endpoint options: END

// component headers: START
include::partial$component-endpoint-headers.adoc[]
// component headers: END

== Usage

=== Consumer and Producer

This component implements the Consumer and Producer.

=== Authentication Information

You have three different Credential Types: AZURE_IDENTITY, TOKEN_CREDENTIAL and CONNECTION_STRING. You can also provide a client instance yourself.
To use this component, you have three options to provide the required Azure authentication information:

*CONNECTION_STRING*:

- Provide `connectionString` string it is the simplest option to get started.

*TOKEN_CREDENTIAL*:

- Provide an implementation of `com.azure.core.credential.TokenCredential` into the Camel's Registry, e.g., using the `com.azure.identity.DefaultAzureCredentialBuilder().build();` API.
See the documentation https://docs.microsoft.com/en-us/azure/active-directory/authentication/overview-authentication[here about Azure-AD authentication].

*AZURE_IDENTITY*:

- This will use `com.azure.identity.DefaultAzureCredentialBuilder().build();` instance. This will follow the Default Azure Credential Chain.
See the documentation https://docs.microsoft.com/en-us/azure/active-directory/authentication/overview-authentication[here about Azure-AD authentication].

*Client instance*:

- You can provide a `com.azure.messaging.servicebus.ServiceBusSenderClient` for sending message and/or `com.azure.messaging.servicebus.ServiceBusReceiverClient` to receive messages. If you provide the instances, they will be autowired.

=== Message Body
In the producer, this component accepts message body of `String`, `byte[]` and `BinaryData` types or `List<String>`, `List<byte[]>` and `List<BinaryData>` to send batch messages.

In the consumer, the returned message body will be of type `String.


=== Azure ServiceBus Producer operations
[width="100%",cols="10%,90%",options="header",]
|===
|Operation |Description
|`sendMessages`  | Sends a set of messages to a Service Bus queue or topic using a batched approach.
|`scheduleMessages`  | Sends a scheduled message to the Azure Service Bus entity this sender is connected to. A scheduled message is enqueued and made available to receivers only at the scheduled enqueue time.
|===


=== Azure ServiceBus Consumer operations
[width="100%",cols="10%,90%",options="header",]
|===
|Operation |Description
|`receiveMessages`  | Receives an <b>infinite</b> stream of messages from the Service Bus entity.
|`peekMessages`  | Reads the next batch of active messages without changing the state of the receiver or the message source.
|===


== Examples

- `sendMessages`

[source,java]
--------------------------------------------------------------------------------
from("direct:start")
  .process(exchange -> {
         final List<Object> inputBatch = new LinkedList<>();
            inputBatch.add("test batch 1");
            inputBatch.add("test batch 2");
            inputBatch.add("test batch 3");
            inputBatch.add(123456);

            exchange.getIn().setBody(inputBatch);
       })
  .to("azure-servicebus:test//?connectionString=test")
  .to("mock:result");
--------------------------------------------------------------------------------


- `scheduleMessages`

[source,java]
--------------------------------------------------------------------------------
from("direct:start")
  .process(exchange -> {
         final List<Object> inputBatch = new LinkedList<>();
            inputBatch.add("test batch 1");
            inputBatch.add("test batch 2");
            inputBatch.add("test batch 3");
            inputBatch.add(123456);

            exchange.getIn().setHeader(ServiceBusConstants.SCHEDULED_ENQUEUE_TIME, OffsetDateTime.now());
            exchange.getIn().setBody(inputBatch);
       })
  .to("azure-servicebus:test//?connectionString=test&producerOperation=scheduleMessages")
  .to("mock:result");
--------------------------------------------------------------------------------

- `receiveMessages`

[source,java]
--------------------------------------------------------------------------------
from("azure-servicebus:test//?connectionString=test")
  .log("${body}")
  .to("mock:result");
--------------------------------------------------------------------------------



include::spring-boot:partial$starter.adoc[]
