mirror of https://github.com/apache/activemq.git
added a fix for http://issues.apache.org/activemq/browse/AMQ-1636 to avoid creating unnecessary connections when sending messages from Camel
git-svn-id: https://svn.apache.org/repos/asf/activemq/trunk@642166 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
76ac7588e8
commit
53c9bab824
|
@ -16,17 +16,23 @@
|
||||||
*/
|
*/
|
||||||
package org.apache.activemq.camel.component;
|
package org.apache.activemq.camel.component;
|
||||||
|
|
||||||
import org.apache.activemq.pool.PooledConnectionFactory;
|
import java.lang.reflect.Constructor;
|
||||||
import org.apache.activemq.spring.ActiveMQConnectionFactory;
|
import java.lang.reflect.InvocationTargetException;
|
||||||
import org.apache.camel.component.jms.JmsConfiguration;
|
|
||||||
|
|
||||||
import javax.jms.ConnectionFactory;
|
import javax.jms.ConnectionFactory;
|
||||||
|
|
||||||
|
import org.apache.activemq.spring.ActiveMQConnectionFactory;
|
||||||
|
import org.apache.camel.component.jms.JmsConfiguration;
|
||||||
|
import org.springframework.jms.connection.SingleConnectionFactory;
|
||||||
|
import org.springframework.jms.core.JmsTemplate;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @version $Revision$
|
* @version $Revision$
|
||||||
*/
|
*/
|
||||||
public class ActiveMQConfiguration extends JmsConfiguration {
|
public class ActiveMQConfiguration extends JmsConfiguration {
|
||||||
private String brokerURL = ActiveMQConnectionFactory.DEFAULT_BROKER_URL;
|
private String brokerURL = ActiveMQConnectionFactory.DEFAULT_BROKER_URL;
|
||||||
|
private boolean useSingleConnection = true;
|
||||||
|
private boolean usePooledConnection = false;
|
||||||
|
|
||||||
public ActiveMQConfiguration() {
|
public ActiveMQConfiguration() {
|
||||||
}
|
}
|
||||||
|
@ -45,6 +51,40 @@ public class ActiveMQConfiguration extends JmsConfiguration {
|
||||||
this.brokerURL = brokerURL;
|
this.brokerURL = brokerURL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean isUseSingleConnection() {
|
||||||
|
return useSingleConnection;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Enables or disables whether a Spring {@link SingleConnectionFactory} will be used so that when
|
||||||
|
* messages are sent to ActiveMQ from outside of a message consuming thread, pooling will be used rather
|
||||||
|
* than the default with the Spring {@link JmsTemplate} which will create a new connection, session, producer
|
||||||
|
* for each message then close them all down again.
|
||||||
|
* <p/>
|
||||||
|
* The default value is true so that a single connection is used by default.
|
||||||
|
*
|
||||||
|
* @param useSingleConnection
|
||||||
|
*/
|
||||||
|
public void setUseSingleConnection(boolean useSingleConnection) {
|
||||||
|
this.useSingleConnection = useSingleConnection;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isUsePooledConnection() {
|
||||||
|
return usePooledConnection;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Enables or disables whether a PooledConnectionFactory will be used so that when
|
||||||
|
* messages are sent to ActiveMQ from outside of a message consuming thread, pooling will be used rather
|
||||||
|
* than the default with the Spring {@link JmsTemplate} which will create a new connection, session, producer
|
||||||
|
* for each message then close them all down again.
|
||||||
|
* <p/>
|
||||||
|
* The default value is false by default as it requires an extra dependency on commons-pool.
|
||||||
|
*/
|
||||||
|
public void setUsePooledConnection(boolean usePooledConnection) {
|
||||||
|
this.usePooledConnection = usePooledConnection;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected ConnectionFactory createConnectionFactory() {
|
protected ConnectionFactory createConnectionFactory() {
|
||||||
ActiveMQConnectionFactory answer = new ActiveMQConnectionFactory();
|
ActiveMQConnectionFactory answer = new ActiveMQConnectionFactory();
|
||||||
|
@ -52,6 +92,46 @@ public class ActiveMQConfiguration extends JmsConfiguration {
|
||||||
answer.setBeanName("Camel");
|
answer.setBeanName("Camel");
|
||||||
}
|
}
|
||||||
answer.setBrokerURL(getBrokerURL());
|
answer.setBrokerURL(getBrokerURL());
|
||||||
|
if (isUsePooledConnection()) {
|
||||||
|
return createPooledConnectionFactory(answer);
|
||||||
|
}
|
||||||
|
else if (isUseSingleConnection()) {
|
||||||
|
return new SingleConnectionFactory(answer);
|
||||||
|
//return new PooledConnectionFactory(answer);
|
||||||
|
}
|
||||||
|
else {
|
||||||
return answer;
|
return answer;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected ConnectionFactory createPooledConnectionFactory(ActiveMQConnectionFactory connectionFactory) {
|
||||||
|
// lets not use classes directly to avoid a runtime dependency on commons-pool
|
||||||
|
// for folks not using this option
|
||||||
|
try {
|
||||||
|
Class type = loadClass("org.apache.activemq.pool.PooledConnectionFactory", getClass().getClassLoader());
|
||||||
|
Constructor constructor = type.getConstructor(ActiveMQConnectionFactory.class);
|
||||||
|
return (ConnectionFactory) constructor.newInstance(connectionFactory);
|
||||||
|
}
|
||||||
|
catch (Exception e) {
|
||||||
|
throw new RuntimeException("Failed to instantiate PooledConnectionFactory: " + e, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Class<?> loadClass(String name, ClassLoader loader) throws ClassNotFoundException {
|
||||||
|
ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
|
||||||
|
if (contextClassLoader != null) {
|
||||||
|
try {
|
||||||
|
return contextClassLoader.loadClass(name);
|
||||||
|
}
|
||||||
|
catch (ClassNotFoundException e) {
|
||||||
|
try {
|
||||||
|
return loader.loadClass(name);
|
||||||
|
}
|
||||||
|
catch (ClassNotFoundException e1) {
|
||||||
|
throw e1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue