Home » activemq-parent-5.3.1-source-release » org.apache » activemq » camel » component » [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.camel.component;
   18   
   19   import java.lang.reflect.Constructor;
   20   
   21   import javax.jms.ConnectionFactory;
   22   
   23   import org.apache.activemq.Service;
   24   import org.apache.activemq.spring.ActiveMQConnectionFactory;
   25   import org.apache.camel.component.jms.JmsConfiguration;
   26   import org.springframework.jms.connection.SingleConnectionFactory;
   27   import org.springframework.jms.connection.JmsTransactionManager;
   28   import org.springframework.jms.core.JmsTemplate;
   29   import org.springframework.transaction.PlatformTransactionManager;
   30   
   31   /**
   32    * @version $Revision: 910986 $
   33    */
   34   public class ActiveMQConfiguration extends JmsConfiguration {
   35       private String brokerURL = ActiveMQConnectionFactory.DEFAULT_BROKER_URL;
   36       private boolean useSingleConnection = false;
   37       private boolean usePooledConnection = true;
   38       private String userName;
   39       private String password;
   40       private ActiveMQComponent activeMQComponent;
   41   
   42       public ActiveMQConfiguration() {
   43       }
   44   
   45       public String getBrokerURL() {
   46           return brokerURL;
   47       }
   48   
   49       /**
   50        * Sets the broker URL to use to connect to ActiveMQ using the
   51        * <a href="http://activemq.apache.org/configuring-transports.html">ActiveMQ URI format</a>
   52        *
   53        * @param brokerURL the URL of the broker.
   54        */
   55       public void setBrokerURL(String brokerURL) {
   56           this.brokerURL = brokerURL;
   57       }
   58   
   59       public boolean isUseSingleConnection() {
   60           return useSingleConnection;
   61       }
   62   
   63       public String getUserName() {
   64           return userName;
   65       }
   66   
   67       /**
   68        * Sets the username to be used to login to ActiveMQ
   69        * @param userName
   70        */
   71       public void setUserName(String userName) {
   72           this.userName = userName;
   73       }
   74   
   75       public String getPassword() {
   76           return password;
   77       }
   78   
   79       /**
   80        * Sets the password/passcode used to login to ActiveMQ
   81        *
   82        * @param password
   83        */
   84       public void setPassword(String password) {
   85           this.password = password;
   86       }
   87   
   88       /**
   89        * Enables or disables whether a Spring {@link SingleConnectionFactory} will be used so that when
   90        * messages are sent to ActiveMQ from outside of a message consuming thread, pooling will be used rather
   91        * than the default with the Spring {@link JmsTemplate} which will create a new connection, session, producer
   92        * for each message then close them all down again.
   93        * <p/>
   94        * The default value is true so that a single connection is used by default.
   95        *
   96        * @param useSingleConnection
   97        */
   98       public void setUseSingleConnection(boolean useSingleConnection) {
   99           this.useSingleConnection = useSingleConnection;
  100       }
  101   
  102       public boolean isUsePooledConnection() {
  103           return usePooledConnection;
  104       }
  105   
  106       /**
  107        * Enables or disables whether a PooledConnectionFactory will be used so that when
  108        * messages are sent to ActiveMQ from outside of a message consuming thread, pooling will be used rather
  109        * than the default with the Spring {@link JmsTemplate} which will create a new connection, session, producer
  110        * for each message then close them all down again.
  111        * <p/>
  112        * The default value is false by default as it requires an extra dependency on commons-pool.
  113        */
  114       public void setUsePooledConnection(boolean usePooledConnection) {
  115           this.usePooledConnection = usePooledConnection;
  116       }
  117   
  118       @Override
  119       public PlatformTransactionManager getTransactionManager() {
  120           PlatformTransactionManager answer = super.getTransactionManager();
  121           if (isTransacted() && answer == null) {
  122               // lets auto-default the transaction manager if its not specified
  123               answer = createTransactionManager();
  124               setTransactionManager(answer);
  125               answer = getTransactionManager();
  126           }
  127           return answer;
  128       }
  129   
  130       /**
  131        * Factory method to create a default transaction manager if one is not specified
  132        */
  133       protected PlatformTransactionManager createTransactionManager() {
  134           JmsTransactionManager answer = new JmsTransactionManager(getConnectionFactory());
  135           answer.afterPropertiesSet();
  136           return answer;
  137       }
  138   
  139       protected void setActiveMQComponent(ActiveMQComponent activeMQComponent) {
  140           this.activeMQComponent = activeMQComponent;
  141       }
  142   
  143       @Override
  144       protected ConnectionFactory createConnectionFactory() {
  145           ActiveMQConnectionFactory answer = new ActiveMQConnectionFactory();
  146           if (userName != null) {
  147               answer.setUserName(userName);
  148           }
  149           if (password != null) {
  150               answer.setPassword(password);
  151           }
  152           if (answer.getBeanName() == null) {
  153               answer.setBeanName("Camel");
  154           }
  155           answer.setBrokerURL(getBrokerURL());
  156           if (isUseSingleConnection()) {
  157               SingleConnectionFactory scf = new SingleConnectionFactory(answer);
  158               if (activeMQComponent != null) {
  159                   activeMQComponent.addSingleConnectionFactory(scf);
  160               }
  161               return scf;
  162           }
  163           else if (isUsePooledConnection()) {
  164               ConnectionFactory pcf = createPooledConnectionFactory(answer);
  165               if (activeMQComponent != null) {
  166                   activeMQComponent.addPooledConnectionFactoryService((Service) pcf);
  167               }
  168               return pcf;
  169           }
  170           else {
  171               return answer;
  172           }
  173       }
  174   
  175       protected ConnectionFactory createPooledConnectionFactory(ActiveMQConnectionFactory connectionFactory) {
  176           // lets not use classes directly to avoid a runtime dependency on commons-pool
  177           // for folks not using this option
  178           try {
  179               Class type = loadClass("org.apache.activemq.pool.PooledConnectionFactory", getClass().getClassLoader());
  180               Constructor constructor = type.getConstructor(org.apache.activemq.ActiveMQConnectionFactory.class);
  181               return (ConnectionFactory) constructor.newInstance(connectionFactory);
  182           }
  183           catch (Exception e) {
  184               throw new RuntimeException("Failed to instantiate PooledConnectionFactory: " + e, e);
  185           }
  186       }
  187   
  188       public static Class<?> loadClass(String name, ClassLoader loader) throws ClassNotFoundException {
  189           ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
  190           if (contextClassLoader != null) {
  191               try {
  192                   return contextClassLoader.loadClass(name);
  193               }
  194               catch (ClassNotFoundException e) {
  195                   try {
  196                       return loader.loadClass(name);
  197                   }
  198                   catch (ClassNotFoundException e1) {
  199                       throw e1;
  200                   }
  201               }
  202           } else {
  203               return loader.loadClass(name);
  204           }
  205       }
  206   }

Home » activemq-parent-5.3.1-source-release » org.apache » activemq » camel » component » [javadoc | source]