Binding
- Exchange和Exchange、Queue之间的连接关系。
Queue
- 消息队列,事件存储消息
- Durability 是否持久化,Durable是,Transient否
- Auto Delete 最后一个监听被移除之后,该Queue是否自动被删除,yes是
Message
- 服务器和应用程序之间传送的数据
- 本质上就是一段数据,由Properties和Payload组成
- Deliver Mode、Headers、Content_type、Content_encoding、priority等
public class Producer {
public static void main(String[] args) throws IOException, TimeoutException {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("192.168.6.99");
factory.setPort(5672);
factory.setVirtualHost("/");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
Map<String, Object> map = new HashMap<>();
map.put("a","b");
AMQP.BasicProperties properties = new AMQP.BasicProperties.Builder().deliveryMode(2).contentEncoding("utf-8").expiration("1000").headers(map).build();
channel.basicPublish("","test",properties,"hello world".getBytes());
channel.close();
connection.close();
}
}
public class Consumer {
public static void main(String[] args) throws IOException, TimeoutException, InterruptedException {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("192.168.6.99");
factory.setPort(5672);
factory.setVirtualHost("/");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
channel.queueDeclare("test", true, false, false, null);
QueueingConsumer queueingConsumer = new QueueingConsumer(channel);
channel.basicConsume("test",true,queueingConsumer);
while (true){
QueueingConsumer.Delivery delivery = queueingConsumer.nextDelivery();
AMQP.BasicProperties properties = delivery.getProperties();
Map<String, Object> headers = properties.getHeaders();
System.out.println(headers);
String message = new String(delivery.getBody(), "UTF-8");
System.out.println(" Received '" + message + "'");
}
}
}
Virtual host虚拟主机
- 虚拟地址,用于进行逻辑隔离,最上层的消息路由
- 一个Virtual host里面可以有多个Exchange和Queue,但不能有同名的Exchange或Queue
网友评论