阿神老师为什么我的QueueingConsumer不能import
来源:2-11 生产者消费者模型构建-2

昊南同学
2018-10-11
写回答
4回答
-
杰克不接客
2019-05-19
老师的方案已经有点过时了,这个课程的讨论去很不活跃,老师回复的也很不及时。我也遇到同样的问题,最新的解决方案如下,希望能给其他兄弟提供帮助: package com.rabbitmq.demo.quickstart; import java.io.IOException; import java.util.concurrent.TimeoutException; import com.rabbitmq.client.AMQP; import com.rabbitmq.client.Channel; import com.rabbitmq.client.Connection; import com.rabbitmq.client.ConnectionFactory; import com.rabbitmq.client.Consumer; import com.rabbitmq.client.DefaultConsumer; import com.rabbitmq.client.Envelope; public class MsgConsumer { private final static String QUEUE_NAME = "test001"; public static void main(String[] args) throws IOException, TimeoutException { // step01: create a new connection factory and configure the connection ConnectionFactory connectionFactory = new ConnectionFactory(); connectionFactory.setHost("127.0.0.1"); connectionFactory.setUsername("guest"); connectionFactory.setPassword("guest"); connectionFactory.setPort(5672); connectionFactory.setVirtualHost("/"); Connection connection = null; Channel channel = null; try { // Step02: Create connection with connection factory connection = connectionFactory.newConnection(); // Step03: create a channel with connection channel = connection.createChannel(); // Step04: declare(create) one queue, the queue we are listening to channel.queueDeclare(QUEUE_NAME, false, false, false, null); // Step05: Create a consumer - where there is a message arrived, // the handleDelivery method will be called System.out.println("We consumer are waiting your messages"); Consumer consumer = new DefaultConsumer(channel){ @Override public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { String message = new String(body, "UTF-8"); System.out.println("Customer Received '" + message + "'"); } }; // Step06: let the consumer consume the queue channel.basicConsume(QUEUE_NAME, true, consumer); } catch (Exception e) { System.out.println("Opps, there is something wrong!"); e.printStackTrace(); } finally { System.out.println("Connection was closed!"); // channel.close(); // connection.close(); } } }
20 -
幕布斯2529510
2019-03-17
QueueingConsummer 在4.X版本的时候被Deprecated,你可以查看下你pom文件引入的版本在3.x版本里client包里是有QueueingConsummer.cless这个类的,但是4.x版本里就没有这个类了。我看了篇博文上面说QueueingConsumer容易造成内存溢出,所以在4.x版本以后就推荐使用DefaultConsumer。
012019-05-19 -
阿神
2018-10-11
看一下引入的jar是否和我一致,版本
052018-10-12 -
阿神
2018-10-11
应该是jar没引进来
032019-05-19
相似问题