This post will show how to use Eclipse to build a web application that uses JMS to send AMQP 1.0 messages to Azure Event Hubs.
I have been working on a project that leverages Azure Event Hubs, Azure Stream Analytics, and other related technologies to demonstrate a highly scalable IOT platform. Since many of the customers that would potentially use this solution are already familiar with Java, I decided to build a demo using Eclipse.
This post uses an environment that includes Tomcat, Eclipse, and the Azure Toolkit for Eclipse. For instructions on creating this environment, see my blog post Creating an Eclipse Development Environment for Azure.
This post follows the instructions in the article How to Use JMS with AMQP 1.0 in Azure with Eclipse, with a few slight changes and deeper explanations.
Create the Web Project
In Eclipse, choose File / New / Dynamic Web Project.
I’ll name it JMSDemo. On the Dynamic Web Project page, click the New Runtime button to define a new runtime. I am using Apache Tomcat 8.0.
On the next page, we configured the Tomcat Server by providing the root directory where Tomcat is installed, and we choose the installed JRE.
Click Finish. The Dynamic Web Project looks like this:
Add Some JSP Pages
Right-click the new WebContent folder and add a new JSP page named index.jsp.
Right-click the new WebContent folder again and add a new JSP page, this time named sendMessages.jsp.
Now that we’ve created the JSP pages, we need to implement the MyAMQPSender class.
Have Some Class
Right-click the project node in the Project Explorer pane and choose New Class. Use the following properties:
- Package: MyPackage
- Name: MyAMQPSender
Click Finish to close the dialog. Now edit the contents of the MyAMQPSender.java class:
When you add the class, you are going to see red squiggly underlines and errors all over the place. This is because we need to leverage additional libraries. Because we have already installed the Azure Toolkit for Eclipse (see my blog post Creating an Eclipse Development Environment for Azure for step-by-step instructions), we can easily add the Apache Qpid Client Libraries for JMS provided by MS Open Tech.
To add the package, right-click the project and choose Properties. Next, go to the Java Build Path node, then go to the Libraries tab. Click the Add Library button and choose Package for Apache Qpid Client Libraries for JMS (by MS Open Tech).
Click Next, and then ensure the Include in the project deployment assembly option is selected.
Click Finish, then OK to close the Properties dialog. Your errors should now be gone.
Create an Event Hub
Click the New button on the bottom left of the screen and choose Service Bus / Event Hub / Custom Create.
Give it a name, choose the region closest to you, and create a new namespace.
On the next screen, specify 16 for the partition count and 7 for the message retention.
Choose OK to create the event hub. Once the event hub is created, go to the Event Hubs tab (you may need to refresh the screen) and click on the event hub that you created, then click on Connection Information.
We are going to define a shared access signature policy that enables us to only send messages to the event hub. Create a new policy named SendPolicy with Send permission.
Click Save, and the Shared Access Key Generator is shown. Copy the primary key value.
Create the servicebus.properties File
Go back to Eclipse. Right-click the project and choose New / File. Name the file servicebus.properties.
The servicebus.properties file is used to register a ConnectionFactory in JNDI. Update its contents:
In the connection string, the SendPolicy value is the same name as the SAS policy that we added in the Azure management portal. The “your_url_encoded_key” is the key that we copied from the Shared Access Key Generator, but its value 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. Finally, the “your_queue” value needs to be updated with the name of the Event Hub that you created.
Here is a redacted version of my servicebus.properties file:
We’ll use this file as part of our deployment project.
Create an Azure Deployment Project
We’re almost there… right-click the project in Eclipse and choose Azure / Package for Azure. I name the deployment project JMSDemoDeployment.
On the JDK page, I provide the path to my installed JDK. The storage account setting is left as “(auto)”.
On the Server tab, I provide the root path to the location where I unzipped Apache Tomcat to, and choose the Type as Apache Tomcat 8. I leave the storage account as “(auto)”.
Click Finish to close the wizard.
Now right-click the deployment project and choose Properties, then go to the Azure / Roles node. Highlight the worker role and click Edit.
Go to the Component tab and choose Add. In the Azure Role Component dialog, use the File button to locate your servicebus.properties file. The import method is copy, and the “as name” value is “servicebus.properties”. Under the deploy from package settings, the method is copy, and the “to directory” value is %CATALINA_HOME%\bin.
Click OK, OK, OK to close the windows. Click the deployment project’s node in the Project Explorer pane and choose the Run in Azure Emulator button.
After some time, you will see the output in the compute emulator that Java is running.
Go to http://localhost:8080/JMSDemo/ and you will see the following JSP page served by Tomcat within the Azure Emulator.
Enter some values then click the button and we should see the following:
We successfully sent a message to Event Hub!
In order to receive the message, we need some code to process the message. This is beyond the scope of this article, but you can refer to the Event Hub documentation for a walkthrough creating a cross-platform messaging solution between JMS and .NET. You can also learn about using Visual Studio to receive messages with EventProcessorHost to create a highly scalable solution to process massive amounts of data with parallel actions.