spring的rabbitmq配置

1、applicationContext-base.xml

 

Xml代码   收藏代码
  1. xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xmlns:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc"  
  5.     xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"  
  6.     xmlns:websocket="http://www.springframework.org/schema/websocket"  
  7.     xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:cache="http://www.springframework.org/schema/cache"  
  8.     xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:rabbit="http://www.springframework.org/schema/rabbit"  
  9.     xmlns:task="http://www.springframework.org/schema/task"  
  10.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  11.     http://www.springframework.org/schema/beans/spring-beans.xsd  
  12.     http://www.springframework.org/schema/mvc  
  13.     http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd  
  14.     http://www.springframework.org/schema/aop  
  15.     http://www.springframework.org/schema/aop/spring-aop-4.1.xsd  
  16.     http://www.springframework.org/schema/context  
  17.     http://www.springframework.org/schema/context/spring-context-4.1.xsd  
  18.     http://www.springframework.org/schema/cache  
  19.     http://www.springframework.org/schema/cache/spring-cache-4.1.xsd  
  20.     http://www.springframework.org/schema/tx  
  21.     http://www.springframework.org/schema/tx/spring-tx-4.1.xsd  
  22.     http://www.springframework.org/schema/websocket  
  23.     http://www.springframework.org/schema/websocket/spring-websocket-4.1.xsd  
  24.     http://www.springframework.org/schema/data/jpa  
  25.     http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd  
  26.     http://www.springframework.org/schema/jdbc  
  27.     http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd  
  28.     http://www.springframework.org/schema/rabbit  
  29.     http://www.springframework.org/schema/rabbit/spring-rabbit-1.4.xsd  
  30.     http://www.springframework.org/schema/task  
  31.     http://www.springframework.org/schema/task/spring-task-4.1.xsd">  
  32.   
  33.       
  34.     <context:component-scan base-package="com.test.**">  
  35.         <context:exclude-filter type="annotation"  
  36.             expression="org.springframework.stereotype.Controller" />  
  37.     context:component-scan>  
  38.   
  39.       
  40.     <tx:annotation-driven transaction-manager="transactionManager"  
  41.         proxy-target-class="true" />  
  42.   
  43.     <jpa:repositories base-package="com.test.  
  44.         repository-impl-postfix="Impl" entity-manager-factory-ref="entityManagerFactory"  
  45.         transaction-manager-ref="transactionManager" />  
  46.   
  47.     <bean id="entityManagerFactory"  
  48.         class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">  
  49.         <property name="dataSource" ref="dataSource" />  
  50.         <property name="packagesToScan" value="com.test. />  
  51.         <property name="persistenceProvider">  
  52.             <bean class="org.hibernate.ejb.HibernatePersistence" />  
  53.         property>  
  54.         <property name="jpaVendorAdapter">  
  55.             <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">  
  56.                 <property name="generateDdl" value="true" />  
  57.                 <property name="databasePlatform" value="${hibernate.dialect}" />  
  58.                 <property name="showSql" value="${hibernate.show_sql}" />  
  59.             bean>  
  60.         property>  
  61.         <property name="jpaDialect">  
  62.             <bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />  
  63.         property>  
  64.         <property name="jpaPropertyMap">  
  65.             <map>  
  66.                 <entry key="hibernate.query.substitutions" value="true 1, false 0" />  
  67.                 <entry key="hibernate.default_batch_fetch_size" value="16" />  
  68.                 <entry key="hibernate.max_fetch_depth" value="2" />  
  69.                 <entry key="hibernate.generate_statistics" value="true" />  
  70.                 <entry key="hibernate.bytecode.use_reflection_optimizer"  
  71.                     value="true" />  
  72.                 <entry key="hibernate.cache.use_second_level_cache" value="${hibernate.cache.use_second_level_cache}" />  
  73.                 <entry key="hibernate.cache.use_query_cache" value="${hibernate.cache.use_query_cache}" />  
  74.                 <entry key="hibernate.hbm2ddl.auto" value="${hibernate.hbm2ddl.auto}" />  
  75.             map>  
  76.         property>  
  77.     bean>  
  78.   
  79.       
  80.     <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">  
  81.         <property name="entityManagerFactory" ref="entityManagerFactory" />  
  82.     bean>  
  83.   
  84.       
  85.     <bean name="dataSource"  
  86.         class="org.springframework.jdbc.datasource.DriverManagerDataSource">  
  87.         <property name="driverClassName" value="${hibernate.connection.driver_class}" />  
  88.         <property name="url" value="${hibernate.connection.url}" />  
  89.         <property name="username" value="${hibernate.connection.username}" />  
  90.         <property name="password" value="${hibernate.connection.password}" />  
  91.     bean>  
  92.   
  93.     <bean id="objectMapper" class="com.test.core.utils.JsonObjectMapper" />  
  94.   
  95.       
  96.     <jdbc:initialize-database data-source="dataSource"  
  97.         ignore-failures="ALL">  
  98.         <jdbc:script location="classpath:*.sql" encoding="UTF-8" />  
  99.     jdbc:initialize-database>  
  100.   
  101.       
  102.     <task:executor id="taskExecutor" pool-size="4-256" queue-capacity="128" />  
  103.   
  104.       
  105.     <rabbit:annotation-driven />  
  106.   
  107.     <bean id="rabbitListenerContainerFactory" class="org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory">  
  108.         <property name="connectionFactory" ref="rabbitConnFactory" />  
  109.         <property name="transactionManager" ref="transactionManager" />  
  110.         <property name="concurrentConsumers" value="1" />  
  111.         <property name="maxConcurrentConsumers" value="10" />  
  112.         <property name="messageConverter" ref="jsonMessageConverter" />  
  113.         <property name="taskExecutor" ref="taskExecutor" />  
  114.         <property name="channelTransacted" value="true" />  
  115.         <property name="adviceChain">  
  116.             <array>  
  117.                 <ref bean="retryInterceptor" />  
  118.             array>  
  119.         property>  
  120.     bean>  
  121.       
  122.   
  123.     <bean id="rabbitConnectionFactory" class="com.rabbitmq.client.ConnectionFactory">  
  124.         <property name="host" value="${rabbitmq.host}" />  
  125.         <property name="port" value="${rabbitmq.port}" />  
  126.         <property name="username" value="${rabbitmq.username}" />  
  127.         <property name="password" value="${rabbitmq.password}" />  
  128.         <property name="virtualHost" value="${rabbitmq.vhost}" />  
  129.         <property name="connectionTimeout" value="${rabbitmq.connection.timeout}" />  
  130.     bean>  
  131.   
  132.     <bean id="rabbitConnFactory" class="org.springframework.amqp.rabbit.connection.CachingConnectionFactory">  
  133.         <constructor-arg ref="rabbitConnectionFactory" />  
  134.         <property name="channelCacheSize" value="25" />  
  135.         <property name="executor" ref="taskExecutor" />  
  136.     bean>  
  137.   
  138.     <rabbit:admin connection-factory="rabbitConnFactory" id="rabbitAdmin" />  
  139.   
  140.       
  141.     <rabbit:template id="amqpTemplate" reply-timeout="1000" connection-factory="rabbitConnFactory" message-converter="jsonMessageConverter" />  
  142.   
  143.   
  144.       
  145.     <util:map id="dlxNaming" key-type="java.lang.String" value-type="java.lang.String">  
  146.         <entry key="zkcloud.subsystem.dlx.queue" value="#{'$dlx_queue_'+(T(com.zkteco.timecube.zkcloud.core.utils.PropertiesUtil).getValue('zkcloud.subsystem.code'))}" />  
  147.         <entry key="zkcloud.subsystem.dlx.exchange" value="#{'$dlx_ex_'+(T(com.zkteco.timecube.zkcloud.core.utils.PropertiesUtil).getValue('zkcloud.subsystem.code'))}" />  
  148.     util:map>  
  149.   
  150.     <rabbit:queue id="zkcloud.subsystem.dlx.queue" name="#{dlxNaming['zkcloud.subsystem.dlx.queue']}">  
  151.         <rabbit:queue-arguments>  
  152.             <entry key="x-message-ttl">  
  153.                 <value type="java.lang.Long">86400000value>  
  154.             entry>  
  155.             <entry key="x-max-length">  
  156.                 <value type="java.lang.Long">100value>  
  157.             entry>  
  158.         rabbit:queue-arguments>  
  159.     rabbit:queue>  
  160.   
  161.     <rabbit:fanout-exchange id="zkcloud.subsystem.dlx.exchange" name="#{dlxNaming['zkcloud.subsystem.dlx.exchange']}">  
  162.         <rabbit:bindings>  
  163.             <rabbit:binding queue="zkcloud.subsystem.dlx.queue" />  
  164.         rabbit:bindings>  
  165.     rabbit:fanout-exchange>  
  166.   
  167.     <bean id="retryInterceptor" class="org.springframework.amqp.rabbit.config.StatelessRetryOperationsInterceptorFactoryBean">  
  168.         <property name="messageRecoverer" ref="messageRecoverer" />  
  169.         <property name="retryOperations" ref="retryTemplate" />  
  170.     bean>  
  171.   
  172.       
  173.       
  174.     <bean id="messageRecoverer" class="com.test.retry.RejectAndRplyToRequeueRecoverer">  
  175.         <property name="replyToTemplate" ref="amqpTemplate"/>  
  176.     bean>  
  177.   
  178.     <bean id="retryTemplate" class="org.springframework.retry.support.RetryTemplate">  
  179.         <property name="backOffPolicy">  
  180.             <bean class="org.springframework.retry.backoff.ExponentialBackOffPolicy">  
  181.                 <property name="initialInterval" value="1000" />  
  182.                 <property name="maxInterval" value="10000" />  
  183.             bean>  
  184.         property>  
  185.         <property name="retryPolicy">  
  186.             <bean class="org.springframework.retry.policy.SimpleRetryPolicy">  
  187.                 <property name="maxAttempts" value="1" />  
  188.             bean>  
  189.         property>  
  190.     bean>  
  191.   
  192.     <bean id="jsonMessageConverter"  
  193.         class="org.springframework.amqp.support.converter.Jackson2JsonMessageConverter">bean>  
  194.   
  195.   
  196.       
  197.     <bean class="com.zkteco.timecube.quartz.QuartJobSchedulingListener" />  
  198.     <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">  
  199.         <property name="jobFactory">  
  200.             <bean class="com.zkteco.timecube.quartz.SpringQuartzJobFactory">bean>  
  201.         property>  
  202.         <property name="dataSource" ref="dataSource" />  
  203.           
  204.         <property name="configLocation" value="classpath:config/quartz.properties" />  
  205.     bean>  
  206.       
  207.   
  208.     <beans profile="develop">  
  209.         <bean id="propertyConfigurer" class="com.test.core.utils.PropertiesUtil"  
  210.             lazy-init="false">  
  211.             <property name="locations">  
  212.                 <list>  
  213.                     <value>classpath*:config/*.propertiesvalue>  
  214.                 list>  
  215.             property>  
  216.             <property name="fileEncoding" value="utf-8" />  
  217.         bean>  
  218.           
  219.         <rabbit:connection-factory id="rabbitConnFactory"  
  220.             host="localhost" username="guest" password="guest" port="5672"  
  221.             virtual-host="/" connection-timeout="30000" executor="taskExecutor" />  
  222.     beans>  
  223.   
  224.     <beans profile="test">  
  225.         <bean id="propertyConfigurer" class="com.test.core.utils.PropertiesUtil"  
  226.             lazy-init="false">  
  227.             <property name="locations">  
  228.                 <list>  
  229.                     <value>classpath*:config/*.propertiesvalue>  
  230.                     <value>classpath*:config/test/*.propertiesvalue>  
  231.                 list>  
  232.             property>  
  233.             <property name="fileEncoding" value="utf-8" />  
  234.         bean>  
  235.           
  236.         <rabbit:connection-factory id="rabbitConnFactory"  
  237.             host="192.168.0.179" username="guest" password="timeucbe" port="5672"  
  238.             virtual-host="/" connection-timeout="30000" executor="taskExecutor" />  
  239.     beans>  
  240.   
  241.     <beans profile="production">  
  242.         <bean id="propertyConfigurer" class="com.test.core.utils.PropertiesUtil"  
  243.             lazy-init="false">  
  244.             <property name="locations">  
  245.                 <list>  
  246.                     <value>classpath*:config/*.propertiesvalue>  
  247.                     <value>classpath*:config/production/*.propertiesvalue>  
  248.                 list>  
  249.             property>  
  250.             <property name="fileEncoding" value="utf-8" />  
  251.               
  252.             <rabbit:connection-factory id="rabbitConnFactory"  
  253.                 host="114.215.82.3" username="guest" password="timecube" port="5672"  
  254.                 virtual-host="/" connection-timeout="30000" executor="taskExecutor" />  
  255.         bean>  
  256.     beans>  
  257.   
  258.   
  259. beans>  

 2、Exchanges、routing keys、binding keys的配置

 

 

Xml代码   收藏代码
  1. xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xmlns:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc"  
  5.     xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"  
  6.     xmlns:websocket="http://www.springframework.org/schema/websocket"  
  7.     xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:cache="http://www.springframework.org/schema/cache"  
  8.     xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:rabbit="http://www.springframework.org/schema/rabbit"  
  9.     xmlns:task="http://www.springframework.org/schema/task"  
  10.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  11.     http://www.springframework.org/schema/beans/spring-beans.xsd  
  12.     http://www.springframework.org/schema/mvc  
  13.     http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd  
  14.     http://www.springframework.org/schema/aop  
  15.     http://www.springframework.org/schema/aop/spring-aop-4.1.xsd  
  16.     http://www.springframework.org/schema/context  
  17.     http://www.springframework.org/schema/context/spring-context-4.1.xsd  
  18.     http://www.springframework.org/schema/cache  
  19.     http://www.springframework.org/schema/cache/spring-cache-4.1.xsd  
  20.     http://www.springframework.org/schema/tx  
  21.     http://www.springframework.org/schema/tx/spring-tx-4.1.xsd  
  22.     http://www.springframework.org/schema/websocket  
  23.     http://www.springframework.org/schema/websocket/spring-websocket-4.1.xsd  
  24.     http://www.springframework.org/schema/data/jpa  
  25.     http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd  
  26.     http://www.springframework.org/schema/jdbc  
  27.     http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd  
  28.     http://www.springframework.org/schema/rabbit  
  29.     http://www.springframework.org/schema/rabbit/spring-rabbit-1.4.xsd  
  30.     http://www.springframework.org/schema/task  
  31.     http://www.springframework.org/schema/task/spring-task-4.1.xsd">  
  32.   
  33.     <rabbit:queue id="queue_one" durable="true" auto-delete="false"  
  34.         name="queue_one">  
  35.         

你可能感兴趣的:(java,spring,rabbitMQ)