Return-Path: Delivered-To: apmail-activemq-users-archive@www.apache.org Received: (qmail 85919 invoked from network); 10 May 2010 20:31:44 -0000 Received: from unknown (HELO mail.apache.org) (140.211.11.3) by 140.211.11.9 with SMTP; 10 May 2010 20:31:44 -0000 Received: (qmail 89340 invoked by uid 500); 10 May 2010 20:31:44 -0000 Delivered-To: apmail-activemq-users-archive@activemq.apache.org Received: (qmail 89304 invoked by uid 500); 10 May 2010 20:31:44 -0000 Mailing-List: contact users-help@activemq.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: users@activemq.apache.org Delivered-To: mailing list users@activemq.apache.org Received: (qmail 89296 invoked by uid 99); 10 May 2010 20:31:44 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 10 May 2010 20:31:44 +0000 X-ASF-Spam-Status: No, hits=2.2 required=10.0 tests=FREEMAIL_ENVFROM_END_DIGIT,FREEMAIL_FROM,SPF_PASS,T_TO_NO_BRKTS_FREEMAIL X-Spam-Check-By: apache.org Received-SPF: pass (nike.apache.org: domain of tabish121@gmail.com designates 209.85.161.43 as permitted sender) Received: from [209.85.161.43] (HELO mail-fx0-f43.google.com) (209.85.161.43) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 10 May 2010 20:31:37 +0000 Received: by fxm14 with SMTP id 14so8509069fxm.2 for ; Mon, 10 May 2010 13:31:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:subject:from:to:in-reply-to :references:content-type:date:message-id:mime-version:x-mailer :content-transfer-encoding; bh=GbVWagPENzHzklmmzzkLU/l0PmjT1ODWelS01R0oGOo=; b=l290mA02PS+XsCjA0wz/GEuDki5fAoLPfmd25Bz/QPid6wJAzclxtJq7m4gOsNZIeB YdWxEOe+JocMXpqp+aJH+lsewCWLfO6nvP6m6s2f5Ey7fPxEjjgJVi+QKO81+yZ27z1u oP2VEjOE5WZp1/460Oiz/TFOywfutHyD8Vbpo= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=subject:from:to:in-reply-to:references:content-type:date:message-id :mime-version:x-mailer:content-transfer-encoding; b=JZ8QBVIBlfKfMEWiCBo15Z/2hCOYbuVbkc16QdFJQl4GE+rvNYz9q6bJgAqjZnZsrv UIPMBCvuXjUsLqy/7C83D/gGXYcNErhMnYZ3xSHSXjOjYp4ZeqF90B7FjlAPQzETIFdv ZzE+lpT+l/yhToGNvLZXgV8C58/taY1BSqJT4= Received: by 10.223.51.152 with SMTP id d24mr5049982fag.36.1273523474354; Mon, 10 May 2010 13:31:14 -0700 (PDT) Received: from [192.168.2.150] (c-76-21-195-19.hsd1.va.comcast.net [76.21.195.19]) by mx.google.com with ESMTPS id 2sm23098068faf.3.2010.05.10.13.31.09 (version=SSLv3 cipher=RC4-MD5); Mon, 10 May 2010 13:31:11 -0700 (PDT) Subject: Re: NMS - cannot recover from connection failure From: Timothy Bish To: users@activemq.apache.org In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" Date: Mon, 10 May 2010 16:31:07 -0400 Message-ID: <1273523468.2625.33.camel@localhost> Mime-Version: 1.0 X-Mailer: Evolution 2.28.3 (2.28.3-1.fc12) Content-Transfer-Encoding: 7bit X-Virus-Checked: Checked by ClamAV on apache.org On Mon, 2010-05-10 at 15:04 +0100, Tammer Salem wrote: > Hello All, > I have an NMS client using the stomp broker on a .netcf device. For the most > part it works absolutely fine, but now I'm trying to harden the device (for > network failures etc.). > I have added an exception listener and specifically look for IOExceptions > (which I believe are what .NET throws if the GPRS/3G network connectivity > dies). I can successfully capture this exception and then I fire off a > thread to restart the connection and add my listeners again. > > At the moment this method doesn't seem to work - I can never get a > connection again. Can anyone advise what is the best way of doing this? > So what happens exactly when the code attempts to create the new Connection object and restore the Sessions and Destinations etc? Is there an exception being thrown? Regards Tim. > I've also attached my code so that you know what I'm trying to do. > > class AMQMessageHandler > > { > > private String myQueue; > > private String connecturi; > > > > //private ISession session; > > IConnectionFactory factory; > > IConnection connection; > > ISession session; > > IDestination destination; > > IMessageConsumer messageConsumer; > > > > /* > > * the UI thread within whose context the code will be executed > > */ > > private Form uiThread; > > > > /* > > * function in the UI thread to handle exceptions > > */ > > private Delegate exceptionDelegate; > > > > > > public AMQMessageHandler(String connectionURI, String driverID, > > Delegate exceptionDelegate, Form uiThread) > > { > > this.connecturi = connectionURI; > > this.myQueue = "driver.channel." + driverID; > > this.uiThread = uiThread; > > this.exceptionDelegate = exceptionDelegate; > > } > > > > public void start() > > { > > > > try > > { > > this.factory = new ConnectionFactory(connecturi); > > this.connection = this.factory.CreateConnection(); > > this.session = this.connection.CreateSession(); > > this.destination = SessionUtil.GetDestination(session, > > "queue://" + myQueue); > > this.messageConsumer = session.CreateConsumer(destination); > > > > messageConsumer.Listener += new > > MessageListener(messageConsumer_Listener); > > connection.ExceptionListener += new > > ExceptionListener(connection_ExceptionListener); > > connection.Start(); > > > > } > > catch (Exception ex) > > { > > uiThread.Invoke(exceptionDelegate,ex.Message); > > } > > } > > > > public delegate void RestartConnectionDelegate(); > > > > private void restartConnection() > > { > > new System.Threading.Thread(threadedStartConnection).Start(); > > } > > > > private void threadedStartConnection() > > { > > int retryCount = 1; > > bool keepTryingtoRestart = true; > > > > while (keepTryingtoRestart == true) > > { > > //tsalem - add some kind of logging here > > //listBox.Items.Add("attempting to reconnect - pass " + > > retryCount); > > System.Threading.Thread.Sleep(3000); > > > > try > > { > > this.factory = new ConnectionFactory(connecturi); > > this.connection = this.factory.CreateConnection(); > > this.session = this.connection.CreateSession(); > > this.destination = SessionUtil.GetDestination(session, > > "queue://" + myQueue); > > this.messageConsumer = > > session.CreateConsumer(destination); > > messageConsumer.Listener += new > > MessageListener(messageConsumer_Listener); > > connection.ExceptionListener += new > > ExceptionListener(connection_ExceptionListener); > > connection.Start(); > > > > keepTryingtoRestart = false; > > } > > catch (Exception ex) > > { > > uiThread.Invoke(exceptionDelegate, ex.Message +"\n" + > > ex.StackTrace); > > //tsalem change this to check for keep retrying rather > > than on a counter > > if (retryCount == 50) > > { > > keepTryingtoRestart = false; > > } > > > > } > > > > retryCount++; > > } > > } > > > > > > public void messageConsumer_Listener(IMessage receivedMessage) > > { > > try > > { > > ITextMessage message = receivedMessage as ITextMessage; > > message.Acknowledge(); > > //tsalem - todo - handle message > > if (message.Text != null) > > { > > AMQUtil.Instance.ResponseAvailable(message.Text, > > uiThread); > > } > > else > > { > > uiThread.Invoke(exceptionDelegate, "ERROR: AMQ Message > > is empty"); > > } > > } > > catch (Exception ex) > > { > > uiThread.Invoke(exceptionDelegate,ex.Message); > > } > > } > > > > > > > > void connection_ExceptionListener(Exception exception) > > { > > uiThread.Invoke(exceptionDelegate,exception.Message); > > > > if (exception is System.IO.IOException) > > { > > //Invoke(new > > RestartConnectionDelegate(threadedStartConnection)); > > threadedStartConnection(); > > } > > } > > } > > > regards, > Tammer -- Tim Bish Open Source Integration: http://fusesource.com ActiveMQ in Action: http://www.manning.com/snyder/ Follow me on Twitter: http://twitter.com/tabish121 My Blog: http://timbish.blogspot.com/