和maven工程结合
1. 在pom.xml文件中加入依赖。
<!--rabbitmq依赖 -->
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-rabbit</artifactId>
<version>1.4.5.RELEASE</version>
</dependency>
<dependency>
<groupId>com.rabbitmq</groupId>
<artifactId>amqp-client</artifactId>
<version>3.5.1</version>
</dependency>
<!-- rabbitMQ end -->
2. 编写rabbitMQ的配置文件。我起名叫spring-rabbit.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:rabbit="http://www.springframework.org/schema/rabbit"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/rabbit
http://www.springframework.org/schema/rabbit/spring-rabbit-1.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<description>rabbitmq 连接服务配置</description>
<!-- 连接配置 -->
<rabbit:connection-factory id="connectionFactory" host="127.0.0.1" username="guest" password="guest" port="5672" virtual-host="/" />
<rabbit:admin connection-factory="connectionFactory"/>
<!-- spring template声明 -->
<rabbit:template exchange="test-mq-exchange" id="amqpTemplate" connection-factory="connectionFactory" message-converter="jsonMessageConverter" />
<!-- direct Exchange -->
<rabbit:direct-exchange name="test-mq-exchange" durable="true" auto-delete="false" id="test-mq-exchange">
<rabbit:bindings>
<rabbit:binding queue="test_queue_key" key="test_queue_key"/>
</rabbit:bindings>
</rabbit:direct-exchange>
<rabbit:queue id="test_queue_key" name="test_queue_key" durable="true" auto-delete="false" exclusive="false" />
<rabbit:listener-container connection-factory="connectionFactory" acknowledge="auto">
<rabbit:listener queues="test_queue_key" ref="queueListenter"/>
</rabbit:listener-container>
<!-- topic exchange -->
<rabbit:template exchange="exchangeTopic" id="amqpTemplate1" connection-factory="connectionFactory" message-converter="jsonMessageConverter" />
<rabbit:topic-exchange name="exchangeTopic" id="exchangeTopic"
durable="true" auto-delete="false">
<rabbit:bindings>
<rabbit:binding queue="queueTopic" pattern="liangrui.#"></rabbit:binding>
</rabbit:bindings>
</rabbit:topic-exchange>
<rabbit:queue id="queueTopic" name="queueTopic" durable="true" auto-delete="false" exclusive="false" />
<rabbit:listener-container connection-factory="connectionFactory" acknowledge="auto">
<rabbit:listener queues="queueTopic" ref="queueListenter"/>
</rabbit:listener-container>
<!-- 消息对象json转换类 -->
<bean id="jsonMessageConverter" class="org.springframework.amqp.support.converter.Jackson2JsonMessageConverter" />
<bean id="queueListenter" class="com.thinkgem.jeesite.modules.cust.service.QueueListenter" />
<bean id="mqProducerImpl" class="com.thinkgem.jeesite.modules.cust.service.MQProducerImpl" />
</beans>
3. 编写生产者代码。
a. 先写接口。MQProducer.Java
package com.thinkgem.jeesite.modules.cust.service;
public interface MQProducer {
/**
* 发送消息到指定队列
* @param queueKey
* @param object
*/
public void sendDataToQueue(Object object);
}
b. 再写实现类。MQProducerImpl.java
package com.thinkgem.jeesite.modules.cust.service;
import org.apache.log4j.Logger;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class MQProducerImpl implements MQProducer {
@Autowired
private AmqpTemplate amqpTemplate;
@Autowired
private AmqpTemplate amqpTemplate1;
//direct exchange
final String queue_key = "test_queue_key";
//topic exchange
final String queue_pat = "liangrui.#";
private final static Logger LOGGER = Logger.getLogger(MQProducerImpl.class);
/* (non-Javadoc)
* @see com.stnts.tita.rm.api.mq.MQProducer#sendDataToQueue(java.lang.String, java.lang.Object)
*/
@Override
public void sendDataToQueue(Object object) {
try {
System.out.println("before send");
//direct exchange
//amqpTemplate.convertAndSend(queue_key, object);
//topic exchange
amqpTemplate1.convertAndSend("liangrui.123", object);
System.out.println("after send");
} catch (Exception e) {
LOGGER.error(e);
}
}
}
4.编写消费者代码 QueueListenter.java
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;
import org.springframework.stereotype.Component;
@Component
public class QueueListenter implements MessageListener {
@Override
public void onMessage(Message msg) {
try{
System.out.println("QueueListenter onMessage: " + msg.toString());
}catch(Exception e){
e.printStackTrace();
}
}
}
5. 编写测试类TestQueue.java
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.thinkgem.jeesite.modules.cust.service.MQProducerImpl;
@RunWith(value = SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {
"classpath:rabbitMQ.xml"})
@Service
public class TestQueue{
@Autowired
MQProducer mqProducer;
@Test
public void send(){
//Map<String,Object> msg = new HashMap<>();
//msg.put("data","hello,rabbmitmq!");
MyMessage msg = new MyMessage();
msg.setFrom("from");
msg.setTo("to");
msg.setIsRead(1);
msg.setSender("sender");
msg.setReceiver("receiver");
msg.setMessage("messagebody");
msg.setSendTime(new Date());
msg.setReceiveTime(new Date());
mqProducer.sendDataToQueue(msg);
}
}
6. 实体类MyMessage.java
import java.util.Date;
public class MyMessage {
private String from;
private String to;
private String sender;
private Object message;
private Integer isRead;
private String receiver;
private Date sendTime;
private Date receiveTime;
public String getFrom() {
return from;
}
public void setFrom(String from) {
this.from = from;
}
public String getTo() {
return to;
}
public void setTo(String to) {
this.to = to;
}
public String getSender() {
return sender;
}
public void setSender(String sender) {
this.sender = sender;
}
public Object getMessage() {
return message;
}
public void setMessage(Object message) {
this.message = message;
}
public Integer getIsRead() {
return isRead;
}
public void setIsRead(Integer isRead) {
this.isRead = isRead;
}
public String getReceiver() {
return receiver;
}
public void setReceiver(String receiver) {
this.receiver = receiver;
}
public Date getSendTime() {
return sendTime;
}
public void setSendTime(Date sendTime) {
this.sendTime = sendTime;
}
public Date getReceiveTime() {
return receiveTime;
}
public void setReceiveTime(Date receiveTime) {
this.receiveTime = receiveTime;
}
}