[rabbitmq-discuss] How to close connection and channel when an exception occur

Tim Watson tim at rabbitmq.com
Fri Sep 27 14:40:29 BST 2013


Phil,

On 27 Sep 2013, at 14:31, Phil wrote:
> I'm a newbie on RabbitMQ.  I'm writing a java service to consume a message from the queue.  Everything is working as designed but I cannot seem to code "channel.close() or conn.close()" in the catch  or finally block.  Java is complaining about that the "cannot find symbol".
> 
> symbol  : variable channel
> location: class RouteToMercury.javaServices
> 			channel.close();
> 

It's impossible to help with a compiler error if you don't post that code that you're trying to compile.

> Can I close the connection and channel if an exception occur?

Yes, absolutely. The above looks like a scoping issue. I suspect you've done something like...

try {
  final Channel channel = connection.createChannel();
  doSomeWork(channel);
} finally {
  channel.close();
}

Am I right? If so, you need to move the channel variable to the outer scope.

Cheers,
Tim


More information about the rabbitmq-discuss mailing list