001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.samples.jms;
018:
019: import java.util.Hashtable;
020:
021: import javax.jms.DeliveryMode;
022: import javax.jms.JMSException;
023: import javax.jms.Session;
024: import javax.jms.TextMessage;
025: import javax.jms.Topic;
026: import javax.jms.TopicConnection;
027: import javax.jms.TopicConnectionFactory;
028: import javax.jms.TopicPublisher;
029: import javax.jms.TopicSession;
030: import javax.naming.Context;
031: import javax.naming.InitialContext;
032: import javax.naming.NamingException;
033:
034: import org.hsqldb.Trigger;
035:
036: /**
037: * Example Trigger for HSQLDB doing cache invalidation through the eventcache
038: * block and JMS messages.
039: *
040: * @version CVS $Id: JMSTrigger.java 433543 2006-08-22 06:22:54Z crossley $
041: * @author <a href="mailto:haul@apache.org">haul</a>
042: */
043: public class JMSTrigger implements Trigger {
044:
045: // this exmaple class uses defaults to run with OpenJMS
046: // TODO make this somehow configurable...
047: protected String contextFactoryName = "org.exolab.jms.jndi.InitialContextFactory";
048: protected String scheme = "rmi";
049: protected String host = "localhost";
050: protected String port = "";
051: protected String jndiname = "";
052: protected String topicFactoryName = "JmsTopicConnectionFactory";
053: protected String topicName = "topic1";
054: protected int deliveryMode = DeliveryMode.NON_PERSISTENT;
055: protected int priority = 4;
056: protected long timeToLive = 10000;
057:
058: protected Topic topic = null;
059: protected TopicPublisher publisher = null;
060: protected TopicSession session = null;
061: protected TopicConnection connection = null;
062: protected Context context = null;
063: protected TopicConnectionFactory topicConnectionFactory = null;
064:
065: /**
066: *
067: */
068: public JMSTrigger() {
069: super ();
070: }
071:
072: /**
073: * Get initial context.
074: *
075: * @return initial context
076: * @throws NamingException
077: */
078: public Context getContext() throws NamingException {
079:
080: Hashtable properties = new Hashtable();
081:
082: properties.put(Context.INITIAL_CONTEXT_FACTORY,
083: this .contextFactoryName);
084:
085: if (this .port.length() == 0) {
086: if (scheme.equals("tcp") || scheme.equals("tcps")) {
087: port = "3035";
088: } else if (scheme.equals("http")) {
089: port = "8080";
090: } else if (scheme.equals("https")) {
091: port = "8443";
092: } else if (scheme.equals("rmi")) {
093: port = "1099";
094: }
095: }
096:
097: String name = "";
098: if (scheme.equals("rmi")) {
099: name = this .jndiname;
100: }
101:
102: String url = scheme + "://" + host + ":" + port + "/" + name;
103:
104: properties.put(Context.PROVIDER_URL, url);
105: return new InitialContext(properties);
106: }
107:
108: private void setupConnection() throws NamingException, JMSException {
109: // setup JMS connection
110: this .context = this .getContext();
111: this .topicConnectionFactory = (TopicConnectionFactory) this .context
112: .lookup(this .topicFactoryName);
113: this .connection = this .topicConnectionFactory
114: .createTopicConnection();
115: this .connection.start();
116: }
117:
118: private void setupSession() throws JMSException {
119: this .session = connection.createTopicSession(false,
120: Session.CLIENT_ACKNOWLEDGE);
121: this .topic = session.createTopic(this .topicName);
122: this .publisher = session.createPublisher(topic);
123: }
124:
125: private void connect() throws NamingException, JMSException {
126: if (this .connection == null)
127: this .setupConnection();
128: if (this .session == null)
129: this .setupSession();
130: }
131:
132: private void disconnect() throws JMSException, NamingException {
133: // do we really need to do this every time??
134: // OTOH we should expect to run this trigger rather infrequently.
135: this .session.close();
136: this .session = null;
137: this .connection.close();
138: this .connection = null;
139: this .topicConnectionFactory = null;
140: this .context.close();
141: this .context = null;
142: }
143:
144: /*
145: * @see org.hsqldb.Trigger#fire(java.lang.String, java.lang.String, java.lang.Object[])
146: */
147: public void fire(String trigName, String tabName, Object[] row) {
148: try {
149: connect();
150: TextMessage message = this .session
151: .createTextMessage(trigName.toLowerCase() + "|"
152: + tabName.toLowerCase());
153: this .publisher.publish(this .topic, message,
154: this .deliveryMode, this .priority, this .timeToLive);
155: disconnect();
156:
157: } catch (Exception e) {
158: e.printStackTrace();
159: }
160: }
161:
162: /* (non-Javadoc)
163: * @see org.hsqldb.Trigger#fire(int, java.lang.String, java.lang.String, java.lang.Object[], java.lang.Object[])
164: */
165: public void fire(int arg0, String arg1, String arg2, Object[] arg3,
166: Object[] arg4) {
167: // TODO Auto-generated method stub
168:
169: }
170: }
|