pom:html
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.toov5.rabibitMQScribe</groupId> <artifactId>rabibitMQScribe</artifactId> <version>0.0.1-SNAPSHOT</version> <dependencies> <dependency> <groupId>com.rabbitmq</groupId> <artifactId>amqp-client</artifactId> <version>3.6.5</version> </dependency> </dependencies> </project>
鏈接工具類:java
package com.toov5.utils; import java.io.IOException; import java.util.concurrent.TimeoutException; import com.rabbitmq.client.Connection; import com.rabbitmq.client.ConnectionFactory; //沒有作成單例的 VirtualHost 須要複用 public class MQConnectionUtils { //建立新的鏈接 public static Connection newConnection() throws IOException, TimeoutException { //建立鏈接工廠 ConnectionFactory factory= new ConnectionFactory(); //連接地址 factory.setHost("192.168.91.6"); //用戶名稱 factory.setUsername("admin"); //用戶密碼 factory.setPassword("admin"); //amqp端口號 factory.setPort(5672); //鏈接virtualhost factory.setVirtualHost("/admin_toov5"); Connection connection = factory.newConnection(); return connection; } }
生產者:apache
package com.toov5.fanout; import java.io.IOException; import java.util.concurrent.TimeoutException; import com.rabbitmq.client.Channel; import com.rabbitmq.client.Connection; import com.toov5.utils.MQConnectionUtils; //生產者 交換機類型 producerFanout類型 public class FanoutProducer { //交換機名稱 private static final String EXCHANGE_NAME = "my_fanout"; public static void main(String[] args) throws IOException, TimeoutException { //創建MQ鏈接 Connection connection = MQConnectionUtils.newConnection(); //建立通道 Channel channel = connection.createChannel(); //生產者綁定交換機 channel.exchangeDeclare(EXCHANGE_NAME, "fanout"); //交換機名稱 交換機類型 //建立對應的消息 String msString = "my_fanout_destination_msg"; //經過頻道 發送消息 System.out.println("生產者投遞消息:"+msString); //消息投遞到交換機裏面去 channel.basicPublish(EXCHANGE_NAME, "", null, msString.getBytes()); //關閉通道 和 鏈接 channel.close(); connection.close(); } }
消費者maven
package com.toov5.fanout; import java.io.IOException; import java.util.concurrent.TimeoutException; import com.rabbitmq.client.Channel; import com.rabbitmq.client.Connection; import com.rabbitmq.client.DefaultConsumer; import com.rabbitmq.client.Envelope; import com.rabbitmq.client.AMQP.BasicProperties; import com.toov5.utils.MQConnectionUtils; //郵件消費者 public class ConsumerEmailFanout { private static final String EMAIL_QUEUE ="email_queue_fanout"; //交換機名稱 private static final String EXCHANGE_NAME = "my_fanout"; public static void main(String[] args) throws IOException, TimeoutException { System.out.println("郵件消費者啓動"); //創建MQ鏈接 Connection connection = MQConnectionUtils.newConnection(); //建立通道 Channel channel = connection.createChannel(); //消費者聲明隊列 channel.queueDeclare(EMAIL_QUEUE, false, false, false, null); //消費者隊列綁定交換機 channel.queueBind(EMAIL_QUEUE, EXCHANGE_NAME, ""); //消費者監聽消息 DefaultConsumer defaultConsumer = new DefaultConsumer(channel) { //重寫監聽方法 @Override public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties, byte[] body) throws IOException { String msg = new String(body,"UTF-8"); System.out.println("郵件消費者獲取生產者消息"+msg); } }; channel.basicConsume(EMAIL_QUEUE,true, defaultConsumer); //綁定隊列 事件監聽 } }
package com.toov5.fanout; import java.io.IOException; import java.util.concurrent.TimeoutException; import com.rabbitmq.client.Channel; import com.rabbitmq.client.Connection; import com.rabbitmq.client.DefaultConsumer; import com.rabbitmq.client.Envelope; import com.rabbitmq.client.AMQP.BasicProperties; import com.toov5.utils.MQConnectionUtils; //郵件消費者 public class ConsumerSMSFanout { private static final String SMS_QUEUE ="sms_queue_fanout"; //交換機名稱 private static final String EXCHANGE_NAME = "my_fanout"; public static void main(String[] args) throws IOException, TimeoutException { System.out.println("短信消費者啓動"); //創建MQ鏈接 Connection connection = MQConnectionUtils.newConnection(); //建立通道 Channel channel = connection.createChannel(); //消費者聲明隊列 channel.queueDeclare(SMS_QUEUE, false, false, false, null); //消費者隊列綁定交換機 channel.queueBind(SMS_QUEUE, EXCHANGE_NAME, ""); //消費者監聽消息 DefaultConsumer defaultConsumer = new DefaultConsumer(channel) { //重寫監聽方法 @Override public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties, byte[] body) throws IOException { String msg = new String(body,"UTF-8"); System.out.println("郵件消費者獲取生產者消息"+msg); } }; channel.basicConsume(SMS_QUEUE,true, defaultConsumer); //綁定隊列 事件監聽 } }