RabbitMq 创建消费者
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了RabbitMq 创建消费者相关的知识,希望对你有一定的参考价值。
参考技术A import org.springframework.amqp.rabbit.connection.Connection;import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import com.rabbitmq.client.*;
@Autowired
private RabbitTemplate rabbitTemplate;
ConnectionFactory connectionFactory =rabbitTemplate.getConnectionFactory();
Connection connection1 = connectionFactory.createConnection();
Channel channel1 = connection1.createChannel(false);
channel1.queueDeclare(RabbitConfig.QUEUE_A,false,false,false,null);
Consumer consumer =new DefaultConsumer(channel1)
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,byte[] body)
String message =new String(body);
System.out.println("Received: " + message);
// 消息确认
try
channel1.basicAck(envelope.getDeliveryTag(),false);
catch (IOException e)
e.printStackTrace();
;
// 关闭自动消息确认,autoAck = false
channel1.basicConsume(RabbitConfig.QUEUE_A,false, consumer);
以上是关于RabbitMq 创建消费者的主要内容,如果未能解决你的问题,请参考以下文章