RabbitMQ 整合springboot注解开发

 

首先搭建一个springboot环境,这里可以使用idea为我们构建,也可以去 http://spring.io 

next填好项目得groupid之类得到这里我们单单选择一个web即可.

 

加入springboot关于amqp得启动器:

spring-boot-starter-amqp

 

  1. 编写一个consumenr:
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.stereotype.Component;

@Component
@RabbitListener(bindings = @QueueBinding(
        value = @Queue(value = "${spring.rabbitmq.queue}", autoDelete = "true"),
        exchange = @Exchange(value = "${spring.rabbitmq.exchange}", type = ExchangeTypes.DIRECT),
        key = "${spring.rabbitmq.routing_key}"
))
public class MqReciver {

    @RabbitHandler
    public void PrintLog(String msg) {
        System.out.println("我是消息接收者:打印" + msg);
    }

在配置文件application.yml中配置rabbitmq

spring:
  rabbitmq:
    addresses: #######     //mq服务地址
    port: 5672
    username: wuqw
    password: ####
    routing_key: test_msg      //定义路由键名称
    exchange: test_exchange    //定义交换器名
    queue: test_queue          //定义队列
    #开启重试,如果不开启一旦消费者故障,发送者会一直向队列中发送这个消息
    listener:
      retry:
        enabled: true
        #重试次数
        max-attempts: 2

 

2.编写一个provider发送者:

import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.support.CorrelationData;

import java.util.UUID;
/*纯java代码实现发送*/

public class Mqsender implements RabbitTemplate.ConfirmCallback {
    int flag1 = 0;

    public void send() {
        int flag = flag1++;
        CorrelationData correlationData = new 
        CorrelationData(UUID.randomUUID().toString());
        ConnectionFactory bean = getBean();
        AmqpTemplate amqpTemplate = new RabbitTemplate(bean);
        ((RabbitTemplate) amqpTemplate).convertAndSend("test_exchange", "test_msg", "成功测试" + flag, correlationData);   
        //确认
        ((RabbitTemplate) amqpTemplate).setConfirmCallback(this);
    }

    public ConnectionFactory getBean() {
        CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory();
        cachingConnectionFactory.setHost("***88");
        cachingConnectionFactory.setPort(5672);
        cachingConnectionFactory.setUsername("wuqw");
        cachingConnectionFactory.setPassword("*******");
        return cachingConnectionFactory;
    }

    @Override
    public void confirm(CorrelationData correlationData, boolean b) {
        System.out.println("回调确认消息状态" + correlationData);
        if (b) {
            System.out.println("消息发送成功");
        } else {
            System.out.println("消息发送失败");
        }
    }

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值