rabbit mq整理

一.生产者

配置文件:
	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);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值