001: /**
002: * EasyBeans
003: * Copyright (C) 2006 Bull S.A.S.
004: * Contact: easybeans@ow2.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: ClientMessageDriven.java 1970 2007-10-16 11:49:25Z benoitf $
023: * --------------------------------------------------------------------------
024: */package org.ow2.easybeans.examples.messagedrivenbean;
025:
026: import java.util.Hashtable;
027:
028: import javax.jms.Queue;
029: import javax.jms.QueueConnection;
030: import javax.jms.QueueConnectionFactory;
031: import javax.jms.QueueSender;
032: import javax.jms.QueueSession;
033: import javax.jms.Session;
034: import javax.jms.TextMessage;
035: import javax.naming.Context;
036: import javax.naming.InitialContext;
037: import javax.naming.NamingException;
038:
039: /**
040: * Client of the Message Driven Bean.
041: * @author Florent Benoit
042: */
043: public final class ClientMessageDriven {
044:
045: /**
046: * Queue connection factory (Available for clients).
047: */
048: private static final String QUEUE_CONNECTION_FACTORY = "JQCF";
049:
050: /**
051: * Queue name used by this example.
052: */
053: private static final String SAMPLE_QUEUE = "SampleQueue";
054:
055: /**
056: * Number of messages to send.
057: */
058: private static final int NUMBER_MESSAGES = 5;
059:
060: /**
061: * Default InitialContextFactory to use.
062: */
063: private static final String DEFAULT_INITIAL_CONTEXT_FACTORY = "org.objectweb.carol.jndi.spi.MultiOrbInitialContextFactory";
064:
065: /**
066: * Utility class.
067: */
068: private ClientMessageDriven() {
069:
070: }
071:
072: /**
073: * Main method.
074: * @param args the arguments (not required)
075: * @throws Exception if exception is found.
076: */
077: public static void main(final String[] args) throws Exception {
078:
079: // Build Context
080: Context initialContext = getInitialContext();
081:
082: // Get factory
083: QueueConnectionFactory queueConnectionFactory = (QueueConnectionFactory) initialContext
084: .lookup(QUEUE_CONNECTION_FACTORY);
085:
086: // Lookup the Queue through its JNDI name
087: Queue queue = (Queue) initialContext.lookup(SAMPLE_QUEUE);
088:
089: // Create connection
090: QueueConnection queueConnection = queueConnectionFactory
091: .createQueueConnection();
092:
093: // Create session
094: QueueSession queueSession = queueConnection.createQueueSession(
095: false, Session.AUTO_ACKNOWLEDGE);
096:
097: // Create sender
098: QueueSender queueSender = queueSession.createSender(queue);
099:
100: // Send messages
101: TextMessage message = null;
102: for (int i = 0; i < NUMBER_MESSAGES; i++) {
103: message = queueSession.createTextMessage();
104: String text = "Message_" + i;
105: message.setText(text);
106: queueSender.send(message);
107: System.out.println("Message [" + message.getJMSMessageID()
108: + ", text:" + text + "] sent");
109:
110: }
111:
112: // Close JMS objects
113: queueSender.close();
114: queueSession.close();
115: queueConnection.close();
116:
117: }
118:
119: /**
120: * @return Returns the InitialContext.
121: * @throws NamingException If the Context cannot be created.
122: */
123: private static Context getInitialContext() throws NamingException {
124:
125: // if user don't use jclient/client container
126: // we can specify the InitialContextFactory to use
127: // But this is *not recommended*.
128: Hashtable<String, Object> env = new Hashtable<String, Object>();
129: env.put(Context.INITIAL_CONTEXT_FACTORY,
130: getInitialContextFactory());
131:
132: // Usually a simple new InitialContext() without any parameters is sufficent.
133: // return new InitialContext();
134:
135: return new InitialContext(env);
136: }
137:
138: /**
139: * Returns a configurable InitialContextFactory classname.<br/>
140: * Can be configured with the <code>easybeans.client.initial-context-factory</code> System property.
141: * @return Returns a configurable InitialContextFactory classname.
142: */
143: private static String getInitialContextFactory() {
144: String prop = System
145: .getProperty("easybeans.client.initial-context-factory");
146: // If not found, use the default
147: if (prop == null) {
148: prop = DEFAULT_INITIAL_CONTEXT_FACTORY;
149: }
150: return prop;
151: }
152:
153: }
|