http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/6b17d966/examples/features/standard/jms-bridge/readme.html ---------------------------------------------------------------------- diff --git a/examples/features/standard/jms-bridge/readme.html b/examples/features/standard/jms-bridge/readme.html new file mode 100644 index 0000000..1ee0ff6 --- /dev/null +++ b/examples/features/standard/jms-bridge/readme.html @@ -0,0 +1,246 @@ +<!-- +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. +--> + +<html> + <head> + <title>ActiveMQ Artemis JMS Bridge Example</title> + <link rel="stylesheet" type="text/css" href="../../../common/common.css" /> + <link rel="stylesheet" type="text/css" href="../../../common/prettify.css" /> + <script type="text/javascript" src="../../../common/prettify.js"></script> + </head> + <body onload="prettyPrint()"> + <h1>JMS Bridge Example</h1> + + <pre>To run the example, simply type <b>mvn verify</b> from this directory, <br>or <b>mvn -PnoServer verify</b> if you want to start and create the server manually.</pre> + + <p>This example shows you how to create a JMS Bridge between two ActiveMQ Artemis servers.</p> + <img src="jms-bridge.png" /> + <p>The example will use two ActiveMQ Artemis servers:</p> + <ul> + <li>Server #0 – the <em>Source</em> server. It will be configured with a JMS Topic bound to JNDI under <code>source/topic</code> + <li>Server #1 – the <em>Target</em> server. It will be configured with a JMS Queue bound to JNDI under <code>target/queue</code><br /> + </ul> + <p>Both ActiveMQ Artemis server will run their own JNDI server used by the JMS Bridge and the JMS Client to lookup JMS + resources (ConnectionFactory and Destination).</p> + <p>The JMS Bridge will be started in the example code and be configured to bridge messages from the <em>source</em> destination + (the topic hosted on server #0) and the <em>target</em> destination (the queue hosted on server #1)</p> + <p>The client will check the bridge works by:</p> + <ol> + <li>sending a message to the <em>source</em> topic</li> + <li>receive a message from the <em>target</em> queue</li> + <li>check that both messages correspond to the same content.</li> + </ol> + <h3>JMS Bridge Configuration</h3> + <p>The JMS Bridge is a <abbr title="Plain Old Java Object">POJO</abbr> that we configure with both source and target + JNDI configurations. In the actual example we are programatically creating the Bridge, however the following section + describes how you would do this if you wanted to deploy with an actual ActiveMQ Artemis server via the activemq-beans.xml. + + <h4>Configuring the Bridge with the JBoss Microcontainer</h4> + <p> + in which we inject JNDI configurations + so that it looks up its source and target JMS resources. + The JMS Bridge is defined a bean and setup by JBoss Microntainer in the same VM than Server #1, the target server.</p> + + </p>The JMS Bridge sample configuration can be found in <code>resources/activemq-beans.xml</code>, firstly we define the + Bridge itself:</p> + <pre class="prettyprint"> + <!-- The JMS Bridge --> + <bean name="JMSBridge" class="org.apache.activemq.artemis.jms.bridge.impl.JMSBridgeImpl"> + ... + </bean> + </pre> + <p>the <code>JMSBridgeImpl</code> constructor is used to inject all the properties required to run the JMS Bridge.</p> + <p>Its first four arguments defines how the bridge will lookup:</p> + <ol> + <li>its <em>source</em> JMS ConnectionFactory</li> + <li>its <em>source</em> JMS Destination</li> + <li>its <em>target</em> JMS ConnectionFactory</li> + <li>its <em>target</em> JMS Destination</li> + </ol> + <p>Using other POJOs, the JMS Bridge is configured to retrieve:</p> + <ul> + <li>its <em>source</em> JMS ConnectionFactory by looking up <code>/source/ConnectionFactory</code> using + the <code>SourceJNDI</code> configuration</li> + <li>its <em>source</em> JMS Destination by looking up <code>/source/topic</code> using + the <code>SourceJNDI</code> configuration</li> + <li>its <em>target</em> JMS ConnectionFactory by looking up <code>/target/ConnectionFactory</code> using + the <code>TargetJNDI</code> configuration</li> + <li>its <em>target</em> JMS ConnectionFactory by looking up <code>/target/queue</code> using + the <code>TargetJNDI</code> configuration</li> + </ul> + <p>In turn, <code>SourceJNDI</code> and <code>TargetJNDI</code> are POJOs defining how to connect to JNDI server. + SourceJNDI URL must point to your source server, while LocalJNDI must point to your target server:</p> + <pre class="prettyprint"> + <bean name="SourceJNDI" class="java.util.Hashtable"> + ... + <entry> + <key>java.naming.provider.url</key> + <!-- **************************************** --> + <!-- Replace with the *source* server address --> + <!-- **************************************** --> + <value>jnp://192.168.0.10:1099</value> + ... + </bean> + <bean name="TargetJNDI" class="java.util.Hashtable"> + ... + <ntry> + <key>java.naming.provider.url</key> + <!-- **************************************** --> + <!-- Replace with the *target* server address --> + <!-- **************************************** --> + <value>jnp://1192.168.0.11:1099</value> + </entry> + ... + </bean> + </pre> + <h2>Example step-by-step</h2> + <p>To run the example after having setup both ActiveMQ Artemis servers and the JMS bridge:</p> + <ol> + <li>To run the example simply run <code>mvn verify -Pexample</code></li> + </ol> + <p>Let's look at the Client code (in <code>JMSBridgeExample</code> class):</p> + <ol> + <li>First we need to get an initial context so we can look-up the JMS resources</li> + <pre class="prettyprint"> + InitialContext sourceContext = createContext(sourceServer); + InitialContext targetContext = createContext(targetServer); + </pre> + <li>We then create a JMS Bridge and start it, Note, for certain quality of service modes such as + ONCE_AND_ONCE_ONLY and AT_LEAST_ONCE a Transaction Manager is required to ensure Messages are delivered + accordingly. A Transaction Manager can be either loaded via implementation of TransactionManagerLocator intefer + and loaded via standard a ServiceLoader or by explicitly setting an instance of a Transaction Manager on the + bridge using setTranscationManager(TransactionManager tm) method. In this example we'll be using the DUPLICATES_OK + quality of service so there is no need for a Transaction Manager. + <pre class="prettyprint"> + JMSBridge jmsBridge = new JMSBridgeImpl( + new JNDIConnectionFactoryFactory(sourceJndiParams, "source/ConnectionFactory"), + new JNDIConnectionFactoryFactory(targetJndiParams, "target/ConnectionFactory"), + new JNDIDestinationFactory(sourceJndiParams, "source/topic"), + new JNDIDestinationFactory(targetJndiParams, "target/queue"), + null, + null, + null, + null, + null, + 5000, + 10, + QualityOfServiceMode.DUPLICATES_OK, + 1, + -1, + null, + null, + true); + .... + jmsBridge.start(); + </pre> + <li>We look up the JMS resources from the Source server</li> + <pre class="prettyprint"> + ConnectionFactory sourceConnectionFactory = (ConnectionFactory)sourceContext.lookup("source/ConnectionFactory"); + Topic sourceTopic = (Topic)sourceContext.lookup("source/topic"); + </pre> + + <li>We create JMS objects to send a message to the source destination</li> + <pre class="prettyprint"> + sourceConnection = sourceConnectionFactory.createConnection(); + Session sourceSession = sourceConnection.createSession(false, Session.AUTO_ACKNOWLEDGE); + MessageProducer sourceProducer = sourceSession.createProducer(sourceTopic); + </pre> + + <li>We send a message to the source destination</li> + <pre class="prettyprint"> + TextMessage message = sourceSession.createTextMessage("this is a text message sent at " + System.currentTimeMillis()); + sourceProducer.send(message); + </pre> + + <li>We close the connection to the source server</li> + <pre class="prettyprint"> + sourceConnection.close(); + </pre> + + <p>At this point, the JMS Bridge will consume the message from the source topic and + sends it to the target queue. + The client will check the bridge works by consuming a message from the target queue.</p> + + <li>We look up the JMS resources from the target server</li> + <pre class="prettyprint"> + ConnectionFactory targetConnectionFactory = (ConnectionFactory)targetContext.lookup("target/ConnectionFactory"); + Queue targetQueue = (Queue)targetContext.lookup("target/queue"); + </pre> + + <li>We create JMS objects to receive a message from the target destination</li> + <pre class="prettyprint"> + targetConnection = targetConnectionFactory.createConnection(); + Session targetSession = targetConnection.createSession(false, Session.AUTO_ACKNOWLEDGE); + MessageConsumer targetConsumer = targetSession.createConsumer(targetQueue); + </pre> + + <li>We start the target connection to start receiving messages + <pre class="prettyprint"> + targetConnection.start(); + </pre> + + <li>We receive the message and print it. Its content is the same than the message + the client sent to the source topic</li> + <pre class="prettyprint"> + TextMessage messageReceived = (TextMessage)targetConsumer.receive(5000); + </pre> + + <li>We display the message ID and its "bridged" message ID</li> + <pre class="prettyprint"> + System.out.format("Message ID : %s\n", messageReceived.getJMSMessageID()); + System.out.format("Bridged Message ID : %s\n", messageReceived.getStringProperty("AMQ_BRIDGE_MSG_ID_LIST")); + </pre> + + <p>Note that the message received from the target queue is <em>not the same message</em> sent to the source topic + (their message IDs are different) but they have the <em>same content</em>. + + <li>And finally, we stop the Bridge and <b>always</b> remember to close your JMS connections and resources after use, in a <code>finally</code> block. Closing a JMS connection will automatically close all of its sessions, consumers, producer and browser objects</li> + + <pre class="prettyprint"> + <code>finally + { + if (jmsBridge != null) + { + jmsBridge.stop(); + } + if (initialContext != null) + { + initialContext.close(); + } + if (connection != null) + { + connection.close(); + } + }</code> + </pre> + + + + </ol> + + <h2>More information</h2> + + <ul> + <li>User Manual's <a href="../../../docs/user-manual/en/html_single/appserver-integration.html#jms-bridge">JMS Bridge chapter</a></li> + <li>The <a href="../../javaee/jms-bridge/readme.html">Java EE JMS Bridge example</a> shows how to configure a JMS Bridge + inside JBoss Application Server to bridge destinations from the same server.</a> + </p> + + </body> +</html>
http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/6b17d966/examples/features/standard/jms-bridge/src/main/java/org/apache/activemq/artemis/jms/example/JMSBridgeExample.java ---------------------------------------------------------------------- diff --git a/examples/features/standard/jms-bridge/src/main/java/org/apache/activemq/artemis/jms/example/JMSBridgeExample.java b/examples/features/standard/jms-bridge/src/main/java/org/apache/activemq/artemis/jms/example/JMSBridgeExample.java new file mode 100644 index 0000000..8a65cd5 --- /dev/null +++ b/examples/features/standard/jms-bridge/src/main/java/org/apache/activemq/artemis/jms/example/JMSBridgeExample.java @@ -0,0 +1,136 @@ +/* + * 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.jms.example; + +import javax.jms.Connection; +import javax.jms.ConnectionFactory; +import javax.jms.MessageConsumer; +import javax.jms.MessageProducer; +import javax.jms.Queue; +import javax.jms.Session; +import javax.jms.TextMessage; +import javax.jms.Topic; +import javax.naming.InitialContext; +import java.util.Hashtable; + +import org.apache.activemq.artemis.jms.bridge.JMSBridge; +import org.apache.activemq.artemis.jms.bridge.QualityOfServiceMode; +import org.apache.activemq.artemis.jms.bridge.impl.JMSBridgeImpl; +import org.apache.activemq.artemis.jms.bridge.impl.JNDIConnectionFactoryFactory; +import org.apache.activemq.artemis.jms.bridge.impl.JNDIDestinationFactory; + +/** + * An example which sends a message to a source topic and consume from a target queue. + * The source and target destinations are located on 2 different ActiveMQ Artemis server. + * The source and target queues are bridged by a JMS Bridge configured and running on the "target" server. + */ +public class JMSBridgeExample { + + public static void main(final String[] args) throws Exception { + String sourceServer = "tcp://localhost:61616"; + String targetServer = "tcp://localhost:61617"; + + System.out.println("client will publish messages to " + sourceServer + + " and receives message from " + + targetServer); + + // Step 1. Create JNDI contexts for source and target servers + InitialContext sourceContext = JMSBridgeExample.createContext(sourceServer); + InitialContext targetContext = JMSBridgeExample.createContext(targetServer); + + Hashtable<String, String> sourceJndiParams = createJndiParams(sourceServer); + Hashtable<String, String> targetJndiParams = createJndiParams(targetServer); + // Step 2. Create and start a JMS Bridge + // Note, the Bridge needs a transaction manager, in this instance we will use the JBoss TM + JMSBridge jmsBridge = new JMSBridgeImpl(new JNDIConnectionFactoryFactory(sourceJndiParams, "ConnectionFactory"), new JNDIConnectionFactoryFactory(targetJndiParams, "ConnectionFactory"), new JNDIDestinationFactory(sourceJndiParams, "source/topic"), new JNDIDestinationFactory(targetJndiParams, "target/queue"), null, null, null, null, null, 5000, 10, QualityOfServiceMode.DUPLICATES_OK, 1, -1, null, null, true); + + Connection sourceConnection = null; + Connection targetConnection = null; + try { + jmsBridge.start(); + // Step 3. Lookup the *source* JMS resources + ConnectionFactory sourceConnectionFactory = (ConnectionFactory) sourceContext.lookup("ConnectionFactory"); + Topic sourceTopic = (Topic) sourceContext.lookup("source/topic"); + + // Step 4. Create a connection, a session and a message producer for the *source* topic + sourceConnection = sourceConnectionFactory.createConnection(); + Session sourceSession = sourceConnection.createSession(false, Session.AUTO_ACKNOWLEDGE); + MessageProducer sourceProducer = sourceSession.createProducer(sourceTopic); + + // Step 5. Create and send a text message to the *source* queue + TextMessage message = sourceSession.createTextMessage("this is a text message sent at " + System.currentTimeMillis()); + sourceProducer.send(message); + System.out.format("Sent message to %s: %s%n", ((Topic) message.getJMSDestination()).getTopicName(), message.getText()); + System.out.format("Message ID : %s%n", message.getJMSMessageID()); + + // Step 6. Close the *source* connection + sourceConnection.close(); + + // Step 7. Lookup the *target* JMS resources + ConnectionFactory targetConnectionFactory = (ConnectionFactory) targetContext.lookup("ConnectionFactory"); + Queue targetQueue = (Queue) targetContext.lookup("target/queue"); + + // Step 8. Create a connection, a session and a message consumer for the *target* queue + targetConnection = targetConnectionFactory.createConnection(); + Session targetSession = targetConnection.createSession(false, Session.AUTO_ACKNOWLEDGE); + MessageConsumer targetConsumer = targetSession.createConsumer(targetQueue); + + // Step 9. Start the connection to receive messages from the *target* queue + targetConnection.start(); + + // Step 10. Receive a message from the *target* queue + TextMessage messageReceived = (TextMessage) targetConsumer.receive(5000); + System.out.format("%nReceived from %s: %s%n", ((Queue) messageReceived.getJMSDestination()).getQueueName(), messageReceived.getText()); + + // Step 11. Display the received message's ID and this "bridged" message ID + System.out.format("Message ID : %s%n", messageReceived.getJMSMessageID()); + System.out.format("Bridged Message ID : %s%n", messageReceived.getStringProperty("AMQ_BRIDGE_MSG_ID_LIST")); + } + finally { + // Step 12. Be sure to close the resources! + if (jmsBridge != null) { + jmsBridge.stop(); + } + if (sourceContext != null) { + sourceContext.close(); + } + if (targetContext != null) { + targetContext.close(); + } + if (sourceConnection != null) { + sourceConnection.close(); + } + if (targetConnection != null) { + targetConnection.close(); + } + } + } + + private static InitialContext createContext(final String server) throws Exception { + Hashtable<String, String> jndiProps = createJndiParams(server); + return new InitialContext(jndiProps); + } + + private static Hashtable<String, String> createJndiParams(String server) { + Hashtable<String, String> jndiProps = new Hashtable<String, String>(); + jndiProps.put("connectionFactory.ConnectionFactory", server); + jndiProps.put("java.naming.factory.initial", "org.apache.activemq.artemis.jndi.ActiveMQInitialContextFactory"); + jndiProps.put("queue.target/queue", "target"); + jndiProps.put("topic.source/topic", "topic"); + return jndiProps; + } +} http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/6b17d966/examples/features/standard/jms-bridge/src/main/resources/activemq/server0/artemis-roles.properties ---------------------------------------------------------------------- diff --git a/examples/features/standard/jms-bridge/src/main/resources/activemq/server0/artemis-roles.properties b/examples/features/standard/jms-bridge/src/main/resources/activemq/server0/artemis-roles.properties new file mode 100644 index 0000000..4e2d44c --- /dev/null +++ b/examples/features/standard/jms-bridge/src/main/resources/activemq/server0/artemis-roles.properties @@ -0,0 +1,17 @@ +## --------------------------------------------------------------------------- +## 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. +## --------------------------------------------------------------------------- +guest=guest \ No newline at end of file http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/6b17d966/examples/features/standard/jms-bridge/src/main/resources/activemq/server0/artemis-users.properties ---------------------------------------------------------------------- diff --git a/examples/features/standard/jms-bridge/src/main/resources/activemq/server0/artemis-users.properties b/examples/features/standard/jms-bridge/src/main/resources/activemq/server0/artemis-users.properties new file mode 100644 index 0000000..4e2d44c --- /dev/null +++ b/examples/features/standard/jms-bridge/src/main/resources/activemq/server0/artemis-users.properties @@ -0,0 +1,17 @@ +## --------------------------------------------------------------------------- +## 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. +## --------------------------------------------------------------------------- +guest=guest \ No newline at end of file http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/6b17d966/examples/features/standard/jms-bridge/src/main/resources/activemq/server0/broker.xml ---------------------------------------------------------------------- diff --git a/examples/features/standard/jms-bridge/src/main/resources/activemq/server0/broker.xml b/examples/features/standard/jms-bridge/src/main/resources/activemq/server0/broker.xml new file mode 100644 index 0000000..df456d7 --- /dev/null +++ b/examples/features/standard/jms-bridge/src/main/resources/activemq/server0/broker.xml @@ -0,0 +1,53 @@ +<?xml version='1.0'?> +<!-- +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. +--> + +<configuration xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xmlns="urn:activemq" + xsi:schemaLocation="urn:activemq /schema/artemis-server.xsd"> + + <jms xmlns="urn:activemq:jms"> + <topic name="topic"/> + </jms> + + <core xmlns="urn:activemq:core"> + + <bindings-directory>${data.dir:../data}/bindings</bindings-directory> + + <journal-directory>${data.dir:../data}/journal</journal-directory> + + <large-messages-directory>${data.dir:../data}/largemessages</large-messages-directory> + + <paging-directory>${data.dir:../data}/paging</paging-directory> + + <acceptors> + <acceptor name="netty">tcp://localhost:61616</acceptor> + </acceptors> + + <security-settings> + <security-setting match="jms.#"> + <permission type="consume" roles="guest"/> + <permission type="send" roles="guest"/> + <permission type="createNonDurableQueue" roles="guest"/> + <permission type="deleteNonDurableQueue" roles="guest"/> + </security-setting> + </security-settings> + </core> +</configuration> + http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/6b17d966/examples/features/standard/jms-bridge/src/main/resources/activemq/server1/artemis-roles.properties ---------------------------------------------------------------------- diff --git a/examples/features/standard/jms-bridge/src/main/resources/activemq/server1/artemis-roles.properties b/examples/features/standard/jms-bridge/src/main/resources/activemq/server1/artemis-roles.properties new file mode 100644 index 0000000..4e2d44c --- /dev/null +++ b/examples/features/standard/jms-bridge/src/main/resources/activemq/server1/artemis-roles.properties @@ -0,0 +1,17 @@ +## --------------------------------------------------------------------------- +## 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. +## --------------------------------------------------------------------------- +guest=guest \ No newline at end of file http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/6b17d966/examples/features/standard/jms-bridge/src/main/resources/activemq/server1/artemis-users.properties ---------------------------------------------------------------------- diff --git a/examples/features/standard/jms-bridge/src/main/resources/activemq/server1/artemis-users.properties b/examples/features/standard/jms-bridge/src/main/resources/activemq/server1/artemis-users.properties new file mode 100644 index 0000000..4e2d44c --- /dev/null +++ b/examples/features/standard/jms-bridge/src/main/resources/activemq/server1/artemis-users.properties @@ -0,0 +1,17 @@ +## --------------------------------------------------------------------------- +## 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. +## --------------------------------------------------------------------------- +guest=guest \ No newline at end of file http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/6b17d966/examples/features/standard/jms-bridge/src/main/resources/activemq/server1/broker.xml ---------------------------------------------------------------------- diff --git a/examples/features/standard/jms-bridge/src/main/resources/activemq/server1/broker.xml b/examples/features/standard/jms-bridge/src/main/resources/activemq/server1/broker.xml new file mode 100644 index 0000000..e65720c --- /dev/null +++ b/examples/features/standard/jms-bridge/src/main/resources/activemq/server1/broker.xml @@ -0,0 +1,53 @@ +<?xml version='1.0'?> +<!-- +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. +--> + +<configuration xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xmlns="urn:activemq" + xsi:schemaLocation="urn:activemq /schema/artemis-server.xsd"> + + <jms xmlns="urn:activemq:jms"> + <queue name="target"/> + </jms> + + <core xmlns="urn:activemq:core"> + + <bindings-directory>${data.dir:../data}/bindings</bindings-directory> + + <journal-directory>${data.dir:../data}/journal</journal-directory> + + <large-messages-directory>${data.dir:../data}/largemessages</large-messages-directory> + + <paging-directory>${data.dir:../data}/paging</paging-directory> + + <acceptors> + <acceptor name="netty">tcp://localhost:61617</acceptor> + </acceptors> + + <security-settings> + <security-setting match="jms.#"> + <permission type="consume" roles="guest"/> + <permission type="send" roles="guest"/> + <permission type="createNonDurableQueue" roles="guest"/> + <permission type="deleteNonDurableQueue" roles="guest"/> + </security-setting> + </security-settings> + </core> +</configuration> + http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/6b17d966/examples/features/standard/jms-completion-listener/pom.xml ---------------------------------------------------------------------- diff --git a/examples/features/standard/jms-completion-listener/pom.xml b/examples/features/standard/jms-completion-listener/pom.xml new file mode 100644 index 0000000..139432f --- /dev/null +++ b/examples/features/standard/jms-completion-listener/pom.xml @@ -0,0 +1,110 @@ +<?xml version='1.0'?> +<!-- +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. +--> + +<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/maven-v4_0_0.xsd"> + <modelVersion>4.0.0</modelVersion> + + <parent> + <groupId>org.apache.activemq.examples.broker</groupId> + <artifactId>jms-examples</artifactId> + <version>1.0.1-SNAPSHOT</version> + </parent> + + <artifactId>completion-listener</artifactId> + <packaging>jar</packaging> + <name>ActiveMQ Artemis JMS Completion Listener Example</name> + + <properties> + <activemq.basedir>${project.basedir}/../../../..</activemq.basedir> + </properties> + + <dependencies> + <dependency> + <groupId>org.apache.activemq</groupId> + <artifactId>artemis-jms-client</artifactId> + <version>${project.version}</version> + </dependency> + </dependencies> + + <build> + <plugins> + <plugin> + <groupId>org.apache.activemq</groupId> + <artifactId>artemis-maven-plugin</artifactId> + <executions> + <execution> + <id>create</id> + <goals> + <goal>create</goal> + </goals> + <configuration> + <ignore>${noServer}</ignore> + <configuration>${basedir}/target/classes/activemq/server0</configuration> + </configuration> + </execution> + <execution> + <id>start</id> + <goals> + <goal>cli</goal> + </goals> + <configuration> + <ignore>${noServer}</ignore> + <spawn>true</spawn> + <testURI>tcp://localhost:61616</testURI> + <args> + <param>run</param> + </args> + </configuration> + </execution> + <execution> + <id>runClient</id> + <goals> + <goal>runClient</goal> + </goals> + <configuration> + <clientClass>org.apache.activemq.artemis.jms.example.JMSCompletionListenerExample</clientClass> + </configuration> + </execution> + <execution> + <id>stop</id> + <goals> + <goal>cli</goal> + </goals> + <configuration> + <ignore>${noServer}</ignore> + <args> + <param>stop</param> + </args> + </configuration> + </execution> + </executions> + <dependencies> + <dependency> + <groupId>org.apache.activemq.examples.broker</groupId> + <artifactId>completion-listener</artifactId> + <version>${project.version}</version> + </dependency> + </dependencies> + </plugin> + </plugins> + </build> + +</project> http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/6b17d966/examples/features/standard/jms-completion-listener/readme.html ---------------------------------------------------------------------- diff --git a/examples/features/standard/jms-completion-listener/readme.html b/examples/features/standard/jms-completion-listener/readme.html new file mode 100644 index 0000000..36be17a --- /dev/null +++ b/examples/features/standard/jms-completion-listener/readme.html @@ -0,0 +1,112 @@ +<!-- +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. +--> + +<html> + <head> + <title>ActiveMQ Artemis JMS Completion Listener Example</title> + <link rel="stylesheet" type="text/css" href="../../../common/common.css" /> + <link rel="stylesheet" type="text/css" href="../../../common/prettify.css" /> + <script type="text/javascript" src="../../../common/prettify.js"></script> + </head> + <body onload="prettyPrint()"> + <h1>JMS Completion Listener Example</h1> + + <pre>To run the example, simply type <b>mvn verify</b> from this directory, <br>or <b>mvn -PnoServer verify</b> if you want to start and create the server manually.</pre> + + <p>This example shows you how to send a message asynchronously to ActiveMQ Artemis and use a CompletionListener to be notified of + the Broker receiving it</p> + + <h2>Example step-by-step</h2> + + <ol> + <li>First we need to get an initial context so we can look-up the JMS connection factory and destination objects from JNDI. This initial context will get it's properties from the <code>client-jndi.properties</code> file in the directory <code>../common/config</code></li> + <pre class="prettyprint"> + <code>InitialContext initialContext = getContext();</code> + </pre> + + <li>We look-up the JMS queue object from JNDI</li> + <pre class="prettyprint"> + <code>Queue queue = (Queue) initialContext.lookup("/queue/exampleQueue");</code> + </pre> + + <li>We look-up the JMS connection factory object from JNDI</li> + <pre class="prettyprint"> + <code>ConnectionFactory cf = (ConnectionFactory) initialContext.lookup("/ConnectionFactory");</code> + </pre> + + <li>We create a JMS context</li> + <pre class="prettyprint"> + <code>jmsContext = cf.createContext();</code> + </pre> + + <li>We create a JMS Producer.</li> + <pre class="prettyprint"> + <code>JMSProducer producer = jmsContext.createProducer();</code> + </pre> + + <li>We set a CompletionListener on the Producer</li> + <pre class="prettyprint"> + <code>producer.setAsync(new CompletionListener() + { + @Override + public void onCompletion(Message message) + { + System.out.println("message acknowledged by ActiveMQ"); + latch.countDown(); + } + + @Override + public void onException(Message message, Exception e) + { + e.printStackTrace(); + } + });</code> + </pre> + + <li>We send a message</li> + <pre class="prettyprint"> + <code>producer.send(queue, "this is a string");</code> + </pre> + + <li> and then wait for the Completion Listener to be called</li> + <pre class="prettyprint"> + <code>return latch.await(5, TimeUnit.SECONDS);</code> + </pre> + + <li>And finally, <b>always</b> remember to close your JMS connections and resources after use, in a <code>finally</code> block. Closing a JMS connection will automatically close all of its sessions, consumers, producer and browser objects</li> + + <pre class="prettyprint"> + <code>finally + { + if (initialContext != null) + { + initialContext.close(); + } + if (jmsContext != null) + { + jmsContext.close(); + } + }</code> + </pre> + + + + </ol> + </body> +</html> http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/6b17d966/examples/features/standard/jms-completion-listener/src/main/java/org/apache/activemq/artemis/jms/example/JMSCompletionListenerExample.java ---------------------------------------------------------------------- diff --git a/examples/features/standard/jms-completion-listener/src/main/java/org/apache/activemq/artemis/jms/example/JMSCompletionListenerExample.java b/examples/features/standard/jms-completion-listener/src/main/java/org/apache/activemq/artemis/jms/example/JMSCompletionListenerExample.java new file mode 100644 index 0000000..4688ba3 --- /dev/null +++ b/examples/features/standard/jms-completion-listener/src/main/java/org/apache/activemq/artemis/jms/example/JMSCompletionListenerExample.java @@ -0,0 +1,82 @@ +/* + * 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.jms.example; + +import javax.jms.CompletionListener; +import javax.jms.ConnectionFactory; +import javax.jms.JMSContext; +import javax.jms.JMSProducer; +import javax.jms.Message; +import javax.jms.Queue; + +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; + +import org.apache.activemq.artemis.api.jms.ActiveMQJMSClient; +import org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory; + +/** + * A JMS Completion Listener Example. + */ +public class JMSCompletionListenerExample { + + public static void main(final String[] args) throws Exception { + JMSContext jmsContext = null; + try { + // Step 2. Perfom a lookup on the queue + Queue queue = ActiveMQJMSClient.createQueue("exampleQueue"); + + // Step 3. Perform a lookup on the Connection Factory + ConnectionFactory cf = new ActiveMQConnectionFactory("tcp://localhost:61616?confirmationWindowSize=10240"); + + // Step 4.Create a JMS Context + jmsContext = cf.createContext(); + + // Step 5. Create a message producer. + JMSProducer producer = jmsContext.createProducer(); + + final CountDownLatch latch = new CountDownLatch(1); + + //Step 6. We want to send the message Asynchronously and be notified when the Broker receives it so we set a completion handler + producer.setAsync(new CompletionListener() { + @Override + public void onCompletion(Message message) { + System.out.println("message acknowledged by ActiveMQ"); + latch.countDown(); + } + + @Override + public void onException(Message message, Exception e) { + e.printStackTrace(); + } + }); + + //Step 6. Send the Message + producer.send(queue, "this is a string"); + + //Step 7. wait for the Completion handler + if (!latch.await(5, TimeUnit.SECONDS)) { + throw new IllegalStateException("Completion listener not called as expected."); + } + } + finally { + if (jmsContext != null) { + jmsContext.close(); + } + } + } +} http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/6b17d966/examples/features/standard/jms-completion-listener/src/main/resources/jndi.properties ---------------------------------------------------------------------- diff --git a/examples/features/standard/jms-completion-listener/src/main/resources/jndi.properties b/examples/features/standard/jms-completion-listener/src/main/resources/jndi.properties new file mode 100644 index 0000000..93537c4 --- /dev/null +++ b/examples/features/standard/jms-completion-listener/src/main/resources/jndi.properties @@ -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. + +java.naming.factory.initial=org.apache.activemq.artemis.jndi.ActiveMQInitialContextFactory +connectionFactory.ConnectionFactory=tcp://localhost:61616 +queue.queue/exampleQueue=exampleQueue http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/6b17d966/examples/features/standard/jms-context/pom.xml ---------------------------------------------------------------------- diff --git a/examples/features/standard/jms-context/pom.xml b/examples/features/standard/jms-context/pom.xml new file mode 100644 index 0000000..1f3e017 --- /dev/null +++ b/examples/features/standard/jms-context/pom.xml @@ -0,0 +1,109 @@ +<?xml version='1.0'?> +<!-- +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. +--> + +<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/maven-v4_0_0.xsd"> + <modelVersion>4.0.0</modelVersion> + + <parent> + <groupId>org.apache.activemq.examples.broker</groupId> + <artifactId>jms-examples</artifactId> + <version>1.0.1-SNAPSHOT</version> + </parent> + + <artifactId>context</artifactId> + <packaging>jar</packaging> + <name>ActiveMQ Artemis JMS Context Example</name> + + <properties> + <activemq.basedir>${project.basedir}/../../../..</activemq.basedir> + </properties> + + <dependencies> + <dependency> + <groupId>org.apache.activemq</groupId> + <artifactId>artemis-jms-client</artifactId> + <version>${project.version}</version> + </dependency> + </dependencies> + + <build> + <plugins> + <plugin> + <groupId>org.apache.activemq</groupId> + <artifactId>artemis-maven-plugin</artifactId> + <executions> + <execution> + <id>create</id> + <goals> + <goal>create</goal> + </goals> + <configuration> + <ignore>${noServer}</ignore> + </configuration> + </execution> + <execution> + <id>start</id> + <goals> + <goal>cli</goal> + </goals> + <configuration> + <ignore>${noServer}</ignore> + <spawn>true</spawn> + <testURI>tcp://localhost:61616</testURI> + <args> + <param>run</param> + </args> + </configuration> + </execution> + <execution> + <id>runClient</id> + <goals> + <goal>runClient</goal> + </goals> + <configuration> + <clientClass>org.apache.activemq.artemis.jms.example.JMSContextExample</clientClass> + </configuration> + </execution> + <execution> + <id>stop</id> + <goals> + <goal>cli</goal> + </goals> + <configuration> + <ignore>${noServer}</ignore> + <args> + <param>stop</param> + </args> + </configuration> + </execution> + </executions> + <dependencies> + <dependency> + <groupId>org.apache.activemq.examples.broker</groupId> + <artifactId>context</artifactId> + <version>${project.version}</version> + </dependency> + </dependencies> + </plugin> + </plugins> + </build> + +</project> http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/6b17d966/examples/features/standard/jms-context/readme.html ---------------------------------------------------------------------- diff --git a/examples/features/standard/jms-context/readme.html b/examples/features/standard/jms-context/readme.html new file mode 100644 index 0000000..34895a2 --- /dev/null +++ b/examples/features/standard/jms-context/readme.html @@ -0,0 +1,35 @@ +<!-- +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. +--> + +<html> + <head> + <title>ActiveMQ Artemis JMS Context Example</title> + <link rel="stylesheet" type="text/css" href="../../../common/common.css" /> + <link rel="stylesheet" type="text/css" href="../../../common/prettify.css" /> + <script type="text/javascript" src="../../../common/prettify.js"></script> + </head> + <body onload="prettyPrint()"> + <h1>JMS Context Example</h1> + + <pre>To run the example, simply type <b>mvn verify</b> from this directory, <br>or <b>mvn -PnoServer verify</b> if you want to start and create the server manually.</pre> + + <p>This example shows you how to send and receive a message to a JMS Queue using ActiveMQ Artemis by using a JMS Context</p> + <p>A JMSContext is part of JMS 2.0 and combines the JMS Connection and Session Objects into a simple Interface</p> + </body> +</html> http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/6b17d966/examples/features/standard/jms-context/src/main/java/org/apache/activemq/artemis/jms/example/JMSContextExample.java ---------------------------------------------------------------------- diff --git a/examples/features/standard/jms-context/src/main/java/org/apache/activemq/artemis/jms/example/JMSContextExample.java b/examples/features/standard/jms-context/src/main/java/org/apache/activemq/artemis/jms/example/JMSContextExample.java new file mode 100644 index 0000000..d2a341c --- /dev/null +++ b/examples/features/standard/jms-context/src/main/java/org/apache/activemq/artemis/jms/example/JMSContextExample.java @@ -0,0 +1,51 @@ +/* + * 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.jms.example; + +import javax.jms.DeliveryMode; +import javax.jms.JMSContext; +import javax.jms.Queue; + +import org.apache.activemq.artemis.api.jms.ActiveMQJMSClient; +import org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory; + +/** + * A simple JMS Queue example that creates a producer and consumer on a queue and sends then receives a message. + */ +public class JMSContextExample { + + public static void main(final String[] args) throws Exception { + // Instantiate the queue + Queue queue = ActiveMQJMSClient.createQueue("exampleQueue"); + + // Instantiate the ConnectionFactory (Using the default URI on this case) + // Also instantiate the jmsContext + // Using closeable interface + try (ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory(); + JMSContext jmsContext = cf.createContext()) { + // Create a message producer, note that we can chain all this into one statement + jmsContext.createProducer().setDeliveryMode(DeliveryMode.PERSISTENT).send(queue, "this is a string"); + + // Create a Consumer and receive the payload of the message direct. + String payLoad = jmsContext.createConsumer(queue).receiveBody(String.class); + + System.out.println("payLoad = " + payLoad); + + } + + } +} http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/6b17d966/examples/features/standard/jms-shared-consumer/pom.xml ---------------------------------------------------------------------- diff --git a/examples/features/standard/jms-shared-consumer/pom.xml b/examples/features/standard/jms-shared-consumer/pom.xml new file mode 100644 index 0000000..e5fa1ce --- /dev/null +++ b/examples/features/standard/jms-shared-consumer/pom.xml @@ -0,0 +1,111 @@ +<?xml version='1.0'?> +<!-- +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. +--> + +<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/maven-v4_0_0.xsd"> + <modelVersion>4.0.0</modelVersion> + + <parent> + <groupId>org.apache.activemq.examples.broker</groupId> + <artifactId>jms-examples</artifactId> + <version>1.0.1-SNAPSHOT</version> + </parent> + + <artifactId>shared-consumer</artifactId> + <packaging>jar</packaging> + <name>ActiveMQ Artemis JMS Shared Consumer Example</name> + + <properties> + <activemq.basedir>${project.basedir}/../../../..</activemq.basedir> + </properties> + + <dependencies> + <dependency> + <groupId>org.apache.activemq</groupId> + <artifactId>artemis-jms-client</artifactId> + <version>${project.version}</version> + </dependency> + </dependencies> + + <build> + <plugins> + <plugin> + <groupId>org.apache.activemq</groupId> + <artifactId>artemis-maven-plugin</artifactId> + <executions> + <execution> + <id>create</id> + <goals> + <goal>create</goal> + </goals> + <configuration> + <ignore>${noServer}</ignore> + <instance>${basedir}/target/server0</instance> + <configuration>${basedir}/target/classes/activemq/server0</configuration> + </configuration> + </execution> + <execution> + <id>start</id> + <goals> + <goal>cli</goal> + </goals> + <configuration> + <ignore>${noServer}</ignore> + <spawn>true</spawn> + <testURI>tcp://localhost:61616</testURI> + <args> + <param>run</param> + </args> + </configuration> + </execution> + <execution> + <id>runClient</id> + <goals> + <goal>runClient</goal> + </goals> + <configuration> + <clientClass>org.apache.activemq.artemis.jms.example.JMSSharedConsumerExample</clientClass> + </configuration> + </execution> + <execution> + <id>stop</id> + <goals> + <goal>cli</goal> + </goals> + <configuration> + <ignore>${noServer}</ignore> + <args> + <param>stop</param> + </args> + </configuration> + </execution> + </executions> + <dependencies> + <dependency> + <groupId>org.apache.activemq.examples.broker</groupId> + <artifactId>shared-consumer</artifactId> + <version>${project.version}</version> + </dependency> + </dependencies> + </plugin> + </plugins> + </build> + +</project> http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/6b17d966/examples/features/standard/jms-shared-consumer/readme.html ---------------------------------------------------------------------- diff --git a/examples/features/standard/jms-shared-consumer/readme.html b/examples/features/standard/jms-shared-consumer/readme.html new file mode 100644 index 0000000..b92b340 --- /dev/null +++ b/examples/features/standard/jms-shared-consumer/readme.html @@ -0,0 +1,119 @@ +<!-- +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. +--> + +<html> + <head> + <title>ActiveMQ Artemis JMS Shared Consumer Example</title> + <link rel="stylesheet" type="text/css" href="../../../common/common.css" /> + <link rel="stylesheet" type="text/css" href="../../../common/prettify.css" /> + <script type="text/javascript" src="../../../common/prettify.js"></script> + </head> + <body onload="prettyPrint()"> + <h1>JMS Shared Consumer Example</h1> + + <pre>To run the example, simply type <b>mvn verify</b> from this directory, <br>or <b>mvn -PnoServer verify</b> if you want to start and create the server manually.</pre> + + <p>This example shows you how can use shared consumers to share a subscription on a topic. In JMS 1.1 this was not allowed + and so caused a scalability issue. In JMS 2 this restriction has been lifted so you can share the load across different + threads and connections.</p> + + <h2>Example step-by-step</h2> + + <ol> + <li>First we need to get an initial context so we can look-up the JMS connection factory and destination objects from JNDI. This initial context will get it's properties from the <code>client-jndi.properties</code> file in the directory <code>../common/config</code></li> + <pre class="prettyprint"> + <code>InitialContext initialContext = getContext();</code> + </pre> + + <li>We look-up the JMS topic object from JNDI</li> + <pre class="prettyprint"> + <code>Topic topic = (Topic) initialContext.lookup("/topic/exampleTopic");</code> + </pre> + + <li>We look-up the JMS connection factory object from JNDI</li> + <pre class="prettyprint"> + <code>ConnectionFactory cf = (ConnectionFactory) initialContext.lookup("/ConnectionFactory");</code> + </pre> + + <li>We create a JMS context</li> + <pre class="prettyprint"> + <code>jmsContext = cf.createContext();</code> + </pre> + + <li>We create a JMS Producer.</li> + <pre class="prettyprint"> + <code>JMSProducer producer = jmsContext.createProducer();</code> + </pre> + + <li>We create a shared consumer using the subscription name <literal>sc1</literal></li> + <pre class="prettyprint"> + <code>JMSConsumer jmsConsumer = jmsContext.createSharedConsumer(topic, "sc1");</code> + </pre> + + <li>We then create a second JMS context for a second shared consumer</li> + <pre class="prettyprint"> + <code>jmsContext2 = cf.createContext();</code> + </pre> + + <li>we then create the second shared consumer using the same subscription name</li> + <pre class="prettyprint"> + <code>JMSConsumer jmsConsumer2 = jmsContext2.createSharedConsumer(topic, "sc1");</code> + </pre> + + <li>we then send 2 messages</li> + <pre class="prettyprint"> + <code> + producer.send(topic, "this is a String!"); + + producer.send(topic, "this is a second String!") ;</code> + </pre> + + <li>we then receive the 2 messages using both shared consumers</li> + <pre class="prettyprint"> + <code> + String body = jmsConsumer.receiveBody(String.class, 5000); + + body = jmsConsumer2.receiveBody(String.class, 5000);</code> + </pre> + + <li>And finally, <b>always</b> remember to close your JMS connections and resources after use, in a <code>finally</code> block. Closing a JMS connection will automatically close all of its sessions, consumers, producer and browser objects</li> + + <pre class="prettyprint"> + <code>finally + { + if (initialContext != null) + { + initialContext.close(); + } + if (jmsContext != null) + { + jmsContext.close(); + } + if (jmsContext2 != null) + { + jmsContext2.close(); + } + }</code> + </pre> + + + + </ol> + </body> +</html> http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/6b17d966/examples/features/standard/jms-shared-consumer/src/main/java/org/apache/activemq/artemis/jms/example/JMSSharedConsumerExample.java ---------------------------------------------------------------------- diff --git a/examples/features/standard/jms-shared-consumer/src/main/java/org/apache/activemq/artemis/jms/example/JMSSharedConsumerExample.java b/examples/features/standard/jms-shared-consumer/src/main/java/org/apache/activemq/artemis/jms/example/JMSSharedConsumerExample.java new file mode 100644 index 0000000..da43eb0 --- /dev/null +++ b/examples/features/standard/jms-shared-consumer/src/main/java/org/apache/activemq/artemis/jms/example/JMSSharedConsumerExample.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.jms.example; + +import javax.jms.ConnectionFactory; +import javax.jms.JMSConsumer; +import javax.jms.JMSContext; +import javax.jms.JMSProducer; +import javax.jms.Topic; +import javax.naming.InitialContext; + +/** + * A JMS Example that uses shared consumers. + */ +public class JMSSharedConsumerExample { + + public static void main(final String[] args) throws Exception { + InitialContext initialContext = null; + JMSContext jmsContext = null; + JMSContext jmsContext2 = null; + try { + // Step 1. Create an initial context to perform the JNDI lookup. + initialContext = new InitialContext(); + + // Step 2. Perfom a lookup on the queue + Topic topic = (Topic) initialContext.lookup("topic/exampleTopic"); + + // Step 3. Perform a lookup on the Connection Factory + ConnectionFactory cf = (ConnectionFactory) initialContext.lookup("ConnectionFactory"); + + // Step 4.Create a JMS Context + jmsContext = cf.createContext(); + + // Step 5. Create a message producer. + JMSProducer producer = jmsContext.createProducer(); + + // Step 6. Create a shared consumer + JMSConsumer jmsConsumer = jmsContext.createSharedConsumer(topic, "sc1"); + + // Step 7. Create a second JMS Context for a second shared consumer + jmsContext2 = cf.createContext(); + + // Step 8. Create the second shared consumer + JMSConsumer jmsConsumer2 = jmsContext2.createSharedConsumer(topic, "sc1"); + + // Step 9. send 2 messages + producer.send(topic, "this is a String!"); + + producer.send(topic, "this is a second String!"); + + // Step 10. receive the messages shared by both consumers + String body = jmsConsumer.receiveBody(String.class, 5000); + + System.out.println("body = " + body); + + body = jmsConsumer2.receiveBody(String.class, 5000); + + System.out.println("body = " + body); + } + finally { + // Step 11. Be sure to close our JMS resources! + if (initialContext != null) { + initialContext.close(); + } + if (jmsContext != null) { + jmsContext.close(); + } + if (jmsContext2 != null) { + jmsContext2.close(); + } + } + } +} http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/6b17d966/examples/features/standard/jms-shared-consumer/src/main/resources/activemq/server0/artemis-roles.properties ---------------------------------------------------------------------- diff --git a/examples/features/standard/jms-shared-consumer/src/main/resources/activemq/server0/artemis-roles.properties b/examples/features/standard/jms-shared-consumer/src/main/resources/activemq/server0/artemis-roles.properties new file mode 100644 index 0000000..4e2d44c --- /dev/null +++ b/examples/features/standard/jms-shared-consumer/src/main/resources/activemq/server0/artemis-roles.properties @@ -0,0 +1,17 @@ +## --------------------------------------------------------------------------- +## 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. +## --------------------------------------------------------------------------- +guest=guest \ No newline at end of file http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/6b17d966/examples/features/standard/jms-shared-consumer/src/main/resources/activemq/server0/artemis-users.properties ---------------------------------------------------------------------- diff --git a/examples/features/standard/jms-shared-consumer/src/main/resources/activemq/server0/artemis-users.properties b/examples/features/standard/jms-shared-consumer/src/main/resources/activemq/server0/artemis-users.properties new file mode 100644 index 0000000..4e2d44c --- /dev/null +++ b/examples/features/standard/jms-shared-consumer/src/main/resources/activemq/server0/artemis-users.properties @@ -0,0 +1,17 @@ +## --------------------------------------------------------------------------- +## 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. +## --------------------------------------------------------------------------- +guest=guest \ No newline at end of file http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/6b17d966/examples/features/standard/jms-shared-consumer/src/main/resources/activemq/server0/broker.xml ---------------------------------------------------------------------- diff --git a/examples/features/standard/jms-shared-consumer/src/main/resources/activemq/server0/broker.xml b/examples/features/standard/jms-shared-consumer/src/main/resources/activemq/server0/broker.xml new file mode 100644 index 0000000..2b2ba2e --- /dev/null +++ b/examples/features/standard/jms-shared-consumer/src/main/resources/activemq/server0/broker.xml @@ -0,0 +1,61 @@ +<?xml version='1.0'?> +<!-- +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. +--> + +<configuration xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xmlns="urn:activemq" + xsi:schemaLocation="urn:activemq /schema/artemis-server.xsd"> + + <jms xmlns="urn:activemq:jms"> + <!--the topic used by the example--> + <topic name="exampleTopic"/> + </jms> + + <core xmlns="urn:activemq:core"> + + <bindings-directory>${data.dir:../data}/bindings</bindings-directory> + + <journal-directory>${data.dir:../data}/journal</journal-directory> + + <large-messages-directory>${data.dir:../data}/largemessages</large-messages-directory> + + <paging-directory>${data.dir:../data}/paging</paging-directory> + + <!-- Acceptors --> + + <acceptors> + <acceptor name="netty-acceptor">tcp://localhost:61616</acceptor> + </acceptors> + + <!-- Other config --> + + <security-settings> + <!--security for example topic--> + <security-setting match="jms.topic.exampleTopic"> + <permission type="createDurableQueue" roles="guest"/> + <permission type="deleteDurableQueue" roles="guest"/> + <permission type="createNonDurableQueue" roles="guest"/> + <permission type="deleteNonDurableQueue" roles="guest"/> + <permission type="consume" roles="guest"/> + <permission type="send" roles="guest"/> + </security-setting> + </security-settings> + + </core> +</configuration> http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/6b17d966/examples/features/standard/jms-shared-consumer/src/main/resources/jndi.properties ---------------------------------------------------------------------- diff --git a/examples/features/standard/jms-shared-consumer/src/main/resources/jndi.properties b/examples/features/standard/jms-shared-consumer/src/main/resources/jndi.properties new file mode 100644 index 0000000..54bed6d --- /dev/null +++ b/examples/features/standard/jms-shared-consumer/src/main/resources/jndi.properties @@ -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. + +java.naming.factory.initial=org.apache.activemq.artemis.jndi.ActiveMQInitialContextFactory +connectionFactory.ConnectionFactory=tcp://localhost:61616 +topic.topic/exampleTopic=exampleTopic http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/6b17d966/examples/features/standard/jmx/pom.xml ---------------------------------------------------------------------- diff --git a/examples/features/standard/jmx/pom.xml b/examples/features/standard/jmx/pom.xml new file mode 100644 index 0000000..693b2fc --- /dev/null +++ b/examples/features/standard/jmx/pom.xml @@ -0,0 +1,117 @@ +<?xml version='1.0'?> +<!-- +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. +--> + +<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/maven-v4_0_0.xsd"> + <modelVersion>4.0.0</modelVersion> + + <parent> + <groupId>org.apache.activemq.examples.broker</groupId> + <artifactId>jms-examples</artifactId> + <version>1.0.1-SNAPSHOT</version> + </parent> + + <artifactId>jmx</artifactId> + <packaging>jar</packaging> + <name>ActiveMQ Artemis JMS "JMX" Example</name> + + <properties> + <activemq.basedir>${project.basedir}/../../../..</activemq.basedir> + </properties> + + <dependencies> + <dependency> + <groupId>org.apache.activemq</groupId> + <artifactId>artemis-core-client</artifactId> + <version>${project.version}</version> + </dependency> + <dependency> + <groupId>org.apache.activemq</groupId> + <artifactId>artemis-jms-client</artifactId> + <version>${project.version}</version> + </dependency> + </dependencies> + + <build> + <plugins> + <plugin> + <groupId>org.apache.activemq</groupId> + <artifactId>artemis-maven-plugin</artifactId> + <executions> + <execution> + <id>create</id> + <goals> + <goal>create</goal> + </goals> + <configuration> + <ignore>${noServer}</ignore> + <!-- options used for JMX on the example --> + <javaOptions>-Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=3000 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false + </javaOptions> + </configuration> + </execution> + <execution> + <id>start</id> + <goals> + <goal>cli</goal> + </goals> + <configuration> + <ignore>${noServer}</ignore> + <spawn>true</spawn> + <testURI>tcp://localhost:61616</testURI> + <args> + <param>run</param> + </args> + </configuration> + </execution> + <execution> + <id>runClient</id> + <goals> + <goal>runClient</goal> + </goals> + <configuration> + <clientClass>org.apache.activemq.artemis.jms.example.JMXExample</clientClass> + </configuration> + </execution> + <execution> + <id>stop</id> + <goals> + <goal>cli</goal> + </goals> + <configuration> + <ignore>${noServer}</ignore> + <args> + <param>stop</param> + </args> + </configuration> + </execution> + </executions> + <dependencies> + <dependency> + <groupId>org.apache.activemq.examples.broker</groupId> + <artifactId>jmx</artifactId> + <version>${project.version}</version> + </dependency> + </dependencies> + </plugin> + </plugins> + </build> + +</project>