KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > test > JmsResourceProvider


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.activemq.test;
19
20 import javax.jms.Connection JavaDoc;
21 import javax.jms.ConnectionConsumer JavaDoc;
22 import javax.jms.ConnectionFactory JavaDoc;
23 import javax.jms.Destination JavaDoc;
24 import javax.jms.JMSException JavaDoc;
25 import javax.jms.MessageConsumer JavaDoc;
26 import javax.jms.MessageProducer JavaDoc;
27 import javax.jms.ServerSessionPool JavaDoc;
28 import javax.jms.Session JavaDoc;
29 import javax.jms.DeliveryMode JavaDoc;
30 import javax.jms.Topic JavaDoc;
31
32 import org.apache.activemq.ActiveMQConnectionFactory;
33
34 /**
35  * @version $Revision: 1.4 $
36  */

37 public class JmsResourceProvider {
38     
39     private String JavaDoc serverUri = "vm://localhost?broker.persistent=false";
40     private boolean transacted = false;
41     private int ackMode = Session.AUTO_ACKNOWLEDGE;
42     private boolean isTopic;
43     private int deliveryMode=DeliveryMode.PERSISTENT;
44     private String JavaDoc durableName = "DummyName";
45     private String JavaDoc clientID = getClass().getName();
46
47     /**
48      * Creates a connection factory.
49      *
50      * @see org.apache.activemq.test.JmsResourceProvider#createConnectionFactory()
51      */

52     public ConnectionFactory JavaDoc createConnectionFactory() throws Exception JavaDoc {
53         return new ActiveMQConnectionFactory(serverUri);
54     }
55     
56     /**
57      * Creates a connection.
58      *
59      * @see org.apache.activemq.test.JmsResourceProvider#createConnection(javax.jms.ConnectionFactory)
60      */

61     public Connection JavaDoc createConnection(ConnectionFactory JavaDoc cf) throws JMSException JavaDoc {
62         Connection JavaDoc connection = cf.createConnection();
63         if (getClientID()!=null) {
64             connection.setClientID(getClientID());
65         }
66         return connection;
67     }
68     
69     /**
70      * @see org.apache.activemq.test.JmsResourceProvider#createSession(javax.jms.Connection)
71      */

72     public Session JavaDoc createSession(Connection JavaDoc conn) throws JMSException JavaDoc {
73         return conn.createSession(transacted, ackMode);
74     }
75     
76     /**
77      * @see org.apache.activemq.test.JmsResourceProvider#createConsumer(javax.jms.Session,
78      * javax.jms.Destination)
79      */

80     public MessageConsumer JavaDoc createConsumer(Session JavaDoc session,
81             Destination JavaDoc destination) throws JMSException JavaDoc {
82         if (isDurableSubscriber()) {
83             return session.createDurableSubscriber((Topic JavaDoc) destination, durableName);
84         }
85         return session.createConsumer(destination);
86     }
87
88     /**
89      * Creates a connection for a consumer.
90      *
91      * @param ssp - ServerSessionPool
92      * @return ConnectionConsumer
93      */

94     public ConnectionConsumer JavaDoc createConnectionConsumer(Connection JavaDoc connection, Destination JavaDoc destination, ServerSessionPool JavaDoc ssp) throws JMSException JavaDoc {
95         return connection.createConnectionConsumer(destination,null,ssp,1);
96     }
97
98     /**
99      * Creates a producer.
100      *
101      * @see org.apache.activemq.test.JmsResourceProvider#createProducer(javax.jms.Session,
102      * javax.jms.Destination)
103      */

104     public MessageProducer JavaDoc createProducer(Session JavaDoc session,
105             Destination JavaDoc destination) throws JMSException JavaDoc {
106         MessageProducer JavaDoc producer = session.createProducer(destination);
107         producer.setDeliveryMode(deliveryMode);
108         return producer;
109     }
110     
111     /**
112      * Creates a destination, which can either a topic or a queue.
113      *
114      * @see org.apache.activemq.test.JmsResourceProvider#createDestination(javax.jms.Session,
115      * java.lang.String)
116      */

117     public Destination JavaDoc createDestination(Session JavaDoc session, String JavaDoc name)
118             throws JMSException JavaDoc {
119         if( isTopic )
120             return session.createTopic("TOPIC."+name);
121         else
122             return session.createQueue("QUEUE."+name);
123     }
124
125     /**
126      * Returns true if the subscriber is durable.
127      *
128      * @return isDurableSubscriber
129      */

130     public boolean isDurableSubscriber() {
131         return isTopic && durableName!=null;
132     }
133
134     /**
135      * Returns the acknowledgement mode.
136      *
137      * @return Returns the ackMode.
138      */

139     public int getAckMode() {
140         return ackMode;
141     }
142     
143     /**
144      * Sets the acnknowledgement mode.
145      *
146      * @param ackMode The ackMode to set.
147      */

148     public void setAckMode(int ackMode) {
149         this.ackMode = ackMode;
150     }
151     
152     /**
153      * Returns true if the destination is a topic, false if the destination is a queue.
154      *
155      * @return Returns the isTopic.
156      */

157     public boolean isTopic() {
158         return isTopic;
159     }
160     
161     /**
162      * @param isTopic The isTopic to set.
163      */

164     public void setTopic(boolean isTopic) {
165         this.isTopic = isTopic;
166     }
167     
168     /**
169      * Returns the server URI.
170      *
171      * @return Returns the serverUri.
172      */

173     public String JavaDoc getServerUri() {
174         return serverUri;
175     }
176     
177     /**
178      * Sets the server URI.
179      *
180      * @param serverUri - the server URI to set.
181      */

182     public void setServerUri(String JavaDoc serverUri) {
183         this.serverUri = serverUri;
184     }
185     
186     /**
187      * Return true if the session is transacted.
188      *
189      * @return Returns the transacted.
190      */

191     public boolean isTransacted() {
192         return transacted;
193     }
194
195     /**
196      * Sets the session to be transacted.
197      *
198      * @param transacted
199      */

200     public void setTransacted(boolean transacted) {
201         this.transacted = transacted;
202     }
203
204     /**
205      * Returns the delivery mode.
206      *
207      * @return deliveryMode
208      */

209     public int getDeliveryMode() {
210         return deliveryMode;
211     }
212
213     /**
214      * Sets the delivery mode.
215      *
216      * @param deliveryMode
217      */

218     public void setDeliveryMode(int deliveryMode) {
219         this.deliveryMode = deliveryMode;
220     }
221
222     /**
223      * Returns the client id.
224      *
225      * @return clientID
226      */

227     public String JavaDoc getClientID() {
228         return clientID;
229     }
230
231     /**
232      * Sets the client id.
233      *
234      * @param clientID
235      */

236     public void setClientID(String JavaDoc clientID) {
237         this.clientID = clientID;
238     }
239
240     /**
241      * Returns the durable name of the provider.
242      *
243      * @return durableName
244      */

245     public String JavaDoc getDurableName() {
246         return durableName;
247     }
248
249     /**
250      * Sets the durable name of the provider.
251      *
252      * @param durableName
253      */

254     public void setDurableName(String JavaDoc durableName) {
255         this.durableName = durableName;
256     }
257 }
258
Popular Tags