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个回答

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();
    }
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问