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

cwstorm cwstorm at gmail.com
Fri Sep 27 15:13:24 BST 2013


Thank you everyone and you guys are absolutely correct.  I thought I had to declare the connection and channel inside the try block because exception need to be thrown or caught.

Sent from mobile device with autocorrect.

Phil

> On Sep 27, 2013, at 9:40 AM, Tim Watson <tim at rabbitmq.com> wrote:
> 
> 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
> _______________________________________________
> rabbitmq-discuss mailing list
> rabbitmq-discuss at lists.rabbitmq.com
> https://lists.rabbitmq.com/cgi-bin/mailman/listinfo/rabbitmq-discuss


More information about the rabbitmq-discuss mailing list