spring boot rabbitmq

pom.xml

<!-- rebaiit mq -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-amqp</artifactId>
</dependency>

properties

# MQ
spring.rabbitmq.host=172.16.168.63
spring.rabbitmq.port=5672
spring.rabbitmq.username=rabbitadmin
spring.rabbitmq.password=123456
# 开启发送确认  
spring.rabbitmq.publisher-confirms=true  
# 开启发送失败退回  
spring.rabbitmq.publisher-returns=true  
# 开启ACK  
spring.rabbitmq.listener.direct.acknowledge-mode=manual  
spring.rabbitmq.listener.simple.acknowledge-mode=manual  

生产者:

RabbitConfig.java

import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;



@Configuration
public class RabbitConfig {

    @Bean
    public Queue queue() {
        return new Queue("hello",true);
    }
    
}
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;



@Configuration
public class RabbitConfig {

    @Bean
    public Queue topLogQueue() {
        return new Queue("topic.log",true);
    }

    @Bean
    public Queue topNewsQueue() {
        return new Queue("topic.news",true);
    }
    
    @Bean
    public TopicExchange topicExchange() {
        return new TopicExchange("topicExchange");
    }
    
    @Bean
    public Binding bindingExchangeTopLog(Queue topLogQueue, TopicExchange topicExchange) {
        return BindingBuilder.bind(topLogQueue).to(topicExchange).with("#.log.#");
    }
    
    @Bean
    public Binding bindingExchangeTopNews(Queue topNewsQueue, TopicExchange topicExchange) {
        return BindingBuilder.bind(topNewsQueue).to(topicExchange).with("#.news.#");
    }
}


HelloSender.java

import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import lombok.extern.slf4j.Slf4j;

@Component
@Slf4j
public class HelloSender implements org.springframework.amqp.rabbit.core.RabbitTemplate.ReturnCallback{

    @Autowired
    private RabbitTemplate rabbitTemplate;
    
    
    public void send(String msg) {
        this.rabbitTemplate.setReturnCallback(this);  
        this.rabbitTemplate.setConfirmCallback((correlationData, ack, cause) -> {  
            if (!ack) {  
                log.info("HelloSender 消息发送失败! {}:{}", cause, correlationData.toString());  
            } else {  
                log.info("HelloSender 消息发送成功 !");  
            }  
        });
        this.rabbitTemplate.convertAndSend("hello", msg);  
    }


    @Override
    public void returnedMessage(Message message, int replyCode, String replyText, String exchange, String routingKey) {
        log.info("sender return success" + message.toString()+"==="+replyCode+"==="+exchange+"==="+routingKey);
    }

    
    
}

import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import lombok.extern.slf4j.Slf4j;

@Component
@Slf4j
public class HelloSender implements org.springframework.amqp.rabbit.core.RabbitTemplate.ReturnCallback{

    @Autowired
    private RabbitTemplate rabbitTemplate;
    
    
    public void send(String msg) {
        this.rabbitTemplate.setReturnCallback(this);  
        this.rabbitTemplate.setConfirmCallback((correlationData, ack, cause) -> {  
            if (!ack) {  
                log.info("HelloSender 消息发送失败! {}:{}", cause, correlationData.toString());  
            } else {  
                log.info("HelloSender 消息发送成功 !");  
            }  
        });
        this.rabbitTemplate.convertAndSend("topicExchange","log.news",msg);  
    }


    @Override
    public void returnedMessage(Message message, int replyCode, String replyText, String exchange, String routingKey) {
        log.info("sender return success" + message.toString()+"==="+replyCode+"==="+exchange+"==="+routingKey);
    }

    
    
}


    
    
消费者:

HelloReceiver.java

import java.io.IOException;
import java.util.Date;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import com.rabbitmq.client.Channel;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Component
@RabbitListener(queues = "hello")
public class HelloReceiver {
    
    @RabbitHandler  
    public void process(String hello,Channel channel, Message message) throws IOException { 
        try {  
            //告诉服务器收到这条消息 已经被我消费了 可以在队列删掉 这样以后就不会再发了 否则消息服务器以为这条消息没处理掉 后续还会在发  
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false); 
            log.info("receiver success 收到消息  :{},时间:{}",hello,new Date());
        } catch (IOException e) {  
            //丢弃这条消息  
            channel.basicNack(message.getMessageProperties().getDeliveryTag(), false,false);  
            log.info("receiver fail 收到消息  :{} {}",hello, e);
            // 放回队列
            //channel.basicReject(message.getMessageProperties().getDeliveryTag(), true);
        }  
  
    }  
}
import java.io.IOException;
import java.util.Date;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import com.rabbitmq.client.Channel;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Component
@RabbitListener(queues = "topic.log")
public class HelloReceiver {
    
    @RabbitHandler  
    public void process(String hello,Channel channel, Message message) throws IOException { 
        
        try {  
            //告诉服务器收到这条消息 已经被我消费了 可以在队列删掉 这样以后就不会再发了 否则消息服务器以为这条消息没处理掉 后续还会在发  
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false); 
            log.info("receiver success 收到消息  :{},时间:{}",hello,new Date());
        } catch (IOException e) {  
            //丢弃这条消息  
            channel.basicNack(message.getMessageProperties().getDeliveryTag(), false,false);  
            log.info("receiver fail 收到消息  :{} {}",hello, e);
            // 放回队列
            //channel.basicReject(message.getMessageProperties().getDeliveryTag(), true);
        }  
  
    }  
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值