KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > examples > messagedrivenbean > ClientMessageDriven


1 /**
2  * EasyBeans
3  * Copyright (C) 2006 Bull S.A.S.
4  * Contact: easybeans@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: ClientMessageDriven.java 1078 2006-08-10 09:28:57Z sauthieg $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.easybeans.examples.messagedrivenbean;
27
28 import java.util.Hashtable JavaDoc;
29
30 import javax.jms.Queue JavaDoc;
31 import javax.jms.QueueConnection JavaDoc;
32 import javax.jms.QueueConnectionFactory JavaDoc;
33 import javax.jms.QueueSender JavaDoc;
34 import javax.jms.QueueSession JavaDoc;
35 import javax.jms.Session JavaDoc;
36 import javax.jms.TextMessage JavaDoc;
37 import javax.naming.Context JavaDoc;
38 import javax.naming.InitialContext JavaDoc;
39 import javax.naming.NamingException JavaDoc;
40
41 /**
42  * Client of the Message Driven Bean.
43  * @author Florent Benoit
44  */

45 public final class ClientMessageDriven {
46
47     /**
48      * Queue connection factory (Available for clients).
49      */

50     private static final String JavaDoc QUEUE_CONNECTION_FACTORY = "JQCF";
51
52     /**
53      * Queue name used by this example.
54      */

55     private static final String JavaDoc SAMPLE_QUEUE = "SampleQueue";
56
57     /**
58      * Number of messages to send.
59      */

60     private static final int NUMBER_MESSAGES = 5;
61
62     /**
63      * Default InitialContextFactory to use.
64      */

65     private static final String JavaDoc DEFAULT_INITIAL_CONTEXT_FACTORY = "org.objectweb.carol.jndi.spi.MultiOrbInitialContextFactory";
66
67     /**
68      * Utility class.
69      */

70     private ClientMessageDriven() {
71
72     }
73
74     /**
75      * Main method.
76      * @param args the arguments (not required)
77      * @throws Exception if exception is found.
78      */

79     public static void main(final String JavaDoc[] args) throws Exception JavaDoc {
80
81         // Build Context
82
Context JavaDoc initialContext = getInitialContext();
83
84         // Get factory
85
QueueConnectionFactory JavaDoc queueConnectionFactory = (QueueConnectionFactory JavaDoc) initialContext
86                 .lookup(QUEUE_CONNECTION_FACTORY);
87
88         // Lookup the Queue through its JNDI name
89
Queue JavaDoc queue = (Queue JavaDoc) initialContext.lookup(SAMPLE_QUEUE);
90
91         // Create connection
92
QueueConnection JavaDoc queueConnection = queueConnectionFactory.createQueueConnection();
93
94         // Create session
95
QueueSession JavaDoc queueSession = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
96
97         // Create sender
98
QueueSender JavaDoc queueSender = queueSession.createSender(queue);
99
100         // Send messages
101
TextMessage JavaDoc message = null;
102         for (int i = 0; i < NUMBER_MESSAGES; i++) {
103             message = queueSession.createTextMessage();
104             String JavaDoc text = "Message_" + i;
105             message.setText(text);
106             queueSender.send(message);
107             System.out.println("Message [" + message.getJMSMessageID() + ", text:" + text + "] Sended");
108
109         }
110
111         // Close JMS objects
112
queueSender.close();
113         queueSession.close();
114         queueConnection.close();
115
116     }
117
118     /**
119      * @return Returns the InitialContext.
120      * @throws NamingException If the Context cannot be created.
121      */

122     private static Context JavaDoc getInitialContext() throws NamingException JavaDoc {
123
124         // if user don't use jclient/client container
125
// we can specify the InitialContextFactory to use
126
// But this is *not recommended*.
127
Hashtable JavaDoc<String JavaDoc, Object JavaDoc> env = new Hashtable JavaDoc<String JavaDoc, Object JavaDoc>();
128         env.put(Context.INITIAL_CONTEXT_FACTORY, getInitialContextFactory());
129
130         // Usually a simple new InitialContext() without any parameters is sufficent.
131
// return new InitialContext();
132

133         return new InitialContext JavaDoc(env);
134     }
135
136     /**
137      * Returns a configurable InitialContextFactory classname.<br/>
138      * Can be configured with the <code>easybeans.client.initial-context-factory</code> System property.
139      * @return Returns a configurable InitialContextFactory classname.
140      */

141     private static String JavaDoc getInitialContextFactory() {
142         String JavaDoc prop = System.getProperty("easybeans.client.initial-context-factory");
143         // If not found, use the default
144
if (prop == null) {
145             prop = DEFAULT_INITIAL_CONTEXT_FACTORY;
146         }
147         return prop;
148     }
149
150 }
151
Popular Tags