RabbitMQ整合Spring Booot【點對點模式】

pom.xml:apache

<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</groupId>
  <artifactId>rabbitmq</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>

建立鏈接的工具類:maven

//沒有作成單例的  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;
    } 
}

Producer類:ide

public class Producer {
    //隊列名稱
    private static final String UEUE_NAME = "test_queue";
    
    public static void main(String[] args) throws IOException, TimeoutException {
        //建立新的鏈接
    Connection connection = MQConnectionUtils.newConnection();
       //建立Channel
        Channel channel = connection.createChannel();
        //建立隊列
        channel.queueDeclare(UEUE_NAME, false, false, false, null);
        //建立message
        String msg = "toov5_message";
        System.out.println("生產者投遞消息"+msg);
        //生產者發送消息
        channel.basicPublish("",UEUE_NAME, null, msg.getBytes());
        //關閉通道和鏈接
         channel.close();
         connection.close();
    }
}

運行結果,看下這個隊列:工具

 

 模擬Get message:spa

 

 

Consumer跟 Producer基本相似:code

public class Consumer {
  
        //隊列名稱
        private static final String QUEUE_NAME = "test_queue";    
        public static void main(String[] args) throws IOException, TimeoutException {
            System.out.println("消費者啓動..........");
            //建立新的鏈接
        Connection connection = MQConnectionUtils.newConnection();
           //建立Channel
            Channel channel = connection.createChannel();
            // 消費者關聯隊列
            channel.queueDeclare(QUEUE_NAME, false, false, false, null);
            
              DefaultConsumer defaultConsumerr = 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);
                    }
              };
            //牽手模式設置  默認自動應答模式  true:自動應答模式  
              channel.basicConsume(QUEUE_NAME, true, defaultConsumerr);              
              
//            //關閉通道和鏈接
//             channel.close();
//             connection.close();
        }
}

運行後的結果:xml

相關文章
相關標籤/搜索