001: /*
002: * Copyright 2005 Joe Walker
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.getahead.dwrdemo.pubsub;
017:
018: import javax.jms.Connection;
019: import javax.jms.ConnectionFactory;
020: import javax.jms.ExceptionListener;
021: import javax.jms.IllegalStateException;
022: import javax.jms.JMSException;
023: import javax.jms.Message;
024: import javax.jms.MessageConsumer;
025: import javax.jms.MessageListener;
026: import javax.jms.MessageProducer;
027: import javax.jms.Session;
028: import javax.jms.TextMessage;
029: import javax.jms.Topic;
030:
031: import org.apache.commons.logging.Log;
032: import org.apache.commons.logging.LogFactory;
033:
034: /**
035: * A demo of the pub/sub side of JMS.
036: * Currently DWR does not support point-to-point JMS because there doesn't seem
037: * much point - web clients are inherently fickle, so getting the address of a
038: * browser out of JNDI seems silly
039: * @author Joe Walker [joe at getahead dot ltd dot uk]
040: */
041: public class JmsTest {
042: /**
043: * We start open
044: */
045: public JmsTest() throws JMSException {
046: open();
047: }
048:
049: /**
050: * Setup JMS and create a subscriber listener
051: * This method could be exported to the web if needed, but it probably
052: * doesn't make much sense unless it's in an admin role
053: */
054: protected synchronized void open() throws JMSException {
055: getConnection();
056: connection.setExceptionListener(new ExceptionListener() {
057: public void onException(JMSException ex) {
058: log.warn("JMS Failure", ex);
059: }
060: });
061: session = connection.createSession(false,
062: Session.AUTO_ACKNOWLEDGE);
063:
064: // Setup Pub/Sub
065: topic = getTopic();
066: topicProducer = session.createProducer(topic);
067: topicConsumer = session.createConsumer(topic);
068:
069: topicConsumer.setMessageListener(new MessageListener() {
070: public void onMessage(Message message) {
071: if (message instanceof TextMessage) {
072: TextMessage textMessage = (TextMessage) message;
073: try {
074: log.info(textMessage.getText());
075: } catch (JMSException ex) {
076: log
077: .error(
078: "Failed to get text from message",
079: ex);
080: }
081: } else {
082: log
083: .info("Not sure how to convert message to string for type "
084: + message.getClass().getName());
085: }
086: }
087: });
088:
089: connection.start();
090: open = true;
091: }
092:
093: /**
094: * Generally this would be done by JNDI etc
095: */
096: protected Connection getConnection() throws JMSException {
097: try {
098: Class<?> factoryClass = Class.forName(FACTORY_CLASSNAME);
099: ConnectionFactory factory = (ConnectionFactory) factoryClass
100: .newInstance();
101: connection = factory.createConnection();
102: connection.setClientID(CLIENT_ID);
103: return connection;
104: } catch (JMSException ex) {
105: throw ex;
106: } catch (Exception ex) {
107: throw new JMSException(ex.toString());
108: }
109: }
110:
111: /**
112: * Generally this would be done by JNDI etc
113: */
114: protected Topic getTopic() throws JMSException {
115: return session.createTopic(TOPIC_NAME);
116: }
117:
118: /**
119: * Exported method to publish a message to a topic
120: */
121: public synchronized void publish(String data) throws JMSException {
122: if (!open) {
123: throw new IllegalStateException("JmsTest is closed.");
124: }
125:
126: String info = "JmsTest says " + data;
127: log.info("Publishing message '" + info + "' to '"
128: + topic.getTopicName() + "'");
129:
130: TextMessage message = session.createTextMessage(info);
131: topicProducer.send(message);
132: }
133:
134: /**
135: * Close down the JMS connection
136: * This method could be exported to the web if needed, but it probably
137: * doesn't make much sense unless it's in an admin role
138: */
139: public synchronized void close() throws JMSException {
140: topicConsumer.close();
141: topicProducer.close();
142:
143: session.close();
144: connection.close();
145: open = false;
146: }
147:
148: /**
149: * The Default Connection Factory
150: */
151: private static final String FACTORY_CLASSNAME = "org.directwebremoting.jms.DwrConnectionFactory";
152:
153: /**
154: * The default implementation just hard codes a topic name
155: */
156: private static final String TOPIC_NAME = "org.getahead.dwrdemo.pubsub.testtopic";
157:
158: /**
159: * The default implementation just hard codes a client id
160: */
161: private static final String CLIENT_ID = "org.getahead.dwrdemo.pubsub.testservice";
162:
163: /**
164: * Are we open for business
165: */
166: private boolean open = false;
167:
168: /**
169: * The JMS connection
170: */
171: private Connection connection;
172:
173: /**
174: * The train of messages that we send/receive
175: */
176: private Session session;
177:
178: /**
179: * The topic that we subscribe/publish to
180: */
181: private Topic topic;
182:
183: /**
184: * The route to publishing messages to the topic
185: */
186: private MessageProducer topicProducer;
187:
188: /**
189: * The route to getting to messages sent to the topic
190: */
191: protected MessageConsumer topicConsumer;
192:
193: /**
194: * The log stream
195: */
196: protected static final Log log = LogFactory.getLog(JmsTest.class);
197:
198: static {
199: try {
200: org.directwebremoting.jms.DwrConnectionFactory factory = new org.directwebremoting.jms.DwrConnectionFactory();
201: org.directwebremoting.jms.DwrConnection connection = factory
202: .createConnection();
203: connection.setClientID(CLIENT_ID);
204: connection.setExceptionListener(new ExceptionListener() {
205: public void onException(JMSException ex) {
206: log.warn("JMS Failure", ex);
207: }
208: });
209: org.directwebremoting.jms.DwrSession session = connection
210: .createSession(false, Session.AUTO_ACKNOWLEDGE);
211: org.directwebremoting.jms.DwrTopic topic = session
212: .createTopic(TOPIC_NAME);
213:
214: org.directwebremoting.jms.DwrMessageProducer topicProducer = session
215: .createProducer(topic);
216: org.directwebremoting.jms.DwrMessageConsumer topicConsumer = session
217: .createConsumer(topic);
218: topicConsumer.setMessageListener(new MessageListener() {
219: public void onMessage(Message message) {
220: if (message instanceof TextMessage) {
221: TextMessage textMessage = (TextMessage) message;
222: try {
223: log.info(textMessage.getText());
224: } catch (JMSException ex) {
225: log.error(
226: "Failed to get text from message",
227: ex);
228: }
229: } else {
230: log
231: .info("Not sure how to convert message to string for type "
232: + message.getClass().getName());
233: }
234: }
235: });
236: connection.start();
237:
238: String info = "JmsTest says nothing";
239: log.info("Publishing message '" + info + "' to '"
240: + topic.getTopicName() + "'");
241:
242: org.directwebremoting.jms.DwrMessage message = session
243: .createTextMessage(info);
244: topicProducer.send(message);
245:
246: topicConsumer.close();
247: topicProducer.close();
248:
249: session.close();
250: connection.close();
251: } catch (JMSException ex) {
252: }
253: }
254: }
|