Thanks - 

Here is what I've implemented, however I am still having issues with regards to 
closing the connection.  When there is a network failure, I get a ping timeout 
exception, which is what I expect, but when I go to close the connection it 
just hangs there.  I never reach my notify() statement.  What am I missing?  
This works on SilverStreams app server, is there something different that I 
need to do on JBoss?

anonymous wrote : 
  | import javax.jms.*;
  | import javax.naming.InitialContext;
  | import java.io.PrintWriter;
  | import java.io.FileWriter;
  | import java.io.IOException;
  | import java.util.Date;
  | import java.text.DateFormat;
  | 
  | public class SmartListener implements MessageListener, ExceptionListener, 
Runnable{
  |     String topicName;
  |     TopicSubscriber topicSubscriber;
  |     TopicConnection connection;
  | 
  |     public void run(){
  |         startConnection();
  |     }
  | 
  |     public SmartListener(String topicName){
  |          this.topicName = topicName;
  |     }
  | 
  |     public void startConnection(){
  | 
  |         /**
  |          * Thread which performs reconnections, usually waiting for to be 
instructed to start
  |          * the re-connection process
  |          */
  |         Thread t = new Thread(){
  |             public void run(){
  |                 while(true){
  |                     reconnect();
  |                 }
  |             }
  |         };
  |         t.start();
  | 
  |         //Create TopicSubscriber, if it fails immediatley notify re-connect 
thread
  |         try{
  |             topicSubscriber = createSubscriber();
  |         }
  |         catch(JMSException jmse){
  |             System.out.println("JMSException: " + jmse);
  |             synchronized(this){
  |                 notify();
  |             }
  |         }
  |     }
  | 
  |     public String buildFilter(){
  |         String filter = "alias in (";
  |         for(int i=0; i<modelRef.ports.length; i++){
  |             filter += "'" + modelRef.ports.getAlias() + "', ";
  |         }
  |         filter += ")";
  |         return filter;
  |     }
  | 
  |     public void onMessage(Message message){
  |          //do onMessage       
  |          System.out.println("GOT MESSAGE");
  |     }
  | 
  | 
  |     public synchronized boolean reconnect(){
  |         doWait();  //Wait to be notified
  |         System.out.print("Trying to reconnect ...");
  |         for(int i=0; i < 10; i++){
  |             try{
  |                 topicSubscriber = createSubscriber();
  |                 System.out.print("ok");
  |                 return true;
  |             }
  |             catch(Exception e){
  |                 System.out.print(".");
  |                 try{
  |                     Thread.sleep(1000);
  |                 }
  |                 catch(InterruptedException exc){}
  |             }
  |         }
  |         System.err.println("failed after 10 attempts");
  |         return false;
  |     }
  | 
  |     public TopicSubscriber createSubscriber() throws JMSException{
  |         TopicSubscriber subscriber = null;
  | 
  |         try{
  |             InitialContext ctx = new InitialContext();
  |             TopicConnectionFactory factory = 
(TopicConnectionFactory)ctx.lookup("ConnectionFactory");
  |             Topic topic = (Topic)ctx.lookup("topic/powerControlTopic");
  |             connection = factory.createTopicConnection();
  |             connection.start();
  | 
  |             TopicSession session = connection.createTopicSession(false, 
Session.AUTO_ACKNOWLEDGE);
  |             connection.setExceptionListener(this);
  | 
  |             String filter = buildFilter();
  |             subscriber = session.createSubscriber(topic, filter, false);
  |         }
  |         catch(Exception e){
  |             throw new JMSException("Can't initialize: " + e);
  |         }
  |         subscriber.setMessageListener(this);
  |         return subscriber;
  |     }
  | 
  |     public synchronized void onException(JMSException jmse){
  |         System.out.println("onException(): " + jmse);
  |         try{
  |             connection.setExceptionListener(null);
  |             connection.stop();
  |             connection.close();
  |             connection = null;
  |             //topicSubscriber.close();
  |         }
  |         catch(JMSException je){
  | 
  |             je.printStackTrace();
  |         }
  |         System.out.println("Notifying ...");
  |         notify(); //trigger reconnect thread
  |     }
  | 
  |     public synchronized void doWait(){
  |         try{
  |             wait();
  |         }
  |         catch(InterruptedException e){e.printStackTrace();}
  |     }
  | }
  | 
  | 

View the original post : 
http://www.jboss.org/index.html?module=bb&op=viewtopic&p=3854353#3854353

Reply to the post : 
http://www.jboss.org/index.html?module=bb&op=posting&mode=reply&p=3854353


-------------------------------------------------------
This SF.Net email is sponsored by:
Sybase ASE Linux Express Edition - download now for FREE
LinuxWorld Reader's Choice Award Winner for best database on Linux.
http://ads.osdn.com/?ad_id=5588&alloc_id=12065&op=click
_______________________________________________
JBoss-user mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/jboss-user

Reply via email to