RabbitMQ 教程

PushMessage4.java

import com.rabbitmq.client.*;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * 验证通过信道 Channel 的 basicPublish() 方法发送消息,发送一条带有 headers 的消息
 * @author hxstrive.com
 * @date 2022年2月17日13:59:29
 */
public class PushMessage4 {
   private static final String EXCHANGE_NAME = "exchange_" +
         PushMessage4.class.getSimpleName();

   /**
    * 发送消息
    */
   private void sender() {
      Connection connection = null;
      try {
         // 创建连接
         ConnectionFactory factory = new ConnectionFactory();
         factory.setHost("127.0.0.1");
         factory.setPort(5672);
         connection = factory.newConnection();

         // 创建通道
         Channel channel = connection.createChannel();
         channel.exchangeDeclare(EXCHANGE_NAME, "topic");

         // 发送消息
         System.out.println("[Send] Sending Message...");
         byte[] msg = "hello wrold".getBytes();
         Map<String, Object> headers = new HashMap<String, Object>();
         headers.put("location", "here");
         headers.put("time", "today");
         channel.basicPublish(EXCHANGE_NAME, "www.hxstrive.com",
               new AMQP.BasicProperties.Builder()
                     .headers(headers)
                     .build(), msg);

      } catch(Exception e) {
         e.printStackTrace();
      } finally {
         if ( connection != null ) {
            try {
               connection.close();
            } catch (IOException e) {
               e.printStackTrace();
            }
         }
      }
   }

   /**
     * 消费消息
    */
   private void consumer() {
      try {
         // 创建连接
         ConnectionFactory factory = new ConnectionFactory();
         factory.setHost("127.0.0.1");
         factory.setPort(5672);
         Connection connection = factory.newConnection();

         // 创建通道
         Channel channel = connection.createChannel();
         channel.exchangeDeclare(EXCHANGE_NAME, "topic");

         // 绑定exchange与queue
         String queueName = channel.queueDeclare().getQueue();
         channel.queueBind(queueName, EXCHANGE_NAME, "*.hxstrive.com");
         System.out.println("[Receive] Waiting Message...");

         // 消费消息
         channel.basicConsume(queueName, true, new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope,
                  AMQP.BasicProperties properties, byte[] body) throws IOException {
               // 输出收到的消息和消息头
               System.out.println("[Receive] Receive Message :: " + new String(body));
               Map<String,Object> headers = properties.getHeaders();
               System.out.println("[Receive] Headers location = " +
                     headers.get("location"));
               System.out.println("[Receive] Headers time = " +
                     headers.get("time"));
            }
         });
      } catch(Exception e) {
         e.printStackTrace();
      }
   }

   public static void main(String[] args) {
      PushMessage4 demo = new PushMessage4();
      demo.consumer();
      demo.sender();
   }

}
说说我的看法
全部评论(
没有评论
关于
本网站属于个人的非赢利性网站,转载的文章遵循原作者的版权声明,如果原文没有版权声明,请来信告知:hxstrive@outlook.com
公众号