Skip to end of metadata
Go to start of metadata

Introduction

This document describes the implementation required for a Java Message Service queue:

  • to send and receive messages
  • to add an order to a JobScheduler job chain as an example XML command extracted from a message.

A separate implementation is used to send the XML command - JobScheduler libraries are not needed for the example.

The Apache Active MQ server is used in this example.

Mode of Operation

The implementation enables the following steps:

  • Creation of a message producer to send messages to a Message Queue server (MQ server).
  • Creation of a message consumer to receive messages from an MQ server.
  • Sending the message to a JobScheduler.

The message has to be an XML snippet to be processed by the desired JobScheduler. This snippet must be valid XML and compliant with the JobScheduler XML Schema.

Most of the implementation work is done with the standard JMS implementation of Java. The only class from the Active MQ implementation is the ActiveMQConnectionFactory class. It shouldn´t be too complex to change the implementation to the ConnectionFactory of your desired message queue service.

Download

A zip file of this example as a complete maven project implementation is available: active-mq-example.zip.

Prerequisites

  • A running Message Queue server (The example uses Apache Active MQ)
  • A running JobScheduler
  • Maven (only needed if you want to build the example as a maven project)

The MessageProducer

This example describes how to build a connection to an MQ server and send a message to a queue on this server.

  1. Create a connection.
  2. Create a session.
  3. Create a destination.
  4. Create a producer.
  5. Send a message with the producer.
  6. Close the connection.

The Methods

The steps are separated into different methods to make the implementation more readable as well as reusable. 

createConnection(String url)

This method instantiates a ConnectionFactory object with an ActiveMQConnectionFactory object and creates a Connection object through the factory method createConnection().

The ActiveMQConnectionFactory object has to be instantiated with the URL of the MQ server.

createConnection(String url)  Expand source

createSession(Connection connection)

The method is called with an already instantiated Connection object and initiates a Session object through the Connection objects createSession(boolean transacted,  int acknowledgeMode) method.

createSession(Connection connection)  Expand source

createDestination(Session session, String queueName)

This method creates a Destination object. It is called with an active Session object and the name of the queue to write to. The Destination object is initiated through the createQueue(String name) method of the Session object.

createDestination(Session session)  Expand source

createMessageProducer(Session session, Destination destination)

This method is called with an already active Session object as well as an instantiated Destination object. It instantiates a MessageProducer object with the given session and destination.

createProducer(Session session, Destination destination)  Expand source

write(String text, MessageProducer producer)

The method is called with the message to send to the server and the MessageProducer object to use for publishing. The message is a String object. The method instantiates a Message object with the text to send.

write(String text)  Expand source

close()

This method makes sure that the connection used will be closed after a message has been sent.

close()  Expand source

The SOSProducer class

The code example below slightly differs from the examples above. In the class below the write(String text) method already uses the other methods for instantiation, therefore it needs less parameters and it closes the connection itself.

The text in the example below consists of a JobScheduler add_order XML, which can later be used to send to a JobScheduler.

SOSProducer.java  Expand source

The MessageConsumer

This section describes how to build a connection to a MQ server and receive a message from a queue on this server. Furthermore it describes how to build a TCP socket connection to send the received message to a JobScheduler.

  1. Create a connection.
  2. Create a session.
  3. Create a destination.
  4. Create a consumer.
  5. Receive a message with the consumer.
  6. Close the (MQ) connection.
  7. Open a TCP connection to a JobScheduler instance.
  8. Send the Message to the JobScheduler
  9. Close the (TCP) connection.

This section shows examples for the last 5 steps as the first four are similar to the examples already shown above for the instantiation of the MessageProducer.

The Methods

createMessageConsumer(Session session, Destination destination)

This method is called with an already active Session object as well as an instantiated Destination object. It instantiates a MessageConsumer object with the given session and destination.

createMessageConsumer(Session session, Destination destination)  Expand source

read(MessageConsumer consumer)

The method is called with an already instantiated MessageConsumer object to receive a message from the MQ server. It extracts the value from the Message object as a string representation via the Message objects getText() method. 

read()  Expand source
Icon

 Don´t forget to clean up (call close()) after the message has been received.

connect(String host, int port)

This method instantiates a TCP socket connection to a JobScheduler instance with the given host:port.

connect()  Expand source

disconnect()

This method is used to close the socket opened with the above connect() method.

disconnect()  Expand source

sendRequest(String command)

This method sends a request with the given command to the connected JobScheduler instance. The command given in the example is the add_order XML snippet shown in the SOSProducer example.

 Expand source

getResponse()

This message receives the response of the TCP connection described above.

 Expand source

receiveFromQueue()

The receiveFromQueue() method uses the methods described above to connect to a host running a JobScheduler instance, read from a message queue, send the received message to the JobScheduler instance and close the session to the JobScheduler instance.

receiveFromQueue()  Expand source

The SOSConsumer class

The code example below slightly differs from the examples above. In the class below the read() method already uses the other method for instantiation, therefore it needs less parameters and it closes the connection itself.

SOSConsumer  Expand source

Maven Configuration Example

This example shows the dependency needed to build the example above as a Maven project.

Maven Configuration  Expand source