Search code examples
javarabbitmqqueueamqpcloudamqp

Connection Refused cloud amqp


I am using cloudamqp message service for trying out MQs (RabbitMQ in this case). I am trying to push a message into the Queue from a Java class but it always says:

Connection Refused

Also System.getenv returns null (I am stuck with that also).

Below is the code and exception:

    String uri = System.getenv("amqp://xxuserxx:password_xxxxxxxxxx@Cloudamqhost/vhost");
        if (uri == null) uri = "amqp://guest:guest@localhost";
    ConnectionFactory factory = new ConnectionFactory();
    try {
        factory.setUri(uri);
    } catch (URISyntaxException e) {
        e.printStackTrace();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (KeyManagementException e) {
        e.printStackTrace();
    }

    //Recommended settings
    factory.setRequestedHeartbeat(30);
    factory.setConnectionTimeout(60000);

    Connection connection = null;
    try {
        connection = factory.newConnection(); //Getting Null Pointer here
    } catch (TimeoutException e) {
        e.toString();

Stacktrace:

Exception in thread "main" java.net.ConnectException: Connection refused: connect
    at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.net.Socket.connect(Socket.java:589)
    at com.rabbitmq.client.impl.SocketFrameHandlerFactory.create(SocketFrameHandlerFactory.java:60)
    at com.rabbitmq.client.impl.recovery.RecoveryAwareAMQConnectionFactory.newConnection(RecoveryAwareAMQConnectionFactory.java:62)
    at com.rabbitmq.client.impl.recovery.AutorecoveringConnection.init(AutorecoveringConnection.java:134)
    at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:997)
    at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:956)
    at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:914)
    at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:1068)

When I am catching generic Exception, it is catching null pointer exception:

java.lang.NullPointerException @connection = factory.newConnection()


Solution

  • String uri = 
    System.getenv("amqp://xxuserxx:password_xxxxxxxxxx@Cloudamqhost/vhost");
    if (uri == null) uri = "amqp://guest:guest@localhost";
    

    This is strange, either you set the uri as a constant :

    String uri = "amqp://xxuserxx:password_xxxxxxxxxx@Cloudamqhost/vhost";
    

    or you try to get the uri value for an environment variable by it's name

    System.getenv("AMQP_CONNECTION_STRING");
    if (uri == null) uri = "amqp://guest:guest@localhost";