zoukankan      html  css  js  c++  java
  • rabbitmq 学习4初试2

    本例是一个简单的异步发送消息实例
    1,发送端

    @Test(groups = { "sunjun" })
    public class RabbitmqTest {

        private static Connection connection;

        static {
            ConnectionParameters params = new ConnectionParameters();
            ConnectionFactory factory = new ConnectionFactory(params);
            try {
                connection = factory.newConnection("192.168.18.21",
                        AMQP.PROTOCOL.PORT);
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

        public void testSend() {
            try {
                Channel channel = connection.createChannel();
                System.out.println(channel.toString());
                Assert.assertNotNull(channel);
                byte[] messageBodyBytes = "hello world".getBytes();
                channel.basicPublish("exchangeName", "routingKey",
                        MessageProperties.PERSISTENT_TEXT_PLAIN, messageBodyBytes);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    2,接收端
    @Test(groups = { "sunjun" })
    public class RabbitmqTestB {

        private static Connection connection;

        static {
            ConnectionParameters params = new ConnectionParameters();
            ConnectionFactory factory = new ConnectionFactory(params);
            try {
                connection = factory.newConnection("localhost", AMQP.PROTOCOL.PORT);
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

        public void testReceive() {
            try {
                Channel channel = connection.createChannel();
                System.out.println(channel.toString());

                Assert.assertNotNull(channel);

                channel.exchangeDeclare("exchangeName", "direct");
                channel.queueDeclare("queueName");
                channel.queueBind("queueName", "exchangeName", "routingKey");

                boolean noAck = false;
                GetResponse response = channel.basicGet("queueName", true);
                if (response == null) {
                    System.out.println("No message retrieved.");
                } else {
                    AMQP.BasicProperties props = response.getProps();
                    byte[] body = response.getBody();
                    System.out.println(new String(body));
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    先执行下发送端,再执行下接收端,输出:hello world
    ok
  • 相关阅读:
    Python:Lasso方法、GM预测模型、神经网络预测模型之财政收入影响因素分析及预测
    ARIMA模型构建、预测——基于Python
    家用电器用户行为分析与事件识别学习笔记
    JQData数据提取及MySQL简单操作——基于Python
    android 沉浸通知栏
    PullToRefreshScrollView 修改下拉刷新图标
    Android AlertDialog 设置setSingleChoiceItems不显示列表的原因【setMessage和setSingleChoiceItems不能同时使用】
    图片跑马灯抽奖,本地图片变换简单实现
    android知识点大总结
    Android 面试精华题目总结
  • 原文地址:https://www.cnblogs.com/amityat/p/2160040.html
Copyright © 2011-2022 走看看