MQ 入门【6】--路由模式

1.连接

package com.utils;

import java.io.IOException;

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

public class ConnectionUtils {

	public static Connection getConnetions() throws IOException {

		ConnectionFactory factory = new ConnectionFactory();

		factory.setHost("127.0.0.1");
		factory.setPort(5672);
		factory.setVirtualHost("/vhost_zq");

		factory.setUsername("develop");
		factory.setPassword("123456");
		return factory.newConnection();

	}

}

2.生产者

package com.routing;

import java.io.IOException;

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

public class Send {

	/**
	 * 路由模式:                     error 
                                        |===>queue---------->消费者 
            生产者-->交换机【type=direct】
	 *                             error&info&warning 
                                        |===>queue---------->消费者
	 */
	// 定义交换机
	private static final String EXCHANGE_NAME = "test_exchange_direct";

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

		// 建立连接
		Connection connetions = ConnectionUtils.getConnetions();

		// 创建渠道
		Channel channel = connetions.createChannel();

		// 声明交换机
		channel.exchangeDeclare(EXCHANGE_NAME, "direct");

		// 定义每次消费者只能消费一条信息
		channel.basicQos(1);

		String msg = "hello zhangsan!!!";

		// 定义路由
		String routingKey = "info";

		// 发布消息
		channel.basicPublish(EXCHANGE_NAME, routingKey, null, msg.getBytes());

		System.out.println("send " + msg);

		channel.close();

		connetions.close();

	}

}

3.消费者1

package com.routing;

import java.io.IOException;

import com.utils.ConnectionUtils;
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 Rec1 {

	// 队列名称
	private static final String QUEUE_NAME = "test_queue_direct_1";
	// 交换机名称
	private static final String EXCHANGE_NAME = "test_exchange_direct";

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

		// 建立连接
		Connection connection = ConnectionUtils.getConnetions();

		Channel channel = connection.createChannel();

		// 声明队列
		channel.queueDeclare(QUEUE_NAME, false, false, false, null);
		// 声明路由
		String routingKey = "error";
		// 绑定交换机
		channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, routingKey);

		// 设置每次只消费一条消息
		channel.basicQos(1);

		// 定义消费者
		DefaultConsumer consumer = 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("[1] recieve" + msg);

				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				} finally {

					System.out.println("[1] done!!!");

					// 手动应答
					channel.basicAck(envelope.getDeliveryTag(), false);
				}

			}

		};

		// 开始消费消息 关闭自动应答
		channel.basicConsume(QUEUE_NAME, false, consumer);

	}

}

4.消费者2

package com.routing;

import java.io.IOException;

import com.utils.ConnectionUtils;
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 Rec2 {

	private static final String QUEUE_NAME = "test_queue_direct_2";
	private static final String EXCHANGE_NAME = "test_exchange_direct";

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

		Connection connection = ConnectionUtils.getConnetions();

		Channel channel = connection.createChannel();

		channel.queueDeclare(QUEUE_NAME, false, false, false, null);

		// 声明多个路由
		String routingKey1 = "error";
		String routingKey2 = "info";
		String routingKey3 = "warning";
		channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, routingKey1);
		channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, routingKey2);
		channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, routingKey3);

		channel.basicQos(1);

		DefaultConsumer consumer = 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("[2] recieve" + msg);

				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				} finally {

					System.out.println("[2] done!!!");

					channel.basicAck(envelope.getDeliveryTag(), false);
				}

			}

		};

		channel.basicConsume(QUEUE_NAME, false, consumer);

	}

}

根据路由定义走那个队列。就可以根据业务需求来定义queue

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值