001 /** 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 package org.apache.activemq.tool; 018 019 import javax.jms.Connection; 020 import javax.jms.Destination; 021 import javax.jms.JMSException; 022 import javax.jms.Session; 023 024 import org.apache.activemq.ActiveMQConnection; 025 import org.apache.activemq.ActiveMQConnectionFactory; 026 import org.apache.activemq.util.IndentPrinter; 027 028 /** 029 * Abstract base class useful for implementation inheritence 030 * 031 * @version $Revision$ 032 */ 033 public class ToolSupport { 034 035 protected Destination destination; 036 protected String subject = "TOOL.DEFAULT"; 037 protected boolean topic = true; 038 protected String user = ActiveMQConnection.DEFAULT_USER; 039 protected String pwd = ActiveMQConnection.DEFAULT_PASSWORD; 040 protected String url = ActiveMQConnection.DEFAULT_BROKER_URL; 041 protected boolean transacted; 042 protected boolean durable; 043 protected String clientID = getClass().getName(); 044 protected int ackMode = Session.AUTO_ACKNOWLEDGE; 045 protected String consumerName = "James"; 046 047 protected Session createSession(Connection connection) throws Exception { 048 if (durable) { 049 connection.setClientID(clientID); 050 } 051 Session session = connection.createSession(transacted, ackMode); 052 if (topic) { 053 destination = session.createTopic(subject); 054 } else { 055 destination = session.createQueue(subject); 056 } 057 return session; 058 } 059 060 protected Connection createConnection() throws JMSException, Exception { 061 ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(user, pwd, url); 062 return connectionFactory.createConnection(); 063 } 064 065 protected void close(Connection connection, Session session) throws JMSException { 066 // lets dump the stats 067 dumpStats(connection); 068 069 if (session != null) { 070 session.close(); 071 } 072 if (connection != null) { 073 connection.close(); 074 } 075 } 076 077 protected void dumpStats(Connection connection) { 078 ActiveMQConnection c = (ActiveMQConnection)connection; 079 c.getConnectionStats().dump(new IndentPrinter()); 080 } 081 }