SALES: 1-800-867-1380

Samples

Updated: January 22, 2014

A set of sample programs accompanies this developer guide. These samples illustrate how to use Service Bus using the different client libraries. Variants of the same samples are provided in C#, Java, Python, and PHP. For each language, there is a sender program that sends several messages to a Service Bus messaging entity, and a receiver program that receives messages from a Service Bus messaging entity. You can use these samples cross-languages to understand interoperability scenarios. In addition, the C# samples include an admin program that you can use to create Service Bus messaging entities for use with the other samples.

C# Samples

This release includes three sample C# programs: Admin, Sender, and Receiver. All three samples are included in the Sample.sln Visual Studio solution. The solution assumes that you have placed the Microsoft.ServiceBus.Preview.dll assembly in the lib subdirectory within the C# samples directory.

Configuring the Samples for your Service Bus Service Namespace

Before using the samples, you must configure the code to use your service namespace details. To configure the code, edit the App.config configuration file and update the Microsoft.ServiceBus.ConnectionString with the endpoint connection string for your service namespace. You must update the App.config file in each project.

The Admin Sample

The Admin sample, provided in the Admin project of the Samples solution, illustrates how to manage Service Bus brokered messaging entities (queues, topics, and subscriptions) using the Service Bus .NET API. There is nothing AMQP-specific about this sample.

Running the Admin Sample

The admin sample produces output similar to the following:

Initializing NamespaceManager (sb://yournamespace.servicebus.windows.net)

MENU
         P       Provision sample messaging entities
         D       Delete sample messaging entities
         E       Enumerate all messaging entities in namespace
         DA      Delete all messaging entities in namespace - be careful!
         .       Exit
Provision Sample Messaging Entities

Pressing the letter P followed by Enter provisions a set of sample entities that the other sample programs can use. The entities provisioned are defined in the Constants class. As supplied, provisioning produces output similar to the following:

Creating queue: queue1
Creating queue: queue2
Creating queue: queue3
Creating topic: topic1
Creating subscription sub1 on topic topic1
Creating subscription sub2 on topic topic1
Creating topic: topic2
Creating subscription sub1 on topic topic2
Creating subscription sub2 on topic topic2
Creating topic: topic3
Creating subscription sub1 on topic topic3
Creating subscription sub2 on topic topic3
Deleting Sample Messaging Entities

Pressing the letter D followed by Enter deletes the set of sample entities that the other sample programs can use. The entities deleted are defined in the Constants class. As supplied, deleting produces output similar to the following:

Deleting queue: queue1
Deleting queue: queue2
Deleting queue: queue3
Deleting topic: topic1
Deleting topic: topic2
Deleting topic: topic3
Enumerating All Messaging Entities in a Service Namespace

Pressing the letter E followed by Enter enumerates all the entities currently defined in the service namespace. Note that this option displays all entities, not just entities provisioned by this sample. After successful provisioning, this option produces output similar to the following:

Enumerating entities:
        Queue: queue1
        Queue: queue2
        Queue: queue3
        Topic: topic1
                Subscription: sub1
                        Rule: $Default
                Subscription: sub2
                        Rule: $Default
        Topic: topic2
                Subscription: sub1
                        Rule: $Default
                Subscription: sub2
                        Rule: $Default
        Topic: topic3
                Subscription: sub1
                        Rule: $Default
                Subscription: sub2
                        Rule: $Default
Deleting All Messaging Entities in a Service Namespace

Pressing the letters DA followed by Enter deletes all entities from the current service namespace. Note that this deletes all entities, not just entities created by this sample program. Use with care. The option produces output similar to the following:

Deleting queue: queue1
Deleting queue: queue2
Deleting queue: queue3
Deleting topic: topic1
Deleting topic: topic2
Deleting topic: topic3
Exiting the Admin Sample

Pressing the period key (.) followed by Enter exits the Admin sample.

The Sender Sample

The Sender sample, provided in the Sender project of the Samples solution, shows how to send messages to Service Bus brokered message entities (queues and topics) using AMQP 1.0 and the Service Bus .NET API.

Running the Sender Sample

The Sender application expects a single command-line parameter that specifies the entity to which messages are sent. Before running the Sender application, ensure that you have already created the entity to which messages are to be sent. The Admin sample provides a convenient way of creating the entity.

The Sender application produces output similar to the following:

C:>Sender queue1

Creating message sender for: queue1
Sending text message to queue1
Sending bytes message to queue1
Sending map message to queue1
Sending stream message to queue1
Closing MessageSender for queue1
Closing MessagingFactory

The Receiver Sample

The Receiver sample, provided in the Receiver project of the Samples solution, shows how to receive messages from Service Bus brokered message entities (queues and subscriptions) using AMQP 1.0 and the Service Bus .NET API.

Running the Receiver Sample

The Receiver application expects a single command-line parameter that specifies the entity from which messages are received. Before running the Receiver application, ensure that you have already created the entity from which messages are to be received. The Admin sample provides a convenient way of creating the entity.

The Receiver application produces output similar to the following. This trace was obtained after messages had already been sent to the entity queue1 using the Sender sample:

C:>Receiver queue1

Creating message receiver for: queue1
Receiving messages from queue1
Press any key to stop


Received message 1
TextMessage body: 
Text: this is a text string
Standard properties:
ContentType: TestContentType-1
CorrelationID: TestCorrelationId-1
EnqueuedTimeUtc: 10/31/2012 3:39:02 AM
Label: TestLabel-1
MessageId: TestMessageId-1
ReplyTo: TestReplyTo-1
ReplyToSessionId: TestReplyToSessionId-1
ScheduledEnqueueTimeUtc: 10/31/2012 3:38:57 AM
SessionId: TestSessionId-1
TimeToLive: 1.00:00:00
To: TestTo-1
Application properties:
MessageType: TextMessage (System.String)
TestByte: 128 (System.Byte)
TestSbyte: -22 (System.SByte)
TestChar: X (System.Char)
TestShort: -12345 (System.Int16)
TestUshort: 12345 (System.UInt16)
TestInt: -100 (System.Int32)
TestUint: 100 (System.UInt32)
TestLong: -12345 (System.Int64)
TestUlong: 12345 (System.UInt64)
TestFloat: 3.14159 (System.Single)
TestDouble: 3.14159 (System.Double)
TestDecimal: 3.14159 (System.Decimal)
TestBoolean: True (System.Boolean)
TestGuid: 5a18dd37-7c5e-4eee-8bd9-e3f461590b87 (System.Guid)
TestString: Service Bus (System.String)
TestUri: http://www.bing.com/ (System.Uri)
TestDateTime: 10/31/2012 3:38:57 AM (System.DateTime)
TestDateTimeOffSet: 10/31/2012 3:38:57 AM +00:00 (System.DateTimeOffset)
TestTimeSpan: 01:00:00 (System.TimeSpan)
Originator: .NET (System.String)


Received message 2
<...>

Closing MessageReceiver for queue1
Closing MessagingFactory

Java Samples

This release includes two sample Java programs: Sender and Receiver. These Java samples use the AMQP 1.0-based JMS client library developed by the Apache Qpid project. These samples assume that you have placed the Qpid JAR files in the ‘lib’ subdirectory within the Java samples directory.

Configuring the Samples for your Service Bus Service Namespace

Before using the samples, you must configure the code to use your service namespace. To configure the code, edit the servicebus.properties configuration file and update the definition of SBCONNECTIONFACTORY with your service namespace details.

You must also create the Service Bus messaging entities before running the samples. You can create the entities via the Windows Azure Management Portal, or by using the .NET Admin sample previously referenced.

Compiling the Java Samples

These samples were developed using the JDK 1.7 but other versions of the JDK should also work. A project file for use with the IntelliJ IDEA IDE is provided with the Java samples. Alternatively, you can compile the samples by using the supplied ‘buildSamples.bat’ batch file.

The Sender Sample

The Sender sample shows how to send messages to Service Bus brokered message entities (queues and topics) using AMQP 1.0 and the Java Message Service (JMS) API.

Running the Sender Sample

The Sender sample sends messages to the Service Bus entity mapped to the JNDI logical name of “sendToEntity.” A batch file (‘runSender.bat’) has been provided to run the sample easily. Running the batch file produces output similar to the following:

C:>runSender.bat

Sender: Initializing...
BaseSample: Initializing JMS environment
BaseSample: Creating JNDI InitialContext
BaseSample: Looking up ConnectionFactory
BaseSample: Creating a Connection
BaseSample: Creating a non-transacted, auto-acknowledged Session
Sender: Looking up send destination SendToEntity
Sender: Creating a MessageProducer for SendToEntity
Sender: Sending TextMessage
Sender: Sending BytesMessage
Sender: Sending MapMessage
Sender: Sending StreamMessage
Sender: Sending ObjectMessage
Sender: Shutting down...
Sender: Closing MessageProducer
BaseSample: Shutting down...
BaseSample: Closing Session
BaseSample: Closing Connection
BaseSample: Closing JNDI context
Sender: Exiting
Press any key to continue . . .

The Receiver Sample

The Receiver sample shows how to receive messages from Service Bus brokered message entities (queues and subscriptions) using AMQP 1.0 and the Java Message Service (JMS) API.

Running the Receiver Sample

The Receiver sample receives messages from the Service Bus entity mapped to the JNDI logical name of “receiveFromEntity.” You can use the batch file runReceiver.bat to run the sample easily. The batch file produces output similar to the following. This trace was obtained after messages had already been sent to the entity in the Sender sample.

C:>runReceiver.bat

Receiver: Initializing...
BaseSample: Initializing JMS environment
BaseSample: Creating JNDI InitialContext
BaseSample: Looking up ConnectionFactory
BaseSample: Creating a Connection
BaseSample: Creating a non-transacted, auto-acknowledged Session
Receiver: Looking up receiveDestination ReceiveFromEntity
Receiver: Creating a MessageConsumer for ReceiveFromEntity
Listener: Listener constructed
Press <Enter> to stop

Listener: Message received 1

TextMessage body:
Text:This is a JMS TextMessage
JMS headers: 
JMSCorrelationID: TestCorrelationID-1
JMSDeliveryMode: PERSISTENT (2)
JMSDestination: (Queue) queue1
JMSExpiration: 0
JMSMessageID: ID:TestMessageID-1
JMSPriority: 4
JMSRedelivered: true
JMSReplyTo: (Queue) queue1
JMSTimestamp: Tue Oct 30 20:54:50 PDT 2012
JMSType: TestType-1
Application properties: 
TestBoolean: true (Boolean)
TestDouble: 3.14159 (Double)
TestByte: 33 (Byte)
TestFloat: 3.13159 (Float)
TestString: Service Bus (String)
MessageType: TextMessage (String)
TestInt: 100 (Integer)


Listener: Message received 2

<...>

Receiver: Shutting down...
Receiver: Closing MessageConsumer
BaseSample: Shutting down...
BaseSample: Closing Session
BaseSample: Closing Connection
BaseSample: Closing JNDI context
Receiver: Exiting
Press any key to continue . . .

Python Samples

This release includes two sample Python scripts: sender.py and receiver.py. These samples use the Proton-Python AMQP 1.0-based client library developed by the Apache Qpid project. These samples assume that you have installed Proton-Python.

Configuring the Samples for your Service Bus Service Namespace

Before using the samples, you must configure the code to use your service namespace. To configure the code, edit the scripts and update the following variable definitions to match your service namespace details and the name of the Service Bus messaging entity to use:

broker = "amqps://[issuer-name]:[issuer-key]@[namespace].servicebus.windows.net"
entity = "queue1"

You must also create the Service Bus entities before running the samples. You can create the entities via the Windows Azure Management Portal, or by using the .NET Admin sample referenced previously.

The Sender Sample

The Sender sample shows how to send messages to Service Bus brokered message entities (queues and topics) using AMQP 1.0 and the Proton-Python.

Running the Sender Sample

Running the sample produces output similar to the following:

$ python sender.py
Sending messages to amqps://[name]:[key]@[namespace].servicebus.windows.net/queue1
Sent TextMessage with id 106b8e0c-2310-11e2-921c-00155d460d89
Sent BytesMessage with id 129745d6-2310-11e2-921c-00155d460d89
Sent MapMessage with id 12b7728e-2310-11e2-921c-00155d460d89
Sent StreamMessage with id 12d7ba30-2310-11e2-921c-00155d460d89
done

The Receiver Sample

The Receiver sample shows how to receive messages from Service Bus brokered message entities (queues and subscriptions) using AMQP 1.0 and the Proton-Python.

Running the Receiver Sample

Running the sample produces output similar to the following. This trace was obtained after messages had already been sent to the messaging entity in the Sender sample.

$ python receiver.py
Receiving messages from amqps://[name]:[key]@[namespace].servicebus.windows.net/queue1
Press enter to stop

Received message 1
        Body: This is a text message
        Message Properties:
                content_type: TestContentType-106b8e0c-2310-11e2-921c-00155d460d89
                correlation_id: 106b8e0c-2310-11e2-921c-00155d460d89
                subject: TestLabel-106b8e0c-2310-11e2-921c-00155d460d89
                id: TestMessageId-106b8e0c-2310-11e2-921c-00155d460d89
                reply_to: TestReplyTo-106b8e0c-2310-11e2-921c-00155d460d89
                reply_to_group_id: TestReplyToSessionId-106b8e0c-2310-11e2-921c-00155d460d89
                group_id: TestSessionId-106b8e0c-2310-11e2-921c-00155d460d89
                user_id: TestTo-106b8e0c-2310-11e2-921c-00155d460d89
                ttl: 8640000
        Application Properties:
                Originator : u'Proton-Python' (<type 'unicode'>)
                TestFloat : 1.5 (<type 'float'>)
                TestGuid : UUID('106c01d4-2310-11e2-921c-00155d460d89') (<class 'uuid.UUID'>)
                TestString : 'Service Bus' (<type 'str'>)
                MessageType : u'TextMessage' (<type 'unicode'>)
                TestStringUnicode : u'Service Bus !#$' (<type 'unicode'>)
                TestInt : 1 (<type 'int'>)
                TestBoolean : False (<type 'bool'>)
                TestUri : 'http://www.bing.com' (<type 'str'>)
                TestLong : 1000 (<type 'int'>)

Received message 2
<...>

Stopping...
Done

PHP Samples

This release includes two sample PHP scripts: sender.php and receiver.php. These samples use the Proton-Python AMQP 1.0-based client library developed by the Apache Qpid project. These samples assume that you have installed Proton-PHP.

Configuring the Samples for your Service Bus Service Namespace

Before using the samples, you must configure the code to use your service namespace. To configure the code, edit the scripts and update the following variable definitions to match your service namespace details and the name of the Service Bus messaging entity to use:

$broker = "amqps://[issuer-name]:[issuer-key]@[namespace].servicebus.windows.net";
$entity = "queue1";

You must also create the Service Bus entities before running the samples. You can create the entities via the Windows Azure Management Portal, or by using the .NET Admin sample referenced previously.

The Sender Sample

The Sender sample shows how to send messages to Service Bus brokered message entities (queues and topics) using AMQP 1.0 and the Proton-PHP.

Running the Sender Sample

Running the sample produces output similar to the following:

$ php sender.php
Sent TextMessage with id UUID(31323334-3132-3334-3132-333431323334)
Sent BytesMessage with id UUID(31323334-3132-3334-3132-333431323334)
Sent MapMessage with id UUID(31323334-3132-3334-3132-333431323334)
Sent StreamMessage with id UUID(31323334-3132-3334-3132-333431323334)
Done

The Receiver Sample

The Receiver sample shows how to receive messages from Service Bus brokered message entities (queues and subscriptions) using AMQP 1.0 and the Proton-PHP.

Running the Receiver Sample

Running the sample produces output similar to the following. This trace was obtained after messages had already been sent to the messaging entity in the Sender sample.

$ php receiver.php
Press ctrl-C to stop
Receiving messages from amqps://[name]:[key]@[namespace].servicebus.windows.net/queue1

Received message 1
        Body:
                This is a text message
        Message Properties:
                content_type: TestContentType-UUID(31323334-3132-3334-3132-333431323334)
                correlation_id: UUID(31323334-3132-3334-3132-333431323334)
                subject: TestLabel-UUID(31323334-3132-3334-3132-333431323334)
                id: TestMessageId-UUID(31323334-3132-3334-3132-333431323334)
                reply_to: TestReplyTo-UUID(31323334-3132-3334-3132-333431323334)
                reply_to_group_id: TestReplyToSessionId-UUID(31323334-3132-3334-3132-333431323334)
                group_id: TestSessionId-UUID(31323334-3132-3334-3132-333431323334)
                user_id: TestTo-UUID(31323334-3132-3334-3132-333431323334)
                ttl: 8640000
                enqueued time: 1351657600560
                scheduled enqueued time:
        Application properties
                MessageType: TextMessage (string)
                Originator: PROTON-PHP (string)
                TestString: Service Bus (string)
                TestStringUnicode: Binary(Service Bus !#$) (object)
                TestInt: 1 (integer)
                TestFloat: 1.5 (double)
                TestGuid: UUID(31323334-3132-3334-3132-333431323334) (object)
                TestUri: http://www.bing.com (string)
                TestBoolean:  (boolean)

Received message 2 
<...>

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft