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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<bean class="org.springframework.jms.config.DefaultJmsListenerContainerFactory" id="jmsListenerContainerFactory"> | |
<property name="connectionFactory" ref="internalJmsQueueConnectionFactory"></property> | |
</bean> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} } |
Comments
Post a Comment