问题
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