I'm running 2 multi-threaded programs.
Each thread from the first program acts as a producer and writes messages to a queue, whereas each thread from the second program acts as a consumer and reads messages from the same queue.
In both projects I created a connection factory like the following:
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
factory.setAutomaticRecoveryEnabled(true);
factory.setRequestedChannelMax(0);
factory.setUsername("user");
factory.setPassword("password");
However I'm not sure about the recommended approach for the next step.
Should I create a new connection at the start of each thread like:
Connection connection = factory.newConnection();
And then for each request create a new channel like:
Channel channel = connection.createChannel();
Or should I create only a single connection, make the threads share the same connection, and then create a new channel for every request.
I know that the connection is a socket thread-safe connection and it should be created carefully. I'm just asking about whether there is a recommended approach to use in my program, because usually a documentation would contain a recommended way to handle connections and sockets, but I couldn't find such an answer in RabbitMQ's documentation.