zoukankan      html  css  js  c++  java
  • rabbitmq第一弹simple

    本次利用rabbitmq作为中介,实现了简单的消息发送和消费,模式如下: 

    发送接收模式

    官方的介绍文档可以转至链接:https://www.rabbitmq.com/tutorials/tutorial-one-java.html

    常量定义:

    public interface ExampleConstants {
    
        String QUEUE_NAME = "hello";
    
    }
    

      

    发送端代码:

    package com.test.example;
    
    import com.rabbitmq.client.Channel;
    import com.rabbitmq.client.Connection;
    import com.rabbitmq.client.ConnectionFactory;
    
    import java.io.IOException;
    import java.util.concurrent.TimeoutException;
    
    /**
     * @Author 
     * @create 
     */
    public class Sender {
    
        public static void main(String[] args) {
            ConnectionFactory factory = new ConnectionFactory();
            factory.setHost("localhost");
            Connection connection = null;
            Channel channel = null;
            try{
                connection = factory.newConnection();
                channel = connection.createChannel();
                channel.queueDeclare(ExampleConstants.QUEUE_NAME,false,false,false,null);
                String message = "hello lh";
                channel.basicPublish("",ExampleConstants.QUEUE_NAME,null,message.getBytes());
                System.out.println(" [x] Sent '" + message + "'");
            }catch (Exception e){
                e.printStackTrace();
            }finally {
                if(channel != null){
                    try {
                        channel.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    } catch (TimeoutException e) {
                        e.printStackTrace();
                    }
                }
                if(connection != null){
                    try {
                        connection.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    
    }
    

      消费端代码:

    package com.test.example;
    
    import com.rabbitmq.client.*;
    
    import java.io.IOException;
    import java.util.concurrent.TimeoutException;
    
    /**
     * @Author
     * @create 
     */
    public class Receiver {
    
        public static void main(String[] args) {
            ConnectionFactory factory = null;
            Connection connection = null;
            Channel channel = null;
            try{
                factory = new ConnectionFactory();
                factory.setHost("localhost");
                connection = factory.newConnection();
                channel = connection.createChannel();
    
                channel.queueDeclare(ExampleConstants.QUEUE_NAME,false,false,false,null);
    
                DeliverCallback deliverCallback = (consumerTag,delivery) -> {
                    String message = new String(delivery.getBody(),"UTF-8");
                    System.out.println(" [x] Received '" + message + "'");
                };
                channel.basicConsume(ExampleConstants.QUEUE_NAME,true,deliverCallback,consumerTag -> {});
            }catch (Exception e){
                e.printStackTrace();
            }finally {
                if(channel != null){
                    try {
                        channel.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    } catch (TimeoutException e) {
                        e.printStackTrace();
                    }
                }
                if(connection != null){
                    try {
                        connection.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    
    }
    

      

  • 相关阅读:
    进程调度算法
    附近的人,附近的卖家(geohash+前缀树)
    海量信息库,查找是否存在(bloom filter布隆过滤器)
    继承、虚继承和虚函数表对类的大小的影响
    linux 用户空间与内核空间——高端内存详解
    0-1背包问题入门
    范式
    vue的无缝滚动插件vue-seamless-scroll的安装与使用
    在vue项目中使用swiper2.7.6
    vue项目在IE下报 [vuex] vuex requires a Promise polyfill in this browser问题
  • 原文地址:https://www.cnblogs.com/lxk2010012997/p/11295624.html
Copyright © 2011-2022 走看看