// 创建连接工厂
ConnectionFactory factory = new ConnectionFactory();
factory.setHost(host);
factory.setPort(port);
factory.setUsername(username);
factory.setPassword(password);
try (Connection connection = factory.newConnection();
Channel channel = connection.createChannel()) {
// type ("topic","direct" 等)
String type = "direct";
// 声明一个交换机
channel.exchangeDeclare(exchangeName, type, true);
// 声明一个队列
channel.queueDeclare(queueName, true, false, false, null);
// 将队列绑定到交换机
channel.queueBind(queueName, exchangeName, routingKey);
// 发送消息到交换机
channel.basicPublish(exchangeName, routingKey, null, message.getBytes());
} catch (IOException e) {
e.printStackTrace();
} catch (TimeoutException e) {
e.printStackTrace();
}
01-05
1175
11-10
5377
05-27
6173
03-07
1603
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交