Source Code Cross Referenced for JmsTest.java in  » Ajax » dwr » org » getahead » dwrdemo » pubsub » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Ajax » dwr » org.getahead.dwrdemo.pubsub 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.