KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > simplemessageclient > Main


1 /*
2  * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved. U.S.
3  * Government Rights - Commercial software. Government users are subject
4  * to the Sun Microsystems, Inc. standard license agreement and
5  * applicable provisions of the FAR and its supplements. Use is subject
6  * to license terms.
7  *
8  * This distribution may include materials developed by third parties.
9  * Sun, Sun Microsystems, the Sun logo, Java and J2EE are trademarks
10  * or registered trademarks of Sun Microsystems, Inc. in the U.S. and
11  * other countries.
12  *
13  * Copyright (c) 2005 Sun Microsystems, Inc. Tous droits reserves.
14  *
15  * Droits du gouvernement americain, utilisateurs gouvernementaux - logiciel
16  * commercial. Les utilisateurs gouvernementaux sont soumis au contrat de
17  * licence standard de Sun Microsystems, Inc., ainsi qu'aux dispositions
18  * en vigueur de la FAR (Federal Acquisition Regulations) et des
19  * supplements a celles-ci. Distribue par des licences qui en
20  * restreignent l'utilisation.
21  *
22  * Cette distribution peut comprendre des composants developpes par des
23  * tierces parties. Sun, Sun Microsystems, le logo Sun, Java et J2EE
24  * sont des marques de fabrique ou des marques deposees de Sun
25  * Microsystems, Inc. aux Etats-Unis et dans d'autres pays.
26  */

27
28 package simplemessageclient;
29
30 import javax.jms.Connection JavaDoc;
31 import javax.jms.ConnectionFactory JavaDoc;
32 import javax.jms.Destination JavaDoc;
33 import javax.jms.JMSException JavaDoc;
34 import javax.jms.MessageProducer JavaDoc;
35 import javax.jms.Queue JavaDoc;
36 import javax.jms.Session JavaDoc;
37 import javax.jms.TextMessage JavaDoc;
38 import javax.naming.Context JavaDoc;
39 import javax.naming.InitialContext JavaDoc;
40 import javax.naming.NamingException JavaDoc;
41
42 /**
43  *
44  * @author blaha
45  */

46 public class Main {
47     
48     /** Creates a new instance of Main */
49     public Main() {
50     }
51     
52     /**
53      * @param args the command line arguments
54      */

55     public static void main(String JavaDoc[] args) {
56         Context JavaDoc jndiContext = null;
57         ConnectionFactory JavaDoc connectionFactory = null;
58         Connection JavaDoc connection = null;
59         Session JavaDoc session = null;
60         Destination JavaDoc destination = null;
61         MessageProducer JavaDoc messageProducer = null;
62         TextMessage JavaDoc message = null;
63         final int NUM_MSGS = 3;
64         
65         try {
66             jndiContext = new InitialContext JavaDoc();
67         } catch (NamingException JavaDoc e) {
68             System.out.println("Could not create JNDI " + "context: " +
69                     e.toString());
70             System.exit(1);
71         }
72         
73         try {
74             connectionFactory =
75                     (ConnectionFactory JavaDoc) jndiContext.lookup(
76                     "jms/SimpleMessageDestinationFactory");
77             destination =
78                     (Queue JavaDoc) jndiContext.lookup("jms/SimpleMessageBean");
79         } catch (NamingException JavaDoc e) {
80             System.out.println("JNDI lookup failed: " + e.toString());
81             System.exit(1);
82         }
83         
84         try {
85             connection = connectionFactory.createConnection();
86             session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
87             messageProducer = session.createProducer(destination);
88             message = session.createTextMessage();
89             
90             for (int i = 0; i < NUM_MSGS; i++) {
91                 message.setText("This is message " + (i + 1));
92                 System.out.println("Sending message: " + message.getText());
93                 messageProducer.send(message);
94             }
95             
96             System.out.println("To see if the bean received the messages,");
97             System.out.println(
98                     " check <install_dir>/domains/domain1/logs/server.log.");
99         } catch (JMSException JavaDoc e) {
100             System.out.println("Exception occurred: " + e.toString());
101         } finally {
102             if (connection != null) {
103                 try {
104                     connection.close();
105                 } catch (JMSException JavaDoc e) {
106                 }
107             }
108             System.exit(0);
109         }
110     }
111     
112 }
113
Popular Tags