ssm+rabbitMQ整合

1、pom.xml导入相应jar(注:exclusions标签中的是与spring别的jar相冲突的处理,如果不冲突可以不加

<dependency>  
        <groupId>org.springframework.amqp</groupId>  
        <artifactId>spring-rabbit</artifactId>  
         <version>2.0.1.RELEASE</version>
         <exclusions>
				<exclusion>
					<artifactId>spring-context</artifactId>
					<groupId>org.springframework</groupId>
				</exclusion>
			</exclusions>
		</dependency>

2、生产者配置(消息发送者)(标红条目为与MQ相关的文件)

(1)spring文件配置

<?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:context="http://www.springframework.org/schema/context"
	xmlns:p="http://www.springframework.org/schema/p" 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:util="http://www.springframework.org/schema/util"
	xsi:schemaLocation="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 
	 http://www.springframework.org/schema/mvc
	http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd 
	 http://www.springframework.org/schema/tx    
	  http://www.springframework.org/schema/tx/spring-tx-3.0.xsd     
	  http://www.springframework.org/schema/aop     
	  http://www.springframework.org/schema/aop/spring-aop-3.0.xsd     
	  http://www.springframework.org/schema/context     
	  http://www.springframework.org/schema/context/spring-context-3.0.xsd">
	  
	<!-- 第一步:【1.整合dao】 将Mybatis和Spring进行整合MyBatis和Spring整合,通过Spring管理mapper接口。使用mapper的扫描器自动扫描mapper接口在Spring中进行注册。 -->
	<!-- 需要配置:a、数据源 b、SqlSessionFactory c、mapper扫描器 -->
	<!-- 1、数据源定义 -->
	<!-- (1)加载jdbc.properties、redis.properties文件中的内容 -->
	<bean id="propertyConfigurer"
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations">
			<list>
				<value>classpath:jdbc.properties</value>
				<value>classpath:redis.properties</value>
				<value>classpath:global.properties</value>
			</list>
		</property>
	</bean>
	<!-- (2)mysql数据源配置 -->
	<!-- a、数据源 -->
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
		destroy-method="close">
		<property name="driverClassName" value="${connection.driverClassName}" />
		<property name="url" value="${connection.url}" />
		<property name="username" value="${connection.username}" />
		<property name="password" value="${connection.password}" />
		<property name="maxActive" value="${connection.maxActive}" />
		<property name="maxIdle" value="${connection.maxIdle}" />
		<property name="minIdle" value="${connection.minIdle}" />
		<property name="removeAbandoned" value="${connection.removeAbandoned}" />
		<property name="removeAbandonedTimeout" value="${connection.removeAbandonedTimeout}" />
		<property name="logAbandoned" value="${connection.logAbandoned}" />
		<property name="defaultAutoCommit" value="${connection.defaultAutoCommit}" />
		<property name="defaultReadOnly" value="${connection.defaultReadOnly}" />
		<property name="validationQuery" value="${connection.validationQuery}" />
		<property name="testOnBorrow" value="${connection.testOnBorrow}" />
	</bean>
	<!-- b、sqlSessionFactory:创建sqlSessionFactory,同时指定数据源 -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource"></property>
		<property name="configLocation" value="classpath:mybatis-config.xml" />
		<!-- 自动扫描mapper目录, 省掉mybatis-config.xml里的手工配置 -->
		<property name="mapperLocations">
			<list>
				<value>classpath:com/loan/*/dao/xml/*.xml</value>
			</list>
		</property>
	</bean>
	<!-- c、mapper扫描器:通过扫描的模式,扫描目录在com/loan/mapper目录下 -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="com.loan.*.dao.mapper" />
		<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
	</bean>
	<!-- (3)redis数据源 -->
	<!-- a、redis数据源 -->
	<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
	<property name="maxIdle" value="${redis.maxIdle}" />
	<property name="maxTotal" value="${redis.maxActive}" />
	<property name="maxWaitMillis" value="${redis.maxWait}" />
	<property name="testOnBorrow" value="${redis.testOnBorrow}" />
	</bean>
	<!-- b、Spring-redis连接池管理工厂 -->
	<bean id="jedisConnectionFactory"
	class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
	p:host-name="${redis.host}" p:port="${redis.port}" p:password="${redis.pass}"
	p:pool-config-ref="poolConfig" />
	<!-- c、使用中间类解决RedisCache.jedisConnectionFactory的静态注入,从而使MyBatis实现第三方缓存 -->
	<bean id="redisCacheTransfer" class="com.loan.security.cache.RedisCacheTransfer">
	<property name="jedisConnectionFactory" ref="jedisConnectionFactory" />
	</bean>
	<!-- rabbitMQ创建连接类 -->
	<bean class="com.loan.util.rabbit.RabbitMQ" />	
	<bean id="connectionFactory"  class="org.springframework.amqp.rabbit.connection.CachingConnectionFactory">    
        <constructor-arg value="localhost" />    
        <property name="username" value="${rmq.manager.user}" />    
        <property name="password" value="${rmq.manager.password}" />   
        <property name="host" value="${rmq.ip}" />   
        <property name="port" value="${rmq.port}" />   
    </bean>    
       
    <bean id="rabbitAdmin"  class="org.springframework.amqp.rabbit.core.RabbitAdmin">    
        <constructor-arg ref="connectionFactory" />    
    </bean>    
     <!-- 创建rabbitTemplate 消息模板类 -->    
    <bean id="rabbitTemplate"  class="org.springframework.amqp.rabbit.core.RabbitTemplate">    
        <constructor-arg ref="connectionFactory"></constructor-arg>    
    </bean>  
	<!-- bean 注入 -->
	<bean class="com.loan.security.spring.SpringUtils" />
	<!-- 第二步:通过Spring管理Service接口。使用配置方式将Service接口配置在Spring配置文件中。实现事务控制。 -->
	<!-- (事务管理) -->
	<bean id="transactionManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"></property>
	</bean>
	<!-- 使用annotation定义数据库事务,这样可以在类或方法中直接使用@Transactional注解来声明事务 -->
	<tx:annotation-driven transaction-manager="transactionManager" />
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="save*" propagation="REQUIRED" />
			<tx:method name="update*" propagation="REQUIRED" />
			<tx:method name="delete*" propagation="REQUIRED" />
			<tx:method name="load*" propagation="SUPPORTS" read-only="true" />
			<tx:method name="find*" propagation="SUPPORTS" read-only="true" />
			<tx:method name="search*" propagation="SUPPORTS" read-only="true" />
			<tx:method name="approve" propagation="REQUIRED" />
			<tx:method name="undo" propagation="REQUIRED" />
			<tx:method name="*" propagation="SUPPORTS" read-only="true" />
		</tx:attributes>
	</tx:advice>
	<aop:config>
		<aop:pointcut id="serviceMethod"
			expression="execution(* com.loan.*.service..*.*(..))" />
		<aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethod" />
	</aop:config>
    <!-- spring管理:自动搜索注解路径 在xml配置了这个标签后,spring可以自动去扫描base-pack下面或者子包下面的Java文件,如果扫描到有@Component @Controller@Service等这些注解的类,则把这些类注册为bean-->  
    <context:component-scan base-package="com.loan"></context:component-scan>  
</beans>

(2)jdbc.properties

connection.driverClassName=com.mysql.jdbc.Driver
connection.url=jdbc:mysql://你的mysql的IP:port?useUnicode=true&characterEncoding=UTF-8
connection.username=你的数据库用户名
connection.password=你的数据库密码

connection.initialSize=0
connection.maxActive=100
connection.maxIdle=30
connection.minIdle=5 
connection.maxWait=5000
connection.removeAbandoned=true
connection.removeAbandonedTimeout=3000
connection.logAbandoned=false
connection.defaultAutoCommit=true
connection.defaultReadOnly=false
connection.validationQuery=SELECT 1
connection.testOnBorrow=true
(3)redis.properties

# Redis settings  
redis.host=redis服务器IP
redis.port=6379  
redis.pass=redis密码

redis.maxIdle=300  
redis.maxActive=600  
redis.maxWait=1000  
redis.testOnBorrow=true 

(3)global.properties(rabbitMQ相关配置)

rmq.ip=队列IP
rmq.producer.num=20
rmq.port=5672
rmq.manager.user=队列用户名
rmq.manager.password=队列密码
exchange=队列交换机名
routeKey=队列名


3、生产者相关类

(1)RabbitMQ.java

package com.loan.util.rabbit;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Value;

import com.loan.util.RabbitMessage;

public class RabbitMQ {
	@Resource  
    private RabbitTemplate rabbitTemplate; 
	@Value("${exchange}")
	private String exchange;
	@Value("${routeKey}")
	private String routeKey;
	public void pushMessageToMQ(String className, String methodName,Map<String,Object> param) {
        RabbitMessage  msg=new RabbitMessage(exchange,routeKey,className, methodName, param);
        try {
        	rabbitTemplate.convertAndSend(msg.getExchange(), msg.getRouteKey(), msg);
		} catch (Exception e) {
			// TODO: handle exception
		}  
	}
}
(2)RabbitMessage,java

package com.loan.util;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.Serializable;

import lombok.Data;
@Data
public class RabbitMessage implements Serializable {
	private static final long serialVersionUID = -6487839157908352120L;

	private Class<?>[] paramTypes;// 参数类型
	private String exchange;// 交换器

	private Object[] params;

	private String routeKey;// 路由key

	private String methodName;
	private String beanName;
	public RabbitMessage() {
	}

	public RabbitMessage(String exchange, String routeKey, Object... params) {
		this.params = params;
		this.exchange = exchange;
		this.routeKey = routeKey;
	}

	@SuppressWarnings("rawtypes")
	public RabbitMessage(String exchange, String routeKey,String beanName, String methodName, Object... params) {
		this.params = params;
		this.exchange = exchange;
		this.routeKey = routeKey;
		this.methodName=methodName;
		this.beanName=beanName;
		int len = params.length;
		Class[] clazzArray = new Class[len];
		for (int i = 0; i < len; i++)
			clazzArray[i] = params[i].getClass();
		this.paramTypes = clazzArray;
	}

	public byte[] getSerialBytes() {
		byte[] res = new byte[0];
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		ObjectOutputStream oos;
		try {
			oos = new ObjectOutputStream(baos);
			oos.writeObject(this);
			oos.close();
			res = baos.toByteArray();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return res;
	}

}

(3)发送信息测试类

JunitTest.java

package com.loan.util.rabbit;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;

import com.loan.util.RabbitMessage;

@RunWith(SpringJUnit4ClassRunner.class)
//注入bean
@WebAppConfiguration
@ContextConfiguration("classpath:/application-context.xml")
public class JunitTest{
	@Resource
	private RabbitMQ rabbitMQ;
	
	@Test
	public void test(){
       //推送信息
		String beanName="chinaMobile";
        String methodName="chinaMobileV3";//调用的方法
        //参数  
        Map<String,Object> param=new HashMap<String, Object>();
        List list=new ArrayList();
        list.add("TASKYYS100000201712081422290720980846");
        list.add(3);
        param.put("data",list);  
        rabbitMQ.pushMessageToMQ(beanName, methodName, param);
	}
	
}

测试类运行之后会队列中插入一条数据。

至此生产者完成

4、消费者配置

(1)spring 相关配置

<?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:context="http://www.springframework.org/schema/context"
	xmlns:p="http://www.springframework.org/schema/p" 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:util="http://www.springframework.org/schema/util"
	xsi:schemaLocation="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 
	 http://www.springframework.org/schema/mvc
	http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd 
	 http://www.springframework.org/schema/tx    
	  http://www.springframework.org/schema/tx/spring-tx-3.0.xsd     
	  http://www.springframework.org/schema/aop     
	  http://www.springframework.org/schema/aop/spring-aop-3.0.xsd     
	  http://www.springframework.org/schema/context     
	  http://www.springframework.org/schema/context/spring-context-3.0.xsd">

	<!-- 第一步:【1.整合dao】 将Mybatis和Spring进行整合MyBatis和Spring整合,通过Spring管理mapper接口。使用mapper的扫描器自动扫描mapper接口在Spring中进行注册。 -->
	<!-- 需要配置:a、数据源 b、SqlSessionFactory c、mapper扫描器 -->
	<!-- 1、数据源定义 -->
	<!-- (1)加载jdbc.properties、redis.properties文件中的内容 -->
	<bean id="propertyConfigurer"
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations">
			<list>
				<value>classpath:jdbc.properties</value>
				<value>classpath:redis.properties</value>
				<value>classpath:global.properties</value>
			</list>
		</property>
	</bean>
	<!-- (2)mysql数据源配置 -->
	<!-- a、数据源 -->
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
		destroy-method="close">
		<property name="driverClassName" value="${connection.driverClassName}" />
		<property name="url" value="${connection.url}" />
		<property name="username" value="${connection.username}" />
		<property name="password" value="${connection.password}" />
		<property name="maxActive" value="${connection.maxActive}" />
		<property name="maxIdle" value="${connection.maxIdle}" />
		<property name="minIdle" value="${connection.minIdle}" />
		<property name="removeAbandoned" value="${connection.removeAbandoned}" />
		<property name="removeAbandonedTimeout" value="${connection.removeAbandonedTimeout}" />
		<property name="logAbandoned" value="${connection.logAbandoned}" />
		<property name="defaultAutoCommit" value="${connection.defaultAutoCommit}" />
		<property name="defaultReadOnly" value="${connection.defaultReadOnly}" />
		<property name="validationQuery" value="${connection.validationQuery}" />
		<property name="testOnBorrow" value="${connection.testOnBorrow}" />
	</bean>
	<!-- b、sqlSessionFactory:创建sqlSessionFactory,同时指定数据源 -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource"></property>
		<property name="configLocation" value="classpath:mybatis-config.xml" />
		<!-- 自动扫描mapper目录, 省掉mybatis-config.xml里的手工配置 -->
		<property name="mapperLocations">
			<list>
				<value>classpath:com/loan/*/dao/xml/*.xml</value>
			</list>
		</property>
	</bean>
	<!-- c、mapper扫描器:通过扫描的模式,扫描目录在com/loan/mapper目录下 -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="com.loan.*.dao.mapper" />
		<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
	</bean>
	<!-- (3)redis数据源 -->
	<!-- a、redis数据源 -->
	<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
		<property name="maxIdle" value="${redis.maxIdle}" />
		<property name="maxTotal" value="${redis.maxActive}" />
		<property name="maxWaitMillis" value="${redis.maxWait}" />
		<property name="testOnBorrow" value="${redis.testOnBorrow}" />
	</bean>
	<!-- b、Spring-redis连接池管理工厂 -->
	<bean id="jedisConnectionFactory"
		class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
		p:host-name="${redis.host}" p:port="${redis.port}" p:password="${redis.pass}"
		p:pool-config-ref="poolConfig" />
	<!-- c、使用中间类解决RedisCache.jedisConnectionFactory的静态注入,从而使MyBatis实现第三方缓存 -->
	<bean id="redisCacheTransfer" class="com.loan.security.cache.RedisCacheTransfer">
		<property name="jedisConnectionFactory" ref="jedisConnectionFactory" />
	</bean>

	<bean id="connectionFactory"
		class="org.springframework.amqp.rabbit.connection.CachingConnectionFactory">
		<constructor-arg value="localhost" />
		<property name="username" value="${rmq.manager.user}" />
		<property name="password" value="${rmq.manager.password}" />
		<property name="host" value="${rmq.ip}" />
		<property name="port" value="${rmq.port}" />
	</bean>

	<bean id="rabbitAdmin" class="org.springframework.amqp.rabbit.core.RabbitAdmin">
		<constructor-arg ref="connectionFactory" />
	</bean>
	<!-- 创建rabbitTemplate 消息模板类 -->
	<bean id="rabbitTemplate" class="org.springframework.amqp.rabbit.core.RabbitTemplate">
		<constructor-arg ref="connectionFactory"></constructor-arg>
	</bean>
	<!-- 创建消息转换器为SimpleMessageConverter -->
	<bean id="serializerMessageConverter"
		class="org.springframework.amqp.support.converter.SimpleMessageConverter"></bean>


	<!-- 设置持久化的队列 -->
	<bean id="queue" class="org.springframework.amqp.core.Queue">
		<constructor-arg index="0" value="testQueue"></constructor-arg>
		<constructor-arg index="1" value="true"></constructor-arg>
		<constructor-arg index="2" value="false"></constructor-arg>
		<constructor-arg index="3" value="false"></constructor-arg>
	</bean>
	<!-- 1、<bean id="queue1" class="org.springframework.amqp.core.Queue"> -->
	<!-- <constructor-arg index="0" value="${routeKey4}"></constructor-arg> -->
	<!-- <constructor-arg index="1" value="true"></constructor-arg> -->
	<!-- <constructor-arg index="2" value="false"></constructor-arg> -->
	<!-- <constructor-arg index="3" value="false"></constructor-arg> -->
	<!-- </bean> -->

	<!--创建交换器的类型 并持久化 -->
	<bean id="directExchange" class="org.springframework.amqp.core.DirectExchange">
		<constructor-arg index="0" value="TestExchange"></constructor-arg>
		<constructor-arg index="1" value="true"></constructor-arg>
		<constructor-arg index="2" value="false"></constructor-arg>
	</bean>

	<util:map id="arguments"></util:map>


	<!-- 绑定交换器、队列 -->
	<bean id="binding" class="org.springframework.amqp.core.Binding">
		<constructor-arg index="0" value="testQueue"></constructor-arg>
		<constructor-arg index="1" value="QUEUE"></constructor-arg>
		<constructor-arg index="2" value="TestExchange"></constructor-arg>
		<constructor-arg index="3" value="testQueue"></constructor-arg>
		<constructor-arg index="4" value="#{arguments}"></constructor-arg>
	</bean>
	<!-- 2、绑定 -->
	<!-- <bean id="binding1" class="org.springframework.amqp.core.Binding"> -->
	<!-- <constructor-arg index="0" value="${routeKey4}"></constructor-arg> -->
	<!-- <constructor-arg index="1" value="QUEUE"></constructor-arg> -->
	<!-- <constructor-arg index="2" value="${exchange}"></constructor-arg> -->
	<!-- <constructor-arg index="3" value="${routeKey4}"></constructor-arg> -->
	<!-- <constructor-arg index="4" value="#{arguments}"></constructor-arg> -->
	<!-- </bean> -->

	<!-- 用于接收消息的处理类 -->
	<bean id="rmqConsumer" class="com.loan.util.rabbit.RmqConsumer"></bean>


	<bean id="messageListenerAdapter"
		class="org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter">
		<constructor-arg ref="rmqConsumer" />
		<property name="defaultListenerMethod" value="rmqProducerMessage"></property>
		<property name="messageConverter" ref="serializerMessageConverter"></property>
	</bean>

	<!-- 用于消息的监听的容器类SimpleMessageListenerContainer,监听队列 queues可以传多个 -->
	<bean id="listenerContainer"
		class="org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer">
		<property name="queues">
			<list>
				<ref bean="queue"></ref>
				<!-- 3、<ref bean="queue1"></ref> -->
			</list>
		</property>
		<property name="connectionFactory" ref="connectionFactory"></property>
		<property name="messageListener" ref="messageListenerAdapter"></property>
	</bean> <!-- bean 注入 -->
	<bean class="com.loan.security.spring.SpringUtils" /><!-- 第二步:通过Spring管理Service接口。使用配置方式将Service接口配置在Spring配置文件中。实现事务控制。 --><!-- (事务管理) -->
	<bean id="transactionManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"></property>
	</bean><!-- 使用annotation定义数据库事务,这样可以在类或方法中直接使用@Transactional注解来声明事务 -->
	<tx:annotation-driven transaction-manager="transactionManager" />
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="save*" propagation="REQUIRED" />
			<tx:method name="update*" propagation="REQUIRED" />
			<tx:method name="delete*" propagation="REQUIRED" />
			<tx:method name="load*" propagation="SUPPORTS" read-only="true" />
			<tx:method name="find*" propagation="SUPPORTS" read-only="true" />
			<tx:method name="search*" propagation="SUPPORTS" read-only="true" />
			<tx:method name="approve" propagation="REQUIRED" />
			<tx:method name="undo" propagation="REQUIRED" />
			<tx:method name="*" propagation="SUPPORTS" read-only="true" />
		</tx:attributes>
	</tx:advice>
	<aop:config>
		<aop:pointcut id="serviceMethod"
			expression="execution(* com.loan.*.service..*.*(..))" />
		<aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethod" />
	</aop:config> 
	<!-- spring管理:自动搜索注解路径 在xml配置了这个标签后,spring可以自动去扫描base-pack下面或者子包下面的Java文件,如果扫描到有@Component 
		@Controller@Service等这些注解的类,则把这些类注册为bean -->
	<context:component-scan base-package="com.loan"></context:component-scan>
</beans>

5、消费者对应类


(1)RmqConsumer.java

package com.loan.util.rabbit;

import java.lang.reflect.Method;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.loan.util.RabbitMessage;
import com.loan.util.SpringUtils;

public class RmqConsumer {
	private static Logger log=Logger.getLogger(RmqConsumer.class);
	public static void rmqProducerMessage(Object object) throws Exception{  
        RabbitMessage rabbitMessage=(RabbitMessage) object; 
        log.info("从队列里头取出:bean名-"+rabbitMessage.getBeanName()+",方法名:"+rabbitMessage.getMethodName());
        Object o = SpringUtils.getBean(rabbitMessage.getBeanName());
        Class clazz = o.getClass(); 
        Method m=clazz.getDeclaredMethod(rabbitMessage.getMethodName(), List.class);
        m.invoke(o, ((Map<String,Object>)(rabbitMessage.getParams()[0])).get("data"));
    }
}
(2)测试处理类
ChinaMobile.java


package com.loan.rabbit.task;

@Service
public class ChinaMobile {

    public boolean chinaMobileV3(List<?> list) {
    	boolean result=false;
    		String taskId=(String) list.get(0);
    		Integer userId=(Integer) list.get(1);
            
    }
}



版权声明:本文为dreamer_8399原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。