RabbitMQ:SpringBoot+RabbitMQ的简单实现之Direct模式+消息确认ConfirmCallback

RabbitMQ:SpringBoot+RabbitMQ的简单实现之Direct模式+消息确认ConfirmCallback


1.在pom中添加springboot对amqp的支持

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

2.在application.properties中添加RabbitMQ的简单配置信息

spring.rabbitmq.host=127.0.0.1
#5672是发送消息端口,15672是管理端的端口
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest

#消息发送到交换机确认机制,是否确认回调
#如果没有本条配置信息,当消费者收到生产者发送的消息后,生产者无法收到确认成功的回调信息
spring.rabbitmq.publisher-confirms=true

3.配置Queue(消息队列)

@Configuration
public class QueueConfig {
     @Bean
     public Queue queue() {
          return new Queue("queue_direct");
     }
}

4.编写消息生产者

@Component
public class Sender_Direct implements RabbitTemplate.ConfirmCallback {

	@Autowired
	private RabbitTemplate rabbitTemplate;
    
    public void send_callback(String routingKey,Message message,CorrelationData correlationData) {
		rabbitTemplate.setConfirmCallback(this);
		
		System.out.println("CallBackSender  UUID: " + correlationData.getId());
		
		this.rabbitTemplate.convertAndSend(routingKey , message , correlationData);
	}

	public void confirm(CorrelationData correlationData, boolean ack, String cause) {
        System.out.println("CallBackConfirm UUID: " + correlationData.getId());
        
        if(ack) {
        	System.out.println("CallBackConfirm 消息消费成功!");
        }else {
        	System.out.println("CallBackConfirm 消息消费失败!");
        }
        
        if(cause!=null) {
            System.out.println("CallBackConfirm Cause: " + cause);
        }
    }
}

5.编写消息消费者

@Component
public class Receive_Direct {

	//监听器监听指定的Queue
    @RabbitListener(queues="queue_direct")    
    public void processC(Message message) throws UnsupportedEncodingException {
    	MessageProperties messageProperties = message.getMessageProperties();
    	
    	String contentType = messageProperties.getContentType();
    	
        System.out.println("Receive-Direct:"+new String(message.getBody(), contentType));
    }

}

6.编写测试类

@RunWith(SpringRunner.class)
@SpringBootTest
public class TestRabbitMQ_Direct {
    
    @Autowired
    private Sender_Direct sender_Direct; 
    
    @Test
    public void testRabbit_Direct() {
    	/**
    	 * 声明消息 (消息体, 消息属性)
    	 */
        MessageProperties messageProperties = new MessageProperties();
	        //设置消息是否持久化。Persistent表示持久化,Non-persistent表示不持久化
	        messageProperties.setDeliveryMode(MessageDeliveryMode.NON_PERSISTENT);
	        messageProperties.setContentType("UTF-8");
        
        Message message = new Message("hello,rabbit_direct!".getBytes(), messageProperties);

		CorrelationData correlationData = new CorrelationData(UUID.randomUUID().toString());

    	sender_Direct.send_callback("queue_direct",message,correlationData);
    }
}

接下来就可以测试啦,首先启动接收端的应用,紧接着运行发送端的单元测试,接收端应用打印出来接收到的消息,测试即成功!

©️2020 CSDN 皮肤主题: Age of Ai 设计师: meimeiellie 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值