RabbitMQ整合Spring Booot【点对点模式】

pom.xml:

<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>

创建连接的工具类:

//没有做成单例的  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类:

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:

 

 

Consumer跟 Producer基本类似:

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();
        }
}

运行后的结果:

 

posted @ 2019-09-01 12:42  toov5  阅读(482)  评论(0编辑  收藏  举报