home *** CD-ROM | disk | FTP | other *** search
- package org.apache.cocoon.samples.jms;
-
- import java.util.Hashtable;
- import javax.jms.JMSException;
- import javax.jms.TextMessage;
- import javax.jms.Topic;
- import javax.jms.TopicConnection;
- import javax.jms.TopicConnectionFactory;
- import javax.jms.TopicPublisher;
- import javax.jms.TopicSession;
- import javax.naming.Context;
- import javax.naming.InitialContext;
- import javax.naming.NamingException;
- import org.hsqldb.Trigger;
-
- public class JMSTrigger implements Trigger {
- protected String contextFactoryName = "org.exolab.jms.jndi.InitialContextFactory";
- protected String scheme = "rmi";
- protected String host = "localhost";
- protected String port = "";
- protected String jndiname = "";
- protected String topicFactoryName = "JmsTopicConnectionFactory";
- protected String topicName = "topic1";
- protected int deliveryMode = 1;
- protected int priority = 4;
- protected long timeToLive = 10000L;
- protected Topic topic = null;
- protected TopicPublisher publisher = null;
- protected TopicSession session = null;
- protected TopicConnection connection = null;
- protected Context context = null;
- protected TopicConnectionFactory topicConnectionFactory = null;
-
- public Context getContext() throws NamingException {
- Hashtable properties = new Hashtable();
- properties.put("java.naming.factory.initial", this.contextFactoryName);
- if (this.port.equals("")) {
- if (!this.scheme.equals("tcp") && !this.scheme.equals("tcps")) {
- if (this.scheme.equals("http")) {
- this.port = "8080";
- } else if (this.scheme.equals("https")) {
- this.port = "8443";
- } else if (this.scheme.equals("rmi")) {
- this.port = "1099";
- }
- } else {
- this.port = "3035";
- }
- }
-
- String name = "";
- if (this.scheme.equals("rmi")) {
- name = this.jndiname;
- }
-
- String url = this.scheme + "://" + this.host + ":" + this.port + "/" + name;
- properties.put("java.naming.provider.url", url);
- return new InitialContext(properties);
- }
-
- private void setupConnection() throws NamingException, JMSException {
- this.context = this.getContext();
- this.topicConnectionFactory = (TopicConnectionFactory)this.context.lookup(this.topicFactoryName);
- this.connection = this.topicConnectionFactory.createTopicConnection();
- this.connection.start();
- }
-
- private void setupSession() throws JMSException {
- this.session = this.connection.createTopicSession(false, 2);
- this.topic = this.session.createTopic(this.topicName);
- this.publisher = this.session.createPublisher(this.topic);
- }
-
- private void connect() throws NamingException, JMSException {
- if (this.connection == null) {
- this.setupConnection();
- }
-
- if (this.session == null) {
- this.setupSession();
- }
-
- }
-
- private void disconnect() throws JMSException, NamingException {
- this.session.close();
- this.session = null;
- this.connection.close();
- this.connection = null;
- this.topicConnectionFactory = null;
- this.context.close();
- this.context = null;
- }
-
- public void fire(String trigName, String tabName, Object[] row) {
- try {
- this.connect();
- TextMessage message = this.session.createTextMessage(trigName.toLowerCase() + "|" + tabName.toLowerCase());
- this.publisher.publish(this.topic, message, this.deliveryMode, this.priority, this.timeToLive);
- this.disconnect();
- } catch (Exception e) {
- e.printStackTrace();
- }
-
- }
- }
-