一.生产者
配置文件:
spring:
#rabbitmq
rabbitmq:
addresses: 127.0.0.1:5672
username: guest
password: guest
virtual-host: /
connection-timeout: 15000
publisher-confirms: true
publisher-returns: true
template:
mandatory: true
**1.生产者依赖**
<!--rabbitmq依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
**2.数据实体类**
import java.io.Serializable;
public class Order implements Serializable {
private Long id;
private String name;
private Long messageId; //存储消息发送的唯一标识
public Order() {
}
public Order(Long id, String name, Long messageId) {
this.id = id;
this.name = name;
this.messageId = messageId;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Long getMessageId() {
return messageId;
}
public void setMessageId(Long messageId) {
this.messageId = messageId;
}
@Override
public String toString() {
return "Order{" +
"id=" + id +
", name='" + name + '\'' +
", messageId=" + messageId +
'}';
}
}
**3.生产者发送消息**
import io.renren.modules.sys.entity.Order;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.support.CorrelationData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class RabbitOrderSender {
@Autowired
private RabbitTemplate rabbitTemplate;
//回调函数: confirm确认
final RabbitTemplate.ConfirmCallback confirmCallback = new RabbitTemplate.ConfirmCallback() {
@Override
public void confirm(CorrelationData correlationData, boolean ack, String s) {
System.out.println("CorrelationData: "+correlationData);
if (ack){
//如果confirm返回成功 则进行更新
System.out.println("更新");
}else{
//失败则进行具体的后续操作; 重试或者补偿等手段
System.out.println("异常处理....");
}
}
};
public void sendOrder(Order order) throws Exception{
CorrelationData correlationData = new CorrelationData();
correlationData.setId(order.getMessageId()+"");
rabbitTemplate.setConfirmCallback(confirmCallback);
rabbitTemplate.convertAndSend("order-exchange", //exchang 交换机
"order.demo", //routingKey 路由键
order, //消息体内容
correlationData); //correlationData 消息唯一ID
}
二.消费者
**配置文件:**
spring:
#rabbitmq
rabbitmq:
addresses: 127.0.0.1:5672
username: guest
password: guest
virtual-host: /
connection-timeout: 15000
publisher-confirms: true
publisher-returns: true
listener:
simple:
concurrency: 5
max-concurrency: 15
acknowledge-mode: manual
prefetch: 1
**1.依赖:**
<!--rabbitmq依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
**2.接收数据实体类**
import java.io.Serializable;
public class Order implements Serializable {
private Long id;
private String name;
private Long messageId; //存储消息发送的唯一标识
public Order() {
}
public Order(Long id, String name, Long messageId) {
this.id = id;
this.name = name;
this.messageId = messageId;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Long getMessageId() {
return messageId;
}
public void setMessageId(Long messageId) {
this.messageId = messageId;
}
@Override
public String toString() {
return "Order{" +
"id=" + id +
", name='" + name + '\'' +
", messageId=" + messageId +
'}';
}
}
3.接收数据
import com.rabbitmq.client.Channel;
import io.renren.modules.sys.entity.Order;
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 java.util.Map;
@Component
public class OrderReceiver {
@RabbitListener(bindings = @QueueBinding(
value = @Queue(value = "order-queue",durable = "true"),
exchange = @Exchange(name = "order-exchange", durable = "true", type = "topic"),
key = "order.*"
))
@RabbitHandler
public void onOrderMessage(@Payload Order order,
@Headers Map<String,Object> headers,
Channel channel) throws Exception{
//消费者操作
System.out.println("---- 收到消息,开始消费 ----");
System.out.println("订单ID: "+ order.getId());
System.out.println("订单名称: "+ order.getName());
Long deliveryTag = (Long)headers.get(AmqpHeaders.DELIVERY_TAG);
//手动必须
channel.basicAck(deliveryTag,false);
}
}