Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

This article describes how to implement Java jobs to that communicate over a Message Queue (MQ). This includes one Java These jobs include a job for publishing and one for subscribing and receivingreceiving and executing JobScheduler API commands.

The document explains which classes and methods have to be created and for which purposetheir purposes. This example was developed with the use of Apache MQ.

...

The example covers the specifics required to achieve the use case described below. It neither covers the complete job api API nor the complete possibilities of JMS.

For the coverage of the JobScheduler job api API refer to the XML part of the API Documentation.

...

  • How to implement a Java job using the job apiAPI
  • How to implement the a Producer Job
  • How to implement the a Consumer Job
  • How to execute an XML fragment using the job apiAPI
  • How to deploy the jobs to the JobScheduler
  • How to configure the JobScheduler Jobs with JOE using the developed classes

Prerequisites

To The following dependency is needed to write a Java job for the JobScheduler the following dependency is needed.

  • engine-job-api.jar
    • The library is hosted on Maven Central. Please download the jar and add it to the classpath of the Java project. 
    • If the Java project already is a maven Maven project, simply add the following dependency to the project configuration.

...

    • Make sure to use the correct version suitable for the JobScheduler version in use.

For this example the activemq-all-5.13.0.jar library is used.

  • Either download the jar file and add it to the classpath
  • or in case of a maven Maven project add the following dependency to the project configuration.

...

If the Java job is an order job, it has to return true in case no error occurs for the order to be able to continue with the next task if the job ends without an error.

If the Java job  is is a standalone job it has to return false for the JobScheduler to be able to recognize that the task has finished.

This can be achieved by using the return value spooler_job.order_queue() != null. It determines if an order is in the queue of the job (true) or not (false).

...

Because the Java Jobs run API methods, we can directly use the logging feature of the JobScheduler. As shown in the detailed method examples below, logging is done by using the spooler_log method.

Make sure to throw the catched caught exception in order to hand it over to the spooler_process() method. This is needed for a job running in a job chain to determine that it has to fail in case if an error occurs.

Initialization of the MQ Connection

Both Java jobs need some methods to initialize a connection to an MQ Server. This section of the document shows how to implement them. The implementation is based on the JMS implementation used and shipped by ActiveMQ.

To keep it simple for this example simple these methods are put in both Java job classes. To prevent duplicates in a real project´s source code, it is better to put these methods in one a single class and extend the class in the two job classes.

...

In summary the acknowledge mode AUTO_ACKNOWLEDGE mode results in the message be dequeued when one consumer has read the message from the MQ server, whereas the CLIENT_ACKNOWLEDGE puts the responsibility on the client.

When the acknowledge mode CLIENTCLIENT_ACKNOWLEDGE mode is set, the message will stay present for all consumers to read until a consumer acknowledges the message. Only then the message will be dequeued and is not be available for further consumers.

...

This is a helper method which creates a connection URI based on the job parameters. It simply uses a StringBuilder to create a string in the format [protocol]://[hostname]:[port].

...

There are three more methods needed for the producer Java job to run.:

  • createMessageProducer(Session session, Destination destination)
  • write(String text, String connectionUrl, String queueName)
  • execute()

...

The method is called with the message to send to be sent to the server, the connection URL to the server and the MessageProducer object the queue name to use for publishing. The message is a String object. The method instantiates a Message object with the text to send. This method makes use of the methods described above. It instantiates all objects needed for the connection and sends a Message object. Also the methods throws an error if one occurs it still calls the MessageProducer´s send(Message message) in a try..catch..finally block. That is because the use of the finally block to close the connection in the end.

...