1 22 package org.jboss.ejb3.mdb; 23 24 import java.io.Externalizable ; 25 import java.io.IOException ; 26 import java.io.ObjectInput ; 27 import java.io.ObjectOutput ; 28 import java.io.Serializable ; 29 import java.util.HashMap ; 30 import java.util.Hashtable ; 31 import javax.jms.Connection ; 32 import javax.jms.ConnectionFactory ; 33 import javax.jms.Destination ; 34 import javax.jms.JMSException ; 35 import javax.jms.MessageProducer ; 36 import javax.jms.ObjectMessage ; 37 import javax.jms.Session ; 38 import javax.naming.InitialContext ; 39 import javax.naming.NamingException ; 40 import org.jboss.annotation.ejb.DeliveryMode; 41 import org.jboss.annotation.ejb.MessageProperties; 42 import org.jboss.annotation.ejb.Producer; 43 import org.jboss.aop.advice.Interceptor; 44 import org.jboss.aop.joinpoint.Invocation; 45 import org.jboss.aop.joinpoint.MethodInvocation; 46 import org.jboss.ejb3.EJB3Util; 47 import org.jboss.logging.Logger; 48 49 54 public class ProducerManagerImpl implements ProducerManager, Externalizable , Interceptor 55 { 56 private static final Logger log = Logger.getLogger(ProducerManagerImpl.class); 57 58 private static final int PERSISTENT = javax.jms.DeliveryMode.PERSISTENT; 59 private static final int NON_PERSISTENT = javax.jms.DeliveryMode.NON_PERSISTENT; 60 61 protected Producer producer; 62 protected Destination destination; 63 protected String factoryLookup; 64 65 protected int deliveryMode = javax.jms.DeliveryMode.PERSISTENT; 66 protected int timeToLive = 0; 67 protected int priority = 4; 68 protected HashMap methodMap; 69 70 protected transient ConnectionFactory factory; 71 protected transient Connection connection; 72 protected transient Session session; 73 protected transient MessageProducer msgProducer; 74 protected transient String username; 75 protected transient String password; 76 protected transient InitialContext initialContext; 77 protected Hashtable initialContextProperties; 78 79 public void writeExternal(ObjectOutput out) throws IOException 80 { 81 out.writeObject(producer); 82 out.writeObject(destination); 83 out.writeObject(factoryLookup); 84 out.writeInt(deliveryMode); 85 out.writeInt(timeToLive); 86 out.writeInt(priority); 87 out.writeObject(methodMap); 88 out.writeObject(initialContextProperties); 89 if (factoryLookup == null) 90 { 91 out.writeObject(factory); 92 } 93 } 94 95 public void readExternal(ObjectInput in) throws IOException , ClassNotFoundException 96 { 97 producer = (Producer ) in.readObject(); 98 destination = (Destination ) in.readObject(); 99 factoryLookup = (String ) in.readObject(); 100 deliveryMode = in.readInt(); 101 timeToLive = in.readInt(); 102 priority = in.readInt(); 103 methodMap = (HashMap ) in.readObject(); 104 initialContextProperties = (Hashtable )in.readObject(); 105 try 106 { 107 initialContext = EJB3Util.getInitialContext(initialContextProperties); 108 } 109 catch (NamingException e) 110 { 111 throw new RuntimeException (e); 112 } 113 if (factoryLookup != null) 114 { 115 try 116 { 117 factory = (ConnectionFactory ) initialContext.lookup(factoryLookup); 118 } 119 catch (NamingException e) 120 { 121 throw new RuntimeException (e); 122 } 123 } 124 else 125 { 126 factory = (ConnectionFactory ) in.readObject(); 127 } 128 } 129 130 public ProducerManagerImpl(Producer producer, Destination destination, ConnectionFactory factory, DeliveryMode deliveryMode, int timeToLive, int priority, HashMap methodMap, Hashtable initialContextProperties) 131 { 132 this.initialContextProperties = initialContextProperties; 133 try 134 { 135 this.initialContext = EJB3Util.getInitialContext(initialContextProperties); 136 } 137 catch (NamingException e) 138 { 139 throw new RuntimeException (e); 140 } 141 this.producer = producer; 142 this.destination = destination; 143 this.factory = factory; 144 145 int mode = deliveryMode.ordinal(); 146 switch (mode) 147 { 148 case PERSISTENT: 149 this.deliveryMode = javax.jms.DeliveryMode.PERSISTENT; 150 break; 151 case NON_PERSISTENT: 152 this.deliveryMode = javax.jms.DeliveryMode.NON_PERSISTENT; 153 break; 154 } 155 this.timeToLive = timeToLive; 156 this.priority = priority; 157 this.methodMap = methodMap; 158 } 159 160 public ProducerManagerImpl(Producer producer, Destination destination, String factory, DeliveryMode deliveryMode, int timeToLive, int priority, HashMap methodMap, Hashtable initialContextProperties) 161 { 162 this.initialContextProperties = initialContextProperties; 163 try 164 { 165 this.initialContext = EJB3Util.getInitialContext(initialContextProperties); 166 } 167 catch (NamingException e) 168 { 169 throw new RuntimeException (e); 170 } 171 this.producer = producer; 172 this.destination = destination; 173 this.factoryLookup = factory; 174 175 int mode = deliveryMode.ordinal(); 176 switch (mode) 177 { 178 case PERSISTENT: 179 this.deliveryMode = javax.jms.DeliveryMode.PERSISTENT; 180 break; 181 case NON_PERSISTENT: 182 this.deliveryMode = javax.jms.DeliveryMode.NON_PERSISTENT; 183 break; 184 } 185 this.timeToLive = timeToLive; 186 this.priority = priority; 187 this.methodMap = methodMap; 188 } 189 190 191 public ProducerManagerImpl() 192 { 193 } 194 195 public void setUsername(String user) 196 { 197 this.username = user; 198 } 199 200 public void setPassword(String passwd) 201 { 202 this.password = passwd; 203 } 204 205 public void connect() throws JMSException 206 { 207 if (factory == null) 208 { 209 try 210 { 211 factory = (ConnectionFactory ) initialContext.lookup(factoryLookup); 212 } 213 catch (NamingException e) 214 { 215 throw new RuntimeException (e); 216 } 217 } 218 if (connection != null) return; 219 if (username != null) 220 { 221 connection = factory.createConnection(username, password); 222 } 223 else 224 { 225 connection = factory.createConnection(); 226 } 227 session = connection.createSession(producer.transacted(), producer.acknowledgeMode()); 228 msgProducer = session.createProducer(destination); 229 msgProducer.setDeliveryMode(deliveryMode); 230 msgProducer.setTimeToLive(timeToLive); 231 msgProducer.setPriority(priority); 232 } 233 234 public void close() throws JMSException 235 { 236 msgProducer.close(); 237 msgProducer = null; 238 session.close(); 239 session = null; 240 connection.close(); 241 connection = null; 242 } 243 244 public void commit() throws JMSException 245 { 246 session.commit(); 247 } 248 249 public void rollback() throws JMSException 250 { 251 session.rollback(); 252 } 253 254 public String getName() 255 { 256 return ProducerManager.class.getName(); 257 } 258 259 public Object invoke(Invocation invocation) throws Throwable 260 { 261 if (session == null) 262 { 263 throw new RuntimeException ("You must call connect() on the producer. The JMS session has not been set"); 264 } 265 ObjectMessage msg = session.createObjectMessage((Serializable ) invocation); 266 MethodInvocation mi = (MethodInvocation) invocation; 267 MessageProperties props = (MessageProperties)methodMap.get(new Long (mi.getMethodHash())); 268 if (props != null) 269 { 270 int del = (props.delivery() == DeliveryMode.PERSISTENT) ? javax.jms.DeliveryMode.PERSISTENT : javax.jms.DeliveryMode.NON_PERSISTENT; 271 msgProducer.send(msg, del, props.priority(), props.timeToLive()); 272 } 273 else 274 { 275 msgProducer.send(msg); 276 } 277 return null; 278 } 279 } 280 | Popular Tags |