jar
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
<version>1.5.2.RELEASE</version>
</dependency>
生产端
实现回调,确保数据发送成功
application.properties
server.port=8081
spring.rabbitmq.addresses=127.0.0.1:5672
spring.rabbitmq.username=user
spring.rabbitmq.password=123
spring.rabbitmq.virtual-host=/user
spring.rabbitmq.connection-timeout=1000
spring.rabbitmq.publisher-confirms=true
spring.rabbitmq.publisher-returns=true
spring.rabbitmq.template.mandatory=true
Order
package com.rabbitMQ.po;
import java.io.Serializable;
public class Order implements Serializable{
/**
*
*/
private static final long serialVersionUID = -8676414396395857721L;
private Integer id;
private String name;
private String messageId;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getMessageId() {
return messageId;
}
public void setMessageId(String messageId) {
this.messageId = messageId;
}
}
发送数据(一)
package com.rabbitMQ.sendOrder;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.core.RabbitTemplate.ConfirmCallback;
import org.springframework.amqp.rabbit.core.RabbitTemplate.ReturnCallback;
import org.springframework.amqp.rabbit.support.CorrelationData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.rabbitMQ.po.Order;
@Component
public class Send {
@Autowired
private RabbitTemplate rabbitTemplate;
// 消息发送到交换机回调
final ConfirmCallback confirmCallback = new ConfirmCallback() {
@Override
public void confirm(CorrelationData correlationData, boolean ack, String cause) {
System.err.println("correlationData:" + correlationData);
String massageID = correlationData.getId();
System.err.println("===massageID:" + massageID);
if (ack) {
System.err.println("ACK:" + ack );
} else {
System.err.println("ACK:" + ack+ "===cause:" + cause);
}
}
};
// 交换机到队列报错执行回调
final ReturnCallback returnCallback = new ReturnCallback() {
@Override
public void returnedMessage(Message message, int replyCode, String replyText, String exchange,
String routingKey) {
System.err.println("==========消息主体=========" + message);
System.err.println("==========消息主体=========" + replyCode);
System.err.println("==========描述=========" + replyText);
System.err.println("==========交换机=========" + exchange);
System.err.println("==========路由键=========" + routingKey);
}
};
public void SendOrder(Order order) throws Exception {
rabbitTemplate.setConfirmCallback(confirmCallback);
rabbitTemplate.setReturnCallback(returnCallback);
CorrelationData correlationData = new CorrelationData();
correlationData.setId(order.getMessageId());
rabbitTemplate.convertAndSend("order-exchange", "order.add", order, correlationData);
// Thread.sleep(10);
}
}
发送数据(二)
package com.rabbitMQ.sendOrder;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.core.RabbitTemplate.ConfirmCallback;
import org.springframework.amqp.rabbit.core.RabbitTemplate.ReturnCallback;
import org.springframework.amqp.rabbit.support.CorrelationData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import com.rabbitMQ.po.Order;
@Component
public class Send2 implements ConfirmCallback,ReturnCallback {
private RabbitTemplate rabbitTemplate;
@Autowired
public Send2(RabbitTemplate rabbitTemplate) {
this.rabbitTemplate = rabbitTemplate;
rabbitTemplate.setConfirmCallback(this);
rabbitTemplate.setReturnCallback(this);
}
@Override
public void confirm(CorrelationData correlationData, boolean ack, String cause) {
System.err.println("correlationData:" + correlationData);
String massageID = correlationData.getId();
System.err.println("===massageID:" + massageID);
if (!ack) {
System.err.println("ACK:" + ack+ "===cause:" + cause);
} else {
System.err.println("ACK:" + ack );
}
}
// 交换机到队列报错执行回调
@Override
public void returnedMessage(Message message, int replyCode, String replyText, String exchange,
String routingKey) {
System.err.println("==========消息主体=========" + message);
System.err.println("==========消息主体=========" + replyCode);
System.err.println("==========描述=========" + replyText);
System.err.println("==========交换机=========" + exchange);
System.err.println("==========路由键=========" + routingKey);
}
public void SendOrder(Order order) throws Exception {
CorrelationData correlationData = new CorrelationData();
correlationData.setId(order.getMessageId());
rabbitTemplate.convertAndSend("order-exchange", "order.add", order, correlationData);
}
}
测试发送
package com.rabbitMQ;
import java.util.UUID;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import com.rabbitMQ.po.Order;
import com.rabbitMQ.sendOrder.Send;
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringRabbitMqSendApplicationTests {
@Autowired
private Send send;
@Test
public void sendOrder() throws Exception {
Order order = new Order();
order.setId(1);
order.setName("OrderName");
order.setMessageId(System.currentTimeMillis() + "$" + UUID.randomUUID());
send.SendOrder(order);
}
}
消费者
需要成功接收
application.properties
server.port=8082
spring.rabbitmq.addresses=127.0.0.1:5672
spring.rabbitmq.username=user
spring.rabbitmq.password=123
spring.rabbitmq.virtual-host=/user
spring.rabbitmq.connection-timeout=1000
spring.rabbitmq.listener.simple.concurrency=5
spring.rabbitmq.listener.simple.max-concurrency=10
spring.rabbitmq.listener.simple.acknowledge-mode=manual
spring.rabbitmq.listener.simple.prefetch=1
接收数据
package com.rabbitMQ.customer;
import java.util.Map;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.messaging.handler.annotation.Headers;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;
import com.rabbitMQ.po.Order;
import com.rabbitmq.client.Channel;
@Component
public class OrderRevicer {
@RabbitListener(bindings = @QueueBinding(
value = @Queue(value = "order-queue", durable = "true"),
exchange = @Exchange(value = "order-exchange", durable = "true", type = "direct"),
key = "order.add"))
@RabbitHandler
public void onOrderMessage(@Payload Order order, @Headers Map<String, Object> headers, Channel channel)
throws Exception {
System.err.println("-----------收到消息---------------");
System.err.println("订单ID:" + order.getId());
Long deliveryTag = (Long) headers.get(AmqpHeaders.DELIVERY_TAG);
System.err.println(deliveryTag);
try {
// 成功
// void basicAck(long deliveryTag, boolean multiple)
// deliveryTag:该消息的index
// multiple:是否批量. true:将一次性ack所有小于deliveryTag的消息。
// ACK
//int i=1/0;
channel.basicAck(deliveryTag, true);
System.err.println(channel.isOpen());
} catch (Exception e) {
// 失败
// channel.basicNack(long deliveryTag, boolean multiple, boolean requeue);
// requeue:被拒绝的是否重新入队列。
// channel.basicReject(long deliveryTag, boolean requeue);
// channel.basicNack 与 channel.basicReject
// 的区别在于basicNack可以批量拒绝多条消息,而basicReject一次只能拒绝一条消息。
// 重新放入队列
//channel.basicNack(deliveryTag, false, true);
// 抛弃此条消息
channel.basicNack(deliveryTag, false, false);
}
}
}
启动项目可接收数据,若rabbitMQ没有交换机和queue,启动消费项目,就可自动生成