RabbitMQ(Docker 独立部署)
最编程
2024-05-05 14:55:40
...
public void static main() throws IOException, TimeoutException {
// 配置连接
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("xxx.xxx.xxx.xxx");
factory.setPort(5672);
factory.setVirtualHost("/");
factory.setUsername("rabbit");
factory.setPassword("rabbit");
// 获取连接对象
Connection connection = factory.newConnection();
// 获取频道
Channel channel = connection.createChannel();
// 创建队列
String queueName = "simple.queue";
channel.queueDeclare(queueName, false, false, false, null);
// 订阅消息
channel.basicConsume(queueName, true, new DefaultConsumer(channel) {
@Override
public void handleDelivery(String consumerTag,
Envelope envelope,
AMQP.BasicProperties properties,
byte[] body) throws IOException {
String msg = new String(body);
System.out.println(msg);
}
});
}