Remotely connect to RabbitMQ containing MQTT

432 views
Skip to first unread message

Nuno Santos

unread,
Jul 16, 2017, 4:45:37 PM7/16/17
to rabbitmq-users
I've been writing a simple client to connect to a rabbitMQ.

This works perfectly fine, if I use an sdk like eclipse paho java for MQTT.

The connection details I use are:

host: tcp://myurl:1883
user: test
pwd: guest

This is successful with my MQTT client.

Now, if I tried to develop a sample with with AMQP in Java, which connections should I use? I've tried two ways:

The first one:

ConnectionFactory factory = new ConnectionFactory();
factory.setHost("myurl");
factory.setUsername("test");
factory.setPassword("guest");
factory.setPort(1883);
System.out.println("connecting....");
Connection connection = factory.newConnection();

System.out.println("connected....");

Channel channel = connection.createChannel();
channel.close();
System.out.println("closed....");

This throws the following exception:

Exception in thread "main" java.util.concurrent.TimeoutException
at com.rabbitmq.utility.BlockingCell.get(BlockingCell.java:77)
at com.rabbitmq.utility.BlockingCell.uninterruptibleGet(BlockingCell.java:120)
at com.rabbitmq.utility.BlockingValueOrException.uninterruptibleGetValue(BlockingValueOrException.java:36)
at com.rabbitmq.client.impl.AMQChannel$BlockingRpcContinuation.getReply(AMQChannel.java:412)
at com.rabbitmq.client.impl.AMQConnection.start(AMQConnection.java:304)
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:911)
at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:870)
at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:828)
at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:979)
at rabbitmq.tut.Worker.main(Worker.java:20)


If I make a slight change, changing from port 1883 to 5672, I get:

Exception in thread "main" java.net.SocketTimeoutException: connect timed out
at java.net.PlainSocketImpl.socketConnect(Native Method)
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.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at com.rabbitmq.client.impl.SocketFrameHandlerFactory.create(SocketFrameHandlerFactory.java:50)
at com.rabbitmq.client.impl.recovery.RecoveryAwareAMQConnectionFactory.newConnection(RecoveryAwareAMQConnectionFactory.java:61)
at com.rabbitmq.client.impl.recovery.AutorecoveringConnection.init(AutorecoveringConnection.java:99)
at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:911)
at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:870)
at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:828)
at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:979)
at rabbitmq.tut.Worker.main(Worker.java:20)


I'm not sure what I could be doing wrong. Perhaps configuration in the broker? 

Michael Klishin

unread,
Jul 16, 2017, 8:01:04 PM7/16/17
to rabbitm...@googlegroups.com
java.net.SocketTimeoutException: connect timed out

means that TCP connection times out.

Most likely because you use ConnectionFactory#setHost to set a URI. Consider using setUri instead
or pass a host to setHost.


Remote connections will require setting up a new user and granting it access to the vhost you use,
since guest/guest is limited to localhost connections by default (and we really do not recommend
that it is whitelisted for remote connections). This is covered in http://www.rabbitmq.com/access-control.html.


--
You received this message because you are subscribed to the Google Groups "rabbitmq-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rabbitmq-users+unsubscribe@googlegroups.com.
To post to this group, send email to rabbitmq-users@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
MK

Staff Software Engineer, Pivotal/RabbitMQ

Nuno Santos

unread,
Jul 17, 2017, 3:50:13 AM7/17/17
to rabbitmq-users
Hi Michael,

Thanks for your reply.

I am not using the guest user, those are just sample values.

I'm using the same credentials I use with this client https://github.com/eclipse/paho.mqtt.java which works fine.

I tried your suggestion, but I still seem to be getting the same problem. I do not understand why this is an issue with the AMQP SDK.

ConnectionFactory factory = new ConnectionFactory();

factory
.setUri("amqp://test:gu...@myhost.net");


System.out.println("connecting....");
Connection connection = factory.newConnection();

System.out.println("connected....");
Channel channel = connection.createChannel();
channel
.close();
System.out.println("closed....");


I've tried setting both the port 1883 and 5672 but I always get a SockettimeoutException. I must not be doing something right still.

I've even downloaded a client tool and it works
To unsubscribe from this group and stop receiving emails from it, send an email to rabbitmq-user...@googlegroups.com.
To post to this group, send email to rabbitm...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Michael Klishin

unread,
Jul 17, 2017, 5:28:16 AM7/17/17
to rabbitm...@googlegroups.com
RabbitMQ Java client does not use port 1883. It uses 5672 (or 5671 if TLS is enabled) by default. Port 1883
is only used by MQTT clients and RabbitMQ Java client uses AMQP 0-9-1: http://www.rabbitmq.com/tutorials/amqp-concepts.html.

A socket timeout exception strongly suggests that the client cannot open a TCP connection to the target node,
e.g. because a firewall blocks it. If a node was not running it would immediately get a TCP reset, not a timeout.

See http://www.rabbitmq.com/networking.html for the list of ports used by RabbitMQ and investigate if you can
open a connection to port 5672 on the target node using telnet or netcat or similar (RabbitMQ will still log an error
as a telnet connection likely won't perform the expected protocol handshake).

See server logs as any inbound connection that sends data is logged (successful or not). If there are no connections
in the log when Java client tries to connect, it's just another indication that TCP connections are blocked.

Wireshark (https://www.rabbitmq.com/amqp-wireshark.html) and tcpdump are wonderful tools that will help you
collect a lot of relevant information. Don't guess, collect data instead.


To unsubscribe from this group and stop receiving emails from it, send an email to rabbitmq-users+unsubscribe@googlegroups.com.
To post to this group, send email to rabbitmq-users@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages