Source Code Cross Referenced for JMS10BroadcastingListener.java in  » Cache » OSCache » com » opensymphony » oscache » plugins » clustersupport » 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
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Cache » OSCache » com.opensymphony.oscache.plugins.clustersupport 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright (c) 2002-2003 by OpenSymphony
003:         * All rights reserved.
004:         */
005:        package com.opensymphony.oscache.plugins.clustersupport;
006:
007:        import com.opensymphony.oscache.base.Cache;
008:        import com.opensymphony.oscache.base.Config;
009:        import com.opensymphony.oscache.base.FinalizationException;
010:        import com.opensymphony.oscache.base.InitializationException;
011:
012:        import org.apache.commons.logging.Log;
013:        import org.apache.commons.logging.LogFactory;
014:
015:        import javax.jms.*;
016:
017:        import javax.naming.InitialContext;
018:
019:        /**
020:         * A JMS 1.0.x based clustering implementation. This implementation is independent of
021:         * the JMS provider and uses non-persistent messages on a publish subscribe protocol.
022:         *
023:         * @author <a href="&#109;a&#105;&#108;&#116;&#111;:chris&#64;swebtec.&#99;&#111;&#109;">Chris Miller</a>
024:         */
025:        public class JMS10BroadcastingListener extends
026:                AbstractBroadcastingListener {
027:            private final static Log log = LogFactory
028:                    .getLog(JMS10BroadcastingListener.class);
029:
030:            /**
031:             * The name of this cluster. Used to identify the sender of a message.
032:             */
033:            private String clusterNode;
034:
035:            /**
036:             *The JMS connection used
037:             */
038:            private TopicConnection connection;
039:
040:            /**
041:             * Th object used to publish new messages
042:             */
043:            private TopicPublisher publisher;
044:
045:            /**
046:             * The current JMS session
047:             */
048:            private TopicSession publisherSession;
049:
050:            /**
051:             * <p>Called by the cache administrator class when a cache is instantiated.</p>
052:             * <p>The JMS broadcasting implementation requires the following configuration
053:             * properties to be specified in <code>oscache.properties</code>:
054:             * <ul>
055:             * <li><b>cache.cluster.jms.topic.factory</b> - The JMS connection factory to use</li>
056:             * <li><b>cache.cluster.jms.topic.name</b> - The JMS topic name</li>
057:             * <li><b>cache.cluster.jms.node.name</b> - The name of this node in the cluster. This should
058:             * be unique for each node.</li>
059:             * Please refer to the clustering documentation for further details on configuring
060:             * the JMS clustered caching.</p>
061:             *
062:             * @param cache the cache instance that this listener is attached to.
063:             *
064:             * @throws InitializationException thrown when there was a
065:             * problem initializing the listener. The cache administrator will log this error and
066:             * disable the listener.
067:             */
068:            public void initialize(Cache cache, Config config)
069:                    throws InitializationException {
070:                super .initialize(cache, config);
071:
072:                // Get the name of this node
073:                clusterNode = config.getProperty("cache.cluster.jms.node.name");
074:
075:                String topic = config
076:                        .getProperty("cache.cluster.jms.topic.name");
077:                String topicFactory = config
078:                        .getProperty("cache.cluster.jms.topic.factory");
079:
080:                if (log.isInfoEnabled()) {
081:                    log.info("Starting JMS clustering (node name="
082:                            + clusterNode + ", topic=" + topic
083:                            + ", topic factory=" + topicFactory + ")");
084:                }
085:
086:                try {
087:                    // Make sure you have specified the necessary JNDI properties (usually in
088:                    // a jndi.properties resource file, or as system properties)
089:                    InitialContext jndi = new InitialContext();
090:
091:                    // Look up a JMS connection factory
092:                    TopicConnectionFactory connectionFactory = (TopicConnectionFactory) jndi
093:                            .lookup(topicFactory);
094:
095:                    // Create a JMS connection
096:                    connection = connectionFactory.createTopicConnection();
097:
098:                    // Create session objects
099:                    publisherSession = connection.createTopicSession(false,
100:                            Session.AUTO_ACKNOWLEDGE);
101:
102:                    TopicSession subSession = connection.createTopicSession(
103:                            false, Session.AUTO_ACKNOWLEDGE);
104:
105:                    // Look up the JMS topic
106:                    Topic chatTopic = (Topic) jndi.lookup(topic);
107:
108:                    // Create the publisher and subscriber
109:                    publisher = publisherSession.createPublisher(chatTopic);
110:
111:                    TopicSubscriber subscriber = subSession
112:                            .createSubscriber(chatTopic);
113:
114:                    // Set the message listener
115:                    subscriber.setMessageListener(new MessageListener() {
116:                        public void onMessage(Message message) {
117:                            try {
118:                                //check the message type
119:                                ObjectMessage objectMessage = null;
120:
121:                                if (!(message instanceof  ObjectMessage)) {
122:                                    log
123:                                            .error("Cannot handle message of type (class="
124:                                                    + message.getClass()
125:                                                            .getName()
126:                                                    + "). Notification ignored.");
127:                                    return;
128:                                }
129:
130:                                objectMessage = (ObjectMessage) message;
131:
132:                                //check the message content
133:                                if (!(objectMessage.getObject() instanceof  ClusterNotification)) {
134:                                    log
135:                                            .error("An unknown cluster notification message received (class="
136:                                                    + objectMessage.getObject()
137:                                                            .getClass()
138:                                                            .getName()
139:                                                    + "). Notification ignored.");
140:                                    return;
141:                                }
142:
143:                                if (log.isDebugEnabled()) {
144:                                    log.debug(objectMessage.getObject());
145:                                }
146:
147:                                // This prevents the notification sent by this node from being handled by itself
148:                                if (!objectMessage
149:                                        .getStringProperty("nodeName").equals(
150:                                                clusterNode)) {
151:                                    //now handle the message
152:                                    ClusterNotification notification = (ClusterNotification) objectMessage
153:                                            .getObject();
154:                                    handleClusterNotification(notification);
155:                                }
156:                            } catch (JMSException jmsEx) {
157:                                log.error("Cannot handle cluster Notification",
158:                                        jmsEx);
159:                            }
160:                        }
161:                    });
162:
163:                    // Start the JMS connection; allows messages to be delivered
164:                    connection.start();
165:                } catch (Exception e) {
166:                    throw new InitializationException(
167:                            "Initialization of the JMS10BroadcastingListener failed: "
168:                                    + e);
169:                }
170:            }
171:
172:            /**
173:             * Called by the cache administrator class when a cache is destroyed.
174:             *
175:             * @throws FinalizationException thrown when there was a problem finalizing the
176:             * listener. The cache administrator will catch and log this error.
177:             */
178:            public void finialize() throws FinalizationException {
179:                try {
180:                    if (log.isInfoEnabled()) {
181:                        log.info("Shutting down JMS clustering...");
182:                    }
183:
184:                    connection.close();
185:
186:                    if (log.isInfoEnabled()) {
187:                        log.info("JMS clustering shutdown complete.");
188:                    }
189:                } catch (JMSException e) {
190:                    log
191:                            .warn(
192:                                    "A problem was encountered when closing the JMS connection",
193:                                    e);
194:                }
195:            }
196:
197:            protected void sendNotification(ClusterNotification message) {
198:                try {
199:                    ObjectMessage objectMessage = publisherSession
200:                            .createObjectMessage();
201:                    objectMessage.setObject(message);
202:
203:                    //sign the message, with the name of this node
204:                    objectMessage.setStringProperty("nodeName", clusterNode);
205:                    publisher.publish(objectMessage);
206:                } catch (JMSException e) {
207:                    log.error("Cannot send notification " + message, e);
208:                }
209:            }
210:        }
www.java2java.com | Contact Us
Copyright 2010 - 2030 Java Source and Support. All rights reserved.
All other trademarks are property of their respective owners.