RabbitMQ 使用样例
package cn.com.kpcq.xunjian.utils; import java.io.IOException; import java.util.Date; import java.util.concurrent.TimeoutException; import com.google.gson.JsonObject; import com.rabbitmq.client.AMQP; 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 Send { private final static String QUEUE_NAME = "QueueTest5"; private static Connection connection=null; public static Connection GetRabbitConnection() { if (connection == null) { ConnectionFactory factory = new ConnectionFactory(); factory.setUsername("whkp"); factory.setPassword("123456"); factory.setHost("127.0.0.1"); factory.setPort(5672); Connection conn = null; try { connection = factory.newConnection(); } catch (Exception e) { e.printStackTrace(); } } return connection; } public static void main(String[] args) throws InterruptedException { Consumer(); System.err.println("消费者开始监听"); Thread.sleep(5000); long start = new Date().getTime(); for(int i=0;i<10;i++){ Publisher(); Thread.sleep(100); } long end = new Date().getTime(); try { connection.close(); } catch (IOException e) { e.printStackTrace(); } } public static void Publisher() { // 创建一个连接 Connection conn = GetRabbitConnection(); if (conn != null) { try { // 创建通道 Channel channel = conn.createChannel(); //声明交换机 channel.exchangeDeclare("myExchange", "fanout"); // fanout 广播模式 ,将消息发送给所有与交换机绑定了的队列 //声明队列 【参数说明:参数一:队列名称,参数二:是否持久化;参数三:是否独占模式;参数四:消费者断开连接时是否删除队列;参数五:消息其他参数】 channel.queueDeclare("myQueueT", false, false, false, null); //队列必须绑定交换机 channel.queueBind("myQueueT", "myExchange", ""); JsonObject object=new JsonObject(); object.addProperty("date", String.format("当前时间:%s", new Date().getTime())); object.addProperty("name", "zhans"); object.addProperty("desc", "闪电发货圣诞节"); // 发送内容【参数说明:参数一:交换机名称;参数二:队列名称,参数三:消息的其他属性-routing headers,此属性为MessageProperties.PERSISTENT_TEXT_PLAIN用于设置纯文本消息存储到硬盘;参数四:消息主体】
//RabbitMQ消息传递模型的核心思想是,【生产者】不直接发送任何信息到队列。事实上,【生产者】根本就不知道消息是否会被传送到任何队列。 channel.basicPublish("myExchange", "", null, object.toString().getBytes("UTF-8")); //将消息直接发给交换机时第二个参数可以不用设置,由交换机决定消息的走向 System.out.println("已发送消息:" + object.toString()); // 关闭连接 channel.close(); //conn.close(); } catch (Exception e) { e.printStackTrace(); } } } public static void Consumer() { // 创建一个连接 Connection conn = GetRabbitConnection(); if (conn != null) { try { // 创建通道 Channel channel = conn.createChannel(); //声明交换机 channel.exchangeDeclare("myExchange", "fanout"); // fanout指分发模式(将每一条消息都发送到与交换机绑定的队列 // 声明队列 【参数说明:参数一:队列名称,参数二:是否持久化;参数三:是否独占模式;参数四:消费者断开连接时是否删除队列;参数五:消息其他参数】 channel.queueDeclare("myQueueT", false, false, false, null); // 创建订阅器, 监听队列 并接受消息 channel.basicConsume("myQueueT", false, "", new DefaultConsumer(channel) { @Override public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { String routingKey = envelope.getRoutingKey(); // 队列名称 String contentType = properties.getContentType(); // 内容类型 String content = new String(body, "utf-8"); // 消息正文 System.out.println("消息正文:" + content); channel.basicAck(envelope.getDeliveryTag(), false); // 手动确认消息【参数说明:参数一:该消息的index;参数二:是否批量应答,true批量确认小于index的消息】 } }); } catch (Exception e) { e.printStackTrace(); } } } }