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.
Utility to listen for activeMQ messages
Messages are expected to be JSON strings contained either as UTF-8 in aBytesMessage 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
Consumerthat 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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidCreate a consumer to listen for activeMQ messages.Get the handler currently being used to handle messagesgetTopic()Get the currently configure activeMQ topicvoidsetHandler(Consumer<T> handler) Set the handler for received messages.voidSet the topic this listener listens to.voidshutdown()Close the activeMQ connection and stop processing messages
-
Constructor Details
-
JsonMessageListener
-
-
Method Details
-
configure
public void configure() throws javax.jms.JMSExceptionCreate 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.JMSExceptionClose the activeMQ connection and stop processing messages- Throws:
javax.jms.JMSException
-
getTopic
Get the currently configure activeMQ topic -
setTopic
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
Set the handler for received messages.- Parameters:
handler- to accept instances of this listener's message type
-
getHandler
Get the handler currently being used to handle messages
-