Currently I have two rabbitmq docker containers in my environment. One of them has the external port set to 5672 and another one is using 5673. From my java program (which is not dockerized) I am trying to connect to the container listening on 5673 by using the amqp-client maven library.
The code looks something like this:
ConnectionFactory factory = new ConnectionFactory();
Connection connection = factory.newConnection("amqp://guest:guest@localhost:5673");
Channel channel = connection.createChannel();
channel.queueDeclare("region", true, false, false, null);
DefaultConsumer consumer =
new DefaultConsumer(channel) {
@Override
public void handleConsumeOk(String consumerTag) {
super.handleConsumeOk(consumerTag);
}
@Override
public void handleDelivery(
String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body)
throws IOException {
String message = new String(body, StandardCharsets.UTF_8);
log.info(message);
}
};
channel.queueBind("region", "gas_region", "");
channel.basicConsume("region", true, consumer);
The problem I have is that my Java program keeps connecting to the 5672 instance instead of the one listening on 5673. I noticed that because I see the created queue in the 5672 instance instead of the other one and also my program doesn’t find the configured exchange in the 5673 instance.
Does someone have any ideas on what could be the problem?
The docker compose configuration for my rabbitmq instances looks like this:
input_mb:
image: rabbitmq:3-management
hostname: rabbit1
container_name: input_mb
ports:
- 5672:5672
- 15672:15672
volumes:
- "./input_mb/rabbitmq.config:/etc/rabbitmq/rabbitmq.config"
- "./input_mb/definitions.json:/etc/rabbitmq/definitions.json"
networks:
- common_network
output_mb:
image: rabbitmq:3-management
hostname: rabbit2
container_name: output_mb
ports:
- 5673:5672
- 15673:15672
volumes:
- "./output_mb/rabbitmq.config:/etc/rabbitmq/rabbitmq.config"
- "./output_mb/definitions.json:/etc/rabbitmq/definitions.json"
networks:
- common_network
2
Answers
Apparently, I had to specify
before
I hope this helps someone.
You are using the
newConnection
override that specifies a connection name: codeIf you wish to use an AMQP URI, use the
setUri
method on theConnectionFactory
instance: codeNOTE: Team RabbitMQ monitors the
rabbitmq-users
mailing list and only sometimes answers questions on StackOverflow.