DoctorMckay
DoctorMckay

Reputation: 155

RabbitMQ Connection reset

I'm trying to connect a simple RabbitMQ using java code to my server (which is executing the RabbitMQ service). Executing the following code (source here) gives me the java.net.SocketException: Connection Reset exception.

import java.io.*;
import java.security.*;


import com.rabbitmq.client.*;

public class test
{
    public static void main(String[] args) throws Exception
    {

        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("myIP");  //myIP is just dummy text, I have a real IP there
        factory.setPort(5672);
        factory.setUsername("admin");
        factory.setPassword("sesgo");
        factory.setVirtualHost("vSESGO");

        factory.useSslProtocol();

        Connection conn = factory.newConnection();
        Channel channel = conn.createChannel();

        channel.queueDeclare("rabbitmq-java-test", false, true, true, null);
        channel.basicPublish("", "rabbitmq-java-test", null, "Hello, World".getBytes());


        GetResponse chResponse = channel.basicGet("rabbitmq-java-test", false);
        if(chResponse == null) {
            System.out.println("No message retrieved");
        } else {
            byte[] body = chResponse.getBody();
            System.out.println("Recieved: " + new String(body));
        }


        channel.close();
        conn.close();
    }
}

I've looked for an answer online and I've already tried:

  1. Verifying the server has the port I'm connecting to opened.
  2. Verifying the client does not block my connection with firewalls, etc.
  3. Creating a new Virtual Host on RabbitMQ and giving permissions to it.
  4. Verifying iptables is not blocking me at the server side.

Nothing seems to work, any ideas?

Full stacktrace here:

This trust manager trusts every certificate, effectively disabling peer verification. This is convenient for local development but prone to man-in-the-middle attacks. Please see http://www.rabbitmq.com/ssl.html#validating-cerficates to learn more about peer certificate validation.
Exception in thread "main" java.net.SocketException: Connection reset
 at java.net.SocketInputStream.read(Unknown Source)
 at java.net.SocketInputStream.read(Unknown Source)
 at sun.security.ssl.InputRecord.readFully(Unknown Source)
 at sun.security.ssl.InputRecord.read(Unknown Source)
 at sun.security.ssl.SSLSocketImpl.readRecord(Unknown Source)
 at sun.security.ssl.SSLSocketImpl.performInitialHandshake(Unknown Source)
 at sun.security.ssl.SSLSocketImpl.writeRecord(Unknown Source)
 at sun.security.ssl.AppOutputStream.write(Unknown Source)
 at java.io.BufferedOutputStream.flushBuffer(Unknown Source)
 at java.io.BufferedOutputStream.flush(Unknown Source)
 at java.io.DataOutputStream.flush(Unknown Source)
 at com.rabbitmq.client.impl.SocketFrameHandler.sendHeader(SocketFrameHandler.java:147)
 at com.rabbitmq.client.impl.SocketFrameHandler.sendHeader(SocketFrameHandler.java:153)
 at com.rabbitmq.client.impl.AMQConnection.start(AMQConnection.java:294)
 at com.rabbitmq.client.impl.recovery.RecoveryAwareAMQConnectionFactory.newConnection(RecoveryAwareAMQConnectionFactory.java:63)
 at com.rabbitmq.client.impl.recovery.AutorecoveringConnection.init(AutorecoveringConnection.java:99)
 at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:921)
 at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:880)
 at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:838)
 at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:990)
 at test.main(test.java:25)

Upvotes: 7

Views: 12670

Answers (3)

GreenMachine
GreenMachine

Reputation: 1

I had this exact same error and my issue was in the rabbitmq.conf file. I was trying to use a JKS file for the following ssl options. Generating my own self signed .pem files was able to help fix this. I followed this guide pretty closely https://www.codetd.com/en/article/12031242.

ssl_options.cacertfile = /etc/rabbitmq/ca_certificate.pem
ssl_options.certfile   = /etc/rabbitmq/server_certificate.pem
ssl_options.keyfile    = /etc/rabbitmq/server_key.pem

Upvotes: 0

DistantBlue
DistantBlue

Reputation: 1844

I had the same issue right here: RabbitMQ Connection reset Exception. Solution for Windows was to add backslash in rabbit config file for paths to certs and key.

Upvotes: 1

em_bo
em_bo

Reputation: 682

I don't know if this applies to your situation, but I recently resolved a similar situation while testing RabbitMQ 3.8.3, and the cause was that the key I was referencing was password-protected, but I had failed to provide the password in the RabbitMQ config, like this:

ssl_options.password = password

Unfortunately there was absolutely nothing in the RabbitMQ logs about this, even with the log level set to debug. When testing via various clients, a connection was established, but RabbitMQ immediately sent a connection reset.

Upvotes: 0

Related Questions