RpcClient,RpcServer同步发送接收消息
Channel.basicPublish,Channel.basicGet异步发送接收消息
本例是一个简单的同步发送消息实例
1,发送端
public class Publish {
private static Connection connection;
static {
ConnectionParameters params = new ConnectionParameters();
ConnectionFactory factory = new ConnectionFactory(params);
try {
connection = factory.newConnection("localhost", AMQP.PROTOCOL.PORT);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
try {
Channel channel = connection.createChannel();
RpcClient rpc = new RpcClient(channel, "exchangeName", "routingKey");
byte[] primitiveCall = rpc.primitiveCall("hello world".getBytes());
System.out.println(new String(primitiveCall));
primitiveCall = rpc.primitiveCall("hello world2".getBytes());
System.out.println(new String(primitiveCall));
rpc = new RpcClient(channel, "exchangeName", "routingKey2");
primitiveCall = rpc.primitiveCall("hello world2".getBytes());
System.out.println(new String(primitiveCall));
System.out.println("publish success.");
} catch (Exception e) {
e.printStackTrace();
}
}
}
2,接收端
public class Receive {
private static Connection connection;
static {
ConnectionParameters params = new ConnectionParameters();
ConnectionFactory factory = new ConnectionFactory(params);
try {
connection = factory.newConnection("localhost", AMQP.PROTOCOL.PORT);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
try {
Channel channel = connection.createChannel();
System.out.println(channel.toString());
channel.exchangeDeclare("exchangeName", "topic");
channel.exchangeDeclare("exchangeName2", "topic");
channel.queueDeclare("queueName");
channel.queueBind("queueName", "exchangeName", "routingKey");
channel.queueBind("queueName", "exchangeName", "routingKey2");
channel.queueBind("queueName", "exchangeName2", "routingKey2");
channel.queueBind("queueName", "exchangeName2", "routingKey");
//queue 与 exchange 是多对多的,可以把同一queue和exchange以多个不同的routing进行bind,这样就会有多个routing,而不是一个,虽然说这些rout 是绑定相同的 exchange, queue
final RpcServer rpcServer = new RpcServer(channel, "queueName") {
@Override
public byte[] handleCall(byte[] requestBody, AMQP.BasicProperties replyProperties) {
System.out.println("receive msg: " + new String(requestBody));
return "return message".getBytes();
}
};
Runnable main = new Runnable() {
@Override
public void run() {
try {
throw rpcServer.mainloop();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
};
new Thread(main).start();
System.out.println("receive success.");
} catch (IOException e) {
e.printStackTrace();
}
}
}
分享到:
相关推荐
RabbitMQ rabbitmq-server-3.6.12-1.el6.noarch 及其安装所需要的软件打包都在这里面,主要报卡一下软件:socat-1.7.3.2.tar.gz、rabbitmq-server-3.6.12-1.el6.noarch.rpm、rabbitmq-release-signing-key.asc、otp_...
rabbitmq-server-3.11.13rabbitmq-server-3.11.13rabbitmq-server-3.11.13rabbitmq-server-3.11.13rabbitmq-server-3.11.13rabbitmq-server-3.11.13rabbitmq-server-3.11.13rabbitmq-server-3.11.13rabbitmq-server-...
在Linux环境下,RabbitMQ的安装通常涉及下载其发行版的tar.gz文件,例如"rabbitmq-server-generic-unix-3.5.4.tar.gz"。这个版本是专门为Linux系统设计的。 首先,我们来详细解释一下RabbitMQ的核心概念和功能。...
rabbitmq-server-3.9.11.exe
1. **下载与解压**:首先,从官方网站或者提供的链接下载`rabbitmq-server-mac-standalone-3.5.3.tar.gz`,然后使用`tar -zxvf rabbitmq-server-mac-standalone-3.5.3.tar.gz`命令将其解压缩。 2. **环境配置**:...
rabbitmq-server-3.10.5-1.el8.noarch.rpm
在您提供的资源中,“rabbitmq-server-generic-unix-3.5.7.tar.rar”是一个针对Linux平台的RabbitMQ服务器的离线安装包。这个版本为3.5.7,您需要在Windows环境下解压后再用于Linux系统。下面将详细介绍RabbitMQ的...
最新版linux rabbitmq-server-generic-unix-3.8.14.tar.xz最新版linux rabbitmq-server-generic-unix-3.8.14.tar.xz
最新版linux rabbitmq-server-generic-unix-3.8.5.tar.xz
rabbitmq-server-3.8.8-1.el7.noarch
标题中的“rabbitmq-server-3.6.5-1.noarch.rar”指的是RabbitMQ服务器的一个特定版本,即3.6.5版本。RabbitMQ是一个开源的消息队列系统,基于AMQP(Advanced Message Queuing Protocol)协议,用于在分布式系统中...
通过命令 brew install rabbitmq 安装时下载 rabbitmq-server-generic-unix-3.8.3.tar.xz 失败?当前文件可通过放入 ~/Library/Caches/Homebrew/downloads 目录下,再次运行命令即可安装成功!
这个压缩包“rabbitmq-server-mac-standalone-3.5.7.tar.gz”是专为MAC OS X操作系统设计的RabbitMQ服务器版本,支持10.6.4或更高版本。在本文中,我们将深入探讨RabbitMQ的核心概念、安装过程以及如何在MAC OS X上...
最新版windows rabbitmq-server-3.9.5.exe最新版windows rabbitmq-server-3.9.5.exe
最新版linux rabbitmq-server-generic-unix-3.9.1.tar.xz最新版linux rabbitmq-server-generic-unix-3.9.1.tar.xz
2. 安装完成后,下载并运行"rabbitmq-server-3.8.3.exe",安装RabbitMQ服务器。 3. 安装RabbitMQ时,它会自动配置Erlang环境。 4. 安装完毕后,启动RabbitMQ服务,可以通过Web管理界面或者命令行工具进行管理和监控...
最新版windows rabbitmq-server-3.8.14.exe最新版windows rabbitmq-server-3.8.14.exe
在编译过程中,可能需要确保系统已安装必要的依赖,例如`libssl-dev`和`libcurl4-openssl-dev`,这些是`rabbitmq-c`所需的SSL和Curl库。 `rabbitmq-c`库提供了以下关键功能: - 连接管理:连接到RabbitMQ服务器,...
下载好的包,因为下载太慢 rabbitmq-server-generic-unix-3.7.8.tar.xz
RabbitMQ 3.7.7是该软件的一个稳定版本,而"rabbitmq-server-3.7.7.exe"就是这个版本的安装程序,适用于Windows平台。 在安装RabbitMQ之前,需要先安装其依赖环境——Erlang。Erlang是一种专门设计用于大规模并发...