SlideShare une entreprise Scribd logo
1  sur  31
Spring way of messaging
JMS Basics
 The application hands the message to be send to a
message broker.
 The message broker ensures the message delivery to
the specified destination.
 JMS messages are addressed with a destination.
 Destination are of two types: queues (point-to-point
model) and topics (publish-subscribe model).
Point-to-point & Publish-subscribe
 Point-to-Point:
 Each message has exactly one sender and one receiver.
 When a receiver asks for the next message in the queue, the
message is removed from the queue and delivered to the
receiver.
 Each message in a message queue is delivered to only one
receiver.
 Publish-Subscribe:
 Messages are sent to a topic which is listened by multiple
receivers.
 All subscribers to a topic will receive a copy of the message.
Benefits of JMS
 The client doesn’t need to wait around for it to be
processed or even delivered.
 JMS messages are data-centric and the client isn’t fixed
to a specific method signature.
 JMS clients are unaware of service location and only
know the queue or topic through which the messages
will be sent.
 JMS clients are assured of guaranteed message delivery
even if the service is unavailable when a message is
sent and is stored until the service is available again.
Setting up ActiveMQ in Spring
 Add the incubator-activemq-4.1.0.jar to the application’s
classpath to access ActiveMQ’s API.
 ActiveMQConnectionFactory is the JMS connection factory
for ActiveMQ, to send messages through the message
broker.
 <bean id="connectionFactory"
 class="org.apache.activemq.ActiveMQConnectionFactory">
 <property name="brokerURL"
value="tcp://localhost:61616"/>
 </bean>
 The brokerURL property tells the connection factory where
the message broker is located
Declaring an ActiveMQ message
destination
 A message destination is declared either a queue or topic, for
passing on the messages.
 ActiveMQ queue Declaration:
 <bean id="rantzDestination"
 class="org.apache.activemq.command.ActiveMQQueue">
 <constructor-arg index="0" value="rantz.marketing.queue"/>
 </bean>
 ActiveMQ topic Declaration:
 <bean id="rantzDestination"
 class="org.apache.activemq.command.ActiveMQTopic">
 <constructor-arg index="0" value="rantz.marketing.topic"/>
 </bean>
Conventional JMS Sender
 ConnectionFactory cf =
 new ActiveMQConnectionFactory("tcp://localhost:61616");
 try {
 conn = cf.createConnection();
 session = conn.createSession(false,
 Session.AUTO_ACKNOWLEDGE);
 Destination destination = new ActiveMQQueue("myQueue");
 MessageProducer producer =
 session.createProducer(destination);
 TextMessage message = session.createTextMessage();
 message.setText("Hello world!");
 producer.send(message);
 } catch (JMSException e) { // handle Message}
Conventional JMS Receiver
 ConnectionFactory cf =
 new ActiveMQConnectionFactory("tcp://localhost:61616");
 try {
 conn = cf.createConnection();
 session = conn.createSession(false,
 Session.AUTO_ACKNOWLEDGE);
 Destination destination = new ActiveMQQueue("myQueue");
 MessageConsumer consumer = session.createConsumer(destination);
 conn.start();
 Message message = consumer.receive();
 TextMessage textMessage = (TextMessage) message;
 System.out.println("GOT A MESSAGE: " + textMessage.getText());
 } catch (JMSException e) { // handle Message}
 } finally { … }
JMS Template
 JMS Template handles creating a connection,
obtaining a session, and the actual sending and
receiving of messages.
 It handles any clumsy JMSException that may be
thrown along the way.
 When JMSException is thrown, JmsTemplate will
catch it and rethrow it as one of the unchecked
subclasses of JmsException in
org.springframework.jms.* package.
Wiring the JMS Template
 JmsTemplate needs to get connections to the message
broker, and hence sets a connectionFactory property
with a bean that implements JMS’s ConnectionFactory
interface.
 <bean id="jmsTemplate"
 class="org.springframework.jms.core.JmsTemplate">
 <property name="connectionFactory"
ref="connectionFactory" />
 </bean>
Sending Messages with JMS Templates
 public void sendMotoristInfo(final Motorist motorist) {
 jmsTemplate.send( destination, new MessageCreator() {
 public Message createMessage(Session session)
 throws JMSException {
 MapMessage message = session.createMapMessage();
 message.setString("Name", motorist.getName());
 return message;
 }
 });
 }
 private JmsTemplate jmsTemplate; // Injected
 private Destination destination; // Injected
Setting a default destination
 <bean id="jmsTemplate"
 class="org.springframework.jms.core.JmsTemplate">
 <property name="connectionFactory" ref="connectionFactory" />
 <property name="defaultDestination" ref="rantzDestination" />
 </bean>
 The call to JmsTemplate’s send() method can be simplified slightly by
removing the first parameter:
 jmsTemplate.send(
 new MessageCreator() {
 …
 });
 JmsTemplate assumes that the message sent to the default destination
when the send() method only takes a MessageCreator.
Consuming messages
 public SpammedMotorist receiveSpammedMotorist() {
 MapMessage message = (MapMessage) jmsTemplate.receive();
 SpammedMotorist motorist = new SpammedMotorist();
 try {
 motorist.setFirstName(message.getString("firstName"));
 motorist.setLastName(message.getString("lastName"));
 motorist.setEmail(message.getString("email"));
 } catch (JMSException e) {
 throw JmsUtils.convertJmsAccessException(e);
 }
 return motorist;
 }
 private JmsTemplate jmsTemplate; //injected
Consuming messages: Timeout
 JmsTemplate’s receive() method is synchronous, blocking the call
until a message appears on the destination.
 The receiveTimeout property can be used to configure
JmsTemplate to time out on receives after specified time interval.
 <bean id="jmsTemplate"
 class="org.springframework.jms.core.JmsTemplate">
 <property name="connectionFactory" ref="connectionFactory" />
 <property name="defaultDestination" ref="rantzDestination" />
 <property name="receiveTimeout" value="60000" />
 </bean>
Converting messages
 Spring supports message conversion through its
MessageConverter interface:
 public interface MessageConverter {
 public Message toMessage(Object object, Session session);
 public Object fromMessage(Message message);
 }
 For sending messages, the toMessage() method converts an
object to a Message.
 On the receiving end, the fromMessage() method converts
an incoming Message into an Object.
Implementation of MessageConverter
 public class MotoristMessageConverter implements MessageConverter {
 public Object fromMessage(Message message) throws JMSException, MessageConversionException {
 if(!(message instanceof MapMessage)) {
 throw new MessageConversionException("Message isn't a MapMessage"); }
 MapMessage mapMessage = (MapMessage) message;
 SpammedMotorist motorist = new SpammedMotorist();
 motorist.setFirstName(mapMessage.getString("firstName"));
 return motorist;
 }
 public Message toMessage(Object object, Session session) throws JMSException,
MessageConversionException {
 if(!(object instanceof Motorist)) {
 throw new MessageConversionException("Object isn't a Motorist"); }
 Motorist motorist = (Motorist) object;
 MapMessage message = session.createMapMessage();
 message.setString("firstName", motorist.getFirstName());
 return message;
 }
 }
Sending/Receiving converted messages
 The convertAndSend() method of JmsTemplate can be
simply called instead of explicitly calling the toMessage()
method before sending a message.
 JmsTemplate’s convertAndSend() method automatically
calls the toMessage() method before sending the message
to the destination.
 The receiveAndConvert() method of JmsTemplate can be
simply called instead of explicitly calling the fromMessage()
to convert the message returned from JmsTemplate’s
receive().
 Unless specified, both the convertAndSend() and
receiveAndConvert(), send and receive messages
respectively from the default destination.
Wiring a message converter
 <bean id="motoristConverter"
 class="com.roadrantz.marketing.MotoristMessageConverter" /
 <bean id="jmsTemplate"
 class="org.springframework.jms.core.JmsTemplate">
 <property name="connectionFactory" ref="connectionFactory" />
 <property name="defaultDestination" ref="rantzDestination" />
 <property name="messageConverter"
 ref="motoristConverter" />
 </bean>
Spring JMS Gateway Support
 public class RantzMarketingGatewayImpl extends JmsGatewaySupport
 implements RantzMarketingGateway {
 public void sendMotoristInfo(final Motorist motorist) {
 getJmsTemplate().send("rantz.marketing.queue",
 new MessageCreator() {
 public Message createMessage(Session session)
 throws JMSException {
 MapMessage message = session.createMapMessage();
 message.setString("firstName", motorist.getFirstName());
 return message;
 }
 });
 }
 }
Spring JMS Gateway Support
 A JmsTemplate object can be directly injected into the
jmsTemplate property, or by wiring the connection factory
into the connectionFactory property.
 JmsGatewaySupport automatically creates a JmsTemplate
object based on the injected connection factory bean wired
into the connectionFactory property.
 Shortcomings to wiring connection factory are as follows:
 Can only specify a default destination on a JmsTemplate.
 Can only wire a message converter into a JmsTemplate.
Spring Message Driven Beans
 public class MarketingMdp implements MessageListener {
 public void onMessage(Message message) {
 MapMessage mapMessage = (MapMessage) message;
 try {
 SpammedMotorist motorist = new SpammedMotorist();
 motorist.setFirstName(mapMessage.getString("firstName"));
 motorist.setLastName(mapMessage.getString("lastName"));
 motorist.setEmail(mapMessage.getString("email"));
 processMotoristInfo(motorist);
 } catch (JMSException e) { // handle—somehow }
 }
 private void processMotoristInfo(SpammedMotorist motorist) { … }
 }
 <bean id="rantzMdp“ class="com.roadrantz.marketing.MarketingMdp" />
Message Listener Container
 A message listener container watches a JMS destination for
messages and retrieves the messages on arrival.
 The retrieved messages are passed on to a MessageListener
implementation by calling the onMessage() method.
 onMessage() method is to receive and translate the message.
 SimpleMessageListenerContainer is the simplest of the message
listener containers, configured as follows:
 <bean class="org.springframework.jms.listener.
 ➥ SimpleMessageListenerContainer">
 <property name="connectionFactory" ref="connectionFactory" />
 <property name="destination" ref="rantzDestination" />
 <property name="messageListener" ref="rantzMdp" />
 </bean>
Spring Message Listener Containers
Container class
org.springframework.jms.listener.*
Operation
SimpleMessageListenerContainer This is the simplest message listener
container. It works with a fixed number of
JMS sessions and does not support
transactions.
DefaultMessageListenerContainer This message listener container builds upon
SimpleMessageListenerContainer by
adding support for transactions
serversession.ServerSessionMessage
ListenerContainer
This is the most powerful of the message
listener containers. Like
DefaultMessageListenerContainer, it
supports transactions. However it is unique
in that it allows for dynamic management
of JMS sessions.
Transactional MDPs
 DefaultMessageListenerContainer adds transaction support to the
SimpleMessageListenerContainer.
 <bean class="org.springframework.jms.listener.➥ DefaultMessageListenerContainer">
 <property name="connectionFactory" ref="connectionFactory" />
 <property name="destination" ref="rantzDestination" />
 <property name="messageListener" ref="rantzMdp" />
 <property name="transactionManager" ref="jmsTransactionManager" />
 </bean>
 The transactionManager property is wired with a reference to a transaction manager such
as JmsTransactionManager:
 <bean id="jmsTransactionManager“ class="org.springframework.jms.connection.
 ➥ JmsTransactionManager">
 <property name="connectionFactory" ref="connectionFactory" />
 </bean>
 The transactionManager property of DefaultMessageListenerContainer is optional.
Pure POJO MDP
 The message listener container calls the onMessage() method
when a message arrives, when its messageListener property
being wired with an implementation of MessageListener.
 <bean class="org.springframework.jms.listener.
 ➥ SimpleMessageListenerContainer">
 <property name="connectionFactory" ref="connectionFactory"
/>
 <property name="destination" ref="rantzDestination" />
 <property name="messageListener" ref="purePojoMdp" />
 </bean>
MessageListenerAdapter
 MessageListenerAdapter is a MessageListener that delegates to a bean and
method.
 By default, MessageListenerAdapter calls a handleMessage() method
when a message arrives.
 <bean id="purePojoMdp“
class="org.springframework.jms.listener.adapter.
 ➥ MessageListenerAdapter">
 <property name="delegate" ref="rantzMdp" />
 <property name="defaultListenerMethod“ value="processMotoristInfo" />
 </bean>
 The MessageListenerAdapter above calls the processMotoristInfo()
method (as it’s the defaultListenerMethod) on the rantzMdp bean when a
message arrives on the destination.
 When MessageListenerAdapter receives a message, it considers the
message type and the value of the defaultListenerMethod and tries to find
an appropriate listener method signature to invoke.
Mapping of JMS Message to MDP
Message type Method parameter
TextMessage String or TextMessage
MapMessage java.util.Map or MapMessage
BytesMessage byte[] or BytesMessage
ObjectMessage java.io.Serializable or ObjectMessage
 The listener method can be written to take either
the original JMS message or a simpler type.
Converting MDP messages
 Spring message converters are used to translate messages to and
from domain-specific Java types.
 MessageListenerAdapter has a messageConverter property to set
the corresponding message converter.
 <bean id="purePojoMdp“
 class="org.springframework.jms.listener.adapter.
 ➥ MessageListenerAdapter">
 <property name="delegate" ref="rantzMdp" />
 <property name="defaultListenerMethod“
value="processMotoristInfo" />
 <property name="messageConverter" ref="motoristConverter" />
 </bean>
Lingo
 Lingo is a Spring-based remoting option that bridges
the gap between RPC and asynchronous messaging.
 Lingo provides a service exporter to export bean
functionality as a Lingo service and a clientside proxy
to transparently wire a reference to a remote Lingo
service on the calling side.
 Lingo remoting carries information over a JMS queue
or topic, making the service call held in the
queue/topic until the service is available again.
Exporting Lingo Service
 Lingo provides JmsServiceExporter, a service exporter, were
services are made available through JMS.
 The service implementation implements serviceInterface and is a
pure POJO wired into a MessageListenerAdapter to be used as
message-driven POJO.
 <bean id="server"
 class="org.logicblaze.lingo.jms.JmsServiceExporter">
 <property name="connectionFactory" ref="connectionFactory" />
 <property name="destination" ref="destination" />
 <property name="service" ref="rantzMdp" />
 <property name="serviceInterface"
 value="com.roadrantz.marketing.MarketingService" />
 </bean>
Lingo JMS Proxy
 Lingo provides JmsProxyFactoryBean, a proxy factory bean that
produces proxies to remote Lingo-exported services.
 <bean id="marketing"
 class="org.logicblaze.lingo.jms.JmsProxyFactoryBean">
 <property name="connectionFactory" ref="connectionFactory" />
 <property name="destination" ref="destination" />
 <property name="serviceInterface"
 value="com.roadrantz.marketing.MarketingService" />
 </bean>
 Invoking a Lingo-exported service is no different from invoking an
RMI service, a web service, or even a method on another bean.

Contenu connexe

Tendances

REST APIs with Spring
REST APIs with SpringREST APIs with Spring
REST APIs with SpringJoshua Long
 
Spring Framework - AOP
Spring Framework - AOPSpring Framework - AOP
Spring Framework - AOPDzmitry Naskou
 
Spring boot Introduction
Spring boot IntroductionSpring boot Introduction
Spring boot IntroductionJeevesh Pandey
 
Introduction to Apache Camel
Introduction to Apache CamelIntroduction to Apache Camel
Introduction to Apache CamelClaus Ibsen
 
Workshop spring session 2 - La persistance au sein des applications Java
Workshop spring   session 2 - La persistance au sein des applications JavaWorkshop spring   session 2 - La persistance au sein des applications Java
Workshop spring session 2 - La persistance au sein des applications JavaAntoine Rey
 
Spring - Part 2 - Autowiring, Annotations, Java based Configuration - slides
Spring - Part 2 - Autowiring, Annotations, Java based Configuration - slidesSpring - Part 2 - Autowiring, Annotations, Java based Configuration - slides
Spring - Part 2 - Autowiring, Annotations, Java based Configuration - slidesHitesh-Java
 
Event Driven Systems with Spring Boot, Spring Cloud Streams and Kafka
Event Driven Systems with Spring Boot, Spring Cloud Streams and KafkaEvent Driven Systems with Spring Boot, Spring Cloud Streams and Kafka
Event Driven Systems with Spring Boot, Spring Cloud Streams and KafkaVMware Tanzu
 
Java Thread Synchronization
Java Thread SynchronizationJava Thread Synchronization
Java Thread SynchronizationBenj Del Mundo
 
Using the JMS 2.0 API with Apache Pulsar - Pulsar Virtual Summit Europe 2021
Using the JMS 2.0 API with Apache Pulsar - Pulsar Virtual Summit Europe 2021Using the JMS 2.0 API with Apache Pulsar - Pulsar Virtual Summit Europe 2021
Using the JMS 2.0 API with Apache Pulsar - Pulsar Virtual Summit Europe 2021StreamNative
 

Tendances (20)

REST APIs with Spring
REST APIs with SpringREST APIs with Spring
REST APIs with Spring
 
Spring Framework - AOP
Spring Framework - AOPSpring Framework - AOP
Spring Framework - AOP
 
Spring Boot
Spring BootSpring Boot
Spring Boot
 
Spring Core
Spring CoreSpring Core
Spring Core
 
Support distributed computing and caching avec hazelcast
Support distributed computing and caching avec hazelcastSupport distributed computing and caching avec hazelcast
Support distributed computing and caching avec hazelcast
 
Spring Security 5
Spring Security 5Spring Security 5
Spring Security 5
 
Spring boot Introduction
Spring boot IntroductionSpring boot Introduction
Spring boot Introduction
 
Introduction to spring boot
Introduction to spring bootIntroduction to spring boot
Introduction to spring boot
 
Introduction to Apache Camel
Introduction to Apache CamelIntroduction to Apache Camel
Introduction to Apache Camel
 
Workshop spring session 2 - La persistance au sein des applications Java
Workshop spring   session 2 - La persistance au sein des applications JavaWorkshop spring   session 2 - La persistance au sein des applications Java
Workshop spring session 2 - La persistance au sein des applications Java
 
Maven Introduction
Maven IntroductionMaven Introduction
Maven Introduction
 
Jsf presentation
Jsf presentationJsf presentation
Jsf presentation
 
Spring - Part 2 - Autowiring, Annotations, Java based Configuration - slides
Spring - Part 2 - Autowiring, Annotations, Java based Configuration - slidesSpring - Part 2 - Autowiring, Annotations, Java based Configuration - slides
Spring - Part 2 - Autowiring, Annotations, Java based Configuration - slides
 
Event Driven Systems with Spring Boot, Spring Cloud Streams and Kafka
Event Driven Systems with Spring Boot, Spring Cloud Streams and KafkaEvent Driven Systems with Spring Boot, Spring Cloud Streams and Kafka
Event Driven Systems with Spring Boot, Spring Cloud Streams and Kafka
 
Workshop 21: React Router
Workshop 21: React RouterWorkshop 21: React Router
Workshop 21: React Router
 
Java Thread Synchronization
Java Thread SynchronizationJava Thread Synchronization
Java Thread Synchronization
 
Using the JMS 2.0 API with Apache Pulsar - Pulsar Virtual Summit Europe 2021
Using the JMS 2.0 API with Apache Pulsar - Pulsar Virtual Summit Europe 2021Using the JMS 2.0 API with Apache Pulsar - Pulsar Virtual Summit Europe 2021
Using the JMS 2.0 API with Apache Pulsar - Pulsar Virtual Summit Europe 2021
 
Spring Boot
Spring BootSpring Boot
Spring Boot
 
JavaScript Promises
JavaScript PromisesJavaScript Promises
JavaScript Promises
 
Fetch API Talk
Fetch API TalkFetch API Talk
Fetch API Talk
 

En vedette

Spring JMS and ActiveMQ
Spring JMS and ActiveMQSpring JMS and ActiveMQ
Spring JMS and ActiveMQGeert Pante
 
Enterprise Messaging With ActiveMQ and Spring JMS
Enterprise Messaging With ActiveMQ and Spring JMSEnterprise Messaging With ActiveMQ and Spring JMS
Enterprise Messaging With ActiveMQ and Spring JMSBruce Snyder
 
Introduction to JMS and Message-Driven POJOs
Introduction to JMS and Message-Driven POJOsIntroduction to JMS and Message-Driven POJOs
Introduction to JMS and Message-Driven POJOsMatt Stine
 
Enterprise Messaging With Spring JMS
Enterprise Messaging With Spring JMSEnterprise Messaging With Spring JMS
Enterprise Messaging With Spring JMSBruce Snyder
 
Log management with ELK
Log management with ELKLog management with ELK
Log management with ELKGeert Pante
 
jpa-hibernate-presentation
jpa-hibernate-presentationjpa-hibernate-presentation
jpa-hibernate-presentationJohn Slick
 
Spring + JPA + DAO Step by Step
Spring + JPA + DAO Step by StepSpring + JPA + DAO Step by Step
Spring + JPA + DAO Step by StepGuo Albert
 
JPA - Java Persistence API
JPA - Java Persistence APIJPA - Java Persistence API
JPA - Java Persistence APIThomas Wöhlke
 
Java Persistence API (JPA) - A Brief Overview
Java Persistence API (JPA) - A Brief OverviewJava Persistence API (JPA) - A Brief Overview
Java Persistence API (JPA) - A Brief OverviewCraig Dickson
 
Java Persistence API (JPA) Step By Step
Java Persistence API (JPA) Step By StepJava Persistence API (JPA) Step By Step
Java Persistence API (JPA) Step By StepGuo Albert
 

En vedette (11)

Spring JMS and ActiveMQ
Spring JMS and ActiveMQSpring JMS and ActiveMQ
Spring JMS and ActiveMQ
 
Enterprise Messaging With ActiveMQ and Spring JMS
Enterprise Messaging With ActiveMQ and Spring JMSEnterprise Messaging With ActiveMQ and Spring JMS
Enterprise Messaging With ActiveMQ and Spring JMS
 
Introduction to JMS and Message-Driven POJOs
Introduction to JMS and Message-Driven POJOsIntroduction to JMS and Message-Driven POJOs
Introduction to JMS and Message-Driven POJOs
 
Enterprise Messaging With Spring JMS
Enterprise Messaging With Spring JMSEnterprise Messaging With Spring JMS
Enterprise Messaging With Spring JMS
 
Log management with ELK
Log management with ELKLog management with ELK
Log management with ELK
 
jpa-hibernate-presentation
jpa-hibernate-presentationjpa-hibernate-presentation
jpa-hibernate-presentation
 
Spring + JPA + DAO Step by Step
Spring + JPA + DAO Step by StepSpring + JPA + DAO Step by Step
Spring + JPA + DAO Step by Step
 
JPA - Java Persistence API
JPA - Java Persistence APIJPA - Java Persistence API
JPA - Java Persistence API
 
Java Persistence API (JPA) - A Brief Overview
Java Persistence API (JPA) - A Brief OverviewJava Persistence API (JPA) - A Brief Overview
Java Persistence API (JPA) - A Brief Overview
 
Java Persistence API (JPA) Step By Step
Java Persistence API (JPA) Step By StepJava Persistence API (JPA) Step By Step
Java Persistence API (JPA) Step By Step
 
Support JEE Spring Inversion de Controle IOC et Spring MVC
Support JEE Spring Inversion de Controle IOC et Spring MVCSupport JEE Spring Inversion de Controle IOC et Spring MVC
Support JEE Spring Inversion de Controle IOC et Spring MVC
 

Similaire à Spring JMS (20)

Jms
JmsJms
Jms
 
Jms
JmsJms
Jms
 
Jms
JmsJms
Jms
 
Jms intro
Jms introJms intro
Jms intro
 
Jms
JmsJms
Jms
 
Jms
JmsJms
Jms
 
4Developers: Dominik Przybysz- Message Brokers
4Developers: Dominik Przybysz- Message Brokers4Developers: Dominik Przybysz- Message Brokers
4Developers: Dominik Przybysz- Message Brokers
 
Send Sms with SmsManager Api In Android with Kotlin
Send Sms with SmsManager Api In Android with KotlinSend Sms with SmsManager Api In Android with Kotlin
Send Sms with SmsManager Api In Android with Kotlin
 
Jms слайды
Jms слайдыJms слайды
Jms слайды
 
Weblogic - Introduction to configure JMS
Weblogic  - Introduction to configure JMSWeblogic  - Introduction to configure JMS
Weblogic - Introduction to configure JMS
 
Jsr120 sup
Jsr120 supJsr120 sup
Jsr120 sup
 
test validation
test validationtest validation
test validation
 
Test DB user
Test DB userTest DB user
Test DB user
 
Mule jms
Mule   jmsMule   jms
Mule jms
 
Spring ws
Spring wsSpring ws
Spring ws
 
Automapper
AutomapperAutomapper
Automapper
 
Message processor in mule
Message processor in muleMessage processor in mule
Message processor in mule
 
Mule message processor or routers
Mule message processor or routersMule message processor or routers
Mule message processor or routers
 
Jms introduction
Jms introductionJms introduction
Jms introduction
 
Jms topics
Jms topicsJms topics
Jms topics
 

Plus de Emprovise

Highlights of AWS ReInvent 2023 (Announcements and Best Practices)
Highlights of AWS ReInvent 2023 (Announcements and Best Practices)Highlights of AWS ReInvent 2023 (Announcements and Best Practices)
Highlights of AWS ReInvent 2023 (Announcements and Best Practices)Emprovise
 
Leadership and Success Lessons for Life/Business
Leadership and Success Lessons for Life/BusinessLeadership and Success Lessons for Life/Business
Leadership and Success Lessons for Life/BusinessEmprovise
 
Secure socket layer
Secure socket layerSecure socket layer
Secure socket layerEmprovise
 
Effective java
Effective javaEffective java
Effective javaEmprovise
 
EJB3 Advance Features
EJB3 Advance FeaturesEJB3 Advance Features
EJB3 Advance FeaturesEmprovise
 
Enterprise Java Beans 3 - Business Logic
Enterprise Java Beans 3 - Business LogicEnterprise Java Beans 3 - Business Logic
Enterprise Java Beans 3 - Business LogicEmprovise
 
RESTful WebServices
RESTful WebServicesRESTful WebServices
RESTful WebServicesEmprovise
 
J2EE Patterns
J2EE PatternsJ2EE Patterns
J2EE PatternsEmprovise
 
Spring Web Services
Spring Web ServicesSpring Web Services
Spring Web ServicesEmprovise
 
Spring Web Webflow
Spring Web WebflowSpring Web Webflow
Spring Web WebflowEmprovise
 
Spring Web Views
Spring Web ViewsSpring Web Views
Spring Web ViewsEmprovise
 
Enterprise Spring
Enterprise SpringEnterprise Spring
Enterprise SpringEmprovise
 
Spring Basics
Spring BasicsSpring Basics
Spring BasicsEmprovise
 
Apache Struts 2 Advance
Apache Struts 2 AdvanceApache Struts 2 Advance
Apache Struts 2 AdvanceEmprovise
 
Apache Struts 2 Framework
Apache Struts 2 FrameworkApache Struts 2 Framework
Apache Struts 2 FrameworkEmprovise
 
Java Servlets
Java ServletsJava Servlets
Java ServletsEmprovise
 
Java Advance Concepts
Java Advance ConceptsJava Advance Concepts
Java Advance ConceptsEmprovise
 

Plus de Emprovise (20)

Highlights of AWS ReInvent 2023 (Announcements and Best Practices)
Highlights of AWS ReInvent 2023 (Announcements and Best Practices)Highlights of AWS ReInvent 2023 (Announcements and Best Practices)
Highlights of AWS ReInvent 2023 (Announcements and Best Practices)
 
Leadership and Success Lessons for Life/Business
Leadership and Success Lessons for Life/BusinessLeadership and Success Lessons for Life/Business
Leadership and Success Lessons for Life/Business
 
Secure socket layer
Secure socket layerSecure socket layer
Secure socket layer
 
Effective java
Effective javaEffective java
Effective java
 
EJB3 Advance Features
EJB3 Advance FeaturesEJB3 Advance Features
EJB3 Advance Features
 
Enterprise Java Beans 3 - Business Logic
Enterprise Java Beans 3 - Business LogicEnterprise Java Beans 3 - Business Logic
Enterprise Java Beans 3 - Business Logic
 
EJB3 Basics
EJB3 BasicsEJB3 Basics
EJB3 Basics
 
RESTful WebServices
RESTful WebServicesRESTful WebServices
RESTful WebServices
 
J2EE Patterns
J2EE PatternsJ2EE Patterns
J2EE Patterns
 
JMS
JMSJMS
JMS
 
Spring Web Services
Spring Web ServicesSpring Web Services
Spring Web Services
 
Spring Web Webflow
Spring Web WebflowSpring Web Webflow
Spring Web Webflow
 
Spring Web Views
Spring Web ViewsSpring Web Views
Spring Web Views
 
Spring MVC
Spring MVCSpring MVC
Spring MVC
 
Enterprise Spring
Enterprise SpringEnterprise Spring
Enterprise Spring
 
Spring Basics
Spring BasicsSpring Basics
Spring Basics
 
Apache Struts 2 Advance
Apache Struts 2 AdvanceApache Struts 2 Advance
Apache Struts 2 Advance
 
Apache Struts 2 Framework
Apache Struts 2 FrameworkApache Struts 2 Framework
Apache Struts 2 Framework
 
Java Servlets
Java ServletsJava Servlets
Java Servlets
 
Java Advance Concepts
Java Advance ConceptsJava Advance Concepts
Java Advance Concepts
 

Dernier

08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessPixlogix Infotech
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 

Dernier (20)

08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 

Spring JMS

  • 1. Spring way of messaging
  • 2. JMS Basics  The application hands the message to be send to a message broker.  The message broker ensures the message delivery to the specified destination.  JMS messages are addressed with a destination.  Destination are of two types: queues (point-to-point model) and topics (publish-subscribe model).
  • 3. Point-to-point & Publish-subscribe  Point-to-Point:  Each message has exactly one sender and one receiver.  When a receiver asks for the next message in the queue, the message is removed from the queue and delivered to the receiver.  Each message in a message queue is delivered to only one receiver.  Publish-Subscribe:  Messages are sent to a topic which is listened by multiple receivers.  All subscribers to a topic will receive a copy of the message.
  • 4. Benefits of JMS  The client doesn’t need to wait around for it to be processed or even delivered.  JMS messages are data-centric and the client isn’t fixed to a specific method signature.  JMS clients are unaware of service location and only know the queue or topic through which the messages will be sent.  JMS clients are assured of guaranteed message delivery even if the service is unavailable when a message is sent and is stored until the service is available again.
  • 5. Setting up ActiveMQ in Spring  Add the incubator-activemq-4.1.0.jar to the application’s classpath to access ActiveMQ’s API.  ActiveMQConnectionFactory is the JMS connection factory for ActiveMQ, to send messages through the message broker.  <bean id="connectionFactory"  class="org.apache.activemq.ActiveMQConnectionFactory">  <property name="brokerURL" value="tcp://localhost:61616"/>  </bean>  The brokerURL property tells the connection factory where the message broker is located
  • 6. Declaring an ActiveMQ message destination  A message destination is declared either a queue or topic, for passing on the messages.  ActiveMQ queue Declaration:  <bean id="rantzDestination"  class="org.apache.activemq.command.ActiveMQQueue">  <constructor-arg index="0" value="rantz.marketing.queue"/>  </bean>  ActiveMQ topic Declaration:  <bean id="rantzDestination"  class="org.apache.activemq.command.ActiveMQTopic">  <constructor-arg index="0" value="rantz.marketing.topic"/>  </bean>
  • 7. Conventional JMS Sender  ConnectionFactory cf =  new ActiveMQConnectionFactory("tcp://localhost:61616");  try {  conn = cf.createConnection();  session = conn.createSession(false,  Session.AUTO_ACKNOWLEDGE);  Destination destination = new ActiveMQQueue("myQueue");  MessageProducer producer =  session.createProducer(destination);  TextMessage message = session.createTextMessage();  message.setText("Hello world!");  producer.send(message);  } catch (JMSException e) { // handle Message}
  • 8. Conventional JMS Receiver  ConnectionFactory cf =  new ActiveMQConnectionFactory("tcp://localhost:61616");  try {  conn = cf.createConnection();  session = conn.createSession(false,  Session.AUTO_ACKNOWLEDGE);  Destination destination = new ActiveMQQueue("myQueue");  MessageConsumer consumer = session.createConsumer(destination);  conn.start();  Message message = consumer.receive();  TextMessage textMessage = (TextMessage) message;  System.out.println("GOT A MESSAGE: " + textMessage.getText());  } catch (JMSException e) { // handle Message}  } finally { … }
  • 9. JMS Template  JMS Template handles creating a connection, obtaining a session, and the actual sending and receiving of messages.  It handles any clumsy JMSException that may be thrown along the way.  When JMSException is thrown, JmsTemplate will catch it and rethrow it as one of the unchecked subclasses of JmsException in org.springframework.jms.* package.
  • 10. Wiring the JMS Template  JmsTemplate needs to get connections to the message broker, and hence sets a connectionFactory property with a bean that implements JMS’s ConnectionFactory interface.  <bean id="jmsTemplate"  class="org.springframework.jms.core.JmsTemplate">  <property name="connectionFactory" ref="connectionFactory" />  </bean>
  • 11. Sending Messages with JMS Templates  public void sendMotoristInfo(final Motorist motorist) {  jmsTemplate.send( destination, new MessageCreator() {  public Message createMessage(Session session)  throws JMSException {  MapMessage message = session.createMapMessage();  message.setString("Name", motorist.getName());  return message;  }  });  }  private JmsTemplate jmsTemplate; // Injected  private Destination destination; // Injected
  • 12. Setting a default destination  <bean id="jmsTemplate"  class="org.springframework.jms.core.JmsTemplate">  <property name="connectionFactory" ref="connectionFactory" />  <property name="defaultDestination" ref="rantzDestination" />  </bean>  The call to JmsTemplate’s send() method can be simplified slightly by removing the first parameter:  jmsTemplate.send(  new MessageCreator() {  …  });  JmsTemplate assumes that the message sent to the default destination when the send() method only takes a MessageCreator.
  • 13. Consuming messages  public SpammedMotorist receiveSpammedMotorist() {  MapMessage message = (MapMessage) jmsTemplate.receive();  SpammedMotorist motorist = new SpammedMotorist();  try {  motorist.setFirstName(message.getString("firstName"));  motorist.setLastName(message.getString("lastName"));  motorist.setEmail(message.getString("email"));  } catch (JMSException e) {  throw JmsUtils.convertJmsAccessException(e);  }  return motorist;  }  private JmsTemplate jmsTemplate; //injected
  • 14. Consuming messages: Timeout  JmsTemplate’s receive() method is synchronous, blocking the call until a message appears on the destination.  The receiveTimeout property can be used to configure JmsTemplate to time out on receives after specified time interval.  <bean id="jmsTemplate"  class="org.springframework.jms.core.JmsTemplate">  <property name="connectionFactory" ref="connectionFactory" />  <property name="defaultDestination" ref="rantzDestination" />  <property name="receiveTimeout" value="60000" />  </bean>
  • 15. Converting messages  Spring supports message conversion through its MessageConverter interface:  public interface MessageConverter {  public Message toMessage(Object object, Session session);  public Object fromMessage(Message message);  }  For sending messages, the toMessage() method converts an object to a Message.  On the receiving end, the fromMessage() method converts an incoming Message into an Object.
  • 16. Implementation of MessageConverter  public class MotoristMessageConverter implements MessageConverter {  public Object fromMessage(Message message) throws JMSException, MessageConversionException {  if(!(message instanceof MapMessage)) {  throw new MessageConversionException("Message isn't a MapMessage"); }  MapMessage mapMessage = (MapMessage) message;  SpammedMotorist motorist = new SpammedMotorist();  motorist.setFirstName(mapMessage.getString("firstName"));  return motorist;  }  public Message toMessage(Object object, Session session) throws JMSException, MessageConversionException {  if(!(object instanceof Motorist)) {  throw new MessageConversionException("Object isn't a Motorist"); }  Motorist motorist = (Motorist) object;  MapMessage message = session.createMapMessage();  message.setString("firstName", motorist.getFirstName());  return message;  }  }
  • 17. Sending/Receiving converted messages  The convertAndSend() method of JmsTemplate can be simply called instead of explicitly calling the toMessage() method before sending a message.  JmsTemplate’s convertAndSend() method automatically calls the toMessage() method before sending the message to the destination.  The receiveAndConvert() method of JmsTemplate can be simply called instead of explicitly calling the fromMessage() to convert the message returned from JmsTemplate’s receive().  Unless specified, both the convertAndSend() and receiveAndConvert(), send and receive messages respectively from the default destination.
  • 18. Wiring a message converter  <bean id="motoristConverter"  class="com.roadrantz.marketing.MotoristMessageConverter" /  <bean id="jmsTemplate"  class="org.springframework.jms.core.JmsTemplate">  <property name="connectionFactory" ref="connectionFactory" />  <property name="defaultDestination" ref="rantzDestination" />  <property name="messageConverter"  ref="motoristConverter" />  </bean>
  • 19. Spring JMS Gateway Support  public class RantzMarketingGatewayImpl extends JmsGatewaySupport  implements RantzMarketingGateway {  public void sendMotoristInfo(final Motorist motorist) {  getJmsTemplate().send("rantz.marketing.queue",  new MessageCreator() {  public Message createMessage(Session session)  throws JMSException {  MapMessage message = session.createMapMessage();  message.setString("firstName", motorist.getFirstName());  return message;  }  });  }  }
  • 20. Spring JMS Gateway Support  A JmsTemplate object can be directly injected into the jmsTemplate property, or by wiring the connection factory into the connectionFactory property.  JmsGatewaySupport automatically creates a JmsTemplate object based on the injected connection factory bean wired into the connectionFactory property.  Shortcomings to wiring connection factory are as follows:  Can only specify a default destination on a JmsTemplate.  Can only wire a message converter into a JmsTemplate.
  • 21. Spring Message Driven Beans  public class MarketingMdp implements MessageListener {  public void onMessage(Message message) {  MapMessage mapMessage = (MapMessage) message;  try {  SpammedMotorist motorist = new SpammedMotorist();  motorist.setFirstName(mapMessage.getString("firstName"));  motorist.setLastName(mapMessage.getString("lastName"));  motorist.setEmail(mapMessage.getString("email"));  processMotoristInfo(motorist);  } catch (JMSException e) { // handle—somehow }  }  private void processMotoristInfo(SpammedMotorist motorist) { … }  }  <bean id="rantzMdp“ class="com.roadrantz.marketing.MarketingMdp" />
  • 22. Message Listener Container  A message listener container watches a JMS destination for messages and retrieves the messages on arrival.  The retrieved messages are passed on to a MessageListener implementation by calling the onMessage() method.  onMessage() method is to receive and translate the message.  SimpleMessageListenerContainer is the simplest of the message listener containers, configured as follows:  <bean class="org.springframework.jms.listener.  ➥ SimpleMessageListenerContainer">  <property name="connectionFactory" ref="connectionFactory" />  <property name="destination" ref="rantzDestination" />  <property name="messageListener" ref="rantzMdp" />  </bean>
  • 23. Spring Message Listener Containers Container class org.springframework.jms.listener.* Operation SimpleMessageListenerContainer This is the simplest message listener container. It works with a fixed number of JMS sessions and does not support transactions. DefaultMessageListenerContainer This message listener container builds upon SimpleMessageListenerContainer by adding support for transactions serversession.ServerSessionMessage ListenerContainer This is the most powerful of the message listener containers. Like DefaultMessageListenerContainer, it supports transactions. However it is unique in that it allows for dynamic management of JMS sessions.
  • 24. Transactional MDPs  DefaultMessageListenerContainer adds transaction support to the SimpleMessageListenerContainer.  <bean class="org.springframework.jms.listener.➥ DefaultMessageListenerContainer">  <property name="connectionFactory" ref="connectionFactory" />  <property name="destination" ref="rantzDestination" />  <property name="messageListener" ref="rantzMdp" />  <property name="transactionManager" ref="jmsTransactionManager" />  </bean>  The transactionManager property is wired with a reference to a transaction manager such as JmsTransactionManager:  <bean id="jmsTransactionManager“ class="org.springframework.jms.connection.  ➥ JmsTransactionManager">  <property name="connectionFactory" ref="connectionFactory" />  </bean>  The transactionManager property of DefaultMessageListenerContainer is optional.
  • 25. Pure POJO MDP  The message listener container calls the onMessage() method when a message arrives, when its messageListener property being wired with an implementation of MessageListener.  <bean class="org.springframework.jms.listener.  ➥ SimpleMessageListenerContainer">  <property name="connectionFactory" ref="connectionFactory" />  <property name="destination" ref="rantzDestination" />  <property name="messageListener" ref="purePojoMdp" />  </bean>
  • 26. MessageListenerAdapter  MessageListenerAdapter is a MessageListener that delegates to a bean and method.  By default, MessageListenerAdapter calls a handleMessage() method when a message arrives.  <bean id="purePojoMdp“ class="org.springframework.jms.listener.adapter.  ➥ MessageListenerAdapter">  <property name="delegate" ref="rantzMdp" />  <property name="defaultListenerMethod“ value="processMotoristInfo" />  </bean>  The MessageListenerAdapter above calls the processMotoristInfo() method (as it’s the defaultListenerMethod) on the rantzMdp bean when a message arrives on the destination.  When MessageListenerAdapter receives a message, it considers the message type and the value of the defaultListenerMethod and tries to find an appropriate listener method signature to invoke.
  • 27. Mapping of JMS Message to MDP Message type Method parameter TextMessage String or TextMessage MapMessage java.util.Map or MapMessage BytesMessage byte[] or BytesMessage ObjectMessage java.io.Serializable or ObjectMessage  The listener method can be written to take either the original JMS message or a simpler type.
  • 28. Converting MDP messages  Spring message converters are used to translate messages to and from domain-specific Java types.  MessageListenerAdapter has a messageConverter property to set the corresponding message converter.  <bean id="purePojoMdp“  class="org.springframework.jms.listener.adapter.  ➥ MessageListenerAdapter">  <property name="delegate" ref="rantzMdp" />  <property name="defaultListenerMethod“ value="processMotoristInfo" />  <property name="messageConverter" ref="motoristConverter" />  </bean>
  • 29. Lingo  Lingo is a Spring-based remoting option that bridges the gap between RPC and asynchronous messaging.  Lingo provides a service exporter to export bean functionality as a Lingo service and a clientside proxy to transparently wire a reference to a remote Lingo service on the calling side.  Lingo remoting carries information over a JMS queue or topic, making the service call held in the queue/topic until the service is available again.
  • 30. Exporting Lingo Service  Lingo provides JmsServiceExporter, a service exporter, were services are made available through JMS.  The service implementation implements serviceInterface and is a pure POJO wired into a MessageListenerAdapter to be used as message-driven POJO.  <bean id="server"  class="org.logicblaze.lingo.jms.JmsServiceExporter">  <property name="connectionFactory" ref="connectionFactory" />  <property name="destination" ref="destination" />  <property name="service" ref="rantzMdp" />  <property name="serviceInterface"  value="com.roadrantz.marketing.MarketingService" />  </bean>
  • 31. Lingo JMS Proxy  Lingo provides JmsProxyFactoryBean, a proxy factory bean that produces proxies to remote Lingo-exported services.  <bean id="marketing"  class="org.logicblaze.lingo.jms.JmsProxyFactoryBean">  <property name="connectionFactory" ref="connectionFactory" />  <property name="destination" ref="destination" />  <property name="serviceInterface"  value="com.roadrantz.marketing.MarketingService" />  </bean>  Invoking a Lingo-exported service is no different from invoking an RMI service, a web service, or even a method on another bean.