Book Image

HornetQ Messaging Developer's Guide

By : Piero Giacomelli
Book Image

HornetQ Messaging Developer's Guide

By: Piero Giacomelli

Overview of this book

<p>Messages and information can be exchanged at exponential speed with JBoss HornetQ asynchronous messaging middleware. Learn how to use the JAVA open source Message Oriented Framework, to build a high-performance, multi-protocol, embeddable, clustered system and manage millions of messages per second.<br /><br />In the HornetQ Messaging Developer’s Guide you will find the most common applications of a message exchanger with example code, as part of real-world scenarios. This practical and applicable guide increases reader knowledge chapter by chapter, covering basics to the most advanced features.<br /><br />You will start from a clean installation of a HornetQ sever and, having progressively become a HornetQ master, will finish by being able to use the framework embedded in your software and sharing information in a cluster environment.<br /><br />Starting from writing and reading a single message, we will discover more advanced features like managing queues, clustering the server, and controlling the undelivered messages. The book deals with a real-world advanced medical scenario as the main example that will lead you from learning the basics to the advanced features of HornetQ.</p>
Table of Contents (18 chapters)
HornetQ Messaging Developer's Guide
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Configuring the Eclipse IDE for HornetQ


Following our fictional scenario, we now need to code the production and consumption of the message containing the ECG measurements that came from the patient's ECG device. Our first example will be done in a single Java class, but considering that most of the time you have two different softwares that write/read on HornetQ, a more realistic example will have two classes; one that writes messages to the queue and one that reads those messages from the queue. HornetQ implements the JMS specification, so we will use JMS messages for this example; in the next chapter, we will describe other ways to implement the messages. We will now describe the methods of the Java class that reflect the flow of the messages in our example. The next coding steps involve the implementation of these methods. We named our class ECGMessageProducerConsumerExample.java; once created, we will call the following methods:

  • getInitialContext: This method will initialize the properties that will identify the HornetQ server and queue

  • connectAndCreateSession: This method is used for creating a shared connection and session from both the producer of the message and the consumer of the message

  • produceMessage: A JMS message will be produced in this method and put on the queue

  • consumeMessage: This method will read the message just stored, and by connecting to MongoDB, store it in the database

  • closeConnection: In the end, this method does a clean closing of the resources used

Before entering the coding phase, we need to set up Eclipse to be able to deal with all the HornetQ API that are needed from ECGMessageProducerConsumerExample.java, to talk to the server.

So after firing up Eclipse, follow these steps to create the project for our class:

  1. Select File | New | Java Project so that a new window appears asking for the project name, which is chapter01, and then click on Finish.

  2. Right-click on the project name and choose the Properties item.

  3. Once the Properties window is shown, choose Java Build path | Libraries, and click on the Add external jars button.

  4. Go to the HORNETQ_ROOT\lib folder and select the following .jar files—hornetq-jms.jar, jboss-jms-api.jar, jnp-client.jar, netty.jar, and hornetq-core-client.jar. Then click on OK.

  5. Click on the add external jars button again and go to where you have downloaded the mongodb jar from the MongoDB website, add it to your build path, and close the Project properties windows.

  6. Right-click on the project name and choose New | Class. In the displayed pop-up window, enter ECGMessageConsumerProducerExample as the class name and check the public static void main (string[] args) checkbox, as shown in the following screenshot:

Most of our examples will be implemented using this standard configuration, so remember to re-use it any time you need to code a project for HornetQ. If all the previous steps have been done correctly, in the Explorer tab of Eclipse, you should see the .jar files included, as shown in the following screenshot: