2. Client Configuration

Spring Configuration

This shows configuring the JMS listener using Springs jms custom namespace. The jmsMessageListener bean was loaded by the context:component-scan and implements MessageListener. If it didn't the jms:listener element could specify which method should process a message from the queue. The jms:listener specifies the destination attribute to be 'org.springbyexample.jms.test', which matches the queue defined by the amq:queue element in the embedded ActiveMQ configuration.

The AtomicInteger is used by the listener to increment how many messages it processes, and is also used by the unit test to confirm is received all the messages from the producer.

Excerpt from JmsMessageListenerTest-context.xml

                    
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:jms="http://www.springframework.org/schema/jms"
       xmlns:amq="http://activemq.apache.org/schema/core"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
                           http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/context 
                           http://www.springframework.org/schema/context/spring-context.xsd
                           http://www.springframework.org/schema/jms 
                           http://www.springframework.org/schema/jms/spring-jms.xsd
                           http://activemq.apache.org/schema/core 
                           http://activemq.apache.org/schema/core/activemq-core.xsd">
 
    <context:component-scan base-package="org.springbyexample.jms" />
    
    ...

    <!-- JMS Consumer Configuration -->
    <bean id="jmsConsumerConnectionFactory" 
          class="org.springframework.jms.connection.SingleConnectionFactory"
          depends-on="broker"
          p:targetConnectionFactory-ref="jmsFactory" />
        
    <jms:listener-container container-type="default" 
                            connection-factory="jmsConsumerConnectionFactory"
                            acknowledge="auto">
        <jms:listener destination="org.springbyexample.jms.test" ref="jmsMessageListener" />
    </jms:listener-container>

    <!-- Counter for consumer to increment and test to verify count -->
    <bean id="counter" class="java.util.concurrent.atomic.AtomicInteger" />
    
</beans>
                    
                

Code Example

The JmsMessageListener implements the JMS interface MessageListener. The int property for the message count can be retrieved before casting the message to TextMessage. Then the message and message count are both logged.

Example 2. JmsMessageListener

src/main/java/org/springbyexample/jms/JmsMessageListener.java
                    
@Component
public class JmsMessageListener implements MessageListener { 

    private static final Logger logger = LoggerFactory.getLogger(JmsMessageListener.class);

    @Autowired
    private AtomicInteger counter = null;

    /**
     * Implementation of <code>MessageListener</code>.
     */
    public void onMessage(Message message) {
        try {   
            int messageCount = message.getIntProperty(JmsMessageProducer.MESSAGE_COUNT);
            
            if (message instanceof TextMessage) {
                TextMessage tm = (TextMessage)message;
                String msg = tm.getText();
                
                
                logger.info("Processed message '{}'.  value={}", msg, messageCount);
                
                counter.incrementAndGet();
            }
        } catch (JMSException e) {
            logger.error(e.getMessage(), e);
        }
    }
    
}