JMS-Receiver mit Spring-Konfiguration

Nach den letzten paar Beiträgen, jetzt mal ein JMS-Receiver, der sauber über Spring konfiguriert wird.

die application-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"
	xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

	<bean id="jmsContainer"
		class="org.springframework.jms.listener.DefaultMessageListenerContainer">
		<!-- the connectionFactory -->
		<property name="connectionFactory" ref="activeMQConnectionFactory" />
		<!-- the destination (Queue or Topic) -->
		<property name="destination" ref="destination" />
		<!-- the custom MessageListener-implementation -->
		<property name="messageListener" ref="messageListener" />
		<!-- set the session-transaction -->
		<property name="sessionTransacted" value="true" />
		<!-- some int-properties -->
		<property name="recoveryInterval" value="10000" />
		<property name="concurrentConsumers" value="10" />
	</bean>

	<!-- 
	A JMS provider which pools Connection, Session and MessageProducer instances so it can be used with tools like 
	Spring's JmsTemplate. NOTE this implementation is only intended for use when sending messages. It does not deal 
	with pooling of consumers; for that look at a library like Jencks. 
	will have a look at Jencks: http://jencks.codehaus.org/	
	 -->
	<bean id="pooledConnectionFactory" class="org.apache.activemq.pool.PooledConnectionFactory"
		destroy-method="stop">
		<property name="connectionFactory" ref="activeMQConnectionFactory" />
		<property name="maxConnections" value="8" /> 
		<property name="maximumActive" value="500" />
	</bean>

	<bean id="activeMQConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
		<property name="brokerURL" value="tcp://127.0.0.1:61616" />
	</bean>

	<bean id="destination" class="org.apache.activemq.command.ActiveMQQueue">
		<constructor-arg value="outbox" />
	</bean>

	<bean id="messageListener" class="jmsconsumer.QueueListener" />

</beans>

und wieder der einfache QueueListener als eigenständige Klasse:

package jmsconsumer;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;

import org.apache.log4j.Logger;

public class QueueListener implements MessageListener {

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

    public void onMessage(Message message) {
    	if (message instanceof TextMessage) {
            try {
                System.out.println(((TextMessage) message).getText());
            }
            catch (JMSException ex) {
                logger.error(ex.getMessage());
            }
        }
        else {
            throw new IllegalArgumentException("Message must be of type TextMessage");
        }
    }
}

und natürlich noch die Dependencies:

                <dependency>
			<groupId>org.apache.activemq</groupId>
			<artifactId>activemq-core</artifactId>
			<version>5.5.1</version>
		</dependency>
		<!-- just for the sending-issues, because pooling by PooledConnectionFactory 
			is not supported for receivers -->
		<dependency>
			<groupId>org.apache.activemq</groupId>
			<artifactId>activemq-pool</artifactId>
			<version>5.5.1</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>2.5.6</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-jms</artifactId>
			<version>2.5.6</version>
		</dependency>
		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>1.2.14</version>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-api</artifactId>
			<version>1.6.1</version>
		</dependency>
	</dependencies>

Und gestartet wird das ganze wieder über den XMLApplicationContext:

package jmsconsumer;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Start {
	public static void main(String[] args) {
		ApplicationContext appContext = new ClassPathXmlApplicationContext("application-context.xml");
                while (true){
                    //
                }
        }	
}

Nice 🙂 Und für ein Pooling beim Empfang sollte ich mir mal demnächst Jencks anschauen… sieht erstmal ganz interessant aus.