1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| package com.opensymphony.oscache.plugins.clustersupport; |
6 |
| |
7 |
| import com.opensymphony.oscache.base.Cache; |
8 |
| import com.opensymphony.oscache.base.Config; |
9 |
| import com.opensymphony.oscache.base.FinalizationException; |
10 |
| import com.opensymphony.oscache.base.InitializationException; |
11 |
| |
12 |
| import org.apache.commons.logging.Log; |
13 |
| import org.apache.commons.logging.LogFactory; |
14 |
| |
15 |
| import javax.jms.*; |
16 |
| |
17 |
| import javax.naming.InitialContext; |
18 |
| import javax.naming.NamingException; |
19 |
| |
20 |
| |
21 |
| |
22 |
| |
23 |
| |
24 |
| |
25 |
| |
26 |
| public class JMSBroadcastingListener extends AbstractBroadcastingListener { |
27 |
| |
28 |
| private final static Log log = LogFactory.getLog(JMSBroadcastingListener.class); |
29 |
| |
30 |
| |
31 |
| |
32 |
| |
33 |
| private Connection connection; |
34 |
| |
35 |
| |
36 |
| |
37 |
| |
38 |
| private MessageProducer messagePublisher; |
39 |
| |
40 |
| |
41 |
| |
42 |
| |
43 |
| private Session publisherSession; |
44 |
| |
45 |
| |
46 |
| |
47 |
| |
48 |
| private String clusterNode; |
49 |
| |
50 |
| |
51 |
| |
52 |
| |
53 |
| |
54 |
| |
55 |
| |
56 |
| |
57 |
| |
58 |
| |
59 |
| |
60 |
| |
61 |
| |
62 |
| |
63 |
| |
64 |
| |
65 |
| |
66 |
| |
67 |
| |
68 |
0
| public void initialize(Cache cache, Config config) throws InitializationException {
|
69 |
0
| super.initialize(cache, config);
|
70 |
| |
71 |
| |
72 |
0
| clusterNode = config.getProperty("cache.cluster.jms.node.name");
|
73 |
| |
74 |
0
| String topic = config.getProperty("cache.cluster.jms.topic.name");
|
75 |
0
| String topicFactory = config.getProperty("cache.cluster.jms.topic.factory");
|
76 |
| |
77 |
0
| if (log.isInfoEnabled()) {
|
78 |
0
| log.info("Starting JMS clustering (node name=" + clusterNode + ", topic=" + topic + ", topic factory=" + topicFactory + ")");
|
79 |
| } |
80 |
| |
81 |
0
| try {
|
82 |
| |
83 |
| |
84 |
0
| InitialContext jndi = getInitialContext();
|
85 |
| |
86 |
| |
87 |
0
| ConnectionFactory connectionFactory = (ConnectionFactory) jndi.lookup(topicFactory);
|
88 |
| |
89 |
| |
90 |
0
| connection = connectionFactory.createConnection();
|
91 |
| |
92 |
| |
93 |
0
| publisherSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
|
94 |
| |
95 |
0
| Session subSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
|
96 |
| |
97 |
| |
98 |
0
| Topic chatTopic = (Topic) jndi.lookup(topic);
|
99 |
| |
100 |
| |
101 |
0
| messagePublisher = publisherSession.createProducer(chatTopic);
|
102 |
| |
103 |
0
| MessageConsumer messageConsumer = subSession.createConsumer(chatTopic);
|
104 |
| |
105 |
| |
106 |
0
| messageConsumer.setMessageListener(new MessageListener() {
|
107 |
0
| public void onMessage(Message message) {
|
108 |
0
| try {
|
109 |
| |
110 |
0
| ObjectMessage objectMessage = null;
|
111 |
| |
112 |
0
| if (!(message instanceof ObjectMessage)) {
|
113 |
0
| log.error("Cannot handle message of type (class=" + message.getClass().getName() + "). Notification ignored.");
|
114 |
0
| return;
|
115 |
| } |
116 |
| |
117 |
0
| objectMessage = (ObjectMessage) message;
|
118 |
| |
119 |
| |
120 |
0
| if (!(objectMessage.getObject() instanceof ClusterNotification)) {
|
121 |
0
| log.error("An unknown cluster notification message received (class=" + objectMessage.getObject().getClass().getName() + "). Notification ignored.");
|
122 |
0
| return;
|
123 |
| } |
124 |
| |
125 |
0
| if (log.isDebugEnabled()) {
|
126 |
0
| log.debug(objectMessage.getObject());
|
127 |
| } |
128 |
| |
129 |
| |
130 |
0
| if (!objectMessage.getStringProperty("nodeName").equals(clusterNode)) {
|
131 |
| |
132 |
0
| ClusterNotification notification = (ClusterNotification) objectMessage.getObject();
|
133 |
0
| handleClusterNotification(notification);
|
134 |
| } |
135 |
| } catch (JMSException jmsEx) { |
136 |
0
| log.error("Cannot handle cluster Notification", jmsEx);
|
137 |
| } |
138 |
| } |
139 |
| }); |
140 |
| |
141 |
| |
142 |
0
| connection.start();
|
143 |
| } catch (Exception e) { |
144 |
0
| throw new InitializationException("Initialization of the JMSBroadcastingListener failed: " + e);
|
145 |
| } |
146 |
| } |
147 |
| |
148 |
| |
149 |
| |
150 |
| |
151 |
| |
152 |
| |
153 |
| |
154 |
0
| public void finialize() throws FinalizationException {
|
155 |
0
| try {
|
156 |
0
| if (log.isInfoEnabled()) {
|
157 |
0
| log.info("Shutting down JMS clustering...");
|
158 |
| } |
159 |
| |
160 |
0
| connection.close();
|
161 |
| |
162 |
0
| if (log.isInfoEnabled()) {
|
163 |
0
| log.info("JMS clustering shutdown complete.");
|
164 |
| } |
165 |
| } catch (JMSException e) { |
166 |
0
| log.warn("A problem was encountered when closing the JMS connection", e);
|
167 |
| } |
168 |
| } |
169 |
| |
170 |
0
| protected void sendNotification(ClusterNotification message) {
|
171 |
0
| try {
|
172 |
0
| ObjectMessage objectMessage = publisherSession.createObjectMessage();
|
173 |
0
| objectMessage.setObject(message);
|
174 |
| |
175 |
| |
176 |
0
| objectMessage.setStringProperty("nodeName", clusterNode);
|
177 |
0
| messagePublisher.send(objectMessage);
|
178 |
| } catch (JMSException e) { |
179 |
0
| log.error("Cannot send notification " + message, e);
|
180 |
| } |
181 |
| } |
182 |
| |
183 |
| |
184 |
| |
185 |
| |
186 |
| |
187 |
0
| protected InitialContext getInitialContext() throws NamingException {
|
188 |
0
| return new InitialContext();
|
189 |
| } |
190 |
| |
191 |
| } |