// main rabbitproblem/SimpleClient.java
package rabbitproblem;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* @author alex <a.shneyderman@gmail.com>
* @since 10/12/12
*/
public class SimpleClient {
public static void main(String[] args) throws IOException {
ApplicationContext context = new GenericXmlApplicationContext("classpath:/simple-test.xml");
System.out.println("Press enter to finish experiment.");
new BufferedReader(new InputStreamReader(System.in)).readLine();
}
}
// listener code rabbitproblem/SimpleMsgListener.java
package rabbitproblem;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.amqp.core.Message;
/**
* TODO: description
*
* @author alex <a.shneyderman@gmail.com>
* @since 10/12/12
*/
public class SimpleMsgListener implements org.springframework.amqp.core.MessageListener {
private static final Log log = LogFactory.getLog(SimpleMsgListener.class);
@Override
public void onMessage(Message message) {
log.info("Message received: " + new String(message.getBody()));
}
}
// config
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:rabbit="http://www.springframework.org/schema/rabbit"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/rabbit
http://www.springframework.org/schema/rabbit/spring-rabbit-1.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.0.xsd">
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/>
<property name="locations" value="classpath*:simple-test.properties"/>
</bean>
<!-- connectivity to rabbit -->
<bean id="rabbitConnectionFactory"
class="org.springframework.amqp.rabbit.connection.CachingConnectionFactory">
<property name="host" value="${rabbitmq.connectionfactory.hostname}" />
<property name="username" value="${rabbitmq.connectionfactory.username}" />
<property name="password" value="${rabbitmq.connectionfactory.password}" />
<property name="virtualHost" value="${rabbitmq.connectionfactory.virtualHost}" />
</bean>
<!-- queue -->
<rabbit:queue name="alex.tests.client.queue" auto-delete="false" exclusive="false" durable="false">
<rabbit:queue-arguments key-type="java.lang.String" value-type="java.lang.String">
<entry key="x-dead-letter-exchange" value="Error Messages" />
<entry key="x-dead-letter-routing-key" value="ws.error" />
</rabbit:queue-arguments>
</rabbit:queue>
<!-- queue creation -->
<rabbit:admin connection-factory="rabbitConnectionFactory" />
<!-- retry handler -->
<bean id="retryOperationsInterceptor" class="org.springframework.amqp.rabbit.config.StatefulRetryOperationsInterceptorFactoryBean">
<property name="retryOperations">
<bean class="org.springframework.retry.support.RetryTemplate">
<property name="retryPolicy">
<bean class="org.springframework.retry.policy.SimpleRetryPolicy">
<property name="maxAttempts" value="2" />
</bean>
</property>
<property name="backOffPolicy">
<bean class="org.springframework.retry.backoff.FixedBackOffPolicy">
<property name="backOffPeriod" value="1000" />
</bean>
</property>
</bean>
</property>
</bean>
<util:list id="retryChain">
<bean class="org.springframework.amqp.rabbit.retry.MissingMessageIdAdvice">
<constructor-arg>
<bean class="org.springframework.retry.policy.MapRetryContextCache" />
</constructor-arg>
</bean>
<ref bean="retryOperationsInterceptor" />
</util:list>
<!-- container listener -->
<bean id="listener.container" class="org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer">
<property name="channelTransacted" value="false" />
<property name="concurrentConsumers" value="2" />
<property name="prefetchCount" value="1" />
<property name="connectionFactory" ref="rabbitConnectionFactory" />
<property name="acknowledgeMode" value="AUTO" />
<property name="queues" value="alex.tests.client.queue" />
<property name="autoStartup" value="true" />
<property name="messageListener">
<bean class="rabbitproblem.SimpleMsgListener" />
</property>
<property name="adviceChain" ref="retryChain" />
</bean>
</beans>
// simple-test.properties properties file
rabbitmq.connectionfactory.username=***
rabbitmq.connectionfactory.password=***
rabbitmq.connectionfactory.hostname=****
rabbitmq.connectionfactory.virtualHost=/alex
// log4j.properties
log4j.rootLogger=DEBUG, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] [%t] - %m%n
log4j.logger.org.springframework=DEBUG
log4j.logger.rabbitproblem=DEBUG