1 package ngc; 2 3 import javax.jms.*; 4 import javax.naming.*; 5 import java.util.*; 6 7 public class PublisherTest { 8 public static final String JMS_SCHEME = "ubermq"; 14 15 private static final String JMS_HOST = "localhost"; 16 17 public static final String JMS_PORT = "3999"; 23 24 public static final String JMS_CONTEXT_FACTORY = "com.ubermq.jms.client.JMSInitialContextFactory"; 30 31 public static final String JMS_TOPIC_FACTORY = "connectionFactory"; 35 36 private static final String jndiURL = JMS_SCHEME + "://" + JMS_HOST + ":" + JMS_PORT; 37 38 public PublisherTest () { 39 try { 40 Hashtable props = new Hashtable(); 41 props.put(Context.PROVIDER_URL, jndiURL); 42 props.put(Context.INITIAL_CONTEXT_FACTORY, JMS_CONTEXT_FACTORY); 43 Context jndiContext = new InitialContext(props); 44 45 TopicConnectionFactory topicConnFact = 47 (TopicConnectionFactory) jndiContext.lookup(JMS_TOPIC_FACTORY); 48 49 TopicConnection topicConn = topicConnFact.createTopicConnection(); 51 52 topicConn.start(); 53 54 TopicSession topicSession = topicConn.createTopicSession( 56 false, Session.AUTO_ACKNOWLEDGE); 57 58 String topicName = "hello"; 60 Topic topic = topicSession.createTopic(topicName); 61 62 TopicPublisher topicPub = topicSession.createPublisher(topic); 64 65 TextMessage textMsg1 = topicSession.createTextMessage("Message 1"); 67 textMsg1.setStringProperty("MSG_SEL", "MSG1"); 68 69 TextMessage textMsg2 = topicSession.createTextMessage("Message 2"); 70 textMsg2.setStringProperty("MSG_SEL", "MSG2"); 71 72 topicPub.publish(textMsg1); 74 topicPub.publish(textMsg2); 75 76 } catch (Exception e) { 77 e.printStackTrace(); 78 } 79 } 80 81 public static void main (String [] args) { 82 PublisherTest pubTest = new PublisherTest(); 83 } 84 } | Popular Tags |