Package gda.util

Class JsonMessageListener<T>

java.lang.Object
gda.util.JsonMessageListener<T>
Type Parameters:
T - The class of objects that the this listener expects.

public class JsonMessageListener<T> extends Object

Utility to listen for activeMQ messages

Messages are expected to be JSON strings contained either as UTF-8 in a BytesMessage or as text in a TextMessage. JSON is converted to the configured class and passed to a configurable handler.

This allows the handling of the received message to unaware of its source and not have to manage activeMQ connections.

Example configuration

Configuration requires
  • a FQCN as a constructor argument. This is the class that messages are expected to be.
  • The topic is the activeMQ topic that is used to broadcast the messaged.
  • The handler should be an instance of Consumer that accepts instances of the class specified above.
 
 <bean id="processingListener" class="gda.util.MessageListener"
         c:type="org.opengda.lde.ReductionResponse">
     <property name="topic" value="gda.messages.calibration.xrd2" />
     <property name="handler" ref="processingHandler" />
 </bean>
 
 
  • Constructor Details

    • JsonMessageListener

      public JsonMessageListener(Class<T> type)
  • Method Details

    • configure

      public void configure() throws javax.jms.JMSException
      Create a consumer to listen for activeMQ messages. If a consumer already exists, close existing connection
      Throws:
      javax.jms.JMSException
    • shutdown

      public void shutdown() throws javax.jms.JMSException
      Close the activeMQ connection and stop processing messages
      Throws:
      javax.jms.JMSException
    • getTopic

      public String getTopic()
      Get the currently configure activeMQ topic
    • setTopic

      public void setTopic(String topic) throws javax.jms.JMSException
      Set the topic this listener listens to. If this listener is already configured, shutdown existing consumer and reconfigure with new topic.
      Throws:
      javax.jms.JMSException - if either shutdown or configuration fail
    • setHandler

      public void setHandler(Consumer<T> handler)
      Set the handler for received messages.
      Parameters:
      handler - to accept instances of this listener's message type
    • getHandler

      public Consumer<T> getHandler()
      Get the handler currently being used to handle messages