1 25 package org.objectweb.easybeans.tests.common.jms; 26 27 import java.io.Serializable ; 28 29 import javax.jms.Connection ; 30 import javax.jms.ConnectionFactory ; 31 import javax.jms.Destination ; 32 import javax.jms.Message ; 33 import javax.jms.MessageProducer ; 34 import javax.jms.ObjectMessage ; 35 import javax.jms.Session ; 36 import javax.jms.TextMessage ; 37 import javax.naming.Context ; 38 import javax.naming.InitialContext ; 39 40 import org.objectweb.easybeans.tests.common.ejbs.entity.callbacklogger.OperationType; 41 42 46 public class JMSManager { 47 48 51 private static final String DEFAULT_TEXT_MESSAGE = "d"; 52 53 56 public static final String DEFAULT_QUEUE_CONNECTION_FACTORY = "JQCF"; 57 58 61 public static final String DEFAULT_TOPIC_CONNECTION_FACTORY = "JTCF"; 62 63 66 public static final String DEFAULT_QUEUE = "dummyQueue"; 67 68 71 public static final String DEFAULT_TOPIC = "dummyTopic"; 72 73 76 public static final int SLEEP = 1000; 77 78 81 private Connection cnn = null; 82 83 86 private MessageProducer sender = null; 87 88 91 private Session session = null; 92 93 96 private Destination destination = null; 97 98 101 private ConnectionFactory factory; 102 103 109 public JMSManager(final String jndiConnectionFactoryName, final String jndiDestinationName) throws Exception { 110 System.setProperty(Context.INITIAL_CONTEXT_FACTORY, 111 "org.objectweb.carol.jndi.spi.MultiOrbInitialContextFactory"); 112 113 InitialContext ctx = new InitialContext (); 114 factory = (ConnectionFactory ) ctx.lookup(jndiConnectionFactoryName); 115 destination = (Destination ) ctx.lookup(jndiDestinationName); 116 cnn = factory.createConnection(); 117 session = cnn.createSession(false, Session.AUTO_ACKNOWLEDGE); 118 } 119 120 125 public void sendTextMessage(final String text) throws Exception { 126 TextMessage msg = session.createTextMessage(text); 127 this.sendMessage(msg); 128 } 129 130 136 public void sendControlMessage(final String bean, final OperationType operation) throws Exception { 137 TextMessage msg = session.createTextMessage(DEFAULT_TEXT_MESSAGE); 138 msg.setStringProperty(MessageProperty.TYPE.toString(), bean); 139 msg.setStringProperty(MessageProperty.OPERATION.toString(), operation.toString()); 140 141 this.sendMessage(msg); 142 } 143 144 149 public void sendMessage(final Message msg) throws Exception { 150 sender = session.createProducer(destination); 151 sender.send(msg); 152 153 sender.close(); 154 Thread.sleep(SLEEP); 155 } 156 157 163 public void sendObjectMessage(final String type, final Serializable message) throws Exception { 164 ObjectMessage msg = session.createObjectMessage(message); 165 msg.setStringProperty(MessageProperty.TYPE.toString(), type); 166 167 this.sendMessage(msg); 168 } 169 170 174 public void close() throws Exception { 175 session.close(); 176 cnn.close(); 177 } 178 179 } 180 | Popular Tags |