RabbitMQ简单队列

自动应答:  均摊

手动应答:     能者多劳


获取一个连接

package com.haitao.rabbit;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

public class MQConnectionUtils {

	// 创建新的MQ连接
	public static Connection newConnection() throws IOException, TimeoutException {
		
		// 创建连接工厂
		ConnectionFactory factory = new ConnectionFactory();
		
		
		// 设置连接地址
		factory.setHost("127.0.0.1");
		// 设置amqp协议端口号
		factory.setPort(5672);
		
		
		// 设置用户名称
		factory.setUsername("xuhaitao");
		// 设置用户密码
		factory.setPassword("123456");

		
		// 设置VirtualHost地址
		factory.setVirtualHost("/my");
		Connection connection = factory.newConnection();
		return connection;
	}
}

生产者:

package com.haitao.rabbit;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;

// 简单队列生产者
public class Producer {

	// 队列名称
	private static final String QUEUE_NAME = "queue01";

	public static void main(String[] args) throws IOException, TimeoutException {

		// 1.创建一个新的连接
		Connection connection = MQConnectionUtils.newConnection();
		// 2.创建通道
		Channel channel = connection.createChannel();
		// 3.创建一个队列
		channel.queueDeclare(QUEUE_NAME, false, false, false, null);
		channel.basicQos(1);
		for (int i = 1; i <= 50; i++) {
			// 4.创建msg
			String msg = "msg_" + i;
			// System.out.println("生产者投递消息内容:" + msg);
			// 5.生产者发送消息者
			channel.basicPublish("", QUEUE_NAME, null, msg.getBytes());
		}

		// 关闭通道和连接
		channel.close();
		connection.close();
	}

}

消费者(自动应答):

package com.haitao.rabbit;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;
import com.rabbitmq.client.AMQP.BasicProperties;

public class Consumer1 {
	// 队列名称
	private static final String QUEUE_NAME = "queue01";

	public static void main(String[] args) throws IOException, TimeoutException {
		System.out.println("消费者启动....01");
		// 1.创建一个新的连接
		Connection connection = MQConnectionUtils.newConnection();
		// 2.创建通道
		final Channel channel = connection.createChannel();
		// 3.消费者关联队列
		channel.queueDeclare(QUEUE_NAME, false, false, false, null);
		channel.basicQos(1);
		DefaultConsumer defaultConsumer = new DefaultConsumer(channel) {

			// 监听获取消息
			@Override
			public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties, byte[] body)
					throws IOException {
				String msg = new String(body, "UTF-8");
				System.out.println("消费者获取生产者消息:" + msg);
			}
		};
		// 设置应答模式 如果为true情况下 表示为自动应答模式
		channel.basicConsume(QUEUE_NAME, true, defaultConsumer);
		// // 关闭通道和连接
		// channel.close();
		// connection.close();
	}
}

消费者(主动应答):

package com.haitao.rabbit;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;
import com.rabbitmq.client.AMQP.BasicProperties;

public class Consumer2 {
	// 队列名称
	private static final String QUEUE_NAME = "queue01";

	public static void main(String[] args) throws IOException, TimeoutException {
		System.out.println("消费者启动....02");
		// 创建一个新的连接
		Connection connection = MQConnectionUtils.newConnection();
		// 创建通道
		final Channel channel = connection.createChannel();
		// 消费者关联队列
		channel.queueDeclare(QUEUE_NAME, false, false, false, null);
		channel.basicQos(1);
		DefaultConsumer defaultConsumer = new DefaultConsumer(channel) {

			// 监听获取消息
			@Override
			public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties, byte[] body)
					throws IOException {
				String msg = new String(body, "UTF-8");
				System.out.println("消费者获取生产者消息:" + msg);
				try {
					// 模拟应答等待时间
					Thread.sleep(500);
				} catch (Exception e) {
					// TODO: handle exception
				} finally {
					// 手动告诉给队列服务器,已经处理成功
					 channel.basicAck(envelope.getDeliveryTag(), false);
				}
			}
		};
		// 设置应答模式 如果为false表示为手动应答
		channel.basicConsume(QUEUE_NAME, false, defaultConsumer);
		// // 关闭通道和连接
		// channel.close();
		// connection.close();
	}
}

 

FR:徐海涛(hunk Xu)
QQ技术交流群:386476712

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值