Save This Page
Home » activemq-parent-5.3.1-source-release » org.apache » activemq » broker » util » [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.broker.util;
   18   
   19   import javax.annotation.PostConstruct;
   20   import javax.annotation.PreDestroy;
   21   import javax.jms.Connection;
   22   import javax.jms.ConnectionFactory;
   23   import javax.jms.Destination;
   24   import javax.jms.ExceptionListener;
   25   import javax.jms.JMSException;
   26   import javax.jms.MessageConsumer;
   27   import javax.jms.Session;
   28   import org.apache.activemq.ActiveMQConnectionFactory;
   29   import org.apache.activemq.Service;
   30   import org.apache.activemq.advisory.AdvisorySupport;
   31   import org.apache.activemq.util.ServiceStopper;
   32   import org.apache.commons.logging.Log;
   33   import org.apache.commons.logging.LogFactory;
   34   
   35   /**
   36    * An agent which listens to commands on a JMS destination
   37    * 
   38    * @version $Revision: 916678 $
   39    * @org.apache.xbean.XBean
   40    */
   41   public class CommandAgent implements Service, ExceptionListener {
   42       private static final Log LOG = LogFactory.getLog(CommandAgent.class);
   43   
   44       private String brokerUrl = "vm://localhost";
   45       private String username;
   46       private String password;
   47       private ConnectionFactory connectionFactory;
   48       private Connection connection;
   49       private Destination commandDestination;
   50       private CommandMessageListener listener;
   51       private Session session;
   52       private MessageConsumer consumer;
   53   
   54       /**
   55        *
   56        * @throws Exception
   57        * @org.apache.xbean.InitMethod
   58        */
   59       @PostConstruct
   60       public void start() throws Exception {
   61           session = getConnection().createSession(false, Session.AUTO_ACKNOWLEDGE);
   62           listener = new CommandMessageListener(session);
   63           Destination destination = getCommandDestination();
   64           if (LOG.isDebugEnabled()) {
   65               LOG.debug("Agent subscribing to control destination: " + destination);
   66           }
   67           consumer = session.createConsumer(destination);
   68           consumer.setMessageListener(listener);
   69       }
   70   
   71       /**
   72        *
   73        * @throws Exception
   74        * @org.apache.xbean.DestroyMethod
   75        */
   76       @PreDestroy
   77       public void stop() throws Exception {
   78           ServiceStopper stopper = new ServiceStopper();
   79           if (consumer != null) {
   80               try {
   81                   consumer.close();
   82                   consumer = null;
   83               } catch (JMSException e) {
   84                   stopper.onException(this, e);
   85               }
   86           }
   87           if (session != null) {
   88               try {
   89                   session.close();
   90                   session = null;
   91               } catch (JMSException e) {
   92                   stopper.onException(this, e);
   93               }
   94           }
   95           if (connection != null) {
   96               try {
   97                   connection.close();
   98                   connection = null;
   99               } catch (JMSException e) {
  100                   stopper.onException(this, e);
  101               }
  102           }
  103           stopper.throwFirstException();
  104       }
  105   
  106       // Properties
  107       // -------------------------------------------------------------------------
  108       public String getBrokerUrl() {
  109           return brokerUrl;
  110       }
  111   
  112       public void setBrokerUrl(String brokerUrl) {
  113           this.brokerUrl = brokerUrl;
  114       }    
  115   
  116       public String getUsername() {
  117   		return username;
  118   	}
  119   
  120   	public void setUsername(String username) {
  121   		this.username = username;
  122   	}
  123   
  124   	public String getPassword() {
  125   		return password;
  126   	}
  127   
  128   	public void setPassword(String password) {
  129   		this.password = password;
  130   	}
  131   
  132   	public ConnectionFactory getConnectionFactory() {
  133           if (connectionFactory == null) {
  134               connectionFactory = new ActiveMQConnectionFactory(brokerUrl);
  135           }
  136           return connectionFactory;
  137       }
  138   
  139       public void setConnectionFactory(ConnectionFactory connectionFactory) {
  140           this.connectionFactory = connectionFactory;
  141       }
  142   
  143       public Connection getConnection() throws JMSException {
  144           if (connection == null) {
  145               connection = createConnection();
  146               connection.setExceptionListener(this);
  147               connection.start();
  148           }
  149           return connection;
  150       }
  151   
  152       public void setConnection(Connection connection) {
  153           this.connection = connection;
  154       }
  155   
  156       public Destination getCommandDestination() {
  157           if (commandDestination == null) {
  158               commandDestination = createCommandDestination();
  159           }
  160           return commandDestination;
  161       }
  162   
  163       public void setCommandDestination(Destination commandDestination) {
  164           this.commandDestination = commandDestination;
  165       }
  166   
  167       protected Connection createConnection() throws JMSException {
  168           return getConnectionFactory().createConnection(username, password);
  169       }
  170   
  171       protected Destination createCommandDestination() {
  172           return AdvisorySupport.getAgentDestination();
  173       }
  174   
  175       public void onException(JMSException exception) {
  176           try {
  177               stop();
  178           } catch (Exception e) {
  179           }
  180       }
  181   }

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