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.camel.component; 018 019 import java.util.concurrent.CopyOnWriteArrayList; 020 021 import org.apache.activemq.Service; 022 import org.apache.camel.CamelContext; 023 import org.apache.camel.component.jms.JmsComponent; 024 import org.apache.camel.component.jms.JmsConfiguration; 025 import org.springframework.jms.connection.SingleConnectionFactory; 026 027 /** 028 * The <a href="http://activemq.apache.org/camel/activemq.html">ActiveMQ Component</a> 029 * 030 * @version $Revision: 910986 $ 031 */ 032 public class ActiveMQComponent extends JmsComponent { 033 private final CopyOnWriteArrayList<SingleConnectionFactory> singleConnectionFactoryList = 034 new CopyOnWriteArrayList<SingleConnectionFactory>(); 035 private final CopyOnWriteArrayList<Service> pooledConnectionFactoryServiceList = 036 new CopyOnWriteArrayList<Service>(); 037 private boolean exposeAllQueues; 038 private CamelEndpointLoader endpointLoader; 039 040 /** 041 * Creates an <a href="http://activemq.apache.org/camel/activemq.html">ActiveMQ Component</a> 042 * 043 * @return the created component 044 */ 045 public static ActiveMQComponent activeMQComponent() { 046 return new ActiveMQComponent(); 047 } 048 049 /** 050 * Creates an <a href="http://activemq.apache.org/camel/activemq.html">ActiveMQ Component</a> 051 * connecting to the given <a href="http://activemq.apache.org/configuring-transports.html">broker URL</a> 052 * 053 * @param brokerURL the URL to connect to 054 * @return the created component 055 */ 056 public static ActiveMQComponent activeMQComponent(String brokerURL) { 057 ActiveMQComponent answer = new ActiveMQComponent(); 058 if (answer.getConfiguration() instanceof ActiveMQConfiguration) { 059 ((ActiveMQConfiguration) answer.getConfiguration()) 060 .setBrokerURL(brokerURL); 061 } 062 return answer; 063 } 064 065 public ActiveMQComponent() { 066 } 067 068 public ActiveMQComponent(CamelContext context) { 069 super(context); 070 } 071 072 public ActiveMQComponent(ActiveMQConfiguration configuration) { 073 super(configuration); 074 } 075 076 077 public void setBrokerURL(String brokerURL) { 078 if (getConfiguration() instanceof ActiveMQConfiguration) { 079 ((ActiveMQConfiguration)getConfiguration()).setBrokerURL(brokerURL); 080 } 081 } 082 083 public void setUserName(String userName) { 084 if (getConfiguration() instanceof ActiveMQConfiguration) { 085 ((ActiveMQConfiguration)getConfiguration()).setUserName(userName); 086 } 087 } 088 089 public void setPassword(String password) { 090 if (getConfiguration() instanceof ActiveMQConfiguration) { 091 ((ActiveMQConfiguration)getConfiguration()).setPassword(password); 092 } 093 } 094 095 public boolean isExposeAllQueues() { 096 return exposeAllQueues; 097 } 098 099 /** 100 * If enabled this will cause all Queues in the ActiveMQ broker to be eagerly populated into the CamelContext 101 * so that they can be easily browsed by any Camel tooling. This option is disabled by default. 102 * 103 * @param exposeAllQueues 104 */ 105 public void setExposeAllQueues(boolean exposeAllQueues) { 106 this.exposeAllQueues = exposeAllQueues; 107 } 108 109 public void setUsePooledConnection(boolean usePooledConnection) { 110 if (getConfiguration() instanceof ActiveMQConfiguration) { 111 ((ActiveMQConfiguration)getConfiguration()).setUsePooledConnection(usePooledConnection); 112 } 113 } 114 115 public void setUseSingleConnection(boolean useSingleConnection) { 116 if (getConfiguration() instanceof ActiveMQConfiguration) { 117 ((ActiveMQConfiguration)getConfiguration()).setUseSingleConnection(useSingleConnection); 118 } 119 } 120 121 protected void addPooledConnectionFactoryService(Service pooledConnectionFactoryService) { 122 pooledConnectionFactoryServiceList.add(pooledConnectionFactoryService); 123 } 124 125 protected void addSingleConnectionFactory(SingleConnectionFactory singleConnectionFactory) { 126 singleConnectionFactoryList.add(singleConnectionFactory); 127 } 128 129 @Override 130 protected void doStart() throws Exception { 131 super.doStart(); 132 if (isExposeAllQueues()) { 133 endpointLoader = new CamelEndpointLoader(getCamelContext()); 134 endpointLoader.afterPropertiesSet(); 135 } 136 } 137 138 @Override 139 protected void doStop() throws Exception { 140 if (endpointLoader != null) { 141 endpointLoader.destroy(); 142 endpointLoader = null; 143 } 144 for (Service s : pooledConnectionFactoryServiceList) { 145 s.stop(); 146 } 147 pooledConnectionFactoryServiceList.clear(); 148 for (SingleConnectionFactory s : singleConnectionFactoryList) { 149 s.destroy(); 150 } 151 singleConnectionFactoryList.clear(); 152 super.doStop(); 153 } 154 155 @Override 156 public void setConfiguration(JmsConfiguration configuration) { 157 if (configuration instanceof ActiveMQConfiguration) { 158 ((ActiveMQConfiguration) configuration).setActiveMQComponent(this); 159 } 160 super.setConfiguration(configuration); 161 } 162 163 @Override 164 protected JmsConfiguration createConfiguration() { 165 ActiveMQConfiguration answer = new ActiveMQConfiguration(); 166 answer.setActiveMQComponent(this); 167 return answer; 168 } 169 }