Want more features on Pastebin? Sign Up, it's FREE!
Guest

code and cofing

By: a guest on Oct 12th, 2012  |  syntax: None  |  size: 5.85 KB  |  views: 48  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. // main rabbitproblem/SimpleClient.java
  2. package rabbitproblem;
  3.  
  4. import org.springframework.context.ApplicationContext;
  5. import org.springframework.context.support.GenericXmlApplicationContext;
  6.  
  7. import java.io.BufferedReader;
  8. import java.io.IOException;
  9. import java.io.InputStreamReader;
  10.  
  11. /**
  12.  * @author alex <a.shneyderman@gmail.com>
  13.  * @since 10/12/12
  14.  */
  15. public class SimpleClient {
  16.  
  17.     public static void main(String[] args) throws IOException {
  18.         ApplicationContext context = new GenericXmlApplicationContext("classpath:/simple-test.xml");
  19.         System.out.println("Press enter to finish experiment.");
  20.         new BufferedReader(new InputStreamReader(System.in)).readLine();
  21.     }
  22.  
  23. }
  24.  
  25. // listener code rabbitproblem/SimpleMsgListener.java
  26. package rabbitproblem;
  27.  
  28. import org.apache.commons.logging.Log;
  29. import org.apache.commons.logging.LogFactory;
  30. import org.springframework.amqp.core.Message;
  31.  
  32. /**
  33.  * TODO: description
  34.  *
  35.  * @author alex <a.shneyderman@gmail.com>
  36.  * @since 10/12/12
  37.  */
  38. public class SimpleMsgListener implements org.springframework.amqp.core.MessageListener {
  39.  
  40.     private static final Log log = LogFactory.getLog(SimpleMsgListener.class);
  41.  
  42.     @Override
  43.     public void onMessage(Message message) {
  44.         log.info("Message received: " + new String(message.getBody()));
  45.     }
  46.  
  47. }
  48.  
  49. // config
  50. <beans xmlns="http://www.springframework.org/schema/beans"
  51.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  52.        xmlns:rabbit="http://www.springframework.org/schema/rabbit"
  53.        xmlns:util="http://www.springframework.org/schema/util"
  54.        xsi:schemaLocation="http://www.springframework.org/schema/rabbit
  55.                            http://www.springframework.org/schema/rabbit/spring-rabbit-1.0.xsd
  56.                            http://www.springframework.org/schema/beans
  57.                            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  58.                            http://www.springframework.org/schema/util
  59.                            http://www.springframework.org/schema/util/spring-util-3.0.xsd">
  60.  
  61.     <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  62.         <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/>
  63.         <property name="locations" value="classpath*:simple-test.properties"/>
  64.     </bean>
  65.  
  66.     <!-- connectivity to rabbit -->
  67.     <bean id="rabbitConnectionFactory"
  68.           class="org.springframework.amqp.rabbit.connection.CachingConnectionFactory">
  69.         <property name="host" value="${rabbitmq.connectionfactory.hostname}" />
  70.         <property name="username" value="${rabbitmq.connectionfactory.username}" />
  71.         <property name="password" value="${rabbitmq.connectionfactory.password}" />
  72.         <property name="virtualHost" value="${rabbitmq.connectionfactory.virtualHost}" />
  73.     </bean>
  74.  
  75.     <!-- queue -->
  76.     <rabbit:queue name="alex.tests.client.queue" auto-delete="false" exclusive="false" durable="false">
  77.         <rabbit:queue-arguments key-type="java.lang.String" value-type="java.lang.String">
  78.             <entry key="x-dead-letter-exchange" value="Error Messages" />
  79.             <entry key="x-dead-letter-routing-key" value="ws.error" />
  80.         </rabbit:queue-arguments>
  81.     </rabbit:queue>
  82.  
  83.     <!-- queue creation -->
  84.     <rabbit:admin connection-factory="rabbitConnectionFactory" />
  85.  
  86.     <!-- retry handler -->
  87.     <bean id="retryOperationsInterceptor" class="org.springframework.amqp.rabbit.config.StatefulRetryOperationsInterceptorFactoryBean">
  88.         <property name="retryOperations">
  89.             <bean class="org.springframework.retry.support.RetryTemplate">
  90.                 <property name="retryPolicy">
  91.                     <bean class="org.springframework.retry.policy.SimpleRetryPolicy">
  92.                         <property name="maxAttempts" value="2" />
  93.                     </bean>
  94.                 </property>
  95.                 <property name="backOffPolicy">
  96.                     <bean class="org.springframework.retry.backoff.FixedBackOffPolicy">
  97.                         <property name="backOffPeriod" value="1000" />
  98.                     </bean>
  99.                 </property>
  100.             </bean>
  101.         </property>
  102.     </bean>
  103.  
  104.     <util:list id="retryChain">
  105.         <bean class="org.springframework.amqp.rabbit.retry.MissingMessageIdAdvice">
  106.             <constructor-arg>
  107.                 <bean class="org.springframework.retry.policy.MapRetryContextCache" />
  108.             </constructor-arg>
  109.         </bean>
  110.         <ref bean="retryOperationsInterceptor" />
  111.     </util:list>
  112.  
  113.     <!-- container listener -->
  114.     <bean id="listener.container" class="org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer">
  115.         <property name="channelTransacted" value="false" />
  116.         <property name="concurrentConsumers" value="2" />
  117.         <property name="prefetchCount" value="1" />
  118.         <property name="connectionFactory" ref="rabbitConnectionFactory" />
  119.         <property name="acknowledgeMode" value="AUTO" />
  120.         <property name="queues" value="alex.tests.client.queue" />
  121.         <property name="autoStartup" value="true" />
  122.         <property name="messageListener">
  123.             <bean class="rabbitproblem.SimpleMsgListener" />
  124.         </property>
  125.         <property name="adviceChain" ref="retryChain" />
  126.     </bean>
  127.  
  128. </beans>
  129.  
  130. // simple-test.properties properties file
  131. rabbitmq.connectionfactory.username=***
  132. rabbitmq.connectionfactory.password=***
  133. rabbitmq.connectionfactory.hostname=****
  134. rabbitmq.connectionfactory.virtualHost=/alex
  135.  
  136. // log4j.properties
  137. log4j.rootLogger=DEBUG, stdout
  138.  
  139. log4j.appender.stdout=org.apache.log4j.ConsoleAppender
  140. log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
  141. log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] [%t] - %m%n
  142.  
  143. log4j.logger.org.springframework=DEBUG
  144. log4j.logger.rabbitproblem=DEBUG
clone this paste RAW Paste Data