KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > mq > il > ha > examples > HAJMSClient


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.mq.il.ha.examples;
23
24 import javax.jms.ExceptionListener JavaDoc;
25 import javax.jms.JMSException JavaDoc;
26 import javax.jms.Message JavaDoc;
27 import javax.jms.MessageListener JavaDoc;
28 import javax.jms.Queue JavaDoc;
29 import javax.jms.QueueConnection JavaDoc;
30 import javax.jms.QueueConnectionFactory JavaDoc;
31 import javax.jms.QueueReceiver JavaDoc;
32 import javax.jms.QueueSender JavaDoc;
33 import javax.jms.QueueSession JavaDoc;
34 import javax.jms.TextMessage JavaDoc;
35 import javax.jms.Topic JavaDoc;
36 import javax.jms.TopicConnection JavaDoc;
37 import javax.jms.TopicConnectionFactory JavaDoc;
38 import javax.jms.TopicPublisher JavaDoc;
39 import javax.jms.TopicSession JavaDoc;
40 import javax.jms.TopicSubscriber JavaDoc;
41 import javax.naming.InitialContext JavaDoc;
42 import javax.naming.NamingException JavaDoc;
43
44 import org.jboss.logging.Logger;
45 import org.jboss.system.ServiceMBeanSupport;
46
47 /**
48  *
49  * Helps to manually test the HAIL
50  *
51  * @author Ivelin Ivanov <ivelin@apache.org>
52  *
53  */

54 public class HAJMSClient extends ServiceMBeanSupport
55   implements MessageListener JavaDoc, ExceptionListener JavaDoc, HAJMSClientMBean
56 {
57
58   /**
59    * create connection, sessions and subscribe for topic and queue
60    */

61   protected void startService() throws Exception JavaDoc
62   {
63     connect();
64   }
65    
66   /**
67    * unsubscribe from topic, queue,
68    * stop sessions and connection
69    */

70   protected void stopService() throws Exception JavaDoc
71   {
72     disconnect();
73   }
74
75   /**
76    * Acknowledges connenction exception.
77    * Should be invoked every time the HAIL singleton moves.
78    */

79   public void onException(JMSException JavaDoc connEx)
80   {
81     log.info("Notification received by ExceptionListener. Singleton Probably Moved.");
82     try
83     {
84       reconnect();
85     }
86     catch (Exception JavaDoc e)
87     {
88       e.printStackTrace();
89     }
90     finally
91     {
92       connectionException_ = connEx;
93     }
94   }
95
96   protected void reconnect() throws NamingException JavaDoc, JMSException JavaDoc
97   {
98     log.info("Reconnecting");
99     try
100     {
101       disconnect();
102     }
103     finally
104     {
105       connect();
106     }
107   }
108
109   public void connect() throws NamingException JavaDoc, JMSException JavaDoc
110   {
111     log.info("Connecting");
112
113     InitialContext JavaDoc iniCtx = new InitialContext JavaDoc();
114     Object JavaDoc tmp = iniCtx.lookup("HAILXAConnectionFactory");
115     
116     TopicConnectionFactory JavaDoc tcf = (TopicConnectionFactory JavaDoc)tmp;
117     topicConn_ = tcf.createTopicConnection();
118     topic_ = (Topic JavaDoc)iniCtx.lookup("topic/testTopic");
119     topicSession_ = topicConn_.createTopicSession(false, TopicSession.AUTO_ACKNOWLEDGE);
120     topicConn_.setExceptionListener(this);
121     topicSub_ = topicSession_.createSubscriber(topic_);
122     topicSub_.setMessageListener( this );
123     topicPub_ = topicSession_.createPublisher(topic_);
124     topicConn_.start();
125
126     QueueConnectionFactory JavaDoc qcf = (QueueConnectionFactory JavaDoc)tmp;
127     qConn_ = qcf.createQueueConnection();
128     q_ = (Queue JavaDoc)iniCtx.lookup("queue/testQueue");
129     qSession_ = qConn_.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);
130     qRecv_ = qSession_.createReceiver(q_);
131     qRecv_.setMessageListener( this );
132     qSend_ = qSession_.createSender(q_);
133     qConn_.start();
134
135     log.info("Connected");
136   }
137
138   public void disconnect() throws JMSException JavaDoc
139   {
140     if (topicConn_ == null) return;
141
142     log.info("Disconnecting");
143
144     connectionException_ = null;
145     
146     try
147     {
148     topicConn_.setExceptionListener(null);
149
150     topicSub_.close();
151     topicPub_.close();
152     topicConn_.stop();
153     topicSession_.close();
154     
155     qRecv_.close();
156     qSend_.close();
157     qConn_.stop();
158     qSession_.close();
159     }
160     finally
161     {
162       try
163       {
164         topicConn_.close();
165       }
166       finally
167       {
168         topicConn_ = null;
169         try
170         {
171           qConn_.close();
172         }
173         finally
174         {
175           qConn_ = null;
176         }
177       }
178     
179     }
180     log.info("Disconnected");
181   }
182   
183   /**
184    * Handle JMS message
185    *
186    * @see javax.jms.MessageListener#onMessage(javax.jms.Message)
187    */

188   public void onMessage(Message JavaDoc msg)
189   {
190     lastMessage_ = (TextMessage JavaDoc)msg;
191     log.info("Message received: " + msg);
192   }
193   
194   public String JavaDoc getLastMessage() throws JMSException JavaDoc
195   {
196     if (lastMessage_ == null) return null;
197     return lastMessage_.getText();
198   }
199   
200   public String JavaDoc getConnectionException()
201   {
202     if (connectionException_ == null) return null;
203     return connectionException_.toString();
204   }
205   
206   public void publishMessageToTopic(String JavaDoc text) throws JMSException JavaDoc
207   {
208     TextMessage JavaDoc msg = topicSession_.createTextMessage(text);
209     topicPub_.publish(msg);
210     log.info("HA JMS message published to topic: " + text);
211   }
212   
213   public void sendMessageToQueue(String JavaDoc text) throws JMSException JavaDoc
214   {
215     TextMessage JavaDoc msg = qSession_.createTextMessage(text);
216     qSend_.send(msg);
217     log.info("HA JMS message sent to queue: " + text);
218   }
219
220   private Topic JavaDoc topic_;
221   private TopicSession JavaDoc topicSession_;
222   private TopicConnection JavaDoc topicConn_;
223   private JMSException JavaDoc connectionException_;
224   private TopicSubscriber JavaDoc topicSub_;
225   private TopicPublisher JavaDoc topicPub_;
226   private TextMessage JavaDoc lastMessage_;
227
228   private Queue JavaDoc q_;
229   private QueueConnection JavaDoc qConn_;
230   private QueueSession JavaDoc qSession_;
231   private QueueReceiver JavaDoc qRecv_;
232   private QueueSender JavaDoc qSend_;
233
234   private static Logger log = Logger.getLogger(HAJMSClient.class);
235   
236 }
237
Popular Tags