home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / xampp / xampp-cocoon-addon-1.4.9-installer.exe / JMSTrigger.class (.txt) < prev    next >
Encoding:
Java Class File  |  2004-07-12  |  4.1 KB  |  107 lines

  1. package org.apache.cocoon.samples.jms;
  2.  
  3. import java.util.Hashtable;
  4. import javax.jms.JMSException;
  5. import javax.jms.TextMessage;
  6. import javax.jms.Topic;
  7. import javax.jms.TopicConnection;
  8. import javax.jms.TopicConnectionFactory;
  9. import javax.jms.TopicPublisher;
  10. import javax.jms.TopicSession;
  11. import javax.naming.Context;
  12. import javax.naming.InitialContext;
  13. import javax.naming.NamingException;
  14. import org.hsqldb.Trigger;
  15.  
  16. public class JMSTrigger implements Trigger {
  17.    protected String contextFactoryName = "org.exolab.jms.jndi.InitialContextFactory";
  18.    protected String scheme = "rmi";
  19.    protected String host = "localhost";
  20.    protected String port = "";
  21.    protected String jndiname = "";
  22.    protected String topicFactoryName = "JmsTopicConnectionFactory";
  23.    protected String topicName = "topic1";
  24.    protected int deliveryMode = 1;
  25.    protected int priority = 4;
  26.    protected long timeToLive = 10000L;
  27.    protected Topic topic = null;
  28.    protected TopicPublisher publisher = null;
  29.    protected TopicSession session = null;
  30.    protected TopicConnection connection = null;
  31.    protected Context context = null;
  32.    protected TopicConnectionFactory topicConnectionFactory = null;
  33.  
  34.    public Context getContext() throws NamingException {
  35.       Hashtable properties = new Hashtable();
  36.       properties.put("java.naming.factory.initial", this.contextFactoryName);
  37.       if (this.port.equals("")) {
  38.          if (!this.scheme.equals("tcp") && !this.scheme.equals("tcps")) {
  39.             if (this.scheme.equals("http")) {
  40.                this.port = "8080";
  41.             } else if (this.scheme.equals("https")) {
  42.                this.port = "8443";
  43.             } else if (this.scheme.equals("rmi")) {
  44.                this.port = "1099";
  45.             }
  46.          } else {
  47.             this.port = "3035";
  48.          }
  49.       }
  50.  
  51.       String name = "";
  52.       if (this.scheme.equals("rmi")) {
  53.          name = this.jndiname;
  54.       }
  55.  
  56.       String url = this.scheme + "://" + this.host + ":" + this.port + "/" + name;
  57.       properties.put("java.naming.provider.url", url);
  58.       return new InitialContext(properties);
  59.    }
  60.  
  61.    private void setupConnection() throws NamingException, JMSException {
  62.       this.context = this.getContext();
  63.       this.topicConnectionFactory = (TopicConnectionFactory)this.context.lookup(this.topicFactoryName);
  64.       this.connection = this.topicConnectionFactory.createTopicConnection();
  65.       this.connection.start();
  66.    }
  67.  
  68.    private void setupSession() throws JMSException {
  69.       this.session = this.connection.createTopicSession(false, 2);
  70.       this.topic = this.session.createTopic(this.topicName);
  71.       this.publisher = this.session.createPublisher(this.topic);
  72.    }
  73.  
  74.    private void connect() throws NamingException, JMSException {
  75.       if (this.connection == null) {
  76.          this.setupConnection();
  77.       }
  78.  
  79.       if (this.session == null) {
  80.          this.setupSession();
  81.       }
  82.  
  83.    }
  84.  
  85.    private void disconnect() throws JMSException, NamingException {
  86.       this.session.close();
  87.       this.session = null;
  88.       this.connection.close();
  89.       this.connection = null;
  90.       this.topicConnectionFactory = null;
  91.       this.context.close();
  92.       this.context = null;
  93.    }
  94.  
  95.    public void fire(String trigName, String tabName, Object[] row) {
  96.       try {
  97.          this.connect();
  98.          TextMessage message = this.session.createTextMessage(trigName.toLowerCase() + "|" + tabName.toLowerCase());
  99.          this.publisher.publish(this.topic, message, this.deliveryMode, this.priority, this.timeToLive);
  100.          this.disconnect();
  101.       } catch (Exception e) {
  102.          e.printStackTrace();
  103.       }
  104.  
  105.    }
  106. }
  107.