0

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()

halfer
  • 19,824
  • 17
  • 99
  • 186
Shivam Mishra
  • 73
  • 1
  • 5
  • 1
    What do you think that `System.getenv()` does? You are trying to retrieve an environment variable named `amqp://xxuserxx:password_xxxxxxxxxx@Cloudamqhost/vhost` and somehow I do not think that is what you want to do. – DanielBarbarian May 16 '18 at 14:53
  • You should try to set an environment variable something like `CLOUDAMQP_URL=amqp://xxuserxx:password_xxxxxxxxxx@Cloudamqhost/vhost` using `export` (in linux) or `set` (in windows) – piy26 May 16 '18 at 14:55
  • Im not sure. I just copied the example from cloudamqp tutorial. Let me try using uri = "amqp://xxuserxx:password_xxxxxxxxxx@Cloudamqhost/vhost"; – Shivam Mishra May 16 '18 at 14:55
  • Regarding nullpointer, please confirm what is null ? (connection or factory ) – piy26 May 16 '18 at 14:57
  • Either you set an environment variable as @piy26 suggested and retrieve that or you set the value directly as you have suggested. – DanielBarbarian May 16 '18 at 14:57
  • Please ensure that your rabbitmq server is running – piy26 May 16 '18 at 15:01

1 Answers1

1
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";
Vyncent
  • 1,185
  • 7
  • 17