Save This Page
Home » activemq-parent-5.3.1-source-release » org.apache » activemq » systest » impl » [javadoc | source]
    1   /**
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  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   package org.apache.activemq.systest.impl;
   18   
   19   import org.apache.activemq.systest.AgentStopper;
   20   import org.apache.activemq.systest.AgentSupport;
   21   import org.apache.activemq.systest.BrokerAgent;
   22   import org.apache.activemq.systest.ClientAgent;
   23   
   24   import javax.jms.Connection;
   25   import javax.jms.ConnectionFactory;
   26   import javax.jms.Destination;
   27   import javax.jms.JMSException;
   28   import javax.jms.Session;
   29   
   30   /**
   31    * Simple base class for JMS client agents.
   32    * 
   33    * @version $Revision: 1.1 $
   34    */
   35   public class JmsClientSupport extends AgentSupport implements ClientAgent {
   36   
   37       private ConnectionFactory connectionFactory;
   38       private Connection connection;
   39       private Session session;
   40       private boolean transacted;
   41       private int acknowledgementMode = Session.CLIENT_ACKNOWLEDGE;
   42       private Destination destination;
   43   
   44       public void start() throws Exception {
   45           getConnection().start();
   46       }
   47   
   48       public void connectTo(BrokerAgent broker) throws Exception {
   49           // lets stop the connection then start a new connection
   50           stop();
   51           setConnectionFactory(broker.getConnectionFactory());
   52           start();
   53       }
   54   
   55       // Properties
   56       // -------------------------------------------------------------------------
   57       public Destination getDestination() {
   58           if (destination == null) {
   59               throw new IllegalArgumentException("You must set the 'destination' property");
   60           }
   61           return destination;
   62       }
   63   
   64       public void setDestination(Destination destination) {
   65           this.destination = destination;
   66       }
   67   
   68       public int getAcknowledgementMode() {
   69           return acknowledgementMode;
   70       }
   71   
   72       public void setAcknowledgementMode(int acknowledgementMode) {
   73           this.acknowledgementMode = acknowledgementMode;
   74       }
   75   
   76       public ConnectionFactory getConnectionFactory() {
   77           if (connectionFactory == null) {
   78               throw new IllegalArgumentException("You must set the 'connectionFactory' property");
   79           }
   80           return connectionFactory;
   81       }
   82   
   83       public void setConnectionFactory(ConnectionFactory connectionFactory) {
   84           this.connectionFactory = connectionFactory;
   85       }
   86   
   87       public boolean isTransacted() {
   88           return transacted;
   89       }
   90   
   91       public void setTransacted(boolean transacted) {
   92           this.transacted = transacted;
   93       }
   94   
   95       public Connection getConnection() throws JMSException {
   96           if (connection == null) {
   97               connection = createConnection();
   98           }
   99           return connection;
  100       }
  101   
  102       public Session getSession() throws JMSException {
  103           if (session == null) {
  104               session = createSession();
  105           }
  106           return session;
  107       }
  108   
  109       public void stop(AgentStopper stopper) {
  110           if (session != null) {
  111               try {
  112                   session.close();
  113               }
  114               catch (JMSException e) {
  115                   stopper.onException(this, e);
  116               }
  117               finally {
  118                   session = null;
  119               }
  120           }
  121           if (connection != null) {
  122               try {
  123                   connection.close();
  124               }
  125               catch (JMSException e) {
  126                   stopper.onException(this, e);
  127               }
  128               finally {
  129                   connection = null;
  130               }
  131           }
  132       }
  133   
  134       // Implementation methods
  135       // -------------------------------------------------------------------------
  136       protected Connection createConnection() throws JMSException {
  137           return getConnectionFactory().createConnection();
  138       }
  139   
  140       protected Session createSession() throws JMSException {
  141           return getConnection().createSession(transacted, acknowledgementMode);
  142       }
  143   
  144   }

Save This Page
Home » activemq-parent-5.3.1-source-release » org.apache » activemq » systest » impl » [javadoc | source]