KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ngc > PublisherTest


1 package ngc;
2
3 import javax.jms.*;
4 import javax.naming.*;
5 import java.util.*;
6
7 public class PublisherTest {
8     //Openjms
9
//private static final String JMS_SCHEME = "rmi";
10
//SonicMq
11
//public static final String JMS_SCHEME = "tcp";
12
//uberMq
13
public static final String JavaDoc JMS_SCHEME = "ubermq";
14
15     private static final String JavaDoc JMS_HOST = "localhost";
16
17     //Openjms
18
//private static final String JMS_PORT = "1099";
19
//SonicMq
20
//public static final String JMS_PORT = "2506";
21
//UberMQ
22
public static final String JavaDoc JMS_PORT = "3999";
23
24     //Openjms
25
//private static final String JMS_CONTEXT_FACTORY = "org.exolab.jms.jndi.InitialContextFactory";
26
//SonicMq
27
//public static final String JMS_CONTEXT_FACTORY = "com.sonicsw.jndi.mfcontext.MFContextFactory";
28
//UberMQ
29
public static final String JavaDoc JMS_CONTEXT_FACTORY = "com.ubermq.jms.client.JMSInitialContextFactory";
30
31     //SonicMQ & OpenJms
32
//private static final String JMS_TOPIC_FACTORY = "JmsTopicConnectionFactory";
33
//UberMQ
34
public static final String JavaDoc JMS_TOPIC_FACTORY = "connectionFactory";
35
36     private static final String JavaDoc 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             // lookup the connection factorys from the context
46
TopicConnectionFactory topicConnFact =
47                 (TopicConnectionFactory) jndiContext.lookup(JMS_TOPIC_FACTORY);
48
49             // Get a topic connection
50
TopicConnection topicConn = topicConnFact.createTopicConnection();
51
52             topicConn.start();
53
54             // Create Topic Session
55
TopicSession topicSession = topicConn.createTopicSession(
56                 false, Session.AUTO_ACKNOWLEDGE);
57
58             // Create Topic
59
String JavaDoc topicName = "hello";
60             Topic topic = topicSession.createTopic(topicName);
61
62             // Create Topic Publisher
63
TopicPublisher topicPub = topicSession.createPublisher(topic);
64
65             // Create Text Messages
66
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             // Send messages
73
topicPub.publish(textMsg1);
74             topicPub.publish(textMsg2);
75
76         } catch (Exception JavaDoc e) {
77             e.printStackTrace();
78         }
79     }
80
81     public static void main (String JavaDoc[] args) {
82         PublisherTest pubTest = new PublisherTest();
83     }
84 }
Popular Tags