阿神老师为什么我的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();
		}
	}
}


2
0

幕布斯2529510

2019-03-17

QueueingConsummer 在4.X版本的时候被Deprecated,你可以查看下你pom文件引入的版本在3.x版本里client包里是有QueueingConsummer.cless这个类的,但是4.x版本里就没有这个类了。我看了篇博文上面说QueueingConsumer容易造成内存溢出,所以在4.x版本以后就推荐使用DefaultConsumer。

0
1
杰克不接客
DefaultConsumer如何获得delivery?如何读取消息呢?
2019-05-19
共1条回复

阿神

2018-10-11

看一下引入的jar是否和我一致,版本

0
5
阿神
回复
昊南同学
咱俩不是一个版本鸭,嗯
2018-10-12
共5条回复

阿神

2018-10-11

应该是jar没引进来

0
3
阿神
回复
杰克不接客
后面会更新一些知识点旧的内容,有些确实是原生API,一般使用直接springamqp,很少用原生的
2019-05-19
共3条回复

RabbitMQ精讲 从0到1驾驭RabbitMQ应用与设计

从0到1,全面深入掌握RabbitMQ消息中间件技术

1460 学习 · 443 问题

查看课程