• Java程序RabbitMQ服务端和客户端


    先需要引入相关的依赖文件

    <!--引入先关依赖包文件-->
    <dependencies>
    <dependency>
    <groupId>com.rabbitmq</groupId>
    <artifactId>amqp-client</artifactId>
    <version>3.6.5</version>
    </dependency>
    </dependencies>

    上来就是干:
    编写链接工具类
    public class ConnectionUtil {
    
        private static ConnectionFactory factory;
    
        private static  Channel channel;
    
        public static final String QUEUE_NAME = "cloud_attacking_dream";
    
        /*
        * 单例模式机制获取连接通道
        * */
        public static Channel initConnetion() throws IOException, TimeoutException {
            if (null == factory && null == channel) {
                factory = new ConnectionFactory();
                factory.setHost("localhost");
                factory.setPort(5672);
                //本机调用,用户密码省略
                /*factory.setUsername();
                factory.setPassword();*/
                Connection connection = factory.newConnection();
                channel = connection.createChannel();
                channel.queueDeclare(QUEUE_NAME, false, false, false, null);
            }
            return channel;
        }
    
    }
    

      服务端代码:

    /**
     * @author licunzhi
     * @desc 服务端(生产者)
     * @date 2018-07-20
     */
    public class Server {
        public static void main(String[] args) throws IOException, TimeoutException, InterruptedException {
            Channel channel = ConnectionUtil.initConnetion();
            for (int i =0; i < 100; i++) {
                String message = UUID.randomUUID().toString();
                channel.basicPublish("", ConnectionUtil.QUEUE_NAME, null, message.getBytes("UTF-8"));
                //定時發送消息  展示結果更加明顯
                Thread.sleep(10000);
            }
        }
    

      客户端代码

    /**
     * @author licunzhi
     * @desc 客户端(消费者)
     * @date 2018-07-20
     */
    public class Client {
        public static void main(String[] args) throws IOException, TimeoutException {
            Channel channel = ConnectionUtil.initConnetion();
            Consumer consumer = new DefaultConsumer(channel) {
                @Override
                public void handleDelivery(String consumerTag, Envelope envelope,
                                AMQP.BasicProperties properties, byte[] body)
                                throws IOException {
                    String message = new String(body, "UTF-8");
                    System.out.println("Customer Received '" + message + "'");
                }
            };
            channel.basicConsume( ConnectionUtil.QUEUE_NAME, true, consumer);
        }
    }
    

      

    运行项目查看结果展示:

    如果这些看着还是不爽  还是不解的话 :没关系  我有代码给你玩:

    github: git@github.com:licunzhi/dream_on_sakura_rain.git

    最后欢迎大佬加群指点:589780530

  • 相关阅读:
    File类 文件过滤器
    LinkedList类的基本方法的用法
    Execption异常 手动和自动抛除异常
    Xshell连接Linux慢问题解决办法
    Liunx网络技术管理及进程管理
    Liunx中三种网络模式配置及Xshell连接
    LInux命令英文全称
    Liunx中fstab文件详解
    20190411RAID磁盘阵列及CentOS7系统启动流程
    20190410Linux中磁盘管理及LVM(week2day1)
  • 原文地址:https://www.cnblogs.com/licunzhi/p/9341181.html
Copyright © 2020-2023  润新知