ToolSupport xref

    1   /***
    2    *
    3    * Copyright 2004 Protique Ltd
    4    *
    5    * Licensed under the Apache License, Version 2.0 (the "License");
    6    * you may not use this file except in compliance with the License.
    7    * You may obtain a copy of the License at
    8    *
    9    * http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   *
    17   **/
    18  package org.codehaus.activemq.tool;
    19
    20  import org.codehaus.activemq.ActiveMQConnection;
    21  import org.codehaus.activemq.ActiveMQConnectionFactory;
    22  import org.codehaus.activemq.util.IndentPrinter;
    23
    24  import javax.jms.Connection;
    25  import javax.jms.Destination;
    26  import javax.jms.JMSException;
    27  import javax.jms.Session;
    28
    29  /***
    30   * Abstract base class useful for implementation inheritence
    31   *
    32   * @version $Revision: 1.10 $
    33   */
    34  public class ToolSupport {
    35
    36
    37      protected Destination destination;
    38      protected String subject = "TOOL.DEFAULT";
    39      protected boolean topic = true;
    40      protected String user = ActiveMQConnection.DEFAULT_USER;
    41      protected String pwd = ActiveMQConnection.DEFAULT_PASSWORD;
    42      protected String url = ActiveMQConnection.DEFAULT_URL;
    43      protected boolean transacted = false;
    44      protected boolean durable = false;
    45      protected String clientID = getClass().getName();
    46      protected int ackMode = Session.AUTO_ACKNOWLEDGE;
    47      protected String consumerName = "James";
    48
    49
    50      protected Session createSession(Connection connection) throws Exception {
    51          if (durable) {
    52              connection.setClientID(clientID);
    53          }
    54          Session 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, Exception {
    65          ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(user, pwd, url);
    66          return connectionFactory.createConnection();
    67      }
    68
    69      protected void close(Connection connection, Session session) throws JMSException {
    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  }