Java Reference
In-Depth Information
A POJO is registered to a listener container through a MessageListenerAdapter instance. This adapter
implements the MessageListener interface and will delegate message handling to the target bean's
method via reflection.
<beans ...>
...
<bean id="mailListener"
class="com.apress.springenterpriserecipes.post.MailListener" />
<bean id="mailListenerAdapter"
class="org.springframework.jms.listener.adapter.MessageListenerAdapter">
<property name="delegate" ref="mailListener" />
<property name="defaultListenerMethod" value="displayMail" />
</bean>
<bean
class="org.springframework.jms.listener.SimpleMessageListenerContainer">
<property name="connectionFactory" ref="connectionFactory" />
<property name="destinationName" value="mail.queue" />
<property name="messageListener" ref="mailListenerAdapter" />
</bean>
</beans>
You have to set the delegate property of MessageListenerAdapter to your target bean. By default,
this adapter will call the method whose name is handleMessage on that bean. If you want to call another
method, you can specify it in the defaultListenerMethod property. Finally, notice that you have to
register the listener adapter, not the target bean, with the listener container.
Converting JMS Messages
You can also create a message converter for converting mail objects from JMS messages that contain mail
information. Because message listeners receive messages only, the method toMessage() will not be called,
so you can simply return null for it. However, if you use this message converter for sending messages too,
you have to implement this method. The following example reprints the MailMessageConvertor class
written earlier:
package com.apress.springenterpriserecipes.post;
import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.Session;
import org.springframework.jms.support.converter.MessageConversionException;
import org.springframework.jms.support.converter.MessageConverter;
public class MailMessageConverter implements MessageConverter {
Search WWH ::




Custom Search