KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > samples > jms > JMSTrigger


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.samples.jms;
17
18 import java.util.Hashtable JavaDoc;
19
20 import javax.jms.DeliveryMode JavaDoc;
21 import javax.jms.JMSException JavaDoc;
22 import javax.jms.Session JavaDoc;
23 import javax.jms.TextMessage JavaDoc;
24 import javax.jms.Topic JavaDoc;
25 import javax.jms.TopicConnection JavaDoc;
26 import javax.jms.TopicConnectionFactory JavaDoc;
27 import javax.jms.TopicPublisher JavaDoc;
28 import javax.jms.TopicSession JavaDoc;
29 import javax.naming.Context JavaDoc;
30 import javax.naming.InitialContext JavaDoc;
31 import javax.naming.NamingException JavaDoc;
32
33 import org.hsqldb.Trigger;
34
35 /**
36  * Example Trigger for HSQLDB doing cache invalidation through the eventcache
37  * block and JMS messages.
38  *
39  * @version CVS $Id: JMSTrigger.java 37202 2004-08-30 14:04:43Z cziegeler $
40  * @author <a HREF="mailto:haul@apache.org">haul</a>
41  */

42 public class JMSTrigger implements Trigger {
43
44     // this exmaple class uses defaults to run with OpenJMS
45
// TODO make this somehow configurable...
46
protected String JavaDoc contextFactoryName = "org.exolab.jms.jndi.InitialContextFactory";
47     protected String JavaDoc scheme = "rmi";
48     protected String JavaDoc host = "localhost";
49     protected String JavaDoc port = "";
50     protected String JavaDoc jndiname = "";
51     protected String JavaDoc topicFactoryName = "JmsTopicConnectionFactory";
52     protected String JavaDoc topicName = "topic1";
53     protected int deliveryMode = DeliveryMode.NON_PERSISTENT;
54     protected int priority = 4;
55     protected long timeToLive = 10000;
56
57     protected Topic JavaDoc topic = null;
58     protected TopicPublisher JavaDoc publisher = null;
59     protected TopicSession JavaDoc session = null;
60     protected TopicConnection JavaDoc connection = null;
61     protected Context JavaDoc context = null;
62     protected TopicConnectionFactory JavaDoc topicConnectionFactory = null;
63
64     /**
65      *
66      */

67     public JMSTrigger() {
68         super();
69     }
70
71     /**
72      * Get initial context.
73      *
74      * @return initial context
75      * @throws NamingException
76      */

77     public Context JavaDoc getContext() throws NamingException JavaDoc {
78
79         Hashtable JavaDoc properties = new Hashtable JavaDoc();
80
81         properties.put(Context.INITIAL_CONTEXT_FACTORY, this.contextFactoryName);
82
83         if (this.port.equals("")) {
84             if (scheme.equals("tcp") || scheme.equals("tcps")) {
85                 port = "3035";
86             } else if (scheme.equals("http")) {
87                 port = "8080";
88             } else if (scheme.equals("https")) {
89                 port = "8443";
90             } else if (scheme.equals("rmi")) {
91                 port = "1099";
92             }
93         }
94
95         String JavaDoc name = "";
96         if (scheme.equals("rmi")) {
97             name = this.jndiname;
98         }
99
100         String JavaDoc url = scheme + "://" + host + ":" + port + "/" + name;
101
102         properties.put(Context.PROVIDER_URL, url);
103         return new InitialContext JavaDoc(properties);
104     }
105
106     private void setupConnection() throws NamingException JavaDoc, JMSException JavaDoc {
107         // setup JMS connection
108
this.context = this.getContext();
109         this.topicConnectionFactory = (TopicConnectionFactory JavaDoc) this.context.lookup(this.topicFactoryName);
110         this.connection = this.topicConnectionFactory.createTopicConnection();
111         this.connection.start();
112     }
113
114     private void setupSession() throws JMSException JavaDoc {
115         this.session = connection.createTopicSession(false, Session.CLIENT_ACKNOWLEDGE);
116         this.topic = session.createTopic(this.topicName);
117         this.publisher = session.createPublisher(topic);
118     }
119
120     private void connect() throws NamingException JavaDoc, JMSException JavaDoc {
121         if (this.connection == null)
122             this.setupConnection();
123         if (this.session == null)
124             this.setupSession();
125     }
126
127     private void disconnect() throws JMSException JavaDoc, NamingException JavaDoc {
128         // do we really need to do this every time??
129
// OTOH we should expect to run this trigger rather infrequently.
130
this.session.close();
131         this.session = null;
132         this.connection.close();
133         this.connection = null;
134         this.topicConnectionFactory = null;
135         this.context.close();
136         this.context = null;
137     }
138
139     /*
140      * @see org.hsqldb.Trigger#fire(java.lang.String, java.lang.String, java.lang.Object[])
141      */

142     public void fire(String JavaDoc trigName, String JavaDoc tabName, Object JavaDoc[] row) {
143         try {
144             connect();
145             TextMessage JavaDoc message =
146                 this.session.createTextMessage(
147                     trigName.toLowerCase() + "|" + tabName.toLowerCase());
148             this.publisher.publish(
149                 this.topic,
150                 message,
151                 this.deliveryMode,
152                 this.priority,
153                 this.timeToLive);
154             disconnect();
155
156         } catch (Exception JavaDoc e) {
157             e.printStackTrace();
158         }
159     }
160
161     /* (non-Javadoc)
162      * @see org.hsqldb.Trigger#fire(int, java.lang.String, java.lang.String, java.lang.Object[], java.lang.Object[])
163      */

164     public void fire(int arg0, String JavaDoc arg1, String JavaDoc arg2, Object JavaDoc[] arg3, Object JavaDoc[] arg4) {
165         // TODO Auto-generated method stub
166

167     }
168 }
Popular Tags