Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Enterprise Messaging With Spring JMS
1. Enterprise Messaging With
Spring JMS
Bruce Snyder, Senior Software Engineer, SpringSource
Friday, July 8, 2011
2. Agenda
• Very brief introduction to JMS
• Synchronous JMS With Spring
• Asynchronous JMS With Spring
2
Friday, July 8, 2011
3. What is JMS?
• JMS is:
– An API for client-side communications with a JMS provider
– Included in Java EE
• Also stand alone
• JMS is not:
– A spec for a message broker implementation
3
Friday, July 8, 2011
11. Managed vs. Non-Managed JMS
• Managed
– JMS provider in a Java EE container
– JMS resource pooling
– Transaction support
– Support for EJBs
• Non-Managed
– Stand alone JMS provider
– Manual setup of JMS resources
– No guarantee of transactions
• Spring supports both environments
11
Friday, July 8, 2011
13. Spring JMS
• JMS Template
– Send and receive messages
synchronously
• Message Listener Container
– Receive messages asynchronously
– Message-Driven POJOs (MDPs)
13
Friday, July 8, 2011
14. JmsTemplate
Synchronous
• browse()
– Browse messages in a queue
• convertAndSend()
– Send messages synchronously
– Convert a Java object to a JMS message
• send()
– Send a message synchronously using a MessageCreator
• receive() and receiveAndConvert()
– Receive messages synchronously
• execute()
– Provides access to callbacks for more complex scenarios
• receiveSelected() and receiveSelectedAndConvert()
– Receive filtered messages synchronously
14
Friday, July 8, 2011
16. The Spring JmsTemplate
Synchronous
// Use the default destination
jmsTemplate.convertAndSend("Hello World!");
// Use a different destination
jmsTemplate.convertAndSend(“TEST.BAR”, “Hello World!”);
// Use a different destination
String textMessage1 = (String) jmsTemplate.receiveAndConvert();
// Use a different destination
String textMessage2 = (String) jmsTemplate.receiveAndConvert(“TEST.BAR”);
16
Friday, July 8, 2011
17. The Spring JmsTemplate
Synchronous
• Using send() with a MessageCreator
// Use a MessageCreator callback
jmsTemplate.send(destination, new MessageCreator() {
public Message createMessage(Session session)
throws JMSException {
TextMessage message = session.createTextMessage("Hello World!");
message.setIntProperty("someBusinessId", 22);
return message;
}
});
// Receive raw JMS message
TextMessage message = jmsTemplate.receive();
17
Friday, July 8, 2011
18. The Spring JmsTemplate
Synchronous
• Using execute() and the SessionCallback
// Use a SessionCallback
jmsTemplate.execute(new SessionCallback() {
public Object doInJms(Session session) throws JMSException {
Queue queue = session.createQueue("MY.TEST.QUEUE");
MessageProducer producer = session.createProducer(queue);
TextMessage message = session.createTextMessage("Hello World!");
producer.send(message);
}
});
18
Friday, July 8, 2011
19. The Spring JmsTemplate
Synchronous
• Using execute() with the ProducerCallback
// Use a ProducerCallback
jmsTemplate.execute(new ProducerCallback() {
public Object doInJms(Session session, MessageProducer producer)
throws JMSException {
TextMessage message = session.createTextMessage("Hello World!");
producer.send(destination, message);
}
return null;
}
});
19
Friday, July 8, 2011
20. The Spring JmsTemplate
Synchronous
• Using JMS selector expression
// Using a selector expression
String selectorExpression =
“Timestamp BETWEEN 1218048453251 AND 1218048484330”;
jmsTemplate.receiveSelected(destination, selectorExpression);
20
Friday, July 8, 2011
21. The Spring JmsTemplate
Synchronous
• Resolving JMS destinations
– DynamicDestinationResolver (default)
• Look up destinations via a simple text name
• Just calls session.createQueue() and session.createTopic()
– JndiDestinationResolver
• Option to fall back to DynamicDestinationResolver
– BeanFactoryDestinationResolver
• Look up beans that are javax.jms.Destination objects
21
Friday, July 8, 2011
23. The Spring JmsTemplate
Synchronous
• JmsException hierarchy
– Spring-specific unchecked exceptions
– Corresponds to JMSException
• Advantage
– Automatic clean up of JMS resources
23
Friday, July 8, 2011
24. The Spring JmsTemplate
Synchronous
• Automatically participates in transactions
• Provides support for:
– Java EE transactions
– Spring local transactions (Spring JmsTransactionManager)
– Spring global transactions (Spring JtaTransactionManager)
• XA requires an XA capable ConnectionFactory
• XA resource enlistment is provider specific
24
Friday, July 8, 2011
25. The Spring JmsTemplate
Synchronous
• JmsTemplate does not provide resource pooling
– Utilizes fresh connection/session for every invocation
• JMS resource pooling is responsibility of JMS provider
• Spring provides support
– SingleConnectionFactory
• Returns same connection for all calls to createConnection()
• Ignores all calls to close()
– CachingConnectionFactory
• Extends SingleConnectionFactory to add Session caching and
automatic Connection recovery
25
Friday, July 8, 2011
26. Spring JMS
• JMS Template
– Send and receive messages
synchronously
• Message Listener Container
– Receive messages asynchronously
– Message-Driven POJOs (MDPs)
26
Friday, July 8, 2011
27. Managed vs. Non-Managed JMS
Asynchronous
• Non-Managed
– JMS MessageConsumer registers a MessageListener
– Manual lifecycle management
• Managed
– EJB Message-Driven Beans
27
Friday, July 8, 2011
28. JMS Transaction Support
Asynchronous
• Non-Managed XA Transactions
– A JMS MessageConsumer can use various acknowledge
modes
• AUTO_ACKNOWLEDGE
• CLIENT_ACKNOWLEDGE
• DUPS_OK_ACKNOWLEDGE
• local JMS transaction
– Standard JMS does not support asynchronous message
consumption as part of a XA transaction
• Managed XA Transactions
– Officially supported only by EJBs
28
Friday, July 8, 2011
29. Spring Message-Driven POJOs
Asynchronous
• DefaultMessageListenerContainer
– Most commonly used container
– Allows for dynamic scaling of queue consumers
– Participates in external transactions
• SimpleMessageListenerContainer
– Very basic
– Static configuration
– No external transaction support
29
Friday, July 8, 2011
30. DefaultMessageListenerContainer
Asynchronous
• Highly configurable
– Dynamic scale up/down of consumers
• Threads managed by the container
– Customizable via the Spring TaskExecutor
• Resource caching
– Connection, Session, MessageConsumer
– Default is to cache nothing so as work in Java EE
environments
– See the setCacheLevel() method for more info
• Works in managed and non-managed environments
• Supports XA message consumption
30
Friday, July 8, 2011
31. SimpleMessageListenerContainer
Asynchronous
• No dynamic scaling of consumers
• No support for XA transactions
31
Friday, July 8, 2011
32. Supported Types of Message Listeners
Asynchronous
• javax.jms.MessageListener interface
– Standard Java EE interface
– Threading is up to you
• SessionAwareMessageListener interface
– Spring-specific interface
– Provides access to the Session object
• Very useful for request-response messaging
• MessageListenerAdapter interface
– Spring-specific interface
– Allows for type-specific message handling
– No JMS dependencies whatsoever
32
Friday, July 8, 2011
33. MessageListener
Asynchronous
• Standard JMS MessageListener
• Uses an onMessage() method
public class MyMessageListener implements MessageListener {
private static Logger LOG = Logger.getLogger(MyMessageListener.class);
public void onMessage(Message message) throws JMSException {
try {
LOG.info("Consumed message: “ + message);
// Do some processing here
} catch (JMSException e) {
LOG.error(e.getMessage(), e);
}
}
33
Friday, July 8, 2011