消费者2:
public class Consumer2 { public static final String EXCHANGE_NAME="byte004"; public static void main(String[] args) throws Exception { // 1. 创建出链接工厂 ConnectionFactory factory = new ConnectionFactory(); factory.setHost("127.0.0.1"); factory.setPort(5672); factory.setVirtualHost("http://www.likecs.com/"); // 2. 通过链接工厂创建链接对象 Connection connection = factory.newConnection(); // 3. 通过链接对象创建出channel Channel channel = connection.createChannel(); String queueName = "queue006"; channel.queueDeclare(queueName,false,false,false,null); // 绑定队列到交换机 channel.queueBind(queueName,EXCHANGE_NAME,"key.#"); channel.basicQos(1); DefaultConsumer consumer = new DefaultConsumer(channel) { /** * consumerTag 用来标识.可以再监听队列时候设置 * envelope 信封,通过envelope可以通过这个获取到很多东西 * properties 额外的消息属性 * body:消息体 */ @Override public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { String s = new String(body, "UTF-8"); System.out.println("消费者2:"+s); channel.basicAck(envelope.getDeliveryTag(),false); } }; channel.basicConsume(queueName,false,consumer); } }代码已上传:
github地址: https://github.com/binzh303/zhixie-code-example
gitee地址: https://gitee.com/javaXiaoCaiJi/zhixie-code-example
> 如果文章对您有帮助,请记得点赞关注哟~ > 欢迎大家关注我的公众号:字节传说,每日推送技术文章供大家学习参考。