How to Use JMS with AMQP 1.0 in Azure with Eclipse
Updated: April 4, 2014
Advanced Message Queuing Protocol (AMQP) 1.0 is an efficient and reliable wire-level messaging protocol that can be used to build robust cross-platform messaging applications. AMQP 1.0 support has been added to Azure Service Bus as a preview feature. The following steps show you how to use the Java Message Service (JMS) with AMQP 1.0 in a basic JSP application deployed to Azure using the Azure Toolkit for Eclipse with Java (by Microsoft Open Technologies).
-).
- Familiarity with building an application and deploying it to Azure, as described in Creating a Hello World Application for Azure in Eclipse.
- A Service Bus namespace, which you can create using the Azure Management Portal. For instructions on how to create a Service Bus namespace, see How to Use Service Bus Queues.
- A queue for your Service Bus namespace. You can create a queue in the Service Bus section of the Azure Management Portal.
- Familiarity with the Java AMQP 1.0 command line sample at. You should run the Receiver portion of the command line sample, to be able to receive messages sent from the JSP application that you’ll create using the following instructions.
Create a dynamic web project: Within Eclipse, at the menu click File, click New, and then click Dynamic Web Project. (More detailed steps for creating a dynamic web project are available at Creating a Hello World Application for Azure in Eclipse.)
Add a JSP file named index.jsp to your dynamic web project. Use the following code message sender</title> </head> <form method="post" action="sendMessages.jsp"> <br/> This sample sends a message to a queue. <br/><br/> Enter your message: <input type="text" name="message" /> <input type="submit" name="submit" value="Send Message" /> </form> </html>
Add a JSP file named sendMessages.jsp to your dynamic web project. Use the following code for sendMessages.jsp.
<%@ page import="myAMQPPackage.MyAMQPSender" page 2</title> </head> <body> <% out.print("Sending message -------> " + request.getParameter("message") ); out.print("<br/>"); try { // Send the message. MyAMQPSender.postMessages(request.getParameter("message")); out.println("Message sent successfully\n"); } catch (Exception e) { out.println("Error occurred while sending message to queue.\n"); out.println(e); } %> </body> </html>
Create a Java class, MyAMQPSender, for your dynamic web project. For purposes of this tutorial, name the package that it belongs to myAMQPPackage. Use the following code for MyAMQPSender.java.
package myAMQPPackage; import javax.jms.*; import javax.naming.Context; import javax.naming.InitialContext; import java.util.Hashtable; public class MyAMQPSender { private static Connection connection; private static Session sendSession; private static MessageProducer sender; private static Context context; public static void postMessages(String textMessage) throws Exception { try { init(); sendTextMessage(textMessage); close(); } catch (Exception e) { System.out.println("Exception: " + e); } } private static void sendTextMessage(String textMessage) throws JMSException { TextMessage message = sendSession.createTextMessage(); message.setText(textMessage);(); } }
Add the Package for Apache Qpid Client Libraries for JMS (by MS Open Tech) library to your dynamic web project’s build path and deployment assembly:
- Within Eclipse’s Project Explorer, right-click your dynamic web project and click Properties.
- In the left-hand pane of the Properties dialog, click Java Build Path.
- In the Java Build Path dialog, click the Libraries tab, click Add Library, select Package for Apache Qpid Client Libraries for JMS (by MS Open Tech), and then click Next.
- In the Add Library dialog, ensure Include in the project deployment assembly is checked.
- Click Finish to close the Add Library dialog.
- Click OK to close the Properties dialog.
Create an Azure project for your application: In Eclipse’s Project Explorer, right-click your dynamic web project, click Azure, and then click Package for Azure. Provide values for the fields in the Azure Deployment Project dialog and click Finish. (For detailed instructions on this dialog, see Creating a Hello World Application for Azure in Eclipse.)
Create a file named servicebus.properties. You can pick any local folder, the file will be added as a component in the next step.
Use the following contents for servicebus.properties.
# Register a ConnectionFactory in JNDI using the form: # connectionfactory.[jndiname] = [ConnectionURL] connectionfactory.SBCONNECTIONFACTORY = \ amqps://owner:your_url_encoded_key@your_namespace.servicebus.windows.net # Register some queues in JNDI using the form: # queue.[jndiName] = [physicalName] queue.QUEUE = your_queue
Use your Service Bus namespace name in place of your_namespace, and use your Service Bus access key in place of your_url_encoded_key. Note that within servicebus.properties, the Service Bus key needs to be URL-encoded. One technique for creating a URL encoding is via the web site.
Use the name of the queue that you created in place of your_queue. Also, if your issuer is not owner, modify it in the connectionfactory.SBCONNECTIONFACTORY string too.
For more information about servicebus.properties, see.
Add servicebus.properties as a component to your worker role:
- In Eclipse’s Project Explorer, expand your Azure project, right-click WorkerRole1, click Azure, and then click Components.
- In the Components dialog, click Add.
- In the Add Component dialog under Import, for From path, click File, and select your servicebus.properties file.
- For the Import method, use copy.
- For As name, use servicebus.properties, which will be the default when you click the text box.
- In the Add Component dialog under Deploy, for Method, use copy.
- For To directory, use %CATALINA_HOME%\bin.
- Click OK to close the Add Component dialog, and then click OK to close the Components dialog.
To run in the compute emulator, in the Azure toolbar, click the Run in Azure Emulator icon to run your application in the compute emulator. After the application is running in the compute emulator, launch the
localhostURL for your application. Enter a message and then click Send Message to see the result.
To run in Azure, within the Azure toolbar, click the Publish to Azure Cloud icon and follow the prompts to complete your deployment. (More information about deploying to Azure can be found in Creating a Hello World Application for Azure in Eclipse.) After the application is running as an Azure cloud service, launch the URL for your application. Enter a message and then click Send Message to see the result.