KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > systest > impl > JmsClientSupport


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.systest.impl;
19
20 import org.apache.activemq.systest.AgentStopper;
21 import org.apache.activemq.systest.AgentSupport;
22 import org.apache.activemq.systest.BrokerAgent;
23 import org.apache.activemq.systest.ClientAgent;
24
25 import javax.jms.Connection JavaDoc;
26 import javax.jms.ConnectionFactory JavaDoc;
27 import javax.jms.Destination JavaDoc;
28 import javax.jms.JMSException JavaDoc;
29 import javax.jms.Session JavaDoc;
30
31 /**
32  * Simple base class for JMS client agents.
33  *
34  * @version $Revision: 1.1 $
35  */

36 public class JmsClientSupport extends AgentSupport implements ClientAgent {
37
38     private ConnectionFactory JavaDoc connectionFactory;
39     private Connection JavaDoc connection;
40     private Session JavaDoc session;
41     private boolean transacted;
42     private int acknowledgementMode = Session.CLIENT_ACKNOWLEDGE;
43     private Destination JavaDoc destination;
44
45     public void start() throws Exception JavaDoc {
46         getConnection().start();
47     }
48
49     public void connectTo(BrokerAgent broker) throws Exception JavaDoc {
50         // lets stop the connection then start a new connection
51
stop();
52         setConnectionFactory(broker.getConnectionFactory());
53         start();
54     }
55
56     // Properties
57
// -------------------------------------------------------------------------
58
public Destination JavaDoc getDestination() {
59         if (destination == null) {
60             throw new IllegalArgumentException JavaDoc("You must set the 'destination' property");
61         }
62         return destination;
63     }
64
65     public void setDestination(Destination JavaDoc destination) {
66         this.destination = destination;
67     }
68
69     public int getAcknowledgementMode() {
70         return acknowledgementMode;
71     }
72
73     public void setAcknowledgementMode(int acknowledgementMode) {
74         this.acknowledgementMode = acknowledgementMode;
75     }
76
77     public ConnectionFactory JavaDoc getConnectionFactory() {
78         if (connectionFactory == null) {
79             throw new IllegalArgumentException JavaDoc("You must set the 'connectionFactory' property");
80         }
81         return connectionFactory;
82     }
83
84     public void setConnectionFactory(ConnectionFactory JavaDoc connectionFactory) {
85         this.connectionFactory = connectionFactory;
86     }
87
88     public boolean isTransacted() {
89         return transacted;
90     }
91
92     public void setTransacted(boolean transacted) {
93         this.transacted = transacted;
94     }
95
96     public Connection JavaDoc getConnection() throws JMSException JavaDoc {
97         if (connection == null) {
98             connection = createConnection();
99         }
100         return connection;
101     }
102
103     public Session JavaDoc getSession() throws JMSException JavaDoc {
104         if (session == null) {
105             session = createSession();
106         }
107         return session;
108     }
109
110     public void stop(AgentStopper stopper) {
111         if (session != null) {
112             try {
113                 session.close();
114             }
115             catch (JMSException JavaDoc e) {
116                 stopper.onException(this, e);
117             }
118             finally {
119                 session = null;
120             }
121         }
122         if (connection != null) {
123             try {
124                 connection.close();
125             }
126             catch (JMSException JavaDoc e) {
127                 stopper.onException(this, e);
128             }
129             finally {
130                 connection = null;
131             }
132         }
133     }
134
135     // Implementation methods
136
// -------------------------------------------------------------------------
137
protected Connection JavaDoc createConnection() throws JMSException JavaDoc {
138         return getConnectionFactory().createConnection();
139     }
140
141     protected Session JavaDoc createSession() throws JMSException JavaDoc {
142         return getConnection().createSession(transacted, acknowledgementMode);
143     }
144
145 }
146
Popular Tags