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_...
在编译过程中,可能需要确保系统已安装必要的依赖,例如`libssl-dev`和`libcurl4-openssl-dev`,这些是`rabbitmq-c`所需的SSL和Curl库。 `rabbitmq-c`库提供了以下关键功能: - 连接管理:连接到RabbitMQ服务器,...
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. **环境配置**:...
rpm -ivh rabbitmq-server-3.6.5-1.noarch.rpm systemctl status rabbitmq-server systemctl start rabbitmq-server systemctl stop rabbitmq-server systemctl restart rabbitmq-server systemctl enable ...
4. **插件管理**:RabbitMQ的一些功能如Web管理界面(rabbitmq_management)默认可能未启用,可以通过命令行启用:`rabbitmq-plugins enable rabbitmq_management`。 5. **验证安装**:访问`...
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
这里提供了rabbitmq-server-3.7.3.exe百度网盘下载,官网下载实在是太慢了,亲测有效! rabbitmq-server-3.7.3.exe rabbitmq-server-3.7.3.exe rabbitmq-server-3.7.3.exe
在Windows环境下,RabbitMQ的安装通常通过下载对应的zip文件进行,例如"rabbitmq-server-windows-3.6.10.zip"。 这个压缩包文件包含的是RabbitMQ服务器的Windows版本,版本号为3.6.10。解压后,你可以找到一个名为...
最新版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)协议,用于在分布式系统中...
RabbitMQ源码 rabbitmq-server-generic-unix-3.8.8.tar.xz
这个压缩包“rabbitmq-server-mac-standalone-3.5.7.tar.gz”是专为MAC OS X操作系统设计的RabbitMQ服务器版本,支持10.6.4或更高版本。在本文中,我们将深入探讨RabbitMQ的核心概念、安装过程以及如何在MAC OS X上...
通过命令 brew install rabbitmq 安装时下载 rabbitmq-server-generic-unix-3.8.3.tar.xz 失败?当前文件可通过放入 ~/Library/Caches/Homebrew/downloads 目录下,再次运行命令即可安装成功!
最新版linux rabbitmq-server-generic-unix-3.9.1.tar.xz最新版linux rabbitmq-server-generic-unix-3.9.1.tar.xz