Contact Us 1-800-596-4880

Azure Service Bus Connector 3.4 Examples - Mule 4

The examples in this section show application flows that:

These examples use variables for some field values. You can either:

  • Replace the variables with their values in the code

  • Provide the values for each variable in a properties file and then refer to that file from the connector configuration

    For more information about using a properties file, see Configuring Property Placeholders

Before You Begin

  • Java 8

  • Anypoint Studio 7.1.0 or later

  • Mule Runtime 4.2.x EE or later

  • Creation of a new queue for testing purpose on Azure Portal

Create a Mule Project

After you create a new Mule project and configure your credentials, you can create an application flow for each example by using the provided XML code.

To create a Mule project:

  1. In Studio, select File > New > Mule Project.

  2. Enter a name for your Mule project and click Finish.

  3. Create a file named mule-app.properties and save it in <project-name>/src/main/resources.

  4. Configure your Azure connection credentials in the file mule-app.properties, and include the name of the Azure queue you created, for example:

    #==============================================================
    #Connection Parameters
    #==============================================================
    # Azure common authentication configuration
    azure.namespace=myazure
    queue.name=mytestqueue
    
    # Shared Access Signature authentication
    azure.keyname=SAStokenname
    azure.key=SAStoken
    
    # Azure Active Directory authentication
    azure.tenantId=TenantId
    azure.clientId=ClientId
    azure.clientSecret=ClientSecret
  5. Save the file.

  6. Open the Global Element Configuration and click Test Connection to verify your credentials.

Send and Receive Example

This flow shows an example of how to use the Send and Receive From Queue operations to send and receive messages:

Example Azure Service Bus flow that uses the Send and Receive From Queue operations

To create this flow:

  1. In the Studio canvas, click Configuration XML.

  2. Delete all the contents after the line <?xml version="1.0" encoding="UTF-8"?>.

  3. Copy and paste the following XML after the line <?xml version="1.0" encoding="UTF-8"?>:

    <mule xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core"
    	xmlns:http="http://www.mulesoft.org/schema/mule/http"
    	xmlns:azure-service-bus-messaging="http://www.mulesoft.org/schema/mule/azure-service-bus-messaging" xmlns="http://www.mulesoft.org/schema/mule/core"
    	xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="
    http://www.mulesoft.org/schema/mule/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd
    http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
    http://www.mulesoft.org/schema/mule/azure-service-bus-messaging http://www.mulesoft.org/schema/mule/azure-service-bus-messaging/current/mule-azure-service-bus-messaging.xsd">
    	<configuration-properties file="mule-app.properties" doc:name="Configuration properties"/>
    	<http:listener-config name="HTTP_Listener_config" doc:name="HTTP Listener config" doc:id="290d1aa8-2e61-4707-8f04-65f0e5e4cf07" >
    		<http:listener-connection host="0.0.0.0" port="8081" />
    	</http:listener-config>
    	<azure-service-bus-messaging:config name="Azure_Service_Bus_Messaging_Connector_Config" doc:name="Azure Service Bus Messaging Connector Config" doc:id="71d0e711-6d29-4eff-b640-00986a41dfbc" >
    		<azure-service-bus-messaging:sas-connection namespace="${azure.namespace}" sharedAccessKeyName="${azure.keyname}" sharedAccessKey="${azure.key}" />
    	</azure-service-bus-messaging:config>
    	<flow name="send-flow" doc:id="2c8bce62-f192-4db2-9c70-792a5e78ac7f" >
    		<http:listener doc:name="Send Message Endpoint" doc:id="aa79c404-2e41-4453-9fde-18b8ea74c805" config-ref="HTTP_Listener_config" path="/sendMessage" />
    		<ee:transform doc:name="Set Payload and Attributes" doc:id="bddebf8d-6d63-4db0-990f-94ecec23ddbd">
    			<ee:message>
    				<ee:set-payload><![CDATA[%dw 2.0
    output application/json
    ---
    {
    	key1: "value1",
    	key2: "value2 for Queue"
    }]]></ee:set-payload>
    			</ee:message>
    			<ee:variables>
    				<ee:set-variable variableName="messageAttributes"><![CDATA[%dw 2.0
    output application/json
    ---
    {
    	messageId: "messageId1234",
    	correlationStrategy: "AUTO",
    	correlationId: "correlationId",
    	label: "my_label",
    	partitionKey: "the_partition_key",
    	timeToLive: 60,
    	ttlTimeUnit: "SECONDS",
    	scheduledEnqueueTimeUtc: "2018-12-09 03:01:00.000",
    	properties: {
    		property1: "value1",
    		property2: "value2"
    	}
    }]]></ee:set-variable>
    			</ee:variables>
    		</ee:transform>
    		<azure-service-bus-messaging:send doc:name="Queue Send" doc:id="50b3592c-f777-4667-b042-00fd266120e8" config-ref="Azure_Service_Bus_Messaging_Connector_Config" destinationName="${queue.name}" messageId="#[vars.messageAttributes.messageId]" correlationId="#[vars.messageAttributes.correlationId]" label="#[vars.messageAttributes.label]" sendCorrelationStrategy="#[vars.messageAttributes.correlationStrategy]" partitionKey="#[vars.messageAttributes.partitionKey]" timeToLive="#[vars.messageAttributes.timeToLive]" scheduledEnqueueTimeUtc='#[vars.messageAttributes.scheduledEnqueueTimeUtc as LocalDateTime {format: "yyyy-MM-dd HH:mm:ss.SSS"}]' timeToLiveTimeUnit="#[vars.messageAttributes.ttlTimeUnit]" properties="#[vars.messageAttributes.properties]">
    		</azure-service-bus-messaging:send>
    	</flow>
    	<flow name="queue-receiver-flow" doc:id="9b39aaa5-e81f-4257-b65e-d4f48c8ef2d8" >
    		<http:listener doc:name="Receive From Queue" doc:id="386337a0-99c9-45cf-a81f-95d5e06f0b74" config-ref="HTTP_Listener_config" path="/receive"/>
    		<azure-service-bus-messaging:receive doc:name="Receive From Queue" doc:id="05c8c754-79f6-4873-8329-af7f48f01d3b" config-ref="Azure_Service_Bus_Messaging_Connector_Config" destinationName="${queue.name}"/>
    		<ee:transform doc:name="Attributes to Json" doc:id="a509a361-4bd7-4c6e-81cc-39ad36e7b625">
    			<ee:message>
    			</ee:message>
    			<ee:variables >
    				<ee:set-variable variableName="jsonAttributes" ><![CDATA[%dw 2.0
    output application/json
    ---
    attributes]]></ee:set-variable>
    			</ee:variables>
    		</ee:transform>
    		<logger level="INFO" doc:name="Log Attributes" doc:id="94e7254c-a197-4fa2-b437-b64719acb23d" message="#[vars.jsonAttributes]" />
    		<logger level="INFO" doc:name="Log the message body" doc:id="d802b081-f3fc-4ec2-a23c-96bc0db3863c" message="#[payload]" />
    	</flow>
    </mule>
  4. When you are prompted to regenerate doc:id values, click Yes.

  5. Save the project.

  6. Right-click in the Studio canvas and select Run project <project-name> to run the project.

Batch Send and Receive Example

This application flow uses the Send batch and Receive Batch operations to send and receive batches of messages to and from an Azure Service Bus queue.

The following screenshot shows the application flow for sending and receiving batch messages:

Application flow that shows sending and receiving batch messages

To create this flow:

  1. In the Studio canvas, click Configuration XML.

  2. Delete all the contents after the line <?xml version="1.0" encoding="UTF-8"?>.

  3. Copy and paste the following XML after the line <?xml version="1.0" encoding="UTF-8"?>:

    <mule xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core"
    	xmlns:http="http://www.mulesoft.org/schema/mule/http"
    	xmlns:azure-service-bus-messaging="http://www.mulesoft.org/schema/mule/azure-service-bus-messaging" xmlns="http://www.mulesoft.org/schema/mule/core"
    	xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="
    http://www.mulesoft.org/schema/mule/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd
    http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
    http://www.mulesoft.org/schema/mule/azure-service-bus-messaging http://www.mulesoft.org/schema/mule/azure-service-bus-messaging/current/mule-azure-service-bus-messaging.xsd">
    	<configuration-properties file="mule-app.properties" doc:name="Configuration properties"/>
    	<http:listener-config name="HTTP_Listener_config" doc:name="HTTP Listener config" doc:id="290d1aa8-2e61-4707-8f04-65f0e5e4cf07" >
    		<http:listener-connection host="0.0.0.0" port="8081" />
    	</http:listener-config>
    	<azure-service-bus-messaging:config name="Azure_Service_Bus_Messaging_Connector_Config" doc:name="Azure Service Bus Messaging Connector Config" doc:id="71d0e711-6d29-4eff-b640-00986a41dfbc" >
    		<azure-service-bus-messaging:sas-connection namespace="${azure.namespace}" sharedAccessKeyName="${azure.keyname}" sharedAccessKey="${azure.key}" />
    	</azure-service-bus-messaging:config>
    	<flow name="send-batch-flow" doc:id="2c8bce62-f192-4db2-9c70-792a5e78ac7f" >
    		<http:listener doc:name="Send Message Endpoint" doc:id="aa79c404-2e41-4453-9fde-18b8ea74c805" config-ref="HTTP_Listener_config" path="/sendBatch" />
    		<ee:transform doc:name="Messages with Json Bodies and Properties" doc:id="1f28f155-28c8-4feb-834c-0e790cac7a53" >
                    <ee:message >
                    </ee:message>
                    <ee:variables >
    					<ee:set-variable variableName="messages" ><![CDATA[%dw 2.0
    output application/java
    ---
    [{
    	body: "{\"key1\":\"value1\",\"key2\":\"otherValue1\"}" as Binary,
    	messageId: "messageId1",
    	sendCorrelationStrategy: "AUTO",
    	correlationId: "correlation1",
    	contentType: "application/json",
    	replyToSessionId: "replyToSessionId1",
    	label: "label_msg1",
    	partitionKey: "the_partition_key1",
    	timeToLive: 240,
    	timeToLiveTimeUnit: "SECONDS",
    	zoneId: "America/Buenos_Aires",
    	scheduledEnqueueTimeUtc: "2018-12-09-00-0800" as LocalDateTime {format: "yyyy-MM-dd-HHZZZ"},
    	properties: {
    		property1: "value1",
    		property2: "value2"
    	},
    },
    {
    	body: "{\"key1\":\"value2\",\"key2\":\"otherValue2\"}" as Binary,
    	messageId: "messageId2",
    	sendCorrelationStrategy: "AUTO",
    	correlationId: "correlation2",
    	contentType: "application/json",
    	replyToSessionId: "replyToSessionId2",
    	label: "label_msg2",
    	partitionKey: "the_partition_key2",
    	timeToLive: 240,
    	timeToLiveTimeUnit: "SECONDS",
    	zoneId: "America/Buenos_Aires",
    	scheduledEnqueueTimeUtc: "2018-12-09-00-0800" as LocalDateTime {format: "yyyy-MM-dd-HHZZZ"},
    	properties: {
    		property1: "value1",
    		property2: "value2"
    	},
    },
    {
    	body: "{\"key1\":\"value3\",\"key2\":\"otherValue3\"}" as Binary,
    	messageId: "messageId3",
    	sendCorrelationStrategy: "AUTO",
    	correlationId: "correlation3",
    	contentType: "application/json",
    	replyToSessionId: "replyToSessionId3",
    	label: "label_msg3",
    	partitionKey: "the_partition_key3",
    	timeToLive: 240,
    	timeToLiveTimeUnit: "SECONDS",
    	zoneId: "America/Buenos_Aires",
    	scheduledEnqueueTimeUtc: "2018-12-09-00-0800" as LocalDateTime {format: "yyyy-MM-dd-HHZZZ"},
    	properties: {
    		property1: "value1",
    		property2: "value2"
    	},
    }]]]></ee:set-variable>
                    </ee:variables>
                </ee:transform>
    			<azure-service-bus-messaging:send-message-batch destinationName="${queue.name}" doc:name="Send batch of messages" doc:id="799d9ba3-68a9-4ef7-b518-6a715429b2a7" config-ref="Azure_Service_Bus_Messaging_Connector_Config">
    				<azure-service-bus-messaging:messages ><![CDATA[#[vars.messages]]]></azure-service-bus-messaging:messages>
    			</azure-service-bus-messaging:send-message-batch>
    		<set-payload value='#[%dw 2.0
    output application/json
    ---
    {
    	result: "Messages sent!"
    }]' doc:name="Set Payload" doc:id="c8b39b61-fa84-496d-b92c-e970a1a44003" />
    	</flow>
    	<flow name="queue-receiver-flow" doc:id="9b39aaa5-e81f-4257-b65e-d4f48c8ef2d8" >
    		<http:listener doc:name="Receive Batch From Queue" doc:id="386337a0-99c9-45cf-a81f-95d5e06f0b74" config-ref="HTTP_Listener_config" path="/receiveBatch"/>
    		<azure-service-bus-messaging:receive-batch doc:name="Receive Batch" doc:id="ee30123a-cc9f-4e08-a2e9-c41aede3923f" config-ref="Azure_Service_Bus_Messaging_Connector_Config" maxMessageCount="3" destinationName="${queue.name}"/>
    		<foreach doc:name="For Each" doc:id="16a42925-3142-48c7-9973-f951029af5fa" >
    			<logger level="INFO" doc:name="Logger" doc:id="a5bfc3c6-2335-4c4e-845e-a67cf990a510" message='#["Received message number " ++ vars.counter]'/>
    			<ee:transform doc:name="Attributes to Json" doc:id="a509a361-4bd7-4c6e-81cc-39ad36e7b625">
    			<ee:message>
    			</ee:message>
    			<ee:variables>
    				<ee:set-variable variableName="jsonAttributes"><![CDATA[%dw 2.0
    output application/json
    ---
    attributes]]></ee:set-variable>
    			</ee:variables>
    		</ee:transform>
    			<logger level="INFO" doc:name="Log Attributes" doc:id="94e7254c-a197-4fa2-b437-b64719acb23d" message="#[vars.jsonAttributes]" />
    			<logger level="INFO" doc:name="Log the message body" doc:id="d802b081-f3fc-4ec2-a23c-96bc0db3863c" message="#[%dw 2.0
    output application/json
    ---
    payload]" />
    		</foreach>
    	</flow>
    </mule>
  4. Save the project.

  5. Right-click in the Studio canvas and select Run project <project-name> to run the project.

Send, Listen, and Acknowledge the Message Example

This application flow uses the Send and Message Listener operations. The Message Listener is using the MANUAL acknowledgment type, which means that the application logic is responsible for acknowledging receipt of the message. In this example, that logic is provided by adding the Complete operation to the same flow as the Message Listener.

The following screenshot shows this flow:

Application flow that shows how the Send, Message Listener, and Complete operations are used

To create this flow:

  1. In the Studio canvas, click Configuration XML.

  2. Delete all the contents after the line <?xml version="1.0" encoding="UTF-8"?>.

  3. Copy and paste the following XML after the line <?xml version="1.0" encoding="UTF-8"?>:

    <mule xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core"
    	  xmlns:http="http://www.mulesoft.org/schema/mule/http"
    	  xmlns:azure-service-bus-messaging="http://www.mulesoft.org/schema/mule/azure-service-bus-messaging" xmlns="http://www.mulesoft.org/schema/mule/core"
    	  xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
    	  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="
    http://www.mulesoft.org/schema/mule/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd
    http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
    http://www.mulesoft.org/schema/mule/azure-service-bus-messaging http://www.mulesoft.org/schema/mule/azure-service-bus-messaging/current/mule-azure-service-bus-messaging.xsd">
    	<configuration-properties file="mule-app.properties" doc:name="Configuration properties"/>
    	<http:listener-config name="HTTP_Listener_config" doc:name="HTTP Listener config" doc:id="290d1aa8-2e61-4707-8f04-65f0e5e4cf07" >
    		<http:listener-connection host="0.0.0.0" port="8081" />
    	</http:listener-config>
    	<azure-service-bus-messaging:config name="Azure_Service_Bus_Messaging_Connector_Config" doc:name="Azure Service Bus Messaging Connector Config" doc:id="71d0e711-6d29-4eff-b640-00986a41dfbc" >
    		<azure-service-bus-messaging:sas-connection namespace="${azure.namespace}" sharedAccessKeyName="${azure.keyname}" sharedAccessKey="${azure.key}" />
    	</azure-service-bus-messaging:config>
    	<flow name="send-endpoint-flow" doc:id="2c8bce62-f192-4db2-9c70-792a5e78ac7f" >
    		<http:listener doc:name="Send Queue Endpoint" doc:id="aa79c404-2e41-4453-9fde-18b8ea74c805" config-ref="HTTP_Listener_config" path="/sendMessage" />
    		<ee:transform doc:name="Set Payload and Attributes" doc:id="bddebf8d-6d63-4db0-990f-94ecec23ddbd" >
    			<ee:message >
    				<ee:set-payload ><![CDATA[%dw 2.0
    output application/json
    ---
    {
    	key1: "value1",
    	key2: "value2 for Queue"
    }]]></ee:set-payload>
    			</ee:message>
    			<ee:variables >
    				<ee:set-variable variableName="messageAttributes" ><![CDATA[%dw 2.0
    output application/json
    ---
    {
    	messageId: "messageId1234",
    	correlationStrategy: "AUTO",
    	correlationId: "correlationId",
    	label: "my_label",
    	partitionKey: "the_partition_key",
    	timeToLive: 60,
    	ttlTimeUnit: "SECONDS",
    	scheduledEnqueueTimeUtc: "2018-12-09 03:01:00.000",
    	properties: {
    		property1: "value1",
    		property2: "value2"
    	}
    }]]></ee:set-variable>
    			</ee:variables>
    		</ee:transform>
    		<azure-service-bus-messaging:send doc:name="Queue Send" doc:id="50b3592c-f777-4667-b042-00fd266120e8" config-ref="Azure_Service_Bus_Messaging_Connector_Config" destinationName="${queue.name}" messageId='#[vars.messageAttributes.messageId]' correlationId="#[vars.messageAttributes.correlationId]" label="#[vars.messageAttributes.label]" sendCorrelationStrategy="#[vars.messageAttributes.correlationStrategy]" partitionKey="#[vars.messageAttributes.partitionKey]" timeToLive="#[vars.messageAttributes.timeToLive]" scheduledEnqueueTimeUtc='#[vars.messageAttributes.scheduledEnqueueTimeUtc  as LocalDateTime {format: "yyyy-MM-dd HH:mm:ss.SSS"}]' timeToLiveTimeUnit="#[vars.messageAttributes.ttlTimeUnit]" properties="#[vars.messageAttributes.properties]">
    		</azure-service-bus-messaging:send>
    	</flow>
    	<flow name="listener-manual-ack-flow" doc:id="541d4149-41b1-43e4-8d61-c16cf3ee502f" >
    		<azure-service-bus-messaging:message-listener doc:name="Message listener" doc:id="cb61833e-9e1f-48b3-9430-a83b47c11a4c" config-ref="Azure_Service_Bus_Messaging_Connector_Config" ackMode="MANUAL" destinationName="${queue.name}" numberOfConsumers="1"/>
    		<logger level="INFO" doc:name="Log the message" doc:id="d802b081-f3fc-4ec2-a23c-96bc0db3863c" message="#[payload]"/>
    		<ee:transform doc:name="Attributes to Json" doc:id="a509a361-4bd7-4c6e-81cc-39ad36e7b625" >
    			<ee:message >
    				<ee:set-payload ><![CDATA[%dw 2.0
    output application/json
    ---
    attributes]]></ee:set-payload>
    			</ee:message>
    		</ee:transform>
    		<logger level="INFO" doc:name="Log Attributes" doc:id="94e7254c-a197-4fa2-b437-b64719acb23d" message="#[payload]"/>
    		<azure-service-bus-messaging:complete-message doc:name="Ack of Message" doc:id="6aa7238b-3bc9-4a61-a39b-e1bd260ee7c5" config-ref="Azure_Service_Bus_Messaging_Connector_Config" lockToken="#[attributes.lockToken]" />
    		<logger level="INFO" doc:name="Log Lock Token" doc:id="a91eea3e-231a-4675-9457-b6c7366ab647" message='#["LockToken = " ++ payload.lockToken]'/>
    	</flow>
    </mule>
  4. When you are prompted to regenerate doc:id values, click Yes.

  5. Save the project.

  6. Right-click in the Studio canvas and select Run project <project-name> to run the project.

Using Azure Active Directory Connector Provider

Some organizations use Azure Active Directory to authenticate their instances. Any of the previous examples can be modified to support this scenario. Replace the Shared Access Signature authentication with the Azure Active Directory authentication:

<azure-service-bus-messaging:sas-connection namespace="${azure.namespace}" sharedAccessKeyName="${azure.keyname}" sharedAccessKey="${azure.key}" />
<azure-service-bus-messaging:aad-connection namespace="${azure.namespace}" tenantId="${azure.tenantId}" clientId="${azure.clientId}" clientSecret="${azure.clientSecret}" />

Ensure that:

  • Your mule-app.properties file contains the Tenant Id, Client Id, and Client Secret fields.

  • Your app registration contains the proper permissions and roles.

View on GitHub