I run RabbitMQ in docker using this command:
docker run --rm -it --hostname my-rabbit -p 15672:15672 -p 5672:5672 rabbitmq:3-management
It works and I can connect to RabitMq from java:
ConnectionFactory factory = new ConnectionFactory();
factory.setUsername("guest");
factory.setPassword("guest");
Connection connection = factory.newConnection("http://localhost:5672");
But I need to change port and I do this:
docker run --rm -it --hostname my-rabbit -p 15673:15672 -p 5673:5672 rabbitmq:3-management
When I open http://localhost:15673/ - it works. I see the admin console. But when I try to connect
ConnectionFactory factory = new ConnectionFactory();
factory.setUsername("guest");
factory.setPassword("guest");
Connection connection = factory.newConnection("http://localhost:5673");
I get an exception:
Exception in thread "main" java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:81)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:476)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:218)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:200)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:162)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:394)
at java.net.Socket.connect(Socket.java:606)
at com.rabbitmq.client.impl.SocketFrameHandlerFactory.create(SocketFrameHandlerFactory.java:59)
at com.rabbitmq.client.impl.recovery.RecoveryAwareAMQConnectionFactory.newConnection(RecoveryAwareAMQConnectionFactory.java:63)
at com.rabbitmq.client.impl.recovery.AutorecoveringConnection.init(AutorecoveringConnection.java:160)
at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:1216)
at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:1173)
at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:1310)
at com.project.Main.main(Main.java:14)
EDIT: When I use port 5672 without changes I see this (maybe it can help)
CodePudding user response:
I changed the code to this and it works. Thanks, RabbitMQ for so obvious code!
ConnectionFactory factory = new ConnectionFactory();
factory.setPort(5673);
factory.setHost("localhost");
factory.setUsername("guest");
factory.setPassword("guest");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
CodePudding user response:
You are using an HTTP URI - http://localhost:5673
You should be using an AMQP URI - amqp://localhost:5673
I'm kind of surprised the first one worked at all, without a parsing error. My guess is that since the scheme (http
) was not correct, the client used the default of localhost:5672
.
AMQP URIs are documented here - https://www.rabbitmq.com/uri-spec.html
NOTE: the RabbitMQ team monitors the rabbitmq-users
mailing list and only sometimes answers questions on StackOverflow.