showzing
showzing
采纳率0%
2019-08-30 11:11 阅读 383

java如何通过jms的方式连接到RabbitMQ

求助大神,想通过jms的方式连接到RabbitMQ,但是连接工厂和队列的jdni连接字符串不知道怎么写,网上搜了一段时间也没有找到正确的方式。
这是ActiveMQ的连接字符串:
#jmsc("queue_download_xnkg",
"org.apache.activemq.jndi.ActiveMQInitialContextFactory",
"tcp://192.168.10.23:61616",
"ConnectionFactory",
"dynamicQueues/testquequename",
false,
"admin",
"admin")
其中"org.apache.activemq.jndi.ActiveMQInitialContextFactory"为连接类;
"tcp://192.168.10.23:61616"为连接地址;
"ConnectionFactory"为连接工厂的jndi字符串;
"dynamicQueues/testquequename"为连接队列的jndi字符串;
其余为账号密码。想得到类似的对应RabbitMQ的连接字符串。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • qq_26400953 游语 2019-08-30 16:29
    public class RabbitProducer {
        private static final String EXCHANGE_NAME = "test_exchange";
        private static final String ROUNTING_KEY = "test_rountingkey";
        private static final String QUEUE_NAME = "test_queue";
        private static final String IP_ADDRESS = "127.0.0.1";
        private static final Integer PORT = 5672;
    
        public static void main(String[] args) throws IOException, TimeoutException {
            ConnectionFactory factory = new ConnectionFactory();
            factory.setHost(IP_ADDRESS);
            factory.setPort(PORT);
            factory.setUsername("root");
            factory.setPassword("admin");
    
            Connection connection = factory.newConnection(); //创建链接
            Channel channel = connection.createChannel(); //创建信道
            //创建一个type=direct、持久化的、非自动删除的交换器
            channel.exchangeDeclare(EXCHANGE_NAME, "direct", true, false, null);
            //创建一个持久化的、非拍他的、非自动删除的队列
            channel.queueDeclare(QUEUE_NAME, true, false, false, null);
    
            //将交换器队列通过路由键绑定
            channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, ROUNTING_KEY);
    
            String message = "Hello World";
            channel.basicPublish(EXCHANGE_NAME, ROUNTING_KEY, MessageProperties.PERSISTENT_TEXT_PLAIN, message.getBytes());
    
            channel.close();
            connection.close();
        }
    }
    
    public static void main(String[] args) throws IOException, TimeoutException, InterruptedException {
            Address[] addresses = new Address[] {
                    new Address(IP_ADDRESS, PORT)
            };
    
            ConnectionFactory factory = new ConnectionFactory();
            factory.setPort(PORT);
            factory.setUsername("root");
            factory.setPassword("admin");
    
            Connection connection = factory.newConnection(addresses); //创建链接
            final Channel channel = connection.createChannel(); //创建信道
            channel.basicQos(64); //设置客户端做多接收未被ack的消息的个数
            Consumer consumer = new DefaultConsumer(channel) {
                @Override
                public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                    System.out.println("recv message:" + new String(body));
                    try {
                        TimeUnit.SECONDS.sleep(1);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    channel.basicAck(envelope.getDeliveryTag(), false);
                }
    
            };
            channel.basicConsume(QUEUE_NAME, consumer);
            TimeUnit.SECONDS.sleep(5);
    
            channel.close();
            connection.close();
        }
    
    点赞 评论 复制链接分享

相关推荐