Repository: activemq Updated Branches: refs/heads/master eb9c584fb -> 29e6f63d5
[AMQ-6380] Bundle to create pooled ConnectionFactories from configs in OSGi Project: http://git-wip-us.apache.org/repos/asf/activemq/repo Commit: http://git-wip-us.apache.org/repos/asf/activemq/commit/5a6d1550 Tree: http://git-wip-us.apache.org/repos/asf/activemq/tree/5a6d1550 Diff: http://git-wip-us.apache.org/repos/asf/activemq/diff/5a6d1550 Branch: refs/heads/master Commit: 5a6d1550fb854dc7251dd0e545c2498aea92d012 Parents: eb9c584 Author: Christian Schneider <[email protected]> Authored: Thu Jul 28 17:10:13 2016 +0200 Committer: Christopher L. Shannon (cshannon) <[email protected]> Committed: Mon Aug 1 13:56:04 2016 -0400 ---------------------------------------------------------------------- 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(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/activemq/blob/5a6d1550/activemq-cf/README.md ---------------------------------------------------------------------- diff --git a/activemq-cf/README.md b/activemq-cf/README.md new file mode 100644 index 0000000..6c16215 --- /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. http://git-wip-us.apache.org/repos/asf/activemq/blob/5a6d1550/activemq-cf/org.apache.activemq.cfg ---------------------------------------------------------------------- diff --git a/activemq-cf/org.apache.activemq.cfg b/activemq-cf/org.apache.activemq.cfg new file mode 100644 index 0000000..07899a5 --- /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 http://git-wip-us.apache.org/repos/asf/activemq/blob/5a6d1550/activemq-cf/pom.xml ---------------------------------------------------------------------- diff --git a/activemq-cf/pom.xml b/activemq-cf/pom.xml new file mode 100644 index 0000000..bd8880a --- /dev/null +++ b/activemq-cf/pom.xml @@ -0,0 +1,75 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + + <!-- + + 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. + --> + + <modelVersion>4.0.0</modelVersion> + + <parent> + <groupId>org.apache.activemq</groupId> + <artifactId>activemq-parent</artifactId> + <version>5.14.0-SNAPSHOT</version> + </parent> + + <artifactId>activemq-cf</artifactId> + <name>ActiveMQ :: ConnectionFactory</name> + <description>ActiveMQ ConnectionFactory service</description> + + <packaging>bundle</packaging> + + <dependencies> + <dependency> + <groupId>org.osgi</groupId> + <artifactId>org.osgi.core</artifactId> + </dependency> + <dependency> + <groupId>org.osgi</groupId> + <artifactId>org.osgi.compendium</artifactId> + <version>5.0.0</version> + </dependency> + <dependency> + <groupId>org.apache.activemq</groupId> + <artifactId>activemq-client</artifactId> + </dependency> + <dependency> + <groupId>org.apache.activemq</groupId> + <artifactId>activemq-jms-pool</artifactId> + </dependency> + </dependencies> + + <build> + <plugins> + <plugin> + <groupId>org.apache.felix</groupId> + <artifactId>maven-bundle-plugin</artifactId> + <version>3.2.0</version> + <configuration> + <instructions> + <Export-Package> + !* + </Export-Package> + <Private-Package> + org.apache.activemq.osgi.cf + </Private-Package> + </instructions> + </configuration> + </plugin> + </plugins> + </build> +</project> http://git-wip-us.apache.org/repos/asf/activemq/blob/5a6d1550/activemq-cf/src/main/java/org/apache/activemq/osgi/cf/ConnectionFactoryProvider.java ---------------------------------------------------------------------- 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 0000000..67f64fb --- /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<ConnectionFactory> reg; + + @Activate + public void create(ComponentContext compContext) { + BundleContext context = compContext.getBundleContext(); + Dictionary<String, Object> 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<String, String> props = new Hashtable<String, String>(); + props.put(OSGI_JNDI_SERVICE_NAME, jndiName); + reg = context.registerService(ConnectionFactory.class, pcf, props); + } + + @Deactivate + public void deactivate() { + reg.unregister(); + } + + private String getString(Dictionary<String, Object> config, String key, String defaultValue) { + Object value = config.get(key); + return value != null ? value.toString() : defaultValue; + } +} http://git-wip-us.apache.org/repos/asf/activemq/blob/5a6d1550/activemq-karaf/src/main/resources/features-core.xml ---------------------------------------------------------------------- diff --git a/activemq-karaf/src/main/resources/features-core.xml b/activemq-karaf/src/main/resources/features-core.xml index 04ddab7..6ce95fa 100644 --- a/activemq-karaf/src/main/resources/features-core.xml +++ b/activemq-karaf/src/main/resources/features-core.xml @@ -36,6 +36,12 @@ <bundle dependency="false">mvn:org.apache.xbean/xbean-spring/${xbean-version}</bundle> <bundle>mvn:org.apache.activemq/activemq-osgi/${project.version}</bundle> </feature> + + <feature name="activemq-cf" description="ActiveMQ ConnectionFactory from config" version="${project.version}" resolver="(obr)" start-level="50"> + <feature>activemq-client</feature> + <feature>scr</feature> + <bundle>mvn:org.apache.activemq/activemq-cf/${project.version}</bundle> + </feature> <!-- All bundles needed by the broker --> <feature name="activemq" description="ActiveMQ broker libraries" version="${project.version}" resolver="(obr)" start-level="50"> http://git-wip-us.apache.org/repos/asf/activemq/blob/5a6d1550/pom.xml ---------------------------------------------------------------------- diff --git a/pom.xml b/pom.xml index 6396694..fe46265 100755 --- a/pom.xml +++ b/pom.xml @@ -244,6 +244,7 @@ <module>activemq-karaf</module> <module>activemq-jms-pool</module> <module>activemq-pool</module> + <module>activemq-cf</module> <module>activemq-ra</module> <module>activemq-rar</module> <module>activemq-run</module>
