生產者消費者初級案列
ChannelUtils
package com.hensemlee.rabbitmq;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import java.util.HashMap;
import java.util.Map;
public class ChannelUtils {
private static final String HOST = "192.168.1.5";
private static final int PORT = 5672;
public static Channel getChannel(String connectionDescription) {
try {
ConnectionFactory factory = getConnectionFactory();
Connection connection = factory.newConnection(connectionDescription);
return connection.createChannel();
} catch (Exception e) {
throw new RuntimeException("獲取Channel連接失敗");
}
}
private static ConnectionFactory getConnectionFactory() {
ConnectionFactory factory = new ConnectionFactory();
// 配置連接信息
factory.setHost(HOST);
factory.setPort(PORT);
factory.setUsername("root");
factory.setPassword("root");
// 網絡異常自動連接恢復
factory.setAutomaticRecoveryEnabled(true);
// 每10秒嘗試重試連接一次
factory.setNetworkRecoveryInterval(10000);
// 設置ConnectionFactory屬性信息
Map<String, Object> propertiesMap = new HashMap<>();
propertiesMap.put("principal", "hensemlee");
propertiesMap.put("description", "rabbitmq入門案例");
propertiesMap.put("emailAddress", "[email protected]");
factory.setClientProperties(propertiesMap);
return factory;
}
}
Producer
package com.hensemlee.rabbitmq;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import java.io.IOException;
public class Producer {
private static final String EXCHANGE_NAME = "hello_exchange";
private static final String ROUTINGKEY_NAME = "hello_routingkey";
public static void main(String[] args) throws IOException {
Channel channel = ChannelUtils.getChannel("rabbitmq入門案例消息生產者");
// 聲明交換機 (交換機名, 交換機類型, 是否持久化, 是否自動刪除, 是否是內部交換機, 交換機屬性);
channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.DIRECT, true, false, false, null);
// 設置消息屬性 發佈消息 (交換機名, Routing key, 可靠消息相關屬性 後續會介紹, 消息屬性, 消息體);
AMQP.BasicProperties basicProperties = new AMQP.BasicProperties().builder().deliveryMode(2).contentType("UTF-8").build();
channel.basicPublish(EXCHANGE_NAME, ROUTINGKEY_NAME, false, basicProperties, "Hello RabbitMQ".getBytes());
}
}
Consumer
package com.hensemlee.rabbitmq;
import com.rabbitmq.client.*;
import java.io.IOException;
public class Consumer {
private static final String EXCHANGE_NAME = "hello_exchange";
private static final String QUEUE_NAME = "hello_queue";
private static final String ROUTINGKEY_NAME = "hello_routingkey";
public static void main(String[] args) throws IOException {
Channel channel = ChannelUtils.getChannel("rabbitmq入門案例消息消費者");
// 聲明隊列 (隊列名, 是否持久化, 是否排他, 是否自動刪除, 隊列屬性);
AMQP.Queue.DeclareOk declareOk = channel.queueDeclare(QUEUE_NAME, true, false, false, null);
// 聲明交換機 (交換機名, 交換機類型, 是否持久化, 是否自動刪除, 是否是內部交換機, 交換機屬性);
channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.DIRECT, true, false, false, null);
// 將隊列Binding到交換機上 (隊列名, 交換機名, Routing key, 綁定屬性);
channel.queueBind(declareOk.getQueue(), EXCHANGE_NAME, ROUTINGKEY_NAME, null);
// 消費者訂閱消息 監聽如上聲明的隊列 (隊列名, 是否自動應答(與消息可靠有關 後續會介紹), 消費者標籤, 消費者)
channel.basicConsume(declareOk.getQueue(), true, "rabbitmq入門案例消費者", new DefaultConsumer(channel) {
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
System.out.println(consumerTag);
System.out.println(envelope.toString());
System.out.println(properties.toString());
System.out.println("消息內容:" + new String(body));
}
});
}
}