Java Reference
In-Depth Information
Spring provides several types of message listener containers for you to choose from in the
org.springframework.jms.listener package, of which SimpleMessageListenerContainer and
DefaultMessageListenerContainer are the most commonly used. SimpleMessageListenerContainer
is the simplest one that doesn't support transaction. If you have a transaction requirement in receiving
messages, you have to use DefaultMessageListenerContainer .
Now you can start your message listener with the following main class, which starts the Spring IoC
container only:
package com.apress.springenterpriserecipes.post;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class BackOfficeMain {
public static void main(String[] args) {
new ClassPathXmlApplicationContext("beans-back.xml");
}
}
Listening for JMS Messages with POJOs
While a listener that implements the MessageListener interface can listen for messages, so can an
arbitrary bean declared in the Spring IoC container. Doing so means that beans are decoupled from the
Spring framework interfaces as well as the JMS MessageListener interface. For a method of this bean to
be triggered on message arrival, it must accept one of the following types as its sole method argument:
Raw JMS message type : For TextMessage , MapMessage , BytesMessage , and ObjectMessage
String : For TextMessage only
Map : For MapMessage only
byte[] : For BytesMessage only
Serializable : For ObjectMessage only
For example, to listen for MapMessage , you declare a method that accepts a map as its argument. This
listener no longer needs to implement the MessageListener interface.
package com.apress.springenterpriserecipes.post;
...
public class MailListener {
public void displayMail(Map map) {
Mail mail = new Mail();
mail.setMailId((String) map.get("mailId"));
mail.setCountry((String) map.get("country"));
mail.setWeight((Double) map.get("weight"));
System.out.println("Mail #" + mail.getMailId() + " received");
}
}
Search WWH ::




Custom Search