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: JMSManager.java 1970 2007-10-16 11:49:25Z benoitf $
023: * --------------------------------------------------------------------------
024: */package org.ow2.easybeans.tests.common.jms;
025:
026: import java.io.Serializable;
027:
028: import javax.jms.Connection;
029: import javax.jms.ConnectionFactory;
030: import javax.jms.Destination;
031: import javax.jms.Message;
032: import javax.jms.MessageProducer;
033: import javax.jms.ObjectMessage;
034: import javax.jms.Session;
035: import javax.jms.TextMessage;
036: import javax.naming.Context;
037: import javax.naming.InitialContext;
038:
039: import org.ow2.easybeans.tests.common.ejbs.entity.callbacklogger.OperationType;
040:
041: /**
042: * @author Eduardo Studzinski Estima de Castro
043: * @author Gisele Pinheiro Souza
044: */
045: public class JMSManager {
046:
047: /**
048: * Queue connection factory.
049: */
050: private static final String DEFAULT_TEXT_MESSAGE = "d";
051:
052: /**
053: * Queue connection factory.
054: */
055: public static final String DEFAULT_QUEUE_CONNECTION_FACTORY = "JQCF";
056:
057: /**
058: * Topic connection factory.
059: */
060: public static final String DEFAULT_TOPIC_CONNECTION_FACTORY = "JTCF";
061:
062: /**
063: * Default queue name.
064: */
065: public static final String DEFAULT_QUEUE = "dummyQueue";
066:
067: /**
068: * Default topic name.
069: */
070: public static final String DEFAULT_TOPIC = "dummyTopic";
071:
072: /**
073: * Sleep time.
074: */
075: public static final int SLEEP = 1000;
076:
077: /**
078: * Topic or Queue.
079: */
080: private Connection cnn = null;
081:
082: /**
083: * Sender, Topic or Queue.
084: */
085: private MessageProducer sender = null;
086:
087: /**
088: * Topic or Queue.
089: */
090: private Session session = null;
091:
092: /**
093: * Topic or Queue.
094: */
095: private Destination destination = null;
096:
097: /**
098: * Topic or Queue.
099: */
100: private ConnectionFactory factory;
101:
102: /**
103: * Creates a new instance.
104: * @param jndiConnectionFactoryName JNDI name of the connection factory.
105: * @param jndiDestinationName JNDI name of the destination.
106: * @throws Exception if a problem occurs.
107: */
108: public JMSManager(final String jndiConnectionFactoryName,
109: final String jndiDestinationName) throws Exception {
110: System
111: .setProperty(Context.INITIAL_CONTEXT_FACTORY,
112: "org.objectweb.carol.jndi.spi.MultiOrbInitialContextFactory");
113:
114: InitialContext ctx = new InitialContext();
115: factory = (ConnectionFactory) ctx
116: .lookup(jndiConnectionFactoryName);
117: destination = (Destination) ctx.lookup(jndiDestinationName);
118: cnn = factory.createConnection();
119: session = cnn.createSession(false, Session.AUTO_ACKNOWLEDGE);
120: }
121:
122: /**
123: * Sends a text message.
124: * @param text message text.
125: * @throws Exception if a problem occurs.
126: */
127: public void sendTextMessage(final String text) throws Exception {
128: TextMessage msg = session.createTextMessage(text);
129: this .sendMessage(msg);
130: }
131:
132: /**
133: * Sends a default text message.
134: * @param bean name of the destination bean.
135: * @param operation operation to be performed by the bean.
136: * @throws Exception if a problem occurs.
137: */
138: public void sendControlMessage(final String bean,
139: final OperationType operation) throws Exception {
140: TextMessage msg = session
141: .createTextMessage(DEFAULT_TEXT_MESSAGE);
142: msg.setStringProperty(MessageProperty.TYPE.toString(), bean);
143: msg.setStringProperty(MessageProperty.OPERATION.toString(),
144: operation.toString());
145:
146: this .sendMessage(msg);
147: }
148:
149: /**
150: * Sends a message.
151: * @param msg Message
152: * @throws Exception if a problem occurs.
153: */
154: public void sendMessage(final Message msg) throws Exception {
155: sender = session.createProducer(destination);
156: sender.send(msg);
157:
158: sender.close();
159: Thread.sleep(SLEEP);
160: }
161:
162: /**
163: * Sends object message.
164: * @param type message type.
165: * @param message contains the message to be sent.
166: * @throws Exception if a problem occurs.
167: */
168: public void sendObjectMessage(final String type,
169: final Serializable message) throws Exception {
170: ObjectMessage msg = session.createObjectMessage(message);
171: msg.setStringProperty(MessageProperty.TYPE.toString(), type);
172:
173: this .sendMessage(msg);
174: }
175:
176: /**
177: * Closes JMS connections.
178: * @throws Exception if a problem occurs.
179: */
180: public void close() throws Exception {
181: session.close();
182: cnn.close();
183: }
184:
185: }
|