1 16 package org.apache.cocoon.samples.jms; 17 18 import java.util.Hashtable ; 19 20 import javax.jms.DeliveryMode ; 21 import javax.jms.JMSException ; 22 import javax.jms.Session ; 23 import javax.jms.TextMessage ; 24 import javax.jms.Topic ; 25 import javax.jms.TopicConnection ; 26 import javax.jms.TopicConnectionFactory ; 27 import javax.jms.TopicPublisher ; 28 import javax.jms.TopicSession ; 29 import javax.naming.Context ; 30 import javax.naming.InitialContext ; 31 import javax.naming.NamingException ; 32 33 import org.hsqldb.Trigger; 34 35 42 public class JMSTrigger implements Trigger { 43 44 protected String contextFactoryName = "org.exolab.jms.jndi.InitialContextFactory"; 47 protected String scheme = "rmi"; 48 protected String host = "localhost"; 49 protected String port = ""; 50 protected String jndiname = ""; 51 protected String topicFactoryName = "JmsTopicConnectionFactory"; 52 protected String topicName = "topic1"; 53 protected int deliveryMode = DeliveryMode.NON_PERSISTENT; 54 protected int priority = 4; 55 protected long timeToLive = 10000; 56 57 protected Topic topic = null; 58 protected TopicPublisher publisher = null; 59 protected TopicSession session = null; 60 protected TopicConnection connection = null; 61 protected Context context = null; 62 protected TopicConnectionFactory topicConnectionFactory = null; 63 64 67 public JMSTrigger() { 68 super(); 69 } 70 71 77 public Context getContext() throws NamingException { 78 79 Hashtable properties = new Hashtable (); 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 name = ""; 96 if (scheme.equals("rmi")) { 97 name = this.jndiname; 98 } 99 100 String url = scheme + "://" + host + ":" + port + "/" + name; 101 102 properties.put(Context.PROVIDER_URL, url); 103 return new InitialContext (properties); 104 } 105 106 private void setupConnection() throws NamingException , JMSException { 107 this.context = this.getContext(); 109 this.topicConnectionFactory = (TopicConnectionFactory ) this.context.lookup(this.topicFactoryName); 110 this.connection = this.topicConnectionFactory.createTopicConnection(); 111 this.connection.start(); 112 } 113 114 private void setupSession() throws JMSException { 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 , JMSException { 121 if (this.connection == null) 122 this.setupConnection(); 123 if (this.session == null) 124 this.setupSession(); 125 } 126 127 private void disconnect() throws JMSException , NamingException { 128 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 142 public void fire(String trigName, String tabName, Object [] row) { 143 try { 144 connect(); 145 TextMessage 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 e) { 157 e.printStackTrace(); 158 } 159 } 160 161 164 public void fire(int arg0, String arg1, String arg2, Object [] arg3, Object [] arg4) { 165 167 } 168 } | Popular Tags |