ssm整合rabbitmq

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_36745087/article/details/80544385

1,系统windows7

项目使用maven构建,使用spring+springmvc+mybatis整合rabbitmq

项目能够正常启动,运行,并且连接多数据源(关于多数据源部分,其他的文章里有介绍,不需要可以自行配置)。已经使用postman进行测试,能够发送消息到消息队列并且够取自动取出消息。

下面是关键代码

注意:项目依靠此部分代码无法启动,仅作参考,关键是rabbitmq部分的内容。

=====================================================================================

只附上rabbitmq部分的依赖,版本可以自己选择

1,pom.xml

<dependency>
<groupId>com.rabbitmq</groupId>
<artifactId>amqp-client</artifactId>
<version>3.6.0</version>
</dependency>
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-rabbit</artifactId>
<version>1.6.5.RELEASE</version>
</dependency>

========================================================================================

2,web.xml

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">

<display-name>business-server</display-name>

<welcome-file-list>
<welcome-file>/index.jsp</welcome-file>
</welcome-file-list>

<servlet>
<servlet-name>business-server</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value></param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

<!-- 设置spring访问所有资源 -->
<servlet-mapping>
<servlet-name>business-server</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

<!-- 监听log4j -->
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:log4j/log4j.properties</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<!-- Spring Config -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- Spring刷新Interceptor防止内存泄漏 -->
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>

<!-- 设置session 超时时间为20分钟 -->
<session-config>
<session-timeout>20</session-timeout>
</session-config>

</web-app>

 

============================================================================

3,spring配置文件  applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:task="http://www.springframework.org/schema/task" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd">
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />

<mvc:default-servlet-handler />
<mvc:annotation-driven /> <!-- 必须添加mvc:annotation-driven 否则所有controller失效 -->

<!-- 扫描注解Bean -->
<context:annotation-config></context:annotation-config>
<context:component-scan base-package="com.jjmc.server.controller" />
<context:component-scan base-package="com.jjmc.server.service" />
<context:component-scan base-package="com.jjmc.server.service.impl" />
<context:component-scan base-package="com.jjmc.server.dao" />
<context:component-scan base-package="com.jjmc.server.task" /><!--定时任务-->
<!-- <context:component-scan base-package="com.sgm.qms.*.task" /> -->

<!-- 加载其他spring配置文件 -->
<import resource="classpath:spring/spring-mybatis.xml" />
<import resource="classpath:spring/application-rabbitmq.xml" />
<!--开启这个配置,spring才能识别@Scheduled注解-->
<!-- <task:annotation-driven/> -->

<!-- spring定时调度,开启这个配置,spring才能识别@Scheduled注解 -->
<!-- <task:annotation-driven scheduler="qbScheduler" mode="proxy"/> -->
<!-- <task:scheduler id="qbScheduler" pool-size="10"/>
<task:scheduled-tasks>
<task:scheduled ref="taskJob" method="job" cron="0/10 * * * * ?"/> 每50秒执行一次
</task:scheduled-tasks> -->
</beans>

 

============================================================================

4,mybatis配置文件  spring-mybatis.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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

<!-- 加载数据库配置文件 -->
<context:property-placeholder location="classpath:database/jdbc.properties"
ignore-unresolvable="true" />

<!-- 开启AOP监听 只对当前配置文件有效 -->
<aop:aspectj-autoproxy expose-proxy="true" />

<!-- 声明DataSource 第一个数据源-->
<bean id="dataSource1" class="com.alibaba.druid.pool.DruidDataSource"
init-method="init" destroy-method="close">
<!-- 基本属性 url、user、password -->
<property name="driverClassName" value="${connectionSqlServer.driverClassName}" />
<property name="url" value="${connectionSqlServer.url}" />
<property name="username" value="${connectionSqlServer.username}" />
<property name="password" value="${connectionSqlServer.password}" />
<!-- 配置初始化大小、最小、最大 -->
<property name="initialSize" value="${druid.initialSize}" />
<property name="minIdle" value="${druid.minIdle}" />
<property name="maxActive" value="${druid.maxActive}" />
<!-- 配置获取连接等待超时的时间 -->
<property name="maxWait" value="${druid.maxWait}" />
<!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
<property name="timeBetweenEvictionRunsMillis" value="${druid.timeBetweenEvictionRunsMillis}" />
<!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
<property name="minEvictableIdleTimeMillis" value="${druid.minEvictableIdleTimeMillis}" />
<property name="validationQuery" value="${druid.validationQuery}" />
<property name="testWhileIdle" value="${druid.testWhileIdle}" />
<property name="testOnBorrow" value="${druid.testOnBorrow}" />
<property name="testOnReturn" value="${druid.testOnReturn}" />
<!-- 打开PSCache,并且指定每个连接上PSCache的大小 如果用Oracle,则把poolPreparedStatements配置为true,mysql可以配置为false。 -->
<property name="poolPreparedStatements" value="${druid.poolPreparedStatements}" />
<property name="maxPoolPreparedStatementPerConnectionSize"
value="${druid.maxPoolPreparedStatementPerConnectionSize}" />
<!-- 配置监控统计拦截的filters -->
<property name="filters" value="${druid.filters}" />
</bean>

<!-- 声明DataSource 第二个数据源-->
<bean id="dataSource2" class="com.alibaba.druid.pool.DruidDataSource"
init-method="init" destroy-method="close">
<!-- 基本属性 url、user、password -->
<property name="driverClassName" value="${connectionMysql.driverClassName}" />
<property name="url" value="${connectionMysql.url}" />
<property name="username" value="${connectionMysql.username}" />
<property name="password" value="${connectionMysql.password}" />
<!-- 配置初始化大小、最小、最大 -->
<property name="initialSize" value="${druid.initialSize}" />
<property name="minIdle" value="${druid.minIdle}" />
<property name="maxActive" value="${druid.maxActive}" />
<!-- 配置获取连接等待超时的时间 -->
<property name="maxWait" value="${druid.maxWait}" />
<!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
<property name="timeBetweenEvictionRunsMillis" value="${druid.timeBetweenEvictionRunsMillis}" />
<!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
<property name="minEvictableIdleTimeMillis" value="${druid.minEvictableIdleTimeMillis}" />
<property name="validationQuery" value="${druid.validationQuery}" />
<property name="testWhileIdle" value="${druid.testWhileIdle}" />
<property name="testOnBorrow" value="${druid.testOnBorrow}" />
<property name="testOnReturn" value="${druid.testOnReturn}" />
<!-- 打开PSCache,并且指定每个连接上PSCache的大小 如果用Oracle,则把poolPreparedStatements配置为true,mysql可以配置为false。 -->
<property name="poolPreparedStatements" value="${druid.poolPreparedStatements}" />
<property name="maxPoolPreparedStatementPerConnectionSize"
value="${druid.maxPoolPreparedStatementPerConnectionSize}" />
<!-- 配置监控统计拦截的filters -->
<property name="filters" value="${druid.filters}" />
</bean>

<!-- 声明DataSource 第三个数据源-->
<!-- <bean id="dataSource3" class="com.alibaba.druid.pool.DruidDataSource"
init-method="init" destroy-method="close">
基本属性 url、user、password
<property name="driverClassName" value="${connectionOracle.driverClassName}" />
<property name="url" value="${connectionOracle.url}" />
<property name="username" value="${connectionOracle.username}" />
<property name="password" value="${connectionOracle.password}" />
配置初始化大小、最小、最大
<property name="initialSize" value="${druid.initialSize}" />
<property name="minIdle" value="${druid.minIdle}" />
<property name="maxActive" value="${druid.maxActive}" />
配置获取连接等待超时的时间
<property name="maxWait" value="${druid.maxWait}" />
配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
<property name="timeBetweenEvictionRunsMillis" value="${druid.timeBetweenEvictionRunsMillis}" />
配置一个连接在池中最小生存的时间,单位是毫秒
<property name="minEvictableIdleTimeMillis" value="${druid.minEvictableIdleTimeMillis}" />
<property name="validationQuery" value="${druid.validationQuery}" />
<property name="testWhileIdle" value="${druid.testWhileIdle}" />
<property name="testOnBorrow" value="${druid.testOnBorrow}" />
<property name="testOnReturn" value="${druid.testOnReturn}" />
打开PSCache,并且指定每个连接上PSCache的大小 如果用Oracle,则把poolPreparedStatements配置为true,mysql可以配置为false。
<property name="poolPreparedStatements" value="${druid.poolPreparedStatements}" />
<property name="maxPoolPreparedStatementPerConnectionSize"
value="${druid.maxPoolPreparedStatementPerConnectionSize}" />
配置监控统计拦截的filters
<property name="filters" value="${druid.filters}" />
</bean> -->


<bean id="dataSourceProxy"
class="org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy">
<property name="targetDataSource" ref="dataSource" />
</bean>

<!--指定多数据源 -->
<bean id="dataSource" class="com.jjmc.server.core.DynamicDataSource">
<property name="targetDataSources">
<map key-type="java.lang.String">
<!-- 指定lookupKey和与之对应的数据源 -->
<entry key="dataSource1" value-ref="dataSource1"></entry>
<entry key="dataSource2" value-ref="dataSource2"></entry>
<!-- <entry key="dataSource3" value-ref="dataSource3"></entry> -->
</map>
</property>
<!-- 这里可以指定默认的数据源 -->
<property name="defaultTargetDataSource" ref="dataSource1" />
</bean>

<!--事务管理器配置 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSourceProxy" />
</bean>

<!-- 声明SqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:mybatis/mybatis-config.xml"></property>
<property name="mapperLocations" value="classpath:mapping/*.xml" />
<!-- <property name="plugins">
<array>
<bean class="com.github.pagehelper.PageInterceptor">
<property name="properties">
<value>
helperDialect=oracle
reasonable=true
supportMethodsArguments=true
params=count=countSql
autoRuntimeDialect=true
</value>
</property>
</bean>
</array>
</property> -->
</bean>

<!-- 扫描QMS系统数据层接口 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.jjmc.server.dao" />
</bean>


<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.Exception" />
<tx:method name="insert*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.Exception" />
<tx:method name="update*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.Exception" />
<tx:method name="delete*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.Exception" />
</tx:attributes>
</tx:advice>



<!--多个数据源的事务处理 -->
<!-- <bean id="dataSourceInterceptor" class="com.jjmc.server.core.DynamicDataSourceInterceptor" />
<aop:config>
<aop:aspect id="dataSourceAspect" ref="dataSourceInterceptor">
<aop:pointcut id="dsMysql" expression="execution(* com.jjmc.server.controller.*.*(..))" />
<aop:pointcut id="dsSql" expression="execution(* com.jjmc.server.controller.*.*(..))" />
<aop:pointcut id="dsOracle" expression="execution(* com.jjmc.server.controller.*.*(..))" />
<aop:before method="setdataSourceMysql" pointcut-ref="dsMysql"/>
<aop:before method="setdataSourceSql" pointcut-ref="dsSql"/>
<aop:before method="setdataSourceOracle" pointcut-ref="dsOracle"/>
</aop:aspect>
</aop:config> -->

<bean id="dataSourceAspect" class="com.jjmc.server.core.DynamicDataSourceInterceptor" />
<aop:config>
<aop:aspect ref="dataSourceAspect">
<!-- 拦截所有service方法 -->
<aop:pointcut id="dataSourcePointcut" expression="execution(* com.jjmc.server.service.impl.*.*(..))"/>
<aop:before pointcut-ref="dataSourcePointcut" method="intercept" />
</aop:aspect>
</aop:config>

<!--一个数据源的事务处理 -->
<!-- <aop:config expose-proxy="true" proxy-target-class="true">
只对业务逻辑层实施事务
<aop:pointcut id="txPointcut"
expression="execution(* com.jjmc.server.service..*.*(..))" />
<aop:advisor id="txAdvisor" advice-ref="txAdvice"
pointcut-ref="txPointcut" />
</aop:config> -->
</beans>

============================================================================

5,rabbitmq配置  application-rabbitmq.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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:rabbit="http://www.springframework.org/schema/rabbit"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/rabbit
http://www.springframework.org/schema/rabbit/spring-rabbit-1.4.xsd">
<!-- 加载数据库配置文件 -->
<context:property-placeholder location="classpath:rabbitmq/rabbitmq.properties"
ignore-unresolvable="true" />

<!--配置connection-factory,指定连接rabbit server参数 -->
<rabbit:connection-factory id="connectionFactory"
username="${rabbitmq.username}"
password="${rabbitmq.password}"
host="${rabbitmq.host}"
port="${rabbitmq.port}"
/>

<!--通过指定下面的admin信息,当前producer中的exchange和queue会在rabbitmq服务器上自动生成 -->
<rabbit:admin id="connectAdmin" connection-factory="connectionFactory" />

<!--定义rabbit template用于数据的接收和发送 -->
<rabbit:template id="amqpTemplate" connection-factory="connectionFactory"
exchange="exchangeTest" />

<!--定义queue -->
<!--
durable 是否持久化
exclusive 仅创建者可以使用的私有队列,断开后自动删除
auto-delete 当所有消费端连接断开后,是否自动删除队列 -->
<rabbit:queue id="queueTest" name="queueTest" durable="true" auto-delete="false"
exclusive="false" declared-by="connectAdmin" />

<!-- 定义direct exchange,绑定queueTest -->
<!-- 交换机定义 -->
<!--
交换机:一个交换机可以绑定多个队列,一个队列也可以绑定到多个交换机上。
如果没有队列绑定到交换机上,则发送到该交换机上的信息则会丢失。

direct模式:消息与一个特定的路由器完全匹配,才会转发
topic模式:按规则转发消息,最灵活
-->

<rabbit:direct-exchange name="exchangeTest"
durable="true" auto-delete="false" declared-by="connectAdmin">
<rabbit:bindings>
<rabbit:binding queue="queueTest" key="queueTestKey"></rabbit:binding>
</rabbit:bindings>
</rabbit:direct-exchange>

 

<!-- 消息接收者 -->
<bean id ="messageReceiver" class="com.jjmc.server.filter.MessageConsumer"></bean>

<!-- queue litener 观察 监听模式 当有消息到达时会通知监听在对应的队列上的监听对象 -->
<rabbit:listener-container connection-factory="connectionFactory" acknowledge="auto">
<rabbit:listener queues="queueTest" ref="messageReceiver" />
</rabbit:listener-container>

<!--定义queue -->
<rabbit:queue id="queueChris" name="queueChris" durable="true"
auto-delete="false" exclusive="false" declared-by="connectAdmin" />

<!-- 定义direct exchange,绑定queueTest -->
<rabbit:direct-exchange name="exchangeTest"
durable="true" auto-delete="false" declared-by="connectAdmin">
<rabbit:bindings>
<rabbit:binding queue="queueChris" key="queueTestChris"></rabbit:binding>
</rabbit:bindings>
</rabbit:direct-exchange>

<!-- 消息接收者 -->
<bean id="receiverChris" class="com.jjmc.server.filter.ChrisConsumer"></bean>

<!-- queue litener 观察 监听模式 当有消息到达时会通知监听在对应的队列上的监听对象 -->
<rabbit:listener-container
connection-factory="connectionFactory">
<rabbit:listener queues="queueChris" ref="receiverChris" />
</rabbit:listener-container>

<!-- 分隔线 -->
<!--配置connection-factory,指定连接rabbit server参数 -->
<rabbit:connection-factory id="connectionFactory2"
username="${rabbitmq.username}"
password="${rabbitmq.password}"
host="${rabbitmq.host}"
port="${rabbitmq.port}"
/>

<!--通过指定下面的admin信息,当前producer中的exchange和queue会在rabbitmq服务器上自动生成 -->
<rabbit:admin id="connectAdmin2" connection-factory="connectionFactory2" />

<!--定义queue -->
<rabbit:queue id="queueShijj" name="queueShijj" durable="true"
auto-delete="false" exclusive="false" declared-by="connectAdmin2" />

<!-- 定义direct exchange,绑定queueTest -->
<rabbit:topic-exchange name="exchangeTest2"
durable="true" auto-delete="false" declared-by="connectAdmin2">
<rabbit:bindings>
<rabbit:binding queue="queueShijj" pattern="shijj.#"></rabbit:binding>
</rabbit:bindings>
</rabbit:topic-exchange>

<!--定义rabbit template用于数据的接收和发送 -->
<rabbit:template id="amqpTemplate2" connection-factory="connectionFactory2"
exchange="exchangeTest2" />

<!-- 消息接收者 -->
<bean id="recieverShijj" class="com.jjmc.server.filter.ShijjConsumer"></bean>

<!-- queue litener 观察 监听模式 当有消息到达时会通知监听在对应的队列上的监听对象 -->
<rabbit:listener-container
connection-factory="connectionFactory2">
<rabbit:listener queues="queueShijj" ref="recieverShijj" />
</rabbit:listener-container>
</beans>

=======================================================================================

6,rabbitmq的properties文件

#rabbitmq
rabbitmq.host=127.0.0.1
rabbitmq.port=5672
rabbitmq.username=guest
rabbitmq.password=guest

rabbitmq.virtual.host=rabbit1

rabbitmq.queue.name1=queueTest
rabbitmq.queue.name2=queueChris
rabbitmq.queue.name3=queueShijj

=========================================================================================

7,Controller层

/**
* 2017年11月22日下午2:00:21
*/
package com.jjmc.server.controller;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jjmc.server.service.MessageProducer;

/**
*
* @author huangtao
* 2017年11月22日下午2:00:21
* business-server
* @parameter
* TODO
*
*/
@Controller
public class MessageController {
private static Logger logger = Logger.getLogger(MessageController.class);

@Autowired
private MessageProducer messageProducer;

@RequestMapping(value = "/SendMessage", method = RequestMethod.POST)
@ResponseBody
public void send(HttpServletRequest request, HttpServletResponse response,@RequestBody Object msg) throws IOException{
logger.info(Thread.currentThread().getName()+"------------send to rabbitmq Start");
messageProducer.sendMessage(msg);
logger.info(Thread.currentThread().getName()+"------------send to rabbitmq End");
}


}

 

===============================================================

8,service层

/**
* 2017年11月22日上午11:13:27
*/
package com.jjmc.server.service;

import java.io.IOException;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.stereotype.Service;


/**
*
* @author huangtao
* 2017年11月22日上午11:13:27
* business-server
* @parameter
* TODO
*
*/
@Service
public class MessageProducer {
private static Logger logger = Logger.getLogger(MessageProducer.class);

@Resource(name="amqpTemplate")
private AmqpTemplate amqpTemplate;

@Resource(name="amqpTemplate2")
private AmqpTemplate amqpTemplate2;

public void sendMessage(Object message) throws IOException {
logger.info("to send message:{} "+message);
amqpTemplate.convertAndSend("queueTestKey", message);
amqpTemplate.convertAndSend("queueTestChris", message);
amqpTemplate2.convertAndSend("shijj.xxxx.wsdwd", message);
}
}

==================================================================================

9,filter层

注意:一共有三个消费者,所以有三个类

----------------------------------------------------------------------------

/**
* 2017年11月22日上午11:15:24
*/
package com.jjmc.server.filter;

import org.apache.log4j.Logger;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;
import org.springframework.stereotype.Component;

/**
*
* @author huangtao
* 2017年11月22日上午11:15:24
* business-server
* @parameter
* TODO
*
*/
@Component
public class MessageConsumer implements MessageListener {
private static Logger logger = Logger.getLogger(MessageConsumer.class);
@Override
public void onMessage(Message message) {
logger.info("consumer receive message------->:{} "+message);
}
}

--------------------------------------------------------------------------------

/**
* 2017年11月22日上午11:17:05
*/
package com.jjmc.server.filter;

import org.apache.log4j.Logger;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;
import org.springframework.stereotype.Component;

/**
*
* @author huangtao
* 2017年11月22日上午11:17:05
* business-server
* @parameter
* TODO
*
*/
@Component
public class ChrisConsumer implements MessageListener {
private static Logger logger = Logger.getLogger(ChrisConsumer.class);
@Override
public void onMessage(Message message) {
logger.info("chris receive message------->:{} "+message);
}
}

--------------------------------------------------------------------------------

/**
* 2017年11月22日上午11:18:14
*/
package com.jjmc.server.filter;

import org.apache.log4j.Logger;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;
import org.springframework.stereotype.Component;

/**
*
* @author huangtao
* 2017年11月22日上午11:18:14
* business-server
* @parameter
* TODO
*
*/
@Component
public class ShijjConsumer implements MessageListener {
private static Logger logger = Logger.getLogger(ShijjConsumer.class);

@Override
public void onMessage(Message message) {
logger.info("shijj receive message------->:{} "+message);
}
}

=====================================================================

 

展开阅读全文

没有更多推荐了,返回首页