[rabbitmq-discuss] Failure to return from call to newConnection()
McMahon, James S (TASC)
james.mcmahon at TASC.COM
Mon Oct 8 21:02:25 BST 2012
I am experiencing a problem that I have been unable to pin down. When I attempt to use a java class to listen for messages bound to a particular exchange by a particular routing key, I get a "Connection timed out" error. Here is my code:
public class ReceiveLogsDirect {
private static final String EXCHANGE_NAME = "ABC_SERVER";
public static void main(String[] argv) throws Exception {
String hostName = "localhost";
int portNumber = 5672;
if (argv.length < 1 || argv.length == 2 || argv.length > 3){
System.err.println("Usage: ReceiveLogsDirect [session ID] [RabbitMQ server IP, optional] [RabbitMQ server port #, optional]");
System.exit(1);
}
else if (argv.length == 1) {
hostName = "localhost";
portNumber = 5672; // default port that rabbitMQ listens to
}
else if (argv.length == 3) {
hostName = argv[1];
portNumber = Integer.parseInt(argv[2]);
}
ConnectionFactory factory = new ConnectionFactory();
factory.setHost(hostName);
factory.setPort(portNumber);
factory.setUsername("guest");
factory.setPassword("guest");
factory.setVirtualHost("/");
System.out.println(" [*] HERE 1");
Connection connection = factory.newConnection();
System.out.println(" [*] HERE 2");
Channel channel = connection.createChannel();
channel.exchangeDeclare(EXCHANGE_NAME, "direct");
String queueName = channel.queueDeclare().getQueue();
channel.queueBind(queueName, EXCHANGE_NAME, argv[0]);
System.out.println(" [*] bindingKey " + argv[0] + " is bound to queue on exchange " + EXCHANGE_NAME );
System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
QueueingConsumer consumer = new QueueingConsumer(channel);
channel.basicConsume(queueName, true, consumer);
while (true) {
QueueingConsumer.Delivery delivery = consumer.nextDelivery();
String message = new String(delivery.getBody());
String routingKey = delivery.getEnvelope().getRoutingKey();
System.out.println(" [x] Received message for " + routingKey + " >>>" + message);
}
}
I used simple print statements to pinpoint the source of the problem at the line where I call newConnection().
The error I get is this:
[*] HERE 1
Exception in thread "main" java.net.ConnectException: Connection timed out: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(Unknown Source)
at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at com.rabbitmq.client.ConnectionFactory.createFrameHandler(ConnectionFactory.java:441)
at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:480)
at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:509)
at com.tasc.prism.queue.ReceiveLogsDirect.main(ReceiveLogsDirect.java:55)
My install of rabbitMQ is 2.7.0. Oddly, I have been able to execute this in another command shell window and it works. I am wondering why I'm having trouble in the second shell? Surely I must be able to execute multiple instances of ReceiveLogsDirect simultaneously, right?
Could it be a runtime environment problem? I set my class path exactly the same in both shells.
Any thoughts or similar experience with such a problem? Thank you.
- Jim
CONFIDENTIALITY NOTICE: This message and any attachments or files transmitted with it (collectively, the "Message") are intended only for the addressee and may contain information that is privileged, proprietary and/or prohibited from disclosure by law or contract. If you are not the intended recipient: (a) please do not read, copy or retransmit the Message; (b) permanently delete and/or destroy all electronic and hard copies of the Message; (c) notify us by return email; and (d) you are hereby notified that any dissemination, distribution or copying of the Message is strictly prohibited.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.rabbitmq.com/pipermail/rabbitmq-discuss/attachments/20121008/24bfc369/attachment.htm>
More information about the rabbitmq-discuss
mailing list