Author: jstrachan
Date: Fri Mar 28 02:42:40 2008
New Revision: 642166
URL: http://svn.apache.org/viewvc?rev=642166&view=rev
Log:
added a fix for http://issues.apache.org/activemq/browse/AMQ-1636 to avoid
creating unnecessary connections when sending messages from Camel
Modified:
activemq/trunk/activemq-core/src/main/java/org/apache/activemq/camel/component/ActiveMQConfiguration.java
Modified:
activemq/trunk/activemq-core/src/main/java/org/apache/activemq/camel/component/ActiveMQConfiguration.java
URL:
http://svn.apache.org/viewvc/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/camel/component/ActiveMQConfiguration.java?rev=642166&r1=642165&r2=642166&view=diff
==============================================================================
---
activemq/trunk/activemq-core/src/main/java/org/apache/activemq/camel/component/ActiveMQConfiguration.java
(original)
+++
activemq/trunk/activemq-core/src/main/java/org/apache/activemq/camel/component/ActiveMQConfiguration.java
Fri Mar 28 02:42:40 2008
@@ -16,17 +16,23 @@
*/
package org.apache.activemq.camel.component;
-import org.apache.activemq.pool.PooledConnectionFactory;
-import org.apache.activemq.spring.ActiveMQConnectionFactory;
-import org.apache.camel.component.jms.JmsConfiguration;
+import java.lang.reflect.Constructor;
+import java.lang.reflect.InvocationTargetException;
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$
*/
public class ActiveMQConfiguration extends JmsConfiguration {
private String brokerURL = ActiveMQConnectionFactory.DEFAULT_BROKER_URL;
+ private boolean useSingleConnection = true;
+ private boolean usePooledConnection = false;
public ActiveMQConfiguration() {
}
@@ -45,6 +51,40 @@
this.brokerURL = brokerURL;
}
+ public boolean isUseSingleConnection() {
+ return useSingleConnection;
+ }
+
+ /**
+ * Enables or disables whether a Spring [EMAIL PROTECTED]
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 [EMAIL PROTECTED] 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 [EMAIL PROTECTED] 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
protected ConnectionFactory createConnectionFactory() {
ActiveMQConnectionFactory answer = new ActiveMQConnectionFactory();
@@ -52,6 +92,46 @@
answer.setBeanName("Camel");
}
answer.setBrokerURL(getBrokerURL());
- return answer;
+ if (isUsePooledConnection()) {
+ return createPooledConnectionFactory(answer);
+ }
+ else if (isUseSingleConnection()) {
+ return new SingleConnectionFactory(answer);
+ //return new PooledConnectionFactory(answer);
+ }
+ else {
+ 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;
}
}