20.2. direct

简介:

Send.java

package cn.netkiller;

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

public class DirectPush {

	private static final String EXCHANGE_NAME = "cn.netkiller";

	public static void main(String[] args) throws Exception {
		try {
			ConnectionFactory factory = new ConnectionFactory();
			factory.setHost("192.168.6.1");
			Connection connection = factory.newConnection();
			Channel channel = connection.createChannel();
			channel.exchangeDeclare(EXCHANGE_NAME, "direct");

			String routingKey = "demo";
			String message = "Hello";

			channel.basicPublish(EXCHANGE_NAME, routingKey, null, message.getBytes());
			System.out.println(" [x] Sent '" + routingKey + "':'" + message + "'");

			channel.close();
			connection.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}

		

Recv.java

package cn.netkiller;

import com.rabbitmq.client.*;

import java.io.IOException;

public class DirectReceive {

	private static final String EXCHANGE_NAME = "cn.netkiller";
	//private final static String QUEUE_NAME = "customer";

	public static void main(String[] args) {
		try {
			ConnectionFactory factory = new ConnectionFactory();
			factory.setHost("192.168.6.1");
			Connection connection = factory.newConnection();
			Channel channel = connection.createChannel();
			channel.exchangeDeclare(EXCHANGE_NAME, "direct");

			String queueName = channel.queueDeclare().getQueue();
			System.out.println(queueName);
			channel.queueBind(queueName, EXCHANGE_NAME, "demo");
			channel.queueBind(queueName, EXCHANGE_NAME, "real");

			System.out.println(" [*] Waiting for messages. To exit press CTRL+C");

			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(" [x] Received '" + envelope.getRoutingKey() + "':'" + message + "'");
				}
			};
			channel.basicConsume(queueName, true, consumer);

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
		




原文出处:Netkiller 系列 手札
本文作者:陈景峯
转载请与作者联系,同时请务必标明文章原始出处和作者信息及本声明。

相关实践学习
消息队列RocketMQ版:基础消息收发功能体验
本实验场景介绍消息队列RocketMQ版的基础消息收发功能,涵盖实例创建、Topic、Group资源创建以及消息收发体验等基础功能模块。
消息队列 MNS 入门课程
1、消息队列MNS简介 本节课介绍消息队列的MNS的基础概念 2、消息队列MNS特性 本节课介绍消息队列的MNS的主要特性 3、MNS的最佳实践及场景应用 本节课介绍消息队列的MNS的最佳实践及场景应用案例 4、手把手系列:消息队列MNS实操讲 本节课介绍消息队列的MNS的实际操作演示 5、动手实验:基于MNS,0基础轻松构建 Web Client 本节课带您一起基于MNS,0基础轻松构建 Web Client
目录
相关文章
|
7月前
|
Java
Direct buffer OutOfMemoryError
Direct buffer OutOfMemoryError
42 0
|
7月前
|
消息中间件
PRECONDITION_FAILED - inequivalent arg ‘type‘ for exchange ‘exchange‘ in vhost ‘/‘: received ‘direct
PRECONDITION_FAILED - inequivalent arg ‘type‘ for exchange ‘exchange‘ in vhost ‘/‘: received ‘direct
63 0
Direct exchange
我们构建了一个简单的日志记录系统。我们能够向许多接收者广播日志消息。在本节我们将向其中添加一些特别的功能-比方说我们只让某个消费者订阅发布的部分消息。例如我们只把严重错误消息定向存储到日志文件(以节省磁盘空间),同时仍然能够在控制台上打印所有日志消息。
Direct exchange
|
索引
Atomics.exchange()
Atomics.exchange()
50 0
|
关系型数据库 Oracle iOS开发