http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/21bf4406/examples/broker-features/ha/stop-server-failover/src/main/java/org/apache/activemq/artemis/jms/example/StopServerFailoverExample.java ---------------------------------------------------------------------- diff --git a/examples/broker-features/ha/stop-server-failover/src/main/java/org/apache/activemq/artemis/jms/example/StopServerFailoverExample.java b/examples/broker-features/ha/stop-server-failover/src/main/java/org/apache/activemq/artemis/jms/example/StopServerFailoverExample.java new file mode 100644 index 0000000..38f06c0 --- /dev/null +++ b/examples/broker-features/ha/stop-server-failover/src/main/java/org/apache/activemq/artemis/jms/example/StopServerFailoverExample.java @@ -0,0 +1,117 @@ +/* + * 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.JMSException; +import javax.jms.MessageConsumer; +import javax.jms.MessageProducer; +import javax.jms.Queue; +import javax.jms.Session; +import javax.jms.TextMessage; +import javax.naming.InitialContext; + +/** + * A simple example that demonstrates failover of the JMS connection from one node to another + * when the live server crashes using a JMS <em>non-transacted</em> session. + */ +public class StopServerFailoverExample { + + public static void main(final String[] args) throws Exception { + final int numMessages = 10; + + Connection connection = null; + + InitialContext initialContext = null; + + try { + // Step 1. Get an initial context for looking up JNDI from the server #1 + initialContext = new InitialContext(); + + // Step 2. Look up the JMS resources from JNDI + Queue queue = (Queue) initialContext.lookup("queue/exampleQueue"); + ConnectionFactory connectionFactory = (ConnectionFactory) initialContext.lookup("ConnectionFactory"); + + // Step 3. Create a JMS Connection + connection = connectionFactory.createConnection(); + + // Step 4. Create a *non-transacted* JMS Session with client acknowledgement + Session session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE); + + // Step 5. Start the connection to ensure delivery occurs + connection.start(); + + // Step 6. Create a JMS MessageProducer and a MessageConsumer + MessageProducer producer = session.createProducer(queue); + MessageConsumer consumer = session.createConsumer(queue); + + // Step 7. Send some messages to server #1, the live server + for (int i = 0; i < numMessages; i++) { + TextMessage message = session.createTextMessage("This is text message " + i); + producer.send(message); + System.out.println("Sent message: " + message.getText()); + } + + // Step 8. Receive and acknowledge half of the sent messages + TextMessage message0 = null; + for (int i = 0; i < numMessages / 2; i++) { + message0 = (TextMessage) consumer.receive(5000); + System.out.println("Got message: " + message0.getText()); + } + message0.acknowledge(); + + // Step 9. Receive the 2nd half of the sent messages but *do not* acknowledge them yet + for (int i = numMessages / 2; i < numMessages; i++) { + message0 = (TextMessage) consumer.receive(5000); + System.out.println("Got message: " + message0.getText()); + } + + // Step 10. Crash server #0, the live server, and wait a little while to make sure + // it has really crashed + System.out.println("Stop the live server by logging into JConsole and then press any key to continue..."); + System.in.read(); + + // Step 11. Acknowledging the 2nd half of the sent messages will fail as failover to the + // backup server has occurred + try { + message0.acknowledge(); + } + catch (JMSException e) { + System.err.println("Got exception while acknowledging message: " + e.getMessage()); + } + + // Step 12. Consume again the 2nd half of the messages again. Note that they are not considered as redelivered. + for (int i = numMessages / 2; i < numMessages; i++) { + message0 = (TextMessage) consumer.receive(5000); + System.out.printf("Got message: %s (redelivered?: %s)\n", message0.getText(), message0.getJMSRedelivered()); + } + message0.acknowledge(); + } + finally { + // Step 13. Be sure to close our resources! + + if (connection != null) { + connection.close(); + } + + if (initialContext != null) { + initialContext.close(); + } + } + } +}
http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/21bf4406/examples/broker-features/ha/stop-server-failover/src/main/resources/jndi.properties ---------------------------------------------------------------------- diff --git a/examples/broker-features/ha/stop-server-failover/src/main/resources/jndi.properties b/examples/broker-features/ha/stop-server-failover/src/main/resources/jndi.properties new file mode 100644 index 0000000..7f7a19f --- /dev/null +++ b/examples/broker-features/ha/stop-server-failover/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?ha=true&retryInterval=1000&retryIntervalMultiplier=1.0&reconnectAttempts=-1 +queue.queue/exampleQueue=exampleQueue http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/21bf4406/examples/broker-features/ha/transaction-failover/pom.xml ---------------------------------------------------------------------- diff --git a/examples/broker-features/ha/transaction-failover/pom.xml b/examples/broker-features/ha/transaction-failover/pom.xml new file mode 100644 index 0000000..76a4a8e --- /dev/null +++ b/examples/broker-features/ha/transaction-failover/pom.xml @@ -0,0 +1,104 @@ +<?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.failover</groupId> + <artifactId>broker-failover</artifactId> + <version>1.0.1-SNAPSHOT</version> + </parent> + + <artifactId>transaction-failover</artifactId> + <packaging>jar</packaging> + <name>ActiveMQ Artemis JMS Transaction Failover Example</name> + + <properties> + <activemq.basedir>${project.basedir}/../../../..</activemq.basedir> + </properties> + + <dependencies> + <dependency> + <groupId>org.apache.activemq</groupId> + <artifactId>artemis-cli</artifactId> + <version>${project.version}</version> + </dependency> + <dependency> + <groupId>org.apache.geronimo.specs</groupId> + <artifactId>geronimo-jms_2.0_spec</artifactId> + </dependency> + </dependencies> + + <build> + <plugins> + <plugin> + <groupId>org.apache.activemq</groupId> + <artifactId>artemis-maven-plugin</artifactId> + <executions> + <execution> + <id>create0</id> + <goals> + <goal>create</goal> + </goals> + <configuration> + <instance>${basedir}/target/server0</instance> + <configuration>${basedir}/target/classes/activemq/server0</configuration> + <javaOptions>-Dudp-address=${udp-address}</javaOptions> + </configuration> + </execution> + <execution> + <id>create1</id> + <goals> + <goal>create</goal> + </goals> + <configuration> + <instance>${basedir}/target/server1</instance> + <configuration>${basedir}/target/classes/activemq/server1</configuration> + <javaOptions>-Dudp-address=${udp-address}</javaOptions> + </configuration> + </execution> + <execution> + <id>runClient</id> + <goals> + <goal>runClient</goal> + </goals> + <configuration> + <clientClass>org.apache.activemq.artemis.jms.example.TransactionFailoverExample</clientClass> + <args> + <param>${basedir}/target/server0</param> + <param>${basedir}/target/server1</param> + </args> + </configuration> + </execution> + </executions> + <dependencies> + <dependency> + <groupId>org.apache.activemq.examples.failover</groupId> + <artifactId>transaction-failover</artifactId> + <version>${project.version}</version> + </dependency> + </dependencies> + </plugin> + </plugins> + </build> + +</project> http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/21bf4406/examples/broker-features/ha/transaction-failover/readme.html ---------------------------------------------------------------------- diff --git a/examples/broker-features/ha/transaction-failover/readme.html b/examples/broker-features/ha/transaction-failover/readme.html new file mode 100644 index 0000000..15f119f --- /dev/null +++ b/examples/broker-features/ha/transaction-failover/readme.html @@ -0,0 +1,46 @@ +<!-- +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 Failover With Transaction 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 Failover With Transaction Example</h1> + + <pre>To run the example, simply type <b>mvn verify</b> from this directory.</pre> + + + <p>This example demonstrates two servers coupled as a live-backup pair for high availability (HA), and a client + connection failing over from live to backup when the live server is crashed.</p> + <p>Failover behavior differs whether the JMS session is transacter or not.</p> + <p>When a <em>transacted</em> JMS session is used, once-and-only once delivery is guaranteed.</p> + <ul> + <li>if the failover occurs while there is an in-flight transaction, the transaction will be flagged as <em>rollback only</em>. In that case, the JMS client + will need to retry the transaction work.</li> + <li>if the failover occurs while there is <em>no</em> in-flight transaction, the failover will be transparent to the user.</li> + </ul> + <p>ActiveMQ Artemis also provides an example for <a href="../non-transactional-failover/readme.html">non-transaction failover</a>.</p> + <p>For more information on ActiveMQ Artemis failover and HA, and clustering in general, please see the clustering + section of the user manual.</p> + </body> +</html> http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/21bf4406/examples/broker-features/ha/transaction-failover/src/main/java/org/apache/activemq/artemis/jms/example/TransactionFailoverExample.java ---------------------------------------------------------------------- diff --git a/examples/broker-features/ha/transaction-failover/src/main/java/org/apache/activemq/artemis/jms/example/TransactionFailoverExample.java b/examples/broker-features/ha/transaction-failover/src/main/java/org/apache/activemq/artemis/jms/example/TransactionFailoverExample.java new file mode 100644 index 0000000..58c514a --- /dev/null +++ b/examples/broker-features/ha/transaction-failover/src/main/java/org/apache/activemq/artemis/jms/example/TransactionFailoverExample.java @@ -0,0 +1,167 @@ +/* + * 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 org.apache.activemq.artemis.api.core.Message; +import org.apache.activemq.artemis.util.ServerUtil; + +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.TransactionRolledBackException; +import javax.naming.InitialContext; + +/** + * A simple example that demonstrates failover of the JMS connection from one node to another + * when the live server crashes using a JMS <em>transacted</em> session. + */ +public class TransactionFailoverExample { + + // You need to guarantee uniqueIDs when using duplicate detection + // It needs to be unique even after a restart + // as these IDs are stored on the journal for control + // We recommend some sort of UUID, but for this example the Current Time as string would be enough + static String uniqueID = Long.toString(System.currentTimeMillis()); + + private static Process server0; + + private static Process server1; + + public static void main(final String[] args) throws Exception { + final int numMessages = 10; + + Connection connection = null; + + InitialContext initialContext = null; + + try { + server0 = ServerUtil.startServer(args[0], TransactionFailoverExample.class.getSimpleName() + "0", 0, 5000); + server1 = ServerUtil.startServer(args[1], TransactionFailoverExample.class.getSimpleName() + "1", 1, 5000); + + // Step 1. Get an initial context for looking up JNDI from the server #1 + initialContext = new InitialContext(); + + // Step 2. Look-up the JMS resources from JNDI + Queue queue = (Queue) initialContext.lookup("queue/exampleQueue"); + ConnectionFactory connectionFactory = (ConnectionFactory) initialContext.lookup("ConnectionFactory"); + + // Step 3. We create a JMS Connection + connection = connectionFactory.createConnection(); + + // Step 4. We create a *transacted* JMS Session + Session session = connection.createSession(true, 0); + + // Step 5. We start the connection to ensure delivery occurs + connection.start(); + + // Step 6. We create a JMS MessageProducer + MessageProducer producer = session.createProducer(queue); + + // Step 7. We create a JMS MessageConsumer + MessageConsumer consumer = session.createConsumer(queue); + + // Step 8. We send half of the messages, kill the live server and send the remaining messages + sendMessages(session, producer, numMessages, true); + + // Step 9. As failover occurred during transaction, the session has been marked for rollback only + try { + session.commit(); + } + catch (TransactionRolledBackException e) { + System.err.println("transaction has been rolled back: " + e.getMessage()); + } + + // Step 10. We resend all the messages + sendMessages(session, producer, numMessages, false); + + // Step 11. We commit the session successfully: the messages will be all delivered to the activated backup + // server + session.commit(); + + // Step 12. We are now transparently reconnected to server #0, the backup server. + // We consume the messages sent before the crash of the live server and commit the session. + for (int i = 0; i < numMessages; i++) { + TextMessage message0 = (TextMessage) consumer.receive(5000); + + if (message0 == null) { + throw new IllegalStateException("Example failed - message wasn't received"); + } + + System.out.println("Got message: " + message0.getText()); + } + + session.commit(); + + System.out.println("Other message on the server? " + consumer.receive(5000)); + } + finally { + // Step 13. Be sure to close our resources! + + if (connection != null) { + connection.close(); + } + + if (initialContext != null) { + initialContext.close(); + } + + ServerUtil.killServer(server0); + ServerUtil.killServer(server1); + } + } + + private static void sendMessages(final Session session, + final MessageProducer producer, + final int numMessages, + final boolean killServer) throws Exception { + + // We send half of messages + for (int i = 0; i < numMessages / 2; i++) { + TextMessage message = session.createTextMessage("This is text message " + i); + + message.setStringProperty(Message.HDR_DUPLICATE_DETECTION_ID.toString(), uniqueID + i); + + producer.send(message); + + System.out.println("Sent message: " + message.getText()); + } + + if (killServer) { + Thread.sleep(5000); + + ServerUtil.killServer(server0); + } + + // We send the remaining half of messages + for (int i = numMessages / 2; i < numMessages; i++) { + TextMessage message = session.createTextMessage("This is text message " + i); + + // We set the duplicate detection header - so the server will ignore the same message + // if sent again after failover + + message.setStringProperty(Message.HDR_DUPLICATE_DETECTION_ID.toString(), uniqueID + i); + + producer.send(message); + + System.out.println("Sent message: " + message.getText()); + } + } +} http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/21bf4406/examples/broker-features/ha/transaction-failover/src/main/resources/activemq/server0/broker.xml ---------------------------------------------------------------------- diff --git a/examples/broker-features/ha/transaction-failover/src/main/resources/activemq/server0/broker.xml b/examples/broker-features/ha/transaction-failover/src/main/resources/activemq/server0/broker.xml new file mode 100644 index 0000000..916fdf5 --- /dev/null +++ b/examples/broker-features/ha/transaction-failover/src/main/resources/activemq/server0/broker.xml @@ -0,0 +1,98 @@ +<?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 queue used by the example--> + <queue name="exampleQueue"/> + </jms> + + <core xmlns="urn:activemq:core"> + + <bindings-directory>../data/bindings</bindings-directory> + + <journal-directory>../data/journal</journal-directory> + + <large-messages-directory>../data/largemessages</large-messages-directory> + + <paging-directory>../data/paging</paging-directory> + + <ha-policy> + <shared-store> + <master> + <failover-on-shutdown>true</failover-on-shutdown> + </master> + </shared-store> + </ha-policy> + + <!-- Connectors --> + + <connectors> + <connector name="netty-connector">tcp://localhost:61616</connector> + </connectors> + + <!-- Acceptors --> + <acceptors> + <acceptor name="netty-acceptor">tcp://localhost:61616</acceptor> + </acceptors> + + <broadcast-groups> + <broadcast-group name="bg-group1"> + <group-address>${udp-address:231.7.7.7}</group-address> + <group-port>9876</group-port> + <broadcast-period>1000</broadcast-period> + <connector-ref>netty-connector</connector-ref> + </broadcast-group> + </broadcast-groups> + + <discovery-groups> + <discovery-group name="dg-group1"> + <group-address>${udp-address:231.7.7.7}</group-address> + <group-port>9876</group-port> + <refresh-timeout>60000</refresh-timeout> + </discovery-group> + </discovery-groups> + + <cluster-connections> + <cluster-connection name="my-cluster"> + <address>jms</address> + <connector-ref>netty-connector</connector-ref> + <discovery-group-ref discovery-group-name="dg-group1"/> + </cluster-connection> + </cluster-connections> + <!-- Other config --> + + <security-settings> + <!--security for example queue--> + <security-setting match="jms.queue.exampleQueue"> + <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/21bf4406/examples/broker-features/ha/transaction-failover/src/main/resources/activemq/server1/broker.xml ---------------------------------------------------------------------- diff --git a/examples/broker-features/ha/transaction-failover/src/main/resources/activemq/server1/broker.xml b/examples/broker-features/ha/transaction-failover/src/main/resources/activemq/server1/broker.xml new file mode 100644 index 0000000..055f04a --- /dev/null +++ b/examples/broker-features/ha/transaction-failover/src/main/resources/activemq/server1/broker.xml @@ -0,0 +1,98 @@ +<?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 queue used by the example--> + <queue name="exampleQueue"/> + </jms> + + <core xmlns="urn:activemq:core"> + + <bindings-directory>../data/bindings</bindings-directory> + + <journal-directory>../data/journal</journal-directory> + + <large-messages-directory>../data/largemessages</large-messages-directory> + + <paging-directory>../data/paging</paging-directory> + + <ha-policy> + <shared-store> + <slave> + <failover-on-shutdown>true</failover-on-shutdown> + </slave> + </shared-store> + </ha-policy> + + <!-- Connectors --> + <connectors> + <connector name="netty-connector">tcp://localhost:61617</connector> + </connectors> + + <!-- Acceptors --> + <acceptors> + <acceptor name="netty-acceptor">tcp://localhost:61617</acceptor> + </acceptors> + + <broadcast-groups> + <broadcast-group name="bg-group1"> + <group-address>${udp-address:231.7.7.7}</group-address> + <group-port>9876</group-port> + <broadcast-period>1000</broadcast-period> + <connector-ref>netty-connector</connector-ref> + </broadcast-group> + </broadcast-groups> + + <discovery-groups> + <discovery-group name="dg-group1"> + <group-address>${udp-address:231.7.7.7}</group-address> + <group-port>9876</group-port> + <refresh-timeout>60000</refresh-timeout> + </discovery-group> + </discovery-groups> + + <cluster-connections> + <cluster-connection name="my-cluster"> + <address>jms</address> + <connector-ref>netty-connector</connector-ref> + <discovery-group-ref discovery-group-name="dg-group1"/> + </cluster-connection> + </cluster-connections> + + <!-- Other config --> + + <security-settings> + <!--security for example queue--> + <security-setting match="jms.queue.exampleQueue"> + <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/21bf4406/examples/broker-features/ha/transaction-failover/src/main/resources/jndi.properties ---------------------------------------------------------------------- diff --git a/examples/broker-features/ha/transaction-failover/src/main/resources/jndi.properties b/examples/broker-features/ha/transaction-failover/src/main/resources/jndi.properties new file mode 100644 index 0000000..7f7a19f --- /dev/null +++ b/examples/broker-features/ha/transaction-failover/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?ha=true&retryInterval=1000&retryIntervalMultiplier=1.0&reconnectAttempts=-1 +queue.queue/exampleQueue=exampleQueue http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/21bf4406/examples/broker-features/pom.xml ---------------------------------------------------------------------- diff --git a/examples/broker-features/pom.xml b/examples/broker-features/pom.xml new file mode 100644 index 0000000..6d1aa13 --- /dev/null +++ b/examples/broker-features/pom.xml @@ -0,0 +1,66 @@ +<?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</groupId> + <artifactId>artemis-examples</artifactId> + <version>1.0.1-SNAPSHOT</version> + </parent> + + <groupId>org.apache.activemq.examples.clustered</groupId> + <artifactId>broker-features</artifactId> + <packaging>pom</packaging> + <name>ActiveMQ Artemis Clustered Examples</name> + + <!-- Properties --> + <properties> + <!-- + Explicitly declaring the source encoding eliminates the following + message: [WARNING] Using platform encoding (UTF-8 actually) to copy + filtered resources, i.e. build is platform dependent! + --> + <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> + <activemq.basedir>${project.basedir}/../..</activemq.basedir> + </properties> + + <profiles> + <profile> + <id>examples</id> + <modules> + <module>clustered</module> + <module>ha</module> + <module>standard</module> + <module>sub-modules</module> + </modules> + </profile> + <profile> + <id>release</id> + <modules> + <module>clustered</module> + <module>ha</module> + <module>standard</module> + <module>sub-modules</module> + </modules> + </profile> + </profiles> +</project> http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/21bf4406/examples/broker-features/standard/README.md ---------------------------------------------------------------------- diff --git a/examples/broker-features/standard/README.md b/examples/broker-features/standard/README.md new file mode 100644 index 0000000..e9d9e36 --- /dev/null +++ b/examples/broker-features/standard/README.md @@ -0,0 +1,32 @@ +Running the ActiveMQ Artemis Examples +============================ + +To run an individual example firstly cd into the example directory and run + +```sh +mvn verify +``` + +Most examples offer a way to start them without creating and starting the server (say if you want to do it manually) + +```sh +mvn verify -PnoServer +``` + +If you are running against an un released version, i.e. from master branch, you will have to run `mvn install` on the root +pom.xml and the example/activemq-jms-examples-common/pom.xml first. + +If you want to run all the examples (except those that need to be run standalone) you can run `mvn verify -Pexamples` in the examples +directory but before you do you will need to up the memory used by running: + +``` +export MAVEN_OPTS="-Xmx1024m -XX:MaxPermSize=256m" +``` +### Recreating the examples + +If you are trying to copy the examples somewhere else and modifying them. Consider asking Maven to explicitly list all the dependencies: + +``` +# if trying to modify the 'topic' example: +cd examples/jms/topic && mvn dependency:list +``` http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/21bf4406/examples/broker-features/standard/bridge/pom.xml ---------------------------------------------------------------------- diff --git a/examples/broker-features/standard/bridge/pom.xml b/examples/broker-features/standard/bridge/pom.xml new file mode 100644 index 0000000..db1caf8 --- /dev/null +++ b/examples/broker-features/standard/bridge/pom.xml @@ -0,0 +1,167 @@ +<?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>core-bridge</artifactId> + <packaging>jar</packaging> + <name>ActiveMQ Artemis Core Bridge Example</name> + + <properties> + <activemq.basedir>${project.basedir}/../../../..</activemq.basedir> + </properties> + + <dependencies> + <dependency> + <groupId>org.apache.activemq</groupId> + <artifactId>artemis-server</artifactId> + <version>${project.version}</version> + </dependency> + <dependency> + <groupId>org.apache.geronimo.specs</groupId> + <artifactId>geronimo-jms_2.0_spec</artifactId> + </dependency> + </dependencies> + + <build> + <plugins> + <plugin> + <groupId>org.apache.activemq</groupId> + <artifactId>artemis-maven-plugin</artifactId> + <executions> + <execution> + <id>create0</id> + <goals> + <goal>create</goal> + </goals> + <configuration> + <libList> + <!-- For the transformer --> + <arg>org.apache.activemq.examples.broker:core-bridge:${project.version}</arg> + </libList> + <ignore>${noServer}</ignore> + <instance>${basedir}/target/server0</instance> + <configuration>${basedir}/target/classes/activemq/server0</configuration> + </configuration> + </execution> + <execution> + <id>create1</id> + <goals> + <goal>create</goal> + </goals> + <configuration> + <libList> + <!-- For the transformer --> + <arg>org.apache.activemq.examples.broker:core-bridge:${project.version}</arg> + </libList> + <ignore>${noServer}</ignore> + <instance>${basedir}/target/server1</instance> + <configuration>${basedir}/target/classes/activemq/server1</configuration> + <portOffset>1</portOffset> + </configuration> + </execution> + <execution> + <id>start0</id> + <goals> + <goal>cli</goal> + </goals> + <configuration> + <ignore>${noServer}</ignore> + <spawn>true</spawn> + <location>${basedir}/target/server0</location> + <testURI>tcp://localhost:61616</testURI> + <args> + <param>run</param> + </args> + <name>server0</name> + </configuration> + </execution> + <execution> + <id>start1</id> + <goals> + <goal>cli</goal> + </goals> + <configuration> + <ignore>${noServer}</ignore> + <spawn>true</spawn> + <location>${basedir}/target/server1</location> + <testURI>tcp://localhost:61617</testURI> + <args> + <param>run</param> + </args> + <name>server1</name> + </configuration> + </execution> + <execution> + <id>runClient</id> + <goals> + <goal>runClient</goal> + </goals> + <configuration> + <clientClass>org.apache.activemq.artemis.jms.example.BridgeExample</clientClass> + </configuration> + </execution> + <execution> + <id>stop0</id> + <goals> + <goal>cli</goal> + </goals> + <configuration> + <ignore>${noServer}</ignore> + <location>${basedir}/target/server0</location> + <args> + <param>stop</param> + </args> + </configuration> + </execution> + <execution> + <id>stop1</id> + <goals> + <goal>cli</goal> + </goals> + <configuration> + <ignore>${noServer}</ignore> + <location>${basedir}/target/server1</location> + <args> + <param>stop</param> + </args> + </configuration> + </execution> + </executions> + <dependencies> + <dependency> + <groupId>org.apache.activemq.examples.broker</groupId> + <artifactId>core-bridge</artifactId> + <version>${project.version}</version> + </dependency> + </dependencies> + </plugin> + </plugins> + </build> + +</project> http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/21bf4406/examples/broker-features/standard/bridge/readme.html ---------------------------------------------------------------------- diff --git a/examples/broker-features/standard/bridge/readme.html b/examples/broker-features/standard/bridge/readme.html new file mode 100644 index 0000000..698c6c2 --- /dev/null +++ b/examples/broker-features/standard/bridge/readme.html @@ -0,0 +1,74 @@ +<!-- +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 Core 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>Core 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 demonstrates a core bridge deployed on one server, which consumes messages from a + local queue and forwards them to an address on a second server.</p> + + <p>Core bridges are used to create message flows between any two ActiveMQ Artemis servers which are remotely separated. + Core bridges are resilient and will cope with temporary connection failure allowing them to be an ideal + choice for forwarding over unreliable connections, e.g. a WAN.</p> + <p>They can also be configured with an optional filter expression, and will only forward messages that + match that filter.</p> + <p>Furthermore they can be configured to use an optional Transformer class. A user-defined Transformer class + can be specified which is called at forwarding time. This gives the user the opportunity to transform + the message in some ways, e.g. changing its properties or body</p> + <p>ActiveMQ Artemis also includes a <b>JMS Bridge</b>. This is similar to a core bridge, but uses the JMS API + and can be used to bridge between any two JMS 1.1 compliant messaging systems. The core bridge is limited to bridging + between ActiveMQ Artemis instances, but may provide better performance than the JMS bridge. The JMS bridge is covered in + a separate example.</p> + <p>For more information on bridges, please see the ActiveMQ Artemis user manual.</p> + + <p>In this example we will demonstrate a simple sausage factory for aardvarks.</p> + <p>We have a JMS queue on server 0 named <code>sausage-factory</code>, and we have a + JMS queue on server 1 named <code>mincing-machine</code></p> + <p>We want to forward any messages that are sent to the <code>sausage-factory</code> queue on server 0, to the <code>mincing-machine</code> + on server 1.</p> + <p>We only want to make aardvark sausages, so we only forward messages where the property "name" is set + to "aardvark". It is known that other things, such are Sasquatches are also sent to the <code>sausage-factory</code> and we + want to reject those.</p> + <p>Moreover it is known that Aardvarks normally wear blue hats, and it's important that we only make sausages using + Aardvarks with green hats, so on the way we are going transform the property "hat" from "green" to "blue".</p> + <p>Here's a snippet from <code>broker.xml</code> showing the bridge configuration</p> + <pre class="prettyprint"> + <code> + <bridge name="my-bridge"> + <queue-name>jms.queue.sausage-factory</queue-name> + <forwarding-address>jms.queue.mincing-machine</forwarding-address> + <filter string="name='aardvark'"/> + <transformer-class-name>org.apache.activemq.artemis.jms.example.HatColourChangeTransformer</transformer-class-name> + <reconnect-attempts>-1</reconnect-attempts> + <static-connectors> + <connector-ref>remote-connector</connector-ref> + </static-connectors> + </bridge> + </code> + </pre> + </body> +</html> http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/21bf4406/examples/broker-features/standard/bridge/src/main/java/org/apache/activemq/artemis/jms/example/BridgeExample.java ---------------------------------------------------------------------- diff --git a/examples/broker-features/standard/bridge/src/main/java/org/apache/activemq/artemis/jms/example/BridgeExample.java b/examples/broker-features/standard/bridge/src/main/java/org/apache/activemq/artemis/jms/example/BridgeExample.java new file mode 100644 index 0000000..0fa17c2 --- /dev/null +++ b/examples/broker-features/standard/bridge/src/main/java/org/apache/activemq/artemis/jms/example/BridgeExample.java @@ -0,0 +1,175 @@ +/* + * 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.Message; +import javax.jms.MessageConsumer; +import javax.jms.MessageProducer; +import javax.jms.Queue; +import javax.jms.Session; +import javax.naming.InitialContext; +import java.util.Hashtable; + +/** + * This example demonstrates a core bridge set-up between two nodes, consuming messages from a queue + * on one node and forwarding them to an address on the second node. + */ +public class BridgeExample { + + public static void main(final String[] args) throws Exception { + Connection connection0 = null; + + Connection connection1 = null; + + InitialContext ic0 = null; + + InitialContext ic1 = null; + + try { + // Step 1 - we create an initial context for looking up JNDI on node 0 + + Hashtable<String, Object> properties = new Hashtable<String, Object>(); + properties.put("java.naming.factory.initial", "org.apache.activemq.artemis.jndi.ActiveMQInitialContextFactory"); + properties.put("connectionFactory.ConnectionFactory", "tcp://127.0.0.1:61616"); + properties.put("queue.queue/sausage-factory", "sausage-factory"); + ic0 = new InitialContext(properties); + + // Step 2 - we look up the sausage-factory queue from node 0 + + Queue sausageFactory = (Queue) ic0.lookup("queue/sausage-factory"); + + // Step 3 - we look up a JMS ConnectionFactory object from node 0 + + ConnectionFactory cf0 = (ConnectionFactory) ic0.lookup("ConnectionFactory"); + + // Step 4 - we create an initial context for looking up JNDI on node 1 + + properties = new Hashtable<String, Object>(); + properties.put("java.naming.factory.initial", "org.apache.activemq.artemis.jndi.ActiveMQInitialContextFactory"); + properties.put("connectionFactory.ConnectionFactory", "tcp://127.0.0.1:61617"); + properties.put("queue.queue/mincing-machine", "mincing-machine"); + ic1 = new InitialContext(properties); + + // Step 5 - we look up the mincing-machine queue on node 1 + + Queue mincingMachine = (Queue) ic1.lookup("queue/mincing-machine"); + + // Step 6 - we look up a JMS ConnectionFactory object from node 1 + + ConnectionFactory cf1 = (ConnectionFactory) ic1.lookup("ConnectionFactory"); + + // Step 7. We create a JMS Connection connection0 which is a connection to server 0 + + connection0 = cf0.createConnection(); + + // Step 8. We create a JMS Connection connection1 which is a connection to server 1 + connection1 = cf1.createConnection(); + + // Step 9. We create a JMS Session on server 0 + + Session session0 = connection0.createSession(false, Session.AUTO_ACKNOWLEDGE); + + // Step 10. We create a JMS Session on server 1 + + Session session1 = connection1.createSession(false, Session.AUTO_ACKNOWLEDGE); + + // Step 10. We start the connection to ensure delivery occurs on them + + connection1.start(); + + // Step 11. We create JMS MessageConsumer object + MessageConsumer consumer = session1.createConsumer(mincingMachine); + + // Step 12. We create a JMS MessageProducer object on server 0 + MessageProducer producer = session0.createProducer(sausageFactory); + + // Step 13. We create and send a message representing an aardvark with a green hat to the sausage-factory + // on node 0 + Message message = session0.createMessage(); + + message.setStringProperty("name", "aardvark"); + + message.setStringProperty("hat", "green"); + + producer.send(message); + + System.out.println("Sent " + message.getStringProperty("name") + + " message with " + + message.getStringProperty("hat") + + " hat to sausage-factory on node 0"); + + // Step 14 - we successfully receive the aardvark message from the mincing-machine one node 1. The aardvark's + // hat is now blue since it has been transformed! + + Message receivedMessage = consumer.receive(5000); + + System.out.println("Received " + receivedMessage.getStringProperty("name") + + " message with " + + receivedMessage.getStringProperty("hat") + + " hat from mincing-machine on node 1"); + + // Step 13. We create and send another message, this time representing a sasquatch with a mauve hat to the + // sausage-factory on node 0. This won't be bridged to the mincing-machine since we only want aardvarks, not + // sasquatches + + message = session0.createMessage(); + + message.setStringProperty("name", "sasquatch"); + + message.setStringProperty("hat", "mauve"); + + producer.send(message); + + System.out.println("Sent " + message.getStringProperty("name") + + " message with " + + message.getStringProperty("hat") + + " hat to sausage-factory on node 0"); + + // Step 14. We don't receive the message since it has not been bridged. + + receivedMessage = consumer.receive(1000); + + if (receivedMessage == null) { + System.out.println("Didn't receive that message from mincing-machine on node 1"); + } + else { + throw new IllegalStateException(); + } + } + finally { + // Step 15. Be sure to close our resources! + + if (connection0 != null) { + connection0.close(); + } + + if (connection1 != null) { + connection1.close(); + } + + if (ic0 != null) { + ic0.close(); + } + + if (ic1 != null) { + ic1.close(); + } + } + } +} http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/21bf4406/examples/broker-features/standard/bridge/src/main/java/org/apache/activemq/artemis/jms/example/HatColourChangeTransformer.java ---------------------------------------------------------------------- diff --git a/examples/broker-features/standard/bridge/src/main/java/org/apache/activemq/artemis/jms/example/HatColourChangeTransformer.java b/examples/broker-features/standard/bridge/src/main/java/org/apache/activemq/artemis/jms/example/HatColourChangeTransformer.java new file mode 100644 index 0000000..ae8cf39 --- /dev/null +++ b/examples/broker-features/standard/bridge/src/main/java/org/apache/activemq/artemis/jms/example/HatColourChangeTransformer.java @@ -0,0 +1,38 @@ +/* + * 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 org.apache.activemq.artemis.api.core.SimpleString; +import org.apache.activemq.artemis.core.server.ServerMessage; +import org.apache.activemq.artemis.core.server.cluster.Transformer; + +public class HatColourChangeTransformer implements Transformer { + + public ServerMessage transform(final ServerMessage message) { + SimpleString propName = new SimpleString("hat"); + + SimpleString oldProp = message.getSimpleStringProperty(propName); + + // System.out.println("Old hat colour is " + oldProp); + + // Change the colour + message.putStringProperty(propName, new SimpleString("blue")); + + return message; + } + +} http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/21bf4406/examples/broker-features/standard/bridge/src/main/resources/activemq/server0/broker.xml ---------------------------------------------------------------------- diff --git a/examples/broker-features/standard/bridge/src/main/resources/activemq/server0/broker.xml b/examples/broker-features/standard/bridge/src/main/resources/activemq/server0/broker.xml new file mode 100644 index 0000000..250a851 --- /dev/null +++ b/examples/broker-features/standard/bridge/src/main/resources/activemq/server0/broker.xml @@ -0,0 +1,88 @@ +<?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 queue used by the example--> + <queue name="sausage-factory"/> + </jms> + + <core xmlns="urn:activemq:core"> + + <bindings-directory>${data.dir}/server0/data/messaging/bindings</bindings-directory> + + <journal-directory>${data.dir}/server0/data/messaging/journal</journal-directory> + + <large-messages-directory>${data.dir}/server0/data/messaging/largemessages</large-messages-directory> + + <paging-directory>${data.dir}/server0/data/messaging/paging</paging-directory> + + <!-- Connectors --> + <connectors> + <!-- Connector to the other node --> + <connector name="remote-connector">tcp://localhost:61617</connector> + </connectors> + + <!-- Acceptors --> + <acceptors> + <acceptor name="netty-acceptor">tcp://localhost:61616</acceptor> + </acceptors> + + <!-- We need to create a core queue for the JMS queue explicitly because the bridge will be deployed + before the JMS queue is deployed, so the first time, it otherwise won't find the queue --> + <queues> + <queue name="jms.queue.sausage-factory"> + <address>jms.queue.sausage-factory</address> + </queue> + </queues> + + <!-- We set-up a bridge that forwards from a queue on this node to an address on another node. + We specify a filter with the bridge, and a transformer too. The filter and transformer are optional --> + <bridges> + <bridge name="my-bridge"> + <queue-name>jms.queue.sausage-factory</queue-name> + <forwarding-address>jms.queue.mincing-machine</forwarding-address> + <filter string="name='aardvark'"/> + <transformer-class-name>org.apache.activemq.artemis.jms.example.HatColourChangeTransformer</transformer-class-name> + <reconnect-attempts>-1</reconnect-attempts> + <static-connectors> + <connector-ref>remote-connector</connector-ref> + </static-connectors> + </bridge> + </bridges> + + <!-- Other config --> + + <security-settings> + <!--security for example queue--> + <security-setting match="jms.queue.#"> + <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/21bf4406/examples/broker-features/standard/bridge/src/main/resources/activemq/server1/broker.xml ---------------------------------------------------------------------- diff --git a/examples/broker-features/standard/bridge/src/main/resources/activemq/server1/broker.xml b/examples/broker-features/standard/bridge/src/main/resources/activemq/server1/broker.xml new file mode 100644 index 0000000..516f179 --- /dev/null +++ b/examples/broker-features/standard/bridge/src/main/resources/activemq/server1/broker.xml @@ -0,0 +1,59 @@ +<?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 queue used by the example--> + <queue name="mincing-machine"/> + </jms> + <core xmlns="urn:activemq:core"> + + <bindings-directory>${data.dir}/server1/data/messaging/bindings</bindings-directory> + + <journal-directory>${data.dir}/server1/data/messaging/journal</journal-directory> + + <large-messages-directory>${data.dir}/server1/data/messaging/largemessages</large-messages-directory> + + <paging-directory>${data.dir}/server1/data/messaging/paging</paging-directory> + + <!-- Acceptors --> + <acceptors> + <acceptor name="netty-acceptor">tcp://localhost:61617</acceptor> + </acceptors> + + <!-- Other config --> + + <security-settings> + <!--security for example queue--> + <security-setting match="jms.queue.#"> + <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/21bf4406/examples/broker-features/standard/browser/pom.xml ---------------------------------------------------------------------- diff --git a/examples/broker-features/standard/browser/pom.xml b/examples/broker-features/standard/browser/pom.xml new file mode 100644 index 0000000..7ab1299 --- /dev/null +++ b/examples/broker-features/standard/browser/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>browser</artifactId> + <packaging>jar</packaging> + <name>ActiveMQ Artemis JMS Browser 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> + <phase>verify</phase> + <configuration> + <ignore>${noServer}</ignore> + </configuration> + <goals> + <goal>create</goal> + </goals> + </execution> + <execution> + <id>start</id> + <goals> + <goal>cli</goal> + </goals> + <configuration> + <spawn>true</spawn> + <ignore>${noServer}</ignore> + <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.QueueBrowserExample</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>browser</artifactId> + <version>${project.version}</version> + </dependency> + </dependencies> + </plugin> + </plugins> + </build> + +</project> http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/21bf4406/examples/broker-features/standard/browser/readme.html ---------------------------------------------------------------------- diff --git a/examples/broker-features/standard/browser/readme.html b/examples/broker-features/standard/browser/readme.html new file mode 100644 index 0000000..9f9e01a --- /dev/null +++ b/examples/broker-features/standard/browser/readme.html @@ -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. +--> + +<html> + <head> + <title>ActiveMQ Artemis JMS QueueBrowser 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 QueueBrowser 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 use a JMS <a href="http://java.sun.com/javaee/5/docs/api/javax/jms/QueueBrowser.html">QueueBrowser</a> with ActiveMQ Artemis.<br /> + Queues are a standard part of JMS, please consult the JMS 1.1 specification for full details.<br /> + A QueueBrowser is used to look at messages on the queue without removing them. + It can scan the entire content of a queue or only messages matching a message selector.</p> + <p> + The example will send 2 messages on a queue, use a QueueBrowser to browse + the queue (looking at the message without removing them) and finally consume the 2 messages + </p> + </body> +</html> http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/21bf4406/examples/broker-features/standard/browser/src/main/java/org/apache/activemq/artemis/jms/example/QueueBrowserExample.java ---------------------------------------------------------------------- diff --git a/examples/broker-features/standard/browser/src/main/java/org/apache/activemq/artemis/jms/example/QueueBrowserExample.java b/examples/broker-features/standard/browser/src/main/java/org/apache/activemq/artemis/jms/example/QueueBrowserExample.java new file mode 100644 index 0000000..1c9db3b --- /dev/null +++ b/examples/broker-features/standard/browser/src/main/java/org/apache/activemq/artemis/jms/example/QueueBrowserExample.java @@ -0,0 +1,103 @@ +/* + * 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.QueueBrowser; +import javax.jms.Session; +import javax.jms.TextMessage; +import javax.naming.InitialContext; +import java.util.Enumeration; + +/** + * A simple example which shows how to use a QueueBrowser to look at messages of a queue without removing them from the queue + */ +public class QueueBrowserExample { + + public static void main(final String[] args) throws Exception { + Connection connection = null; + InitialContext initialContext = null; + try { + // Step 1. Create an initial context to perform the JNDI lookup. + initialContext = new InitialContext(); + + // Step 2. Perfom a lookup on the queue + Queue queue = (Queue) initialContext.lookup("queue/exampleQueue"); + + // Step 3. Perform a lookup on the Connection Factory + // you could alternatively instantiate the connection directly + // ConnectionFactory cf = new ActiveMQConnectionFactory(); // this would accept the broker URI as well + ConnectionFactory cf = (ConnectionFactory) initialContext.lookup("ConnectionFactory"); + + // Step 4. Create a JMS Connection + connection = cf.createConnection(); + + // Step 5. Create a JMS Session + Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); + + // Step 6. Create a JMS Message Producer + MessageProducer producer = session.createProducer(queue); + + // Step 7. Create 2 Text Messages + TextMessage message_1 = session.createTextMessage("this is the 1st message"); + TextMessage message_2 = session.createTextMessage("this is the 2nd message"); + + // Step 8. Send the Message + producer.send(message_1); + producer.send(message_2); + + // Step 9. Create the JMS QueueBrowser + QueueBrowser browser = session.createBrowser(queue); + + // Step 10. Browse the messages on the queue + // Browsing a queue does not remove the messages from the queue + Enumeration messageEnum = browser.getEnumeration(); + while (messageEnum.hasMoreElements()) { + TextMessage message = (TextMessage) messageEnum.nextElement(); + System.out.println("Browsing: " + message.getText()); + } + + // Step 11. Close the browser + browser.close(); + + // Step 12. Create a JMS Message Consumer + MessageConsumer messageConsumer = session.createConsumer(queue); + + // Step 13. Start the Connection + connection.start(); + + // Step 14. Receive the 2 messages + TextMessage messageReceived = (TextMessage) messageConsumer.receive(5000); + System.out.println("Received message: " + messageReceived.getText()); + messageReceived = (TextMessage) messageConsumer.receive(5000); + System.out.println("Received message: " + messageReceived.getText()); + } + finally { + // Step 15. Be sure to close our JMS resources! + if (initialContext != null) { + initialContext.close(); + } + if (connection != null) { + connection.close(); + } + } + } +} http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/21bf4406/examples/broker-features/standard/browser/src/main/resources/jndi.properties ---------------------------------------------------------------------- diff --git a/examples/broker-features/standard/browser/src/main/resources/jndi.properties b/examples/broker-features/standard/browser/src/main/resources/jndi.properties new file mode 100644 index 0000000..93537c4 --- /dev/null +++ b/examples/broker-features/standard/browser/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/21bf4406/examples/broker-features/standard/client-kickoff/pom.xml ---------------------------------------------------------------------- diff --git a/examples/broker-features/standard/client-kickoff/pom.xml b/examples/broker-features/standard/client-kickoff/pom.xml new file mode 100644 index 0000000..6ffb19a --- /dev/null +++ b/examples/broker-features/standard/client-kickoff/pom.xml @@ -0,0 +1,112 @@ +<?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>client-kickoff</artifactId> + <packaging>jar</packaging> + <name>ActiveMQ Artemis JMS Kick Off 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> + <!-- 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.ClientKickoffExample</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>client-kickoff</artifactId> + <version>${project.version}</version> + </dependency> + </dependencies> + </plugin> + </plugins> + </build> + +</project> http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/21bf4406/examples/broker-features/standard/client-kickoff/readme.html ---------------------------------------------------------------------- diff --git a/examples/broker-features/standard/client-kickoff/readme.html b/examples/broker-features/standard/client-kickoff/readme.html new file mode 100644 index 0000000..8400e6a --- /dev/null +++ b/examples/broker-features/standard/client-kickoff/readme.html @@ -0,0 +1,54 @@ +<!-- +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 Client Kickoff 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>Client Kickoff 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 how to kick off a client connected to ActiveMQ + using <a href="http://java.sun.com/javase/technologies/core/mntr-mgmt/javamanagement/">JMX</a></p> + + <p>The example will connect to ActiveMQ Artemis. Using JMX, we will list the remote addresses connected to the + server and close the corresponding connections. The client will be kicked off from ActiveMQ Artemis receiving + an exception that its JMS connection was interrupted.</p> + + <h2>Example configuration</h2> + + <p>ActiveMQ Artemis exposes its managed resources by default on the platform MBeanServer.</p> + <p>To access this MBeanServer remotely, the Java Virtual machine must be started with system properties: + <pre class="prettyprint"> + <code>-Dcom.sun.management.jmxremote + -Dcom.sun.management.jmxremote.port=3000 + -Dcom.sun.management.jmxremote.ssl=false + -Dcom.sun.management.jmxremote.authenticate=false</code> + </pre> + <p>These properties are explained in the Java 5 <a href="http://java.sun.com/j2se/1.5.0/docs/guide/management/agent.html#remote">Management guide</a> + (please note that for this example, we will disable user authentication for simplicity).</p> + <p>With these properties, ActiveMQ Artemis server will be manageable remotely using standard JMX URL on port <code>3000</code>.</p> + </p> + </body> +</html>
