How to use Java RabbitMQ and set URI server?

↘锁芯ラ 提交于 2019-12-24 00:13:36

问题


I'm using the RabbitMQ Java API to connect to a RabbitMQ server. I want to use ConnectionFactory.setUri(...) to configure which server to use. It appears to munge the virtual host.

There's a default virtual host named /.

import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.Channel;

import java.net.URI;

public class Worker {

    public static void main(String[] argv) throws Exception {

        ConnectionFactory factory = new ConnectionFactory();
        final URI uri = URI.create("amqp://guest:guest@localhost:5672/");
        factory.setUri(uri);
        final Connection connection = factory.newConnection();
        final Channel channel = connection.createChannel();
    }
}

Using the above code, the configured virtual host is empty. There doesn't seem to be a way, using the URI, to configure the virtual host to be /.

Is there a way to do this?


回答1:


I ended up solving this by not using setUri but rather setting the individual URI components.




回答2:


Your need to URL-encode the '/' using %2F




回答3:


For people coming from google, using the combination of / and the url encoded / which is %2f, worked for me.

ConnectionFactory factory = new ConnectionFactory();
factory.setUri("amqp://guest:guest@localhost:5672/%2F");


来源:https://stackoverflow.com/questions/17221874/how-to-use-java-rabbitmq-and-set-uri-server

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!