(java)一个简单的原生的RabbitMQ的例子。

来源:互联网 发布:安卓数据恢复 编辑:程序博客网 时间:2024/06/11 03:05

生产者(发送端):

package com.centerm.ivycloud.web.share.mqueue;import com.rabbitmq.client.Channel;import com.rabbitmq.client.Connection;import com.rabbitmq.client.ConnectionFactory;public class MyFirstSender {      private static final String message = "星辰大海,生命长河";       private static String QUEUE_NAME = "hello";            public static void main(String[] args){      Channel channel = null;      Connection conn = null;      try {  //初始化连接  ConnectionFactory factory = new ConnectionFactory();  factory.setHost("localhost");  factory.setPort(5672);  factory.setUsername("guest");  factory.setPassword("guest");  factory.setConnectionTimeout(0);  //创建连接   conn = factory.newConnection();  //创建通道  channel = conn.createChannel();  //声明队列  /**   * queue: "QUEUE_NAME",                           * durable: true,                           * exclusive: false,                           * autoDelete: false,                           * arguments: null   */  channel.queueDeclare(QUEUE_NAME, false, false, true, null);  //发送消息  for(int i=1;i<6;i++){  /**   * exchange: "",                   * routingKey: "task_queue",                   * basicProperties: properties,                   * body: body   *    */  channel.basicPublish("", QUEUE_NAME, null, (message+i).getBytes());    }  System.out.println("我创建了一个队列,并发送一条消息");  } catch (Exception e) {e.printStackTrace();  } finally{try {if(channel != null){channel.close();}if(conn != null){conn.close();}} catch (Exception e) {e.printStackTrace();}  }       }}


消费者(接收端):

import java.io.IOException;import com.rabbitmq.client.AMQP.BasicProperties;import com.rabbitmq.client.Channel;import com.rabbitmq.client.Connection;import com.rabbitmq.client.ConnectionFactory;import com.rabbitmq.client.Consumer;import com.rabbitmq.client.DefaultConsumer;import com.rabbitmq.client.Envelope;public class MyFirstReceiver {    public static String QUEUE_NAME = "hello";public static Channel channel = null;public static Connection conn = null;public static void main(String[] args){  try {  //初始化连接  ConnectionFactory factory = new ConnectionFactory();  factory.setHost("localhost");  factory.setPort(5672);  factory.setUsername("guest");  factory.setPassword("guest");  //创建连接   conn = factory.newConnection();  //创建通道  channel = conn.createChannel();  //声明队列  channel.queueDeclare(QUEUE_NAME, false, false, true, null);  /**            * 设置通道同时处理的最大的消息数量,此处设置为1,那么当该消费者有未完成的任务时,而且没有其他空闲消费者            * 那么消息会堆积在队列中等待处理            */  channel.basicQos(1);  //定义消费者  Consumer consumer = new DefaultConsumer(channel){@Overridepublic void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties,byte[] body) throws IOException {String message = new String(body,"UTF-8");System.out.println(message);try {Thread.sleep(5000);//System.out.println(1/0);} catch (InterruptedException e) {e.printStackTrace();}finally{/** * 返回任务执行结果给rabbitMQ服务器, * 若任务已经处理完,则返回结果,若中途失败,则无结果返回,服务器根据有无结果是否删掉消息。 *  */channel.basicAck(envelope.getDeliveryTag(), false);}}  };    /**         * 设置自动确认为false时,rabbitmq不会自动确认消息的消费         * (在发送方未收到消费者确认之前不会删除队列中的消息)         * 以此来保证消息不丢失,即使消费者被杀死或断开连接         * 启动两个worker,发送一个耗时5秒的任务,在任务执行期间断开正在执行任务的消费者连接         * 会发现正在处理的消息被转发给另一个空闲的消费者处理了         */      boolean autoAck = false;  channel.basicConsume(QUEUE_NAME, autoAck, consumer);  System.out.println("我是第一个接受者");  } catch (Exception e) {e.printStackTrace();}          }}
简单的使用无名的交换机,发送消息。

原创粉丝点击