From 5b4c0dc4c25ba168970b74746b10e086699930e4 Mon Sep 17 00:00:00 2001 From: Christian Schneider Date: Thu, 28 Jul 2016 17:10:13 +0200 Subject: [PATCH] [AMQ-6380] Bundle to create pooled ConnectionFactories from configs in OSGi --- activemq-cf/README.md | 11 +++ activemq-cf/org.apache.activemq.cfg | 5 ++ activemq-cf/pom.xml | 75 +++++++++++++++++ .../osgi/cf/ConnectionFactoryProvider.java | 80 +++++++++++++++++++ .../src/main/resources/features-core.xml | 6 ++ pom.xml | 1 + 6 files changed, 178 insertions(+) create mode 100644 activemq-cf/README.md create mode 100644 activemq-cf/org.apache.activemq.cfg create mode 100644 activemq-cf/pom.xml create mode 100644 activemq-cf/src/main/java/org/apache/activemq/osgi/cf/ConnectionFactoryProvider.java diff --git a/activemq-cf/README.md b/activemq-cf/README.md new file mode 100644 index 00000000000..6c16215c295 --- /dev/null +++ b/activemq-cf/README.md @@ -0,0 +1,11 @@ +# Activemq-cf + +Allows to create a Pooled ActiveMQ ConnectionFactory from a config. + +## Install + +Install the activemq-client and scr features and this bundle. Then put the example config org.apache.karaf.activemq.cfg in etc. + + service:list ConnectionFactory + +This should show the ConnectionFactory as a service. diff --git a/activemq-cf/org.apache.activemq.cfg b/activemq-cf/org.apache.activemq.cfg new file mode 100644 index 00000000000..07899a5d69b --- /dev/null +++ b/activemq-cf/org.apache.activemq.cfg @@ -0,0 +1,5 @@ +# Example ConnectionFactory def for decanter +osgi.jndi.service.name=jms/local +url=tcp://localhost:61616 +userName=karaf +password=karaf diff --git a/activemq-cf/pom.xml b/activemq-cf/pom.xml new file mode 100644 index 00000000000..bd8880a200e --- /dev/null +++ b/activemq-cf/pom.xml @@ -0,0 +1,75 @@ + + + + + + 4.0.0 + + + org.apache.activemq + activemq-parent + 5.14.0-SNAPSHOT + + + activemq-cf + ActiveMQ :: ConnectionFactory + ActiveMQ ConnectionFactory service + + bundle + + + + org.osgi + org.osgi.core + + + org.osgi + org.osgi.compendium + 5.0.0 + + + org.apache.activemq + activemq-client + + + org.apache.activemq + activemq-jms-pool + + + + + + + org.apache.felix + maven-bundle-plugin + 3.2.0 + + + + !* + + + org.apache.activemq.osgi.cf + + + + + + + diff --git a/activemq-cf/src/main/java/org/apache/activemq/osgi/cf/ConnectionFactoryProvider.java b/activemq-cf/src/main/java/org/apache/activemq/osgi/cf/ConnectionFactoryProvider.java new file mode 100644 index 00000000000..67f64fb739d --- /dev/null +++ b/activemq-cf/src/main/java/org/apache/activemq/osgi/cf/ConnectionFactoryProvider.java @@ -0,0 +1,80 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.activemq.osgi.cf; + +import java.util.Dictionary; +import java.util.Hashtable; + +import javax.jms.ConnectionFactory; + +import org.apache.activemq.ActiveMQConnectionFactory; +import org.apache.activemq.jms.pool.PooledConnectionFactory; +import org.osgi.framework.BundleContext; +import org.osgi.framework.ServiceRegistration; +import org.osgi.service.component.ComponentContext; +import org.osgi.service.component.annotations.Activate; +import org.osgi.service.component.annotations.Component; +import org.osgi.service.component.annotations.ConfigurationPolicy; +import org.osgi.service.component.annotations.Deactivate; + +@Component // +( // + configurationPid = "org.apache.activemq", // + immediate = true, // + configurationPolicy = ConfigurationPolicy.REQUIRE // +) +public class ConnectionFactoryProvider { + + private static final String OSGI_JNDI_SERVICE_NAME = "osgi.jndi.service.name"; + private ServiceRegistration reg; + + @Activate + public void create(ComponentContext compContext) { + BundleContext context = compContext.getBundleContext(); + Dictionary config = compContext.getProperties(); + String brokerURL = getString(config, "url", "tcp://localhost:61616"); + String jndiName = getString(config, OSGI_JNDI_SERVICE_NAME, "jms/local"); + String userName = getString(config, "userName", null); + String password = getString(config, "password", null); + long expiryTimeout = new Long(getString(config, "expiryTimeout", "0")); + int idleTimeout = new Integer(getString(config, "idleTimeout", "30000")); + int maxConnections = new Integer(getString(config, "maxConnections", "8")); + ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory(brokerURL); + if (userName != null) { + cf.setUserName(userName); + cf.setPassword(password); + } + PooledConnectionFactory pcf = new PooledConnectionFactory(); + pcf.setConnectionFactory(cf); + pcf.setExpiryTimeout(expiryTimeout); + pcf.setIdleTimeout(idleTimeout); + pcf.setMaxConnections(maxConnections); + Dictionary props = new Hashtable(); + props.put(OSGI_JNDI_SERVICE_NAME, jndiName); + reg = context.registerService(ConnectionFactory.class, pcf, props); + } + + @Deactivate + public void deactivate() { + reg.unregister(); + } + + private String getString(Dictionary config, String key, String defaultValue) { + Object value = config.get(key); + return value != null ? value.toString() : defaultValue; + } +} diff --git a/activemq-karaf/src/main/resources/features-core.xml b/activemq-karaf/src/main/resources/features-core.xml index 04ddab794c4..6ce95fabb23 100644 --- a/activemq-karaf/src/main/resources/features-core.xml +++ b/activemq-karaf/src/main/resources/features-core.xml @@ -36,6 +36,12 @@ mvn:org.apache.xbean/xbean-spring/${xbean-version} mvn:org.apache.activemq/activemq-osgi/${project.version} + + + activemq-client + scr + mvn:org.apache.activemq/activemq-cf/${project.version} + diff --git a/pom.xml b/pom.xml index 6396694e970..fe46265fc22 100755 --- a/pom.xml +++ b/pom.xml @@ -244,6 +244,7 @@ activemq-karaf activemq-jms-pool activemq-pool + activemq-cf activemq-ra activemq-rar activemq-run