KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > tests > common > jms > JMSManager


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: JMSManager.java 692 2006-06-20 09:17:57Z studzine $
23  * --------------------------------------------------------------------------
24  */

25 package org.objectweb.easybeans.tests.common.jms;
26
27 import java.io.Serializable JavaDoc;
28
29 import javax.jms.Connection JavaDoc;
30 import javax.jms.ConnectionFactory JavaDoc;
31 import javax.jms.Destination JavaDoc;
32 import javax.jms.Message JavaDoc;
33 import javax.jms.MessageProducer JavaDoc;
34 import javax.jms.ObjectMessage 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
40 import org.objectweb.easybeans.tests.common.ejbs.entity.callbacklogger.OperationType;
41
42 /**
43  * @author Eduardo Studzinski Estima de Castro
44  * @author Gisele Pinheiro Souza
45  */

46 public class JMSManager {
47
48     /**
49      * Queue connection factory.
50      */

51     private static final String JavaDoc DEFAULT_TEXT_MESSAGE = "d";
52
53     /**
54      * Queue connection factory.
55      */

56     public static final String JavaDoc DEFAULT_QUEUE_CONNECTION_FACTORY = "JQCF";
57
58     /**
59      * Topic connection factory.
60      */

61     public static final String JavaDoc DEFAULT_TOPIC_CONNECTION_FACTORY = "JTCF";
62
63     /**
64      * Default queue name.
65      */

66     public static final String JavaDoc DEFAULT_QUEUE = "dummyQueue";
67
68     /**
69      * Default topic name.
70      */

71     public static final String JavaDoc DEFAULT_TOPIC = "dummyTopic";
72
73     /**
74      * Sleep time.
75      */

76     public static final int SLEEP = 1000;
77
78     /**
79      * Topic or Queue.
80      */

81     private Connection JavaDoc cnn = null;
82
83     /**
84      * Sender, Topic or Queue.
85      */

86     private MessageProducer JavaDoc sender = null;
87
88     /**
89      * Topic or Queue.
90      */

91     private Session JavaDoc session = null;
92
93     /**
94      * Topic or Queue.
95      */

96     private Destination JavaDoc destination = null;
97
98     /**
99      * Topic or Queue.
100      */

101     private ConnectionFactory JavaDoc factory;
102
103     /**
104      * Creates a new instance.
105      * @param jndiConnectionFactoryName JNDI name of the connection factory.
106      * @param jndiDestinationName JNDI name of the destination.
107      * @throws Exception if a problem occurs.
108      */

109     public JMSManager(final String JavaDoc jndiConnectionFactoryName, final String JavaDoc jndiDestinationName) throws Exception JavaDoc {
110         System.setProperty(Context.INITIAL_CONTEXT_FACTORY,
111                 "org.objectweb.carol.jndi.spi.MultiOrbInitialContextFactory");
112
113         InitialContext JavaDoc ctx = new InitialContext JavaDoc();
114         factory = (ConnectionFactory JavaDoc) ctx.lookup(jndiConnectionFactoryName);
115         destination = (Destination JavaDoc) ctx.lookup(jndiDestinationName);
116         cnn = factory.createConnection();
117         session = cnn.createSession(false, Session.AUTO_ACKNOWLEDGE);
118     }
119
120     /**
121      * Sends a text message.
122      * @param text message text.
123      * @throws Exception if a problem occurs.
124      */

125     public void sendTextMessage(final String JavaDoc text) throws Exception JavaDoc {
126         TextMessage JavaDoc msg = session.createTextMessage(text);
127         this.sendMessage(msg);
128     }
129
130     /**
131      * Sends a default text message.
132      * @param bean name of the destination bean.
133      * @param operation operation to be performed by the bean.
134      * @throws Exception if a problem occurs.
135      */

136     public void sendControlMessage(final String JavaDoc bean, final OperationType operation) throws Exception JavaDoc {
137         TextMessage JavaDoc msg = session.createTextMessage(DEFAULT_TEXT_MESSAGE);
138         msg.setStringProperty(MessageProperty.TYPE.toString(), bean);
139         msg.setStringProperty(MessageProperty.OPERATION.toString(), operation.toString());
140
141         this.sendMessage(msg);
142     }
143
144     /**
145      * Sends a message.
146      * @param msg Message
147      * @throws Exception if a problem occurs.
148      */

149     public void sendMessage(final Message JavaDoc msg) throws Exception JavaDoc{
150         sender = session.createProducer(destination);
151         sender.send(msg);
152
153         sender.close();
154         Thread.sleep(SLEEP);
155     }
156
157     /**
158      * Sends object message.
159      * @param type message type.
160      * @param message contains the message to be sent.
161      * @throws Exception if a problem occurs.
162      */

163     public void sendObjectMessage(final String JavaDoc type, final Serializable JavaDoc message) throws Exception JavaDoc {
164         ObjectMessage JavaDoc msg = session.createObjectMessage(message);
165         msg.setStringProperty(MessageProperty.TYPE.toString(), type);
166
167         this.sendMessage(msg);
168     }
169
170     /**
171      * Closes JMS connections.
172      * @throws Exception if a problem occurs.
173      */

174     public void close() throws Exception JavaDoc{
175         session.close();
176         cnn.close();
177     }
178
179 }
180
Popular Tags