KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > jms > serverless > client > CommonInterfacePublisher


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.jms.serverless.client;
8
9 import javax.naming.Context JavaDoc;
10 import javax.naming.InitialContext JavaDoc;
11 import javax.naming.NamingException JavaDoc;
12 import org.jboss.logging.Logger;
13 import javax.jms.ConnectionFactory JavaDoc;
14 import javax.jms.Connection JavaDoc;
15 import javax.jms.Session JavaDoc;
16 import javax.jms.MessageProducer JavaDoc;
17 import javax.jms.Destination JavaDoc;
18 import javax.jms.TextMessage JavaDoc;
19 import javax.jms.Topic JavaDoc;
20
21 /**
22  * A simple JMS client that publishes messages on a topic. It uses the common JMS 1.1 interfaces.
23  *
24  * @author Ovidiu Feodorov <ovidiu@jboss.org>
25  * @version $Revision: 1.1 $ $Date: 2004/04/15 22:54:20 $
26  **/

27 public class CommonInterfacePublisher {
28
29     private static final Logger log = Logger.getLogger(CommonInterfacePublisher.class);
30
31     private static final int DEFAULT_NUMBER_OF_MESSAGES = 10;
32
33     /**
34      **/

35     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
36
37         Context JavaDoc initialContext = new InitialContext JavaDoc();
38
39         ConnectionFactory JavaDoc connectionFactory =
40             (ConnectionFactory JavaDoc)initialContext.lookup("ConnectionFactory");
41
42         Destination JavaDoc topic = (Destination JavaDoc)initialContext.lookup("Topic1");
43
44         Connection JavaDoc connection = connectionFactory.createConnection();
45
46         Session JavaDoc session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
47         
48         MessageProducer JavaDoc producer = session.createProducer(topic);
49         connection.start();
50         Thread.sleep(1000);
51
52         int numberOfMessages = getNumberOfMessages(args);
53         log.info("Sending "+numberOfMessages+" text messages ...");
54
55         for(int i = 0; i < numberOfMessages; i++) {
56             TextMessage JavaDoc message = session.createTextMessage();
57             message.setText("This is message "+i);
58             producer.send(message);
59             log.debug("sent message "+i);
60         }
61
62         TextMessage JavaDoc message = session.createTextMessage();
63         message.setText("");
64         producer.send(message);
65         log.debug("sent end-of-communication message");
66
67         log.info("Finished sending messages");
68
69         // TO_DO: If I immediately close the producer after sending the messages, sometimes the
70
// view change arrives before the messages, which are then discared by NACKACK.
71
// Thread.sleep(1000);
72
// connection.close();
73
// log.info("Successfully closed the connection");
74
// System.exit(0);
75
}
76
77
78     private static int getNumberOfMessages(String JavaDoc[] args) {
79         
80         int result = DEFAULT_NUMBER_OF_MESSAGES;
81
82         if (args.length > 0) {
83             try {
84                 result = Integer.parseInt(args[0]);
85             }
86             catch(Exception JavaDoc e) {
87                 log.warn("Invalid number of messages: "+args[0]);
88             }
89         }
90
91         return result;
92     }
93
94 }
95
96
97
98
Popular Tags