一,pom文件jar包引入
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
二,properties文件rabbitmq配置
#MQ消息队列配置
spring.rabbitmq.host=172.20.0.15
spring.rabbitmq.mqRabbitVirtualHost=/
spring.rabbitmq.exchangename=exchangewdservice
spring.rabbitmq.equeueename=exchangewdqueue
spring.rabbitmq.port=5672
spring.rabbitmq.username=root
spring.rabbitmq.password=root
spring.rabbitmq.exchange.name=exchange
spring.rabbitmq.size=100
spring.rabbitmq.consumers=20
spring.rabbitmq.prefetch.count=999999999
# 开启发送确认
spring.rabbitmq.publisher-confirm-type=correlated
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
三,rabbit基础配置类
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
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 TestDirectQueue() {
return new Queue("TestDirectQueue",true);
}
@Bean
public DirectExchange TestDirectExchange() {
return new DirectExchange("TestDirectExchange",true,false);
}
@Bean
public Binding bindingDirect() {
return BindingBuilder.bind(TestDirectQueue()).to(TestDirectExchange()).with("TestDirectRouting");
}
}
四,发送服务类配置
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.ReturnedMessage;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.UUID;
@Service
public class SendMessageService implements RabbitTemplate.ConfirmCallback,RabbitTemplate.ReturnsCallback {
private static Logger logger = LoggerFactory.getLogger(SendMessageService.class);
@Autowired
public RabbitTemplate rabbitTemplate;
public void sendMessage(String str){
rabbitTemplate.setMandatory(true);
rabbitTemplate.setReturnsCallback(this);
rabbitTemplate.setConfirmCallback(this);
rabbitTemplate.convertAndSend("TestDirectExchange","TestDirectRouting", str,new CorrelationData(UUID.randomUUID().toString()));
}
@Override
public void returnedMessage(ReturnedMessage returnedMessage) {
System.out.println("sender return success" + returnedMessage.toString());
}
@Override
public void confirm(CorrelationData correlationData, boolean b, String s) {
logger.info("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
if (!b) {
logger.error("消息发送异常!");
} else {
logger.info("发送者已经收到确认,correlationData={} ,ack={}, cause={}", correlationData.getId(), b, s);
}
}
}
五,接收消息服务类配置
import com.rabbitmq.client.Channel;
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 java.io.IOException;
@Component
@RabbitListener(queues = "TestDirectQueue")
public class DirectReceiver {
@RabbitHandler
public void process(String testMessage, Channel channel, Message message) {
System.out.println("DirectReceiver消费者收到消息 : " + testMessage.toString());
ackMethod(channel,message);
}
public void ackMethod(Channel channel, Message message){
try {
//告诉服务器收到这条消息 已经被我消费了 可以在队列删掉 这样以后就不会再发了 否则消息服务器以为这条消息没处理掉 后续还会在发
channel.basicAck(message.getMessageProperties().getDeliveryTag(),true);
System.out.println("receiver success");
} catch (IOException e) {
e.printStackTrace();
//丢弃这条消息
//channel.basicNack(message.getMessageProperties().getDeliveryTag(), false,false);
System.out.println("receiver fail");
}
}
}
六,controller测试代码
@GetMapping(value = "/sendMessage")
public String sendMessage(){
sendMessageService.sendMessage("TestDirectExchange");
return "ok";
}
七,测试结果