= Azure ServiceBus Component
:doctitle: Azure ServiceBus
:shortname: azure-servicebus
:artifactid: camel-azure-servicebus
:description: The azure-servicebus component that integrates 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.
:since: 3.12
:supportlevel: Stable
: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


== Async Consumer and Producer

This component implements the async Consumer and producer.

This allows camel route to consume and produce events asynchronously without blocking any threads.

== Usage

=== Message headers evaluated by the component producer
[width="100%",cols="10%,10%,10%,70%",options="header",]
|=======================================================================
|Header |Variable Name |Type |Description

|`CamelAzureServiceBusScheduledEnqueueTime`| `ServiceBusConstants.SCHEDULED_ENQUEUE_TIME`|`OffsetDateTime`| Overrides the OffsetDateTime at which the message should appear in the Service Bus queue or topic.
|`CamelAzureServiceBusServiceBusTransactionContext`| `ServiceBusConstants.SERVICE_BUS_TRANSACTION_CONTEXT`|`ServiceBusTransactionContext`| Overrides the transaction in service. This object just contains transaction id.
|`CamelAzureServiceBusProducerOperation`| `ServiceBusConstants.PRODUCER_OPERATION`|`ServiceBusProducerOperationDefinition`| Overrides the desired operation to be used in the producer.
|=======================================================================


=== Message headers set by the component consumer
[width="100%",cols="10%,10%,10%,70%",options="header",]
|=======================================================================
|Header |Variable Name |Type |Description

|`CamelAzureServiceBusApplicationProperties`| `ServiceBusConstants.APPLICATION_PROPERTIES`|`Map<String, Object>`| Gets the set of free-form {@link ServiceBusReceivedMessage} properties which may be used for passing metadata  associated with the {@link ServiceBusReceivedMessage} during Service Bus operations.
|`CamelAzureServiceBusContentType`| `ServiceBusConstants.CONTENT_TYPE`|`String`| Gets the content type of the message.
|`CamelAzureServiceBusMessageId`| `ServiceBusConstants.MESSAGE_ID`|`String`| Gets the identifier for the message.
|`CamelAzureServiceBusCorrelationId`| `ServiceBusConstants.CORRELATION_ID`|`String`| Gets a correlation identifier.
|`CamelAzureServiceBusDeadLetterErrorDescription`| `ServiceBusConstants.DEAD_LETTER_ERROR_DESCRIPTION`|`String`| Gets the description for a message that has been dead-lettered.
|`CamelAzureServiceBusDeadLetterReason`| `ServiceBusConstants.DEAD_LETTER_REASON`|`String`| Gets the reason a message was dead-lettered.
|`CamelAzureServiceBusDeadLetterSource`| `ServiceBusConstants.DEAD_LETTER_SOURCE`|`String`| Gets the name of the queue or subscription that this message was enqueued on, before it was dead-lettered.
|`CamelAzureServiceBusDeliveryCount`| `ServiceBusConstants.DELIVERY_COUNT`|`long`| Gets the number of the times this message was delivered to clients.
|`CamelAzureServiceBusScheduledEnqueueTime`| `ServiceBusConstants.SCHEDULED_ENQUEUE_TIME`|`OffsetDateTime`| Gets the scheduled enqueue time of this message.
|`CamelAzureServiceBusEnqueuedSequenceNumber`| `ServiceBusConstants.ENQUEUED_SEQUENCE_NUMBER`|`long`| Gets the enqueued sequence number assigned to a message by Service Bus.
|`CamelAzureServiceBusEnqueuedTime`| `ServiceBusConstants.ENQUEUED_TIME`|`OffsetDateTime`| Gets the datetime at which this message was enqueued in Azure Service Bus.
|`CamelAzureServiceBusExpiresAt`| `ServiceBusConstants.EXPIRES_AT`|`OffsetDateTime`| Gets the datetime at which this message will expire.
|`CamelAzureServiceBusLockToken`| `ServiceBusConstants.LOCK_TOKEN`|`String`| Gets the lock token for the current message.
|`CamelAzureServiceBusLockedUntil`| `ServiceBusConstants.LOCKED_UNTIL`|`OffsetDateTime`| Gets the datetime at which the lock of this message expires.
|`CamelAzureServiceBusPartitionKey`| `ServiceBusConstants.PARTITION_KEY`|`String`| Gets the partition key for sending a message to a partitioned entity.
|`CamelAzureServiceBusRawAmqpMessage`| `ServiceBusConstants.RAW_AMQP_MESSAGE`|`AmqpAnnotatedMessage`| The representation of message as defined by AMQP protocol.
|`CamelAzureServiceBusReplyTo`| `ServiceBusConstants.REPLY_TO`|`String`| Gets the address of an entity to send replies to.
|`CamelAzureServiceBusReplyToSessionId`| `ServiceBusConstants.REPLY_TO_SESSION_ID`|`String`| Gets or sets a session identifier augmenting the {@link #getReplyTo() ReplyTo} address.
|`CamelAzureServiceBusSequenceNumber`| `ServiceBusConstants.SEQUENCE_NUMBER`|`long`| Gets the unique number assigned to a message by Service Bus.
|`CamelAzureServiceBusSessionId`| `ServiceBusConstants.SESSION_ID`|`String`| Gets the session id of the message.
|`CamelAzureServiceBusSubject`| `ServiceBusConstants.SUBJECT`|`String`| Gets the subject for the message.
|`CamelAzureServiceBusTimeToLive`| `ServiceBusConstants.TIME_TO_LIVE`|`Duration`| Gets the duration before this message expires.
|`CamelAzureServiceBusTimeTo`| `ServiceBusConstants.TO`|`String`| Gets the "to" address.
|=======================================================================


=== Message Body
In the producer, this component accepts message body of `String` type or `List<String>` 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");
--------------------------------------------------------------------------------

- `peekMessages`

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



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