Spring | JMS Listener via JmsListener annotation

Spring framework provides annotation based configuration for dependency injection. Normally, MessageListener interface needs to be implemented in Queue/Topic receiver class for incoming messages. The overridden onMessage method in that class handle the incoming message.

Spring provides below annotations to enable jms communication.
  • @JmsListener annotation can mark any method to handle message. 
  • @EnableJms annotation to detect and register @JmsListener methods for jms messages.
Below is class to handle jms message.

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jms.annotation.EnableJms;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;
import javax.jms.Message;
/**
* Created by rohanlopes on 2017/10/23.
*/
@EnableJms
@Component(value = "msgListnerAnnotaion")
public class MsgListnerAnnotaion {
private static Logger logger = org.apache.log4j.Logger.getLogger(MsgListnerAnnotaion.class);
@Value(value = "${jms.annonationqueue}") /*queue name is provided via property*/
private final String annonationqueue="x";
@JmsListener(destination = annonationqueue)
public void processMessage(Message msg){
/* do message processing */
logger.info("Msg recieved : " + msg.toString());
}
}
@JmsListener requires default jms container factory configuration. Add below lines in bean conf file for this. Please refer this post for conf file.
<bean class="org.springframework.jms.config.DefaultJmsListenerContainerFactory" id="jmsListenerContainerFactory">
<property name="connectionFactory" ref="internalJmsQueueConnectionFactory"></property>
</bean>
Deploy war file to JBoss server and send sample message from Gems tool.
2017-11-09 13:42:17,744 INFO [MsgListnerAnnotaion] Msg recieved : TextMessage={ Header={ JMSMessageID={ID:E4EMS-SERVER.11C759F30C1056E:57087} JMSDestination={Queue[x]} JMSReplyTo={null} JMSDeliveryMode={NON_PERSISTENT} JMSRedelivered={false} JMSCorrelationID={dfd} JMSType={} JMSTimestamp={Thu Nov 09 13:42:17 SAST 2017} JMSExpiration={0} JMSPriority={4} } Properties={ } Text={Helloworld} }
view raw JmsResponse.log hosted with ❤ by GitHub

Comments

Popular posts from this blog

Spring | Using TIBCO EMS with Spring framework

TIBCO | For Loop - Accumulate output

TIBCO | JNDI Server & JMS Instance creation