How to Use JMS with AMQP 1.0 in Windows Azure with Eclipse
Updated: May 1, 2013
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 Windows 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 Windows Azure using the Windows Azure Plugin for Eclipse with Java (by Microsoft Open Technologies).
Prerequisites
-
A Java Developer Kit (JDK), v 1.6 or later.
-
Eclipse IDE for Java EE Developers, Helios SR2 or later. This can be downloaded from http://www.eclipse.org/downloads/.
-
A distribution of a Java-based web server or application server, such as Apache Tomcat, GlassFish, JBoss Application Server, or Jetty.
-
A Windows Azure subscription, which can be acquired from http://www.microsoft.com/windowsazure/offers/.
-
The Windows Azure Plugin for Eclipse with Java (by Microsoft Open Technologies) – May 2013 Preview. For more information, see Installing the Windows Azure Plugin for Eclipse with Java (by Microsoft Open Technologies).
-
Familiarity with building an application and deploying it to Windows Azure, as described in Creating a Hello World Application for Windows Azure in Eclipse.
-
A Service Bus namespace, which you can create using the Windows 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 Windows Azure Management Portal.
-
Familiarity with the Java AMQP 1.0 command line sample at http://www.windowsazure.com/en-us/develop/java/how-to-guides/service-bus-amqp/. 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.
To use JMS with AMQP 1.0 in Windows Azure using Eclipse
-
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 Windows Azure in Eclipse.)
-
Add a JSP file named index.jsp to your dynamic web project. Use the following code for index.jsp.
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>AMQP 1.0 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" language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>AMQP 1.0 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); sender.send(message); System.out.println("Sent message with JMSMessageID = " + message.getJMSMessageID()); } private static void init() { try { // Configure JNDI environment Hashtable<String, String> env = new Hashtable<String, String>(); env.put(Context.INITIAL_CONTEXT_FACTORY, "org.apache.qpid.amqp_1_0.jms.jndi.PropertiesFileInitialContextFactory"); env.put(Context.PROVIDER_URL, "servicebus.properties"); context = new InitialContext(env); // Lookup ConnectionFactory and Queue ConnectionFactory cf = (ConnectionFactory) context.lookup ("SBCONNECTIONFACTORY"); Destination queue = (Destination) context.lookup("QUEUE"); // Create Connection connection = cf.createConnection(); // Create sender-side Session and MessageProducer sendSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); sender = sendSession.createProducer(queue); } catch (Exception e) { System.out.println("Exception: " + e); } } private static void close() throws JMSException { sender.close(); sendSession.close(); connection.close(); } } -
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.
-
Within Eclipse’s Project Explorer, right-click your dynamic web project and click Properties.
-
Create a Windows Azure project for your application: In Eclipse’s Project Explorer, right-click your dynamic web project, click Windows Azure, and then click Package for Windows Azure. Provide values for the fields in the Windows Azure Deployment Project dialog and click Finish. (For detailed instructions on this dialog, see Creating a Hello World Application for Windows 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 http://www.w3schools.com/tags/ref_urlencode.asp.
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 http://www.windowsazure.com/en-us/develop/java/how-to-guides/service-bus-amqp/.
-
Add servicebus.properties as a component to your worker role:
-
In Eclipse’s Project Explorer, expand your Windows Azure project, right-click WorkerRole1, click Windows 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.
-
In Eclipse’s Project Explorer, expand your Windows Azure project, right-click WorkerRole1, click Windows Azure, and then click Components.
-
To run in the compute emulator, in the Windows Azure toolbar, click the Run in Windows 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 Windows Azure, within the Windows Azure toolbar, click the Publish to Windows Azure Cloud icon and follow the prompts to complete your deployment. (More information about deploying to Windows Azure can be found in Creating a Hello World Application for Windows Azure in Eclipse.) After the application is running as a Windows Azure cloud service, launch the URL for your application. Enter a message and then click Send Message to see the result.