KickJava   Java API By Example, From Geeks To Geeks.

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


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 sends messages to a queue. 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:21 $
26  **/

27 public class CommonInterfaceQueueSender {
28
29     private static final Logger log = Logger.getLogger(CommonInterfaceQueueSender.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 queue = (Destination JavaDoc)initialContext.lookup("Queue1");
43
44         Connection JavaDoc connection = connectionFactory.createConnection();
45
46         Session JavaDoc session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
47         
48         MessageProducer JavaDoc sender = session.createProducer(queue);
49
50         int numberOfMessages = getNumberOfMessages(args);
51         log.info("Sending "+numberOfMessages+" text messages ...");
52
53         for(int i = 0; i < numberOfMessages; i++) {
54             TextMessage JavaDoc message = session.createTextMessage();
55             message.setText("QUEUE MESSAGE ["+i+"]");
56             sender.send(message);
57             log.info("Sent message "+i);
58         }
59
60         log.info("Finished sending messages");
61
62         // TO_DO: If I immediately close the producer after sending the messages, sometimes the
63
// view change arrives before the messages, which are then discared by NACKACK.
64
// Thread.sleep(3000);
65
// connection.close();
66

67 // log.info("Successfully closed the connection");
68
// System.exit(0);
69
}
70
71     private static int getNumberOfMessages(String JavaDoc[] args) {
72         
73         int result = DEFAULT_NUMBER_OF_MESSAGES;
74
75         if (args.length > 0) {
76             try {
77                 result = Integer.parseInt(args[0]);
78             }
79             catch(Exception JavaDoc e) {
80                 log.warn("Invalid number of messages: "+args[0]);
81             }
82         }
83
84         return result;
85     }
86
87
88 }
89
90
91
92
Popular Tags