KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > tool > ToolSupport


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.tool;
19
20 import org.apache.activemq.ActiveMQConnection;
21 import org.apache.activemq.ActiveMQConnectionFactory;
22 import org.apache.activemq.util.IndentPrinter;
23
24 import javax.jms.Connection JavaDoc;
25 import javax.jms.Destination JavaDoc;
26 import javax.jms.JMSException JavaDoc;
27 import javax.jms.Session JavaDoc;
28
29 /**
30  * Abstract base class useful for implementation inheritence
31  *
32  * @version $Revision$
33  */

34 public class ToolSupport {
35
36
37     protected Destination JavaDoc destination;
38     protected String JavaDoc subject = "TOOL.DEFAULT";
39     protected boolean topic = true;
40     protected String JavaDoc user = ActiveMQConnection.DEFAULT_USER;
41     protected String JavaDoc pwd = ActiveMQConnection.DEFAULT_PASSWORD;
42     protected String JavaDoc url = ActiveMQConnection.DEFAULT_BROKER_URL;
43     protected boolean transacted = false;
44     protected boolean durable = false;
45     protected String JavaDoc clientID = getClass().getName();
46     protected int ackMode = Session.AUTO_ACKNOWLEDGE;
47     protected String JavaDoc consumerName = "James";
48
49
50     protected Session JavaDoc createSession(Connection connection) throws Exception JavaDoc {
51         if (durable) {
52             connection.setClientID(clientID);
53         }
54         Session JavaDoc session = connection.createSession(transacted, ackMode);
55         if (topic) {
56             destination = session.createTopic(subject);
57         }
58         else {
59             destination = session.createQueue(subject);
60         }
61         return session;
62     }
63
64     protected Connection createConnection() throws JMSException JavaDoc, Exception JavaDoc {
65         ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(user, pwd, url);
66         return connectionFactory.createConnection();
67     }
68
69     protected void close(Connection connection, Session JavaDoc session) throws JMSException JavaDoc {
70         // lets dump the stats
71
dumpStats(connection);
72
73         if (session != null) {
74             session.close();
75         }
76         if (connection != null) {
77             connection.close();
78         }
79     }
80
81     protected void dumpStats(Connection connection) {
82         ActiveMQConnection c = (ActiveMQConnection) connection;
83         c.getConnectionStats().dump(new IndentPrinter());
84     }
85 }
86
Popular Tags