From 2ee742bd751285182fdc188a30409303b1a4dcd7 Mon Sep 17 00:00:00 2001 From: "John D. Ament" Date: Fri, 21 Oct 2016 21:18:59 -0400 Subject: [PATCH 1/3] ARTEMIS-756 Introduced an example for CDI integration, added a README to explain how it works. --- examples/features/sub-modules/cdi/pom.xml | 252 ++++++++++++++++++ examples/features/sub-modules/cdi/readme.md | 86 ++++++ .../artemis/core/example/CDIClientConfig.java | 87 ++++++ .../artemis/core/example/CDIExample.java | 36 +++ .../core/example/CDIMessagingIntegrator.java | 40 +++ .../META-INF/apache-deltaspike.properties | 22 ++ .../cdi/src/main/resources/META-INF/beans.xml | 25 ++ .../resources/activemq/server0/broker.xml | 77 ++++++ examples/features/sub-modules/pom.xml | 1 + .../src/main/resources/META-INF/beans.xml | 25 ++ .../javax.enterprise.inject.spi.Extension | 20 ++ 11 files changed, 671 insertions(+) create mode 100644 examples/features/sub-modules/cdi/pom.xml create mode 100644 examples/features/sub-modules/cdi/readme.md create mode 100644 examples/features/sub-modules/cdi/src/main/java/org/apache/activemq/artemis/core/example/CDIClientConfig.java create mode 100644 examples/features/sub-modules/cdi/src/main/java/org/apache/activemq/artemis/core/example/CDIExample.java create mode 100644 examples/features/sub-modules/cdi/src/main/java/org/apache/activemq/artemis/core/example/CDIMessagingIntegrator.java create mode 100644 examples/features/sub-modules/cdi/src/main/resources/META-INF/apache-deltaspike.properties create mode 100644 examples/features/sub-modules/cdi/src/main/resources/META-INF/beans.xml create mode 100644 examples/features/sub-modules/cdi/src/main/resources/activemq/server0/broker.xml create mode 100644 integration/artemis-cdi-integration/src/main/resources/META-INF/beans.xml create mode 100644 integration/artemis-cdi-integration/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension diff --git a/examples/features/sub-modules/cdi/pom.xml b/examples/features/sub-modules/cdi/pom.xml new file mode 100644 index 0000000000..30580d5b39 --- /dev/null +++ b/examples/features/sub-modules/cdi/pom.xml @@ -0,0 +1,252 @@ + + + + + 4.0.0 + + + org.apache.activemq.examples.modules + broker-modules + 1.5.0-SNAPSHOT + + + artemis-cdi-example + jar + ActiveMQ Artemis CDI Example + + + ${project.basedir}/../../../.. + 1.7.1 + + + + + org.apache.activemq + artemis-server + ${project.version} + + + org.apache.activemq + artemis-core-client + ${project.version} + + + org.apache.activemq + artemis-commons + ${project.version} + + + io.netty + netty-all + ${netty.version} + + + org.apache.geronimo.specs + geronimo-jms_2.0_spec + + + org.apache.activemq + artemis-cdi-client + ${project.version} + + + org.apache.deltaspike.cdictrl + deltaspike-cdictrl-api + ${deltaspike.version} + + + javax.enterprise + cdi-api + + + org.apache.deltaspike.core + deltaspike-core-api + ${deltaspike.version} + + + org.apache.deltaspike.core + deltaspike-core-impl + ${deltaspike.version} + + + + + Weld + + true + + + + + org.apache.activemq + artemis-maven-plugin + + + create0 + + create + + + + org.apache.activemq.examples.modules:artemis-cdi-example:${project.version} + + + ${basedir}/target/server0 + ${basedir}/target/classes/activemq/server0 + + + + start0 + + cli + + + ${noServer} + true + ${basedir}/target/server0 + tcp://localhost:61616 + + run + + server0 + + + + runClient + + runClient + + + org.apache.activemq.artemis.core.example.CDIExample + + + + + + org.apache.activemq.examples.modules + artemis-cdi-example + ${project.version} + + + org.jboss.weld.se + weld-se + ${weld.version} + + + org.apache.deltaspike.cdictrl + deltaspike-cdictrl-weld + ${deltaspike.version} + + + + + + + + OWB + + + + org.apache.activemq + artemis-maven-plugin + + + create0 + + create + + + + org.apache.activemq.examples.modules:artemis-cdi-example:${project.version} + + + ${basedir}/target/server0 + ${basedir}/target/classes/activemq/server0 + + + + start0 + + cli + + + ${noServer} + true + ${basedir}/target/server0 + tcp://localhost:61616 + + run + + server0 + + + + runClient + + runClient + + + org.apache.activemq.artemis.core.example.CDIExample + + + + + + org.apache.activemq.examples.modules + artemis-cdi-example + ${project.version} + + + javax.enterprise + cdi-api + ${cdi-api.version} + + + org.apache.openwebbeans + openwebbeans-impl + ${owb.version} + + + org.apache.openwebbeans + openwebbeans-spi + ${owb.version} + + + org.apache.openwebbeans + openwebbeans-resource + ${owb.version} + + + org.apache.geronimo.specs + geronimo-annotation_1.2_spec + ${geronimo-annotation_1.2_spec.version} + + + org.apache.deltaspike.cdictrl + deltaspike-cdictrl-owb + ${deltaspike.version} + + + + + + + + diff --git a/examples/features/sub-modules/cdi/readme.md b/examples/features/sub-modules/cdi/readme.md new file mode 100644 index 0000000000..7e981ca543 --- /dev/null +++ b/examples/features/sub-modules/cdi/readme.md @@ -0,0 +1,86 @@ + +# ActiveMQ Artemis CDI Integration Example + +This is a simple example that demonstrates how to use CDI to integrate with ActiveMQ Artemis on the client side. It is designed mainly for connecting to a remote broker rather than embedding within your application. + +## Configuring the connection + +While the integration provides an out of the box solution for configuration with some sensible defaults, the values should be configurable. This example leverages [Apache DeltaSpike](https://deltaspike.apache.org) to configure the connectivity. It overrides the username, password and URL for the broker. The configuration hard codes the connector class. This configuration class is a standard CDI bean. + +``` +@ApplicationScoped +public class CDIClientConfig implements ArtemisClientConfiguration { + @Inject + @ConfigProperty(name = "username") + private String username; + + @Inject + @ConfigProperty(name = "password") + private String password; + + @Inject + @ConfigProperty(name = "url") + private String url; + + @Override + public String getUsername() { + return username; + } + + @Override + public String getPassword() { + return password; + } + + @Override + public String getUrl() { + return url; + } + + @Override + public String getConnectorFactory() { + return "org.apache.activemq.artemis.core.remoting.impl.netty.NettyConnectorFactory"; + } +} +``` + +## Setup and Tear Down + +For the example, we leverage DeltaSpike's Container Control to start and stop the container. It is implemented within the main method, and is done in a way to make this work for both Weld and OpenWebBeans. + +## Sending and Receiving Messages + +The key to how the CDI integration works is the built in beans. It provides two out of the box - a `ConnectionFactory` and a `JMSContext`. Most operations should be performed against the JMS 2.0 simplified API using `JMSContext`. The example does this via an observer method, but can be done anywhere. + +``` +@ApplicationScoped +public class CDIMessagingIntegrator { + @Inject + private JMSContext context; + public void init(@Observes @Initialized(ApplicationScoped.class) Object obj) { + String body = "This is a test"; + Queue queue = context.createQueue("test"); + context.createProducer().send(queue, body); + String receivedBody = context.createConsumer(queue).receiveBody(String.class, 5000); + System.out.println("Received a message "+receivedBody); + } +} +``` \ No newline at end of file diff --git a/examples/features/sub-modules/cdi/src/main/java/org/apache/activemq/artemis/core/example/CDIClientConfig.java b/examples/features/sub-modules/cdi/src/main/java/org/apache/activemq/artemis/core/example/CDIClientConfig.java new file mode 100644 index 0000000000..93037ca322 --- /dev/null +++ b/examples/features/sub-modules/cdi/src/main/java/org/apache/activemq/artemis/core/example/CDIClientConfig.java @@ -0,0 +1,87 @@ +/* + * 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.artemis.core.example; + +import javax.enterprise.context.ApplicationScoped; +import javax.inject.Inject; + +import org.apache.artemis.client.cdi.configuration.ArtemisClientConfiguration; +import org.apache.deltaspike.core.api.config.ConfigProperty; + +@ApplicationScoped +public class CDIClientConfig implements ArtemisClientConfiguration { + @Inject + @ConfigProperty(name = "username") + private String username; + + @Inject + @ConfigProperty(name = "password") + private String password; + + @Inject + @ConfigProperty(name = "url") + private String url; + + + @Override + public String getUsername() { + return username; + } + + @Override + public String getPassword() { + return password; + } + + @Override + public String getUrl() { + return url; + } + + @Override + public String getHost() { + return null; + } + + @Override + public Integer getPort() { + return null; + } + + @Override + public String getConnectorFactory() { + return "org.apache.activemq.artemis.core.remoting.impl.netty.NettyConnectorFactory"; + } + + @Override + public boolean startEmbeddedBroker() { + return false; + } + + @Override + public boolean isHa() { + return false; + } + + @Override + public boolean hasAuthentication() { + return false; + } +} diff --git a/examples/features/sub-modules/cdi/src/main/java/org/apache/activemq/artemis/core/example/CDIExample.java b/examples/features/sub-modules/cdi/src/main/java/org/apache/activemq/artemis/core/example/CDIExample.java new file mode 100644 index 0000000000..bbbf49cb07 --- /dev/null +++ b/examples/features/sub-modules/cdi/src/main/java/org/apache/activemq/artemis/core/example/CDIExample.java @@ -0,0 +1,36 @@ +/* + * 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.artemis.core.example; + +import org.apache.deltaspike.cdise.api.CdiContainer; +import org.apache.deltaspike.cdise.api.CdiContainerLoader; + +/** + * A simple example using DeltaSpike CDI Control to connect to an external broker + */ +public class CDIExample { + + public static void main(final String[] args) throws Exception { + CdiContainer cdiContainer = CdiContainerLoader.getCdiContainer(); + cdiContainer.boot(); + + cdiContainer.shutdown(); + } + +} diff --git a/examples/features/sub-modules/cdi/src/main/java/org/apache/activemq/artemis/core/example/CDIMessagingIntegrator.java b/examples/features/sub-modules/cdi/src/main/java/org/apache/activemq/artemis/core/example/CDIMessagingIntegrator.java new file mode 100644 index 0000000000..b4d7817b12 --- /dev/null +++ b/examples/features/sub-modules/cdi/src/main/java/org/apache/activemq/artemis/core/example/CDIMessagingIntegrator.java @@ -0,0 +1,40 @@ +/* + * 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.artemis.core.example; + +import javax.enterprise.context.ApplicationScoped; +import javax.enterprise.context.Initialized; +import javax.enterprise.event.Observes; +import javax.inject.Inject; +import javax.jms.JMSContext; +import javax.jms.Queue; + +@ApplicationScoped +public class CDIMessagingIntegrator { + @Inject + private JMSContext context; + public void init(@Observes @Initialized(ApplicationScoped.class) Object obj) { + String body = "This is a test"; + Queue queue = context.createQueue("test"); + context.createProducer().send(queue, body); + String receivedBody = context.createConsumer(queue).receiveBody(String.class, 5000); + System.out.println("Received a message "+receivedBody); + } +} diff --git a/examples/features/sub-modules/cdi/src/main/resources/META-INF/apache-deltaspike.properties b/examples/features/sub-modules/cdi/src/main/resources/META-INF/apache-deltaspike.properties new file mode 100644 index 0000000000..eb75483b63 --- /dev/null +++ b/examples/features/sub-modules/cdi/src/main/resources/META-INF/apache-deltaspike.properties @@ -0,0 +1,22 @@ +# +# 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. +# + +username=guest +password=guest +url=tcp://localhost:61616 \ No newline at end of file diff --git a/examples/features/sub-modules/cdi/src/main/resources/META-INF/beans.xml b/examples/features/sub-modules/cdi/src/main/resources/META-INF/beans.xml new file mode 100644 index 0000000000..b6f51f1ec0 --- /dev/null +++ b/examples/features/sub-modules/cdi/src/main/resources/META-INF/beans.xml @@ -0,0 +1,25 @@ + + + + \ No newline at end of file diff --git a/examples/features/sub-modules/cdi/src/main/resources/activemq/server0/broker.xml b/examples/features/sub-modules/cdi/src/main/resources/activemq/server0/broker.xml new file mode 100644 index 0000000000..de51e9a36c --- /dev/null +++ b/examples/features/sub-modules/cdi/src/main/resources/activemq/server0/broker.xml @@ -0,0 +1,77 @@ + + + + + + + + + + ./target/paging + + ./target/bindings + + ./target/journal + + 2 + + ./target/large-messages + + + + tcp://${activemq.remoting.default.host:localhost}:${activemq.remoting.default.port:61616} + + + + + tcp://${activemq.remoting.default.host:localhost}:${activemq.remoting.default.port:61616} + + + tcp://${activemq.remoting.amqp.host:localhost}:${activemq.remoting.amqp.port:5672}?protocols=AMQP + + + tcp://${activemq.remoting.stomp.host:localhost}:${activemq.remoting.stomp.port:61613}?protocols=STOMP + + + tcp://${activemq.remoting.hornetq.host:localhost}:${activemq.remoting.hornetq.port:5445}?protocols=CORE,STOMP + + + + + + + + + + + + + + + jms.queue.DLQ + jms.queue.ExpiryQueue + 0 + 10485760 + 10 + BLOCK + + + + diff --git a/examples/features/sub-modules/pom.xml b/examples/features/sub-modules/pom.xml index 5dfc9cd8d0..79fc7d7214 100644 --- a/examples/features/sub-modules/pom.xml +++ b/examples/features/sub-modules/pom.xml @@ -52,6 +52,7 @@ under the License. aerogear artemis-ra-rar vertx + cdi diff --git a/integration/artemis-cdi-integration/src/main/resources/META-INF/beans.xml b/integration/artemis-cdi-integration/src/main/resources/META-INF/beans.xml new file mode 100644 index 0000000000..b6f51f1ec0 --- /dev/null +++ b/integration/artemis-cdi-integration/src/main/resources/META-INF/beans.xml @@ -0,0 +1,25 @@ + + + + \ No newline at end of file diff --git a/integration/artemis-cdi-integration/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension b/integration/artemis-cdi-integration/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension new file mode 100644 index 0000000000..81282d4641 --- /dev/null +++ b/integration/artemis-cdi-integration/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension @@ -0,0 +1,20 @@ +# +# 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. +# + +org.apache.artemis.client.cdi.extension.ArtemisExtension \ No newline at end of file From e71f3d8a34c39f5d166e107d733b66841dba605a Mon Sep 17 00:00:00 2001 From: Clebert Suconic Date: Mon, 24 Oct 2016 10:37:22 -0400 Subject: [PATCH 2/3] ARTEMIS-756 disabling finalize check over CDI Connection Factories --- .../artemis/jms/client/ActiveMQConnection.java | 4 +++- .../jms/client/ActiveMQConnectionFactory.java | 12 ++++++++++++ .../cdi/factory/ConnectionFactoryProvider.java | 3 ++- 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/artemis-jms-client/src/main/java/org/apache/activemq/artemis/jms/client/ActiveMQConnection.java b/artemis-jms-client/src/main/java/org/apache/activemq/artemis/jms/client/ActiveMQConnection.java index 440b576f3c..0c6cf9de3e 100644 --- a/artemis-jms-client/src/main/java/org/apache/activemq/artemis/jms/client/ActiveMQConnection.java +++ b/artemis-jms-client/src/main/java/org/apache/activemq/artemis/jms/client/ActiveMQConnection.java @@ -572,7 +572,9 @@ public class ActiveMQConnection extends ActiveMQConnectionForContextImpl impleme @Override protected final void finalize() throws Throwable { if (!closed) { - ActiveMQJMSClientLogger.LOGGER.connectionLeftOpen(creationStack); + if (this.factoryReference.isFinalizeChecks()) { + ActiveMQJMSClientLogger.LOGGER.connectionLeftOpen(creationStack); + } close(); } diff --git a/artemis-jms-client/src/main/java/org/apache/activemq/artemis/jms/client/ActiveMQConnectionFactory.java b/artemis-jms-client/src/main/java/org/apache/activemq/artemis/jms/client/ActiveMQConnectionFactory.java index 4f6528b496..91dee3cc9b 100644 --- a/artemis-jms-client/src/main/java/org/apache/activemq/artemis/jms/client/ActiveMQConnectionFactory.java +++ b/artemis-jms-client/src/main/java/org/apache/activemq/artemis/jms/client/ActiveMQConnectionFactory.java @@ -83,6 +83,8 @@ public class ActiveMQConnectionFactory implements ConnectionFactoryOptions, Exte private String deserializationWhiteList; + private boolean finalizeChecks; + @Override public void writeExternal(ObjectOutput out) throws IOException { URI uri = toURI(); @@ -148,6 +150,16 @@ public class ActiveMQConnectionFactory implements ConnectionFactoryOptions, Exte } } + public ActiveMQConnectionFactory disableFinalizeChecks() { + this.finalizeChecks = false; + return this; + } + + public boolean isFinalizeChecks() { + return finalizeChecks; + } + + @Override public String getDeserializationBlackList() { return deserializationBlackList; diff --git a/integration/artemis-cdi-integration/src/main/java/org/apache/artemis/client/cdi/factory/ConnectionFactoryProvider.java b/integration/artemis-cdi-integration/src/main/java/org/apache/artemis/client/cdi/factory/ConnectionFactoryProvider.java index b57b771339..b249474246 100644 --- a/integration/artemis-cdi-integration/src/main/java/org/apache/artemis/client/cdi/factory/ConnectionFactoryProvider.java +++ b/integration/artemis-cdi-integration/src/main/java/org/apache/artemis/client/cdi/factory/ConnectionFactoryProvider.java @@ -97,6 +97,7 @@ public class ConnectionFactoryProvider { activeMQConnectionFactory.setUser(configuration.getUsername()); activeMQConnectionFactory.setPassword(configuration.getPassword()); } - return activeMQConnectionFactory; + // The CF will probably be GCed since it was injected, so we disable the finalize check + return activeMQConnectionFactory.disableFinalizeChecks(); } } From 1eb3c70aca7b72bf50e5cf8eafbe73b0c8855432 Mon Sep 17 00:00:00 2001 From: Clebert Suconic Date: Mon, 24 Oct 2016 10:47:02 -0400 Subject: [PATCH 3/3] ARTEMIS-756 removing broker.xml from examples as it can use the standard one from the cli create --- .../resources/activemq/server0/broker.xml | 77 ------------------- 1 file changed, 77 deletions(-) delete mode 100644 examples/features/sub-modules/cdi/src/main/resources/activemq/server0/broker.xml diff --git a/examples/features/sub-modules/cdi/src/main/resources/activemq/server0/broker.xml b/examples/features/sub-modules/cdi/src/main/resources/activemq/server0/broker.xml deleted file mode 100644 index de51e9a36c..0000000000 --- a/examples/features/sub-modules/cdi/src/main/resources/activemq/server0/broker.xml +++ /dev/null @@ -1,77 +0,0 @@ - - - - - - - - - - ./target/paging - - ./target/bindings - - ./target/journal - - 2 - - ./target/large-messages - - - - tcp://${activemq.remoting.default.host:localhost}:${activemq.remoting.default.port:61616} - - - - - tcp://${activemq.remoting.default.host:localhost}:${activemq.remoting.default.port:61616} - - - tcp://${activemq.remoting.amqp.host:localhost}:${activemq.remoting.amqp.port:5672}?protocols=AMQP - - - tcp://${activemq.remoting.stomp.host:localhost}:${activemq.remoting.stomp.port:61613}?protocols=STOMP - - - tcp://${activemq.remoting.hornetq.host:localhost}:${activemq.remoting.hornetq.port:5445}?protocols=CORE,STOMP - - - - - - - - - - - - - - - jms.queue.DLQ - jms.queue.ExpiryQueue - 0 - 10485760 - 10 - BLOCK - - - -