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);
}
}
}