<性能>
rmq.ip=
rmq.producer.num=
rmq.port=
rmq.manager.user=
rmq.manager.password=
<XML配置>
<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="key" class="org.springframework.amqp.core.Queue">
<constructor-arg index="0" value="key"></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="testQueue" 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>
<!--创建交换器的类型 并持久化-->
<bean id="topicExchange" class="org.springframework.amqp.core.TopicExchange">
<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>
<!-- 用于接收消息的处理类 -->
<bean id="rmqMessageProcess" class="cn.sh.bokun.infraops.web.mqServerUtil.RMQMessageProcess"></bean>
<bean id="messageListenerAdapter" class="org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter">
<constructor-arg ref="rmqMessageProcess" />
<property name="defaultListenerMethod" value="messageProcess"></property>
<property name="messageConverter" ref="serializerMessageConverter"></property>
</bean>
<!-- 用于消息的监听的容器类SimpleMessageListenerContainer,监听队列 queueNames可以传多个使用,隔开-->
<bean id="listenerContainer" class="org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer">
<property name="queueNames" value="key"></property>
<property name="connectionFactory" ref="connectionFactory"></property>
<property name="messageListener" ref="messageListenerAdapter"></property>
</bean>
<生产者>
package cn.sh.bokun.infraops.service; import cn.sh.bokun.infraops.web.mqServerUtil.RabbitMessage; import com.alibaba.fastjson.JSONObject; import com.rabbitmq.client.Channel; import com.rabbitmq.client.Connection; import com.rabbitmq.client.ConnectionFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.amqp.AmqpException; import org.springframework.amqp.core.Message; import org.springframework.amqp.core.MessagePostProcessor; import org.springframework.amqp.core.MessageProperties; import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.amqp.rabbit.support.CorrelationData; import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.stereotype.Service; import redis.clients.jedis.Jedis; import javax.annotation.Resource; import java.util.Timer; import java.util.TimerTask; import java.util.UUID; /** * 发送MQ消息 * Created by Zhangyl on 2017/4/12. */ @Service public class RMQMessageSend { private static Logger logger = LoggerFactory.getLogger(RMQMessageSend.class); @Resource private RabbitTemplate rabbitTemplate; //private static final MessageProperties messageProperties = new MessageProperties(); /** * 默认交换器为 exchange * @param queue 队列 * @param jsonObject 消息体 * 请调用者统一平台以header,body发送 * header{ msgID:消息ID,msgType:消息类型,sender:发送者,recevier:接收者,sendTime:接收者 } * body{ 以JSONObject封装相对应的实体类 } * 详情请参见目录 web/mvc/test/TestCtrl下的testMQ()方法 */ public void sendMessage(String queue, JSONObject jsonObject){ try { System.out.println("发送队列"+queue); System.out.println("队列信息:"+rabbitTemplate.getConnectionFactory()); System.out.println("发送消息:"+jsonObject); //发送信息 / rabbitTemplate.send("testExchange",queue, new Message(jsonObject.toString().getBytes(), messageProperties)); rabbitTemplate.convertAndSend("testExchange",queue,jsonObject.toString()); } catch (Exception e) { logger.error("发送MQ消息失败!",e); } } public static void main(String[] argv) throws java.io.IOException, java.lang.InterruptedException {
/* 使用工厂类建立Connection和Channel,并且设置参数 */ ConnectionFactory factory = new ConnectionFactory(); factory.setHost("127.0.0.1");// MQ的IP factory.setPort(5672);// MQ端口 factory.setUsername("guest");// MQ用户名 factory.setPassword("guest");// MQ密码 Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); /* 创建消息队列,并且发送消息 */ channel.queueDeclare("testQueue", false, false, false, null); String message = "消息2"; channel.basicPublish("", "testQueue", null, message.getBytes()); System.out.println("生产了个'" + message + "'"); /* 关闭连接 */ channel.close(); connection.close();
}}
<消费者>
package cn.sh.bokun.infraops.web.mqServerUtil; import cn.sh.bokun.infraops.model.dto.message.MessageRecord; import cn.sh.bokun.infraops.service.RMQMessageSend; import cn.sh.bokun.infraops.service.testService.entity.User; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.amqp.core.Message; import org.springframework.amqp.rabbit.annotation.RabbitListener; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.stereotype.Component; import javax.annotation.Resource; import java.lang.reflect.Field; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Timer; import java.util.TimerTask; /** * Created by Zhangyl on 2017/4/12. */ @Component public class RMQMessageProcess { private final Logger logger = LoggerFactory.getLogger(RMQMessageProcess.class); @Autowired private MongoTemplate mongoTemplate; @Resource private RMQMessageSend rmqMessageSend; public void messageProcess(Object jsonObject){ try{ System.out.println("收到消息------------"+jsonObject); System.out.println(jsonObject.getClass()); JSONObject json =JSON.toJavaObject(JSONObject.parseObject(jsonObject.toString()),JSONObject.class); JSONObject mesg = JSON.toJavaObject(JSONObject.parseObject(json.get("body").toString()),JSONObject.class) }catch (Exception e){ logger.error("RMQ消息处理失败!消息队列为",e); } } }