001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.mq.il.ha.examples;
023:
024: import javax.jms.ExceptionListener;
025: import javax.jms.JMSException;
026: import javax.jms.Message;
027: import javax.jms.MessageListener;
028: import javax.jms.Queue;
029: import javax.jms.QueueConnection;
030: import javax.jms.QueueConnectionFactory;
031: import javax.jms.QueueReceiver;
032: import javax.jms.QueueSender;
033: import javax.jms.QueueSession;
034: import javax.jms.TextMessage;
035: import javax.jms.Topic;
036: import javax.jms.TopicConnection;
037: import javax.jms.TopicConnectionFactory;
038: import javax.jms.TopicPublisher;
039: import javax.jms.TopicSession;
040: import javax.jms.TopicSubscriber;
041: import javax.naming.InitialContext;
042: import javax.naming.NamingException;
043:
044: import org.jboss.logging.Logger;
045: import org.jboss.system.ServiceMBeanSupport;
046:
047: /**
048: *
049: * Helps to manually test the HAIL
050: *
051: * @author Ivelin Ivanov <ivelin@apache.org>
052: *
053: */
054: public class HAJMSClient extends ServiceMBeanSupport implements
055: MessageListener, ExceptionListener, HAJMSClientMBean {
056:
057: /**
058: * create connection, sessions and subscribe for topic and queue
059: */
060: protected void startService() throws Exception {
061: connect();
062: }
063:
064: /**
065: * unsubscribe from topic, queue,
066: * stop sessions and connection
067: */
068: protected void stopService() throws Exception {
069: disconnect();
070: }
071:
072: /**
073: * Acknowledges connenction exception.
074: * Should be invoked every time the HAIL singleton moves.
075: */
076: public void onException(JMSException connEx) {
077: log
078: .info("Notification received by ExceptionListener. Singleton Probably Moved.");
079: try {
080: reconnect();
081: } catch (Exception e) {
082: e.printStackTrace();
083: } finally {
084: connectionException_ = connEx;
085: }
086: }
087:
088: protected void reconnect() throws NamingException, JMSException {
089: log.info("Reconnecting");
090: try {
091: disconnect();
092: } finally {
093: connect();
094: }
095: }
096:
097: public void connect() throws NamingException, JMSException {
098: log.info("Connecting");
099:
100: InitialContext iniCtx = new InitialContext();
101: Object tmp = iniCtx.lookup("HAILXAConnectionFactory");
102:
103: TopicConnectionFactory tcf = (TopicConnectionFactory) tmp;
104: topicConn_ = tcf.createTopicConnection();
105: topic_ = (Topic) iniCtx.lookup("topic/testTopic");
106: topicSession_ = topicConn_.createTopicSession(false,
107: TopicSession.AUTO_ACKNOWLEDGE);
108: topicConn_.setExceptionListener(this );
109: topicSub_ = topicSession_.createSubscriber(topic_);
110: topicSub_.setMessageListener(this );
111: topicPub_ = topicSession_.createPublisher(topic_);
112: topicConn_.start();
113:
114: QueueConnectionFactory qcf = (QueueConnectionFactory) tmp;
115: qConn_ = qcf.createQueueConnection();
116: q_ = (Queue) iniCtx.lookup("queue/testQueue");
117: qSession_ = qConn_.createQueueSession(false,
118: QueueSession.AUTO_ACKNOWLEDGE);
119: qRecv_ = qSession_.createReceiver(q_);
120: qRecv_.setMessageListener(this );
121: qSend_ = qSession_.createSender(q_);
122: qConn_.start();
123:
124: log.info("Connected");
125: }
126:
127: public void disconnect() throws JMSException {
128: if (topicConn_ == null)
129: return;
130:
131: log.info("Disconnecting");
132:
133: connectionException_ = null;
134:
135: try {
136: topicConn_.setExceptionListener(null);
137:
138: topicSub_.close();
139: topicPub_.close();
140: topicConn_.stop();
141: topicSession_.close();
142:
143: qRecv_.close();
144: qSend_.close();
145: qConn_.stop();
146: qSession_.close();
147: } finally {
148: try {
149: topicConn_.close();
150: } finally {
151: topicConn_ = null;
152: try {
153: qConn_.close();
154: } finally {
155: qConn_ = null;
156: }
157: }
158:
159: }
160: log.info("Disconnected");
161: }
162:
163: /**
164: * Handle JMS message
165: *
166: * @see javax.jms.MessageListener#onMessage(javax.jms.Message)
167: */
168: public void onMessage(Message msg) {
169: lastMessage_ = (TextMessage) msg;
170: log.info("Message received: " + msg);
171: }
172:
173: public String getLastMessage() throws JMSException {
174: if (lastMessage_ == null)
175: return null;
176: return lastMessage_.getText();
177: }
178:
179: public String getConnectionException() {
180: if (connectionException_ == null)
181: return null;
182: return connectionException_.toString();
183: }
184:
185: public void publishMessageToTopic(String text) throws JMSException {
186: TextMessage msg = topicSession_.createTextMessage(text);
187: topicPub_.publish(msg);
188: log.info("HA JMS message published to topic: " + text);
189: }
190:
191: public void sendMessageToQueue(String text) throws JMSException {
192: TextMessage msg = qSession_.createTextMessage(text);
193: qSend_.send(msg);
194: log.info("HA JMS message sent to queue: " + text);
195: }
196:
197: private Topic topic_;
198: private TopicSession topicSession_;
199: private TopicConnection topicConn_;
200: private JMSException connectionException_;
201: private TopicSubscriber topicSub_;
202: private TopicPublisher topicPub_;
203: private TextMessage lastMessage_;
204:
205: private Queue q_;
206: private QueueConnection qConn_;
207: private QueueSession qSession_;
208: private QueueReceiver qRecv_;
209: private QueueSender qSend_;
210:
211: private static Logger log = Logger.getLogger(HAJMSClient.class);
212:
213: }
|