From f53e411dad451767a66d72e63686a61b03047c81 Mon Sep 17 00:00:00 2001 From: Justin Bertram Date: Mon, 27 Nov 2017 10:51:54 -0600 Subject: [PATCH] ARTEMIS-1525 refactor embedded examples --- .../artemis/core/server/ActiveMQServers.java | 14 ++++ docs/user-manual/en/examples.md | 6 +- .../standard/embedded-simple/readme.html | 73 ++----------------- .../artemis/jms/example/EmbeddedExample.java | 43 ++++++----- .../src/main/resources/broker.xml | 4 - .../src/main/resources/jndi.properties | 20 +++++ .../features/standard/embedded/readme.html | 10 +-- .../artemis/jms/example/EmbeddedExample.java | 53 ++++++-------- .../src/main/resources/jndi.properties | 20 +++++ 9 files changed, 109 insertions(+), 134 deletions(-) create mode 100644 examples/features/standard/embedded-simple/src/main/resources/jndi.properties create mode 100644 examples/features/standard/embedded/src/main/resources/jndi.properties diff --git a/artemis-server/src/main/java/org/apache/activemq/artemis/core/server/ActiveMQServers.java b/artemis-server/src/main/java/org/apache/activemq/artemis/core/server/ActiveMQServers.java index 4bdfd5b31c..aed594448c 100644 --- a/artemis-server/src/main/java/org/apache/activemq/artemis/core/server/ActiveMQServers.java +++ b/artemis-server/src/main/java/org/apache/activemq/artemis/core/server/ActiveMQServers.java @@ -20,6 +20,8 @@ import javax.management.MBeanServer; import java.lang.management.ManagementFactory; import org.apache.activemq.artemis.core.config.Configuration; +import org.apache.activemq.artemis.core.config.FileDeploymentManager; +import org.apache.activemq.artemis.core.config.impl.FileConfiguration; import org.apache.activemq.artemis.core.config.impl.SecurityConfiguration; import org.apache.activemq.artemis.core.server.impl.ActiveMQServerImpl; import org.apache.activemq.artemis.spi.core.security.ActiveMQJAASSecurityManager; @@ -64,6 +66,18 @@ public final class ActiveMQServers { return ActiveMQServers.newActiveMQServer(config, mbeanServer, true); } + public static ActiveMQServer newActiveMQServer(final String configURL, + final MBeanServer mbeanServer, + final ActiveMQSecurityManager securityManager) throws Exception { + + FileConfiguration config = new FileConfiguration(); + new FileDeploymentManager(configURL).addDeployable(config).readConfiguration(); + + ActiveMQServer server = ActiveMQServers.newActiveMQServer(config, mbeanServer, securityManager); + + return server; + } + public static ActiveMQServer newActiveMQServer(final Configuration config, final MBeanServer mbeanServer, final ActiveMQSecurityManager securityManager) { diff --git a/docs/user-manual/en/examples.md b/docs/user-manual/en/examples.md index c29ee2d2c1..36d46844f8 100644 --- a/docs/user-manual/en/examples.md +++ b/docs/user-manual/en/examples.md @@ -403,14 +403,14 @@ messages. Embedded -------- -The `embedded` example shows how to embed JMS within your own code using +The `embedded` example shows how to embed a broker within your own code using POJO instantiation and no config files. Embedded Simple --------------- -The `embedded` example shows how to embed JMS within your own code using -regular Apache ActiveMQ Artemis XML files. +The `embedded-simple` example shows how to embed a broker within your own code +using regular Apache ActiveMQ Artemis XML files. Message Expiration ------------------ diff --git a/examples/features/standard/embedded-simple/readme.html b/examples/features/standard/embedded-simple/readme.html index 1bbd7aab12..dae7359ae1 100644 --- a/examples/features/standard/embedded-simple/readme.html +++ b/examples/features/standard/embedded-simple/readme.html @@ -19,78 +19,17 @@ under the License. - ActiveMQ Artemis Embedded JMS Server Example + ActiveMQ Artemis Embedded Broker with External Config Example -

Embedded JMS Server Example

+

Embedded Broker with External Config Example

+
To run the example, simply type mvn verify from this directory
-

This examples shows how to setup and run an embedded JMS server using ActiveMQ Artemis along with ActiveMQ Artemis configuration files.

- -

Example step-by-step

-

To run the example, simply type mvn verify -Pexample from this directory

- -
    -
  1. Create ActiveMQ Artemis core configuration files and make sure they are within your classpath. By default, ActiveMQ - expects the configuration file name to be "broker.xml".
  2. -
  3. Create an embedded ActiveMQ Artemis JMS server
  4. -
    -            EmbeddedJMS jmsServer = new EmbeddedJMS();
    -         
    - -
  5. Setup security configurations
  6. -
    -            SecurityConfiguration securityConfig = new SecurityConfiguration();
    -            securityConfig.addUser("guest", "guest");
    -            securityConfig.addRole("guest", "guest");
    -            securityConfig.setDefaultUser("guest");
    -            jmsServer.setSecurityManager(new ActiveMQSecurityManagerImpl(securityConfig));
    -         
    - -
  7. Start the embedded ActiveMQ Artemis JMS server
  8. -
    -            jmsServer.start()
    -         
    - -
  9. Create JMS resources (connection factory and queue) for the example
  10. -
    -            JMSServerManager jmsServerManager = jmsServer.getJMSServerManager();
    -            List connectors = new ArrayList();
    -            connectors.add("in-vm");
    -            jmsServerManager.createConnectionFactory("ConnectionFactory", false, JMSFactoryType.CF, connectors, "ConnectionFactory");
    -            jmsServerManager.createQueue(false, "exampleQueue", null, false, "queue/exampleQueue");
    -         
    - -

    At this point the JMS server is started and any JMS clients can look up JMS resources from the JNDI to send/receive - messages from the server. To keep the example simple, we will send and receive a JMS message from the same JVM - used to run the JMS server.

    - -
  11. Lookup JMS resources defined in the configuration
  12. -
    -            ConnectionFactory cf = (ConnectionFactory)jmsServer.lookup("ConnectionFactory");
    -            Queue queue = (Queue)jmsServer.lookup("queue/exampleQueue");
    -         
    - -
  13. Send and receive a message using JMS API
  14. -

    See the Queue Example for detailed steps to send and receive a JMS message

    - -

    Finally, we stop the JMS server and its associated resources.

    - -
  15. Close the connection
  16. -
    -            if (connection != null)
    -            {
    -               connection.close();
    -            }
    -         
    - -
  17. Stop the JMS server
  18. -
    -            jmsServer.stop();
    -         
    - -
+

This examples shows how to setup and run an embedded broker using ActiveMQ Artemis.

+

ActiveMQ Artemis was designed using POJOs (Plain Old Java Objects) which means embedding ActiveMQ Artemis in your own application is as simple as instantiating a few objects.

+

This example uses an external configuration file (i.e. broker.xml).

diff --git a/examples/features/standard/embedded-simple/src/main/java/org/apache/activemq/artemis/jms/example/EmbeddedExample.java b/examples/features/standard/embedded-simple/src/main/java/org/apache/activemq/artemis/jms/example/EmbeddedExample.java index 7e7dd60cfc..2e711b4bd7 100644 --- a/examples/features/standard/embedded-simple/src/main/java/org/apache/activemq/artemis/jms/example/EmbeddedExample.java +++ b/examples/features/standard/embedded-simple/src/main/java/org/apache/activemq/artemis/jms/example/EmbeddedExample.java @@ -23,45 +23,44 @@ import javax.jms.MessageProducer; import javax.jms.Queue; import javax.jms.Session; import javax.jms.TextMessage; -import java.util.ArrayList; +import javax.naming.InitialContext; import java.util.Date; -import java.util.List; -import org.apache.activemq.artemis.api.jms.JMSFactoryType; import org.apache.activemq.artemis.core.config.impl.SecurityConfiguration; -import org.apache.activemq.artemis.jms.server.JMSServerManager; -import org.apache.activemq.artemis.jms.server.embedded.EmbeddedJMS; +import org.apache.activemq.artemis.core.server.ActiveMQServer; +import org.apache.activemq.artemis.core.server.ActiveMQServers; import org.apache.activemq.artemis.spi.core.security.ActiveMQJAASSecurityManager; import org.apache.activemq.artemis.spi.core.security.jaas.InVMLoginModule; /** - * This example demonstrates how to run an ActiveMQ Artemis embedded with JMS + * This example demonstrates how to run an embedded ActiveMQ Artemis broker with external file configuration */ public class EmbeddedExample { public static void main(final String[] args) throws Exception { - EmbeddedJMS jmsServer = new EmbeddedJMS(); - + // Step 1. Configure security. SecurityConfiguration securityConfig = new SecurityConfiguration(); securityConfig.addUser("guest", "guest"); securityConfig.addRole("guest", "guest"); securityConfig.setDefaultUser("guest"); ActiveMQJAASSecurityManager securityManager = new ActiveMQJAASSecurityManager(InVMLoginModule.class.getName(), securityConfig); - jmsServer.setSecurityManager(securityManager); - jmsServer.start(); - System.out.println("Started Embedded JMS Server"); + // Step 2. Create and start embedded broker. + ActiveMQServer server = ActiveMQServers.newActiveMQServer("broker.xml", null, securityManager); + server.start(); + System.out.println("Started Embedded Broker"); - JMSServerManager jmsServerManager = jmsServer.getJMSServerManager(); - List connectors = new ArrayList<>(); - connectors.add("in-vm"); - jmsServerManager.createConnectionFactory("ConnectionFactory", false, JMSFactoryType.CF, connectors, "ConnectionFactory"); - jmsServerManager.createQueue(false, "exampleQueue", null, false, "queue/exampleQueue"); + InitialContext initialContext = null; + // Step 3. Create an initial context to perform the JNDI lookup. + initialContext = new InitialContext(); - ConnectionFactory cf = (ConnectionFactory) jmsServer.lookup("ConnectionFactory"); - Queue queue = (Queue) jmsServer.lookup("queue/exampleQueue"); + // Step 4. Look-up the JMS queue + Queue queue = (Queue) initialContext.lookup("queue/exampleQueue"); - // Step 10. Send and receive a message using JMS API + // Step 5. Look-up the JMS connection factory + ConnectionFactory cf = (ConnectionFactory) initialContext.lookup("ConnectionFactory"); + + // Step 6. Send and receive a message using JMS API try (Connection connection = cf.createConnection()) { Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); MessageProducer producer = session.createProducer(queue); @@ -73,9 +72,9 @@ public class EmbeddedExample { TextMessage messageReceived = (TextMessage) messageConsumer.receive(1000); System.out.println("Received message:" + messageReceived.getText()); } finally { - // Step 11. Stop the JMS server - jmsServer.stop(); - System.out.println("Stopped the JMS Server"); + // Step 7. Stop the embedded broker. + server.stop(); + System.out.println("Stopped the Embedded Broker"); } } } diff --git a/examples/features/standard/embedded-simple/src/main/resources/broker.xml b/examples/features/standard/embedded-simple/src/main/resources/broker.xml index 0448e3b6d9..a67720c30d 100644 --- a/examples/features/standard/embedded-simple/src/main/resources/broker.xml +++ b/examples/features/standard/embedded-simple/src/main/resources/broker.xml @@ -26,10 +26,6 @@ under the License. false - - vm://0 - - vm://0 diff --git a/examples/features/standard/embedded-simple/src/main/resources/jndi.properties b/examples/features/standard/embedded-simple/src/main/resources/jndi.properties new file mode 100644 index 0000000000..0f9a91f405 --- /dev/null +++ b/examples/features/standard/embedded-simple/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=vm://0 +queue.queue/exampleQueue=exampleQueue diff --git a/examples/features/standard/embedded/readme.html b/examples/features/standard/embedded/readme.html index 2da6ef7612..d277c911fc 100644 --- a/examples/features/standard/embedded/readme.html +++ b/examples/features/standard/embedded/readme.html @@ -19,20 +19,18 @@ under the License. - ActiveMQ Artemis Embedded JMS Server Example + ActiveMQ Artemis Embedded Broker with Programmatic Config Example -

Embedded JMS Server Example

+

Embedded Broker with Programmatic Config Example

To run the example, simply type mvn verify from this directory
-

This examples shows how to setup and run an embedded JMS server using ActiveMQ Artemis.

+

This examples shows how to setup and run an embedded broker using ActiveMQ Artemis.

ActiveMQ Artemis was designed using POJOs (Plain Old Java Objects) which means embedding ActiveMQ Artemis in your own application is as simple as instantiating a few objects.

-

This example does not use any configuration files. The server is configured using POJOs and can be easily ported to any dependency injection framework.
- We will setup and run a full-fledged JMS server which binds its JMS resources to JNDI and can be accessed by remote clients.

- +

This example does not use any configuration files. The server is configured using POJOs and can be easily ported to any dependency injection framework.

diff --git a/examples/features/standard/embedded/src/main/java/org/apache/activemq/artemis/jms/example/EmbeddedExample.java b/examples/features/standard/embedded/src/main/java/org/apache/activemq/artemis/jms/example/EmbeddedExample.java index 54ce764d8e..69a1a29dfa 100644 --- a/examples/features/standard/embedded/src/main/java/org/apache/activemq/artemis/jms/example/EmbeddedExample.java +++ b/examples/features/standard/embedded/src/main/java/org/apache/activemq/artemis/jms/example/EmbeddedExample.java @@ -23,49 +23,38 @@ import javax.jms.MessageProducer; import javax.jms.Queue; import javax.jms.Session; import javax.jms.TextMessage; -import java.util.Arrays; +import javax.naming.InitialContext; import java.util.Date; -import org.apache.activemq.artemis.core.config.Configuration; import org.apache.activemq.artemis.core.config.impl.ConfigurationImpl; -import org.apache.activemq.artemis.jms.server.config.ConnectionFactoryConfiguration; -import org.apache.activemq.artemis.jms.server.config.JMSConfiguration; -import org.apache.activemq.artemis.jms.server.config.JMSQueueConfiguration; -import org.apache.activemq.artemis.jms.server.config.impl.ConnectionFactoryConfigurationImpl; -import org.apache.activemq.artemis.jms.server.config.impl.JMSConfigurationImpl; -import org.apache.activemq.artemis.jms.server.config.impl.JMSQueueConfigurationImpl; -import org.apache.activemq.artemis.jms.server.embedded.EmbeddedJMS; +import org.apache.activemq.artemis.core.server.ActiveMQServer; +import org.apache.activemq.artemis.core.server.ActiveMQServers; /** - * This example demonstrates how to run an ActiveMQ Artemis embedded with JMS + * This example demonstrates how to run an embedded ActiveMQ Artemis broker with programmatic configuration */ public final class EmbeddedExample { public static void main(final String[] args) throws Exception { - // Step 1. Create ActiveMQ Artemis core configuration, and set the properties accordingly - Configuration configuration = new ConfigurationImpl().setPersistenceEnabled(false).setJournalDirectory("target/data/journal").setSecurityEnabled(false).addAcceptorConfiguration("tcp", "tcp://localhost:61616"). - addConnectorConfiguration("connector", "tcp://localhost:61616"); + // Step 1. Configure and start the embedded broker. + ActiveMQServer server = ActiveMQServers.newActiveMQServer(new ConfigurationImpl() + .setPersistenceEnabled(false) + .setJournalDirectory("target/data/journal") + .setSecurityEnabled(false) + .addAcceptorConfiguration("invm", "vm://0")); + server.start(); - // Step 2. Create the JMS configuration - JMSConfiguration jmsConfig = new JMSConfigurationImpl(); + InitialContext initialContext = null; + // Step 2. Create an initial context to perform the JNDI lookup. + initialContext = new InitialContext(); - // Step 3. Configure the JMS ConnectionFactory - ConnectionFactoryConfiguration cfConfig = new ConnectionFactoryConfigurationImpl().setName("cf").setConnectorNames(Arrays.asList("connector")).setBindings("cf"); - jmsConfig.getConnectionFactoryConfigurations().add(cfConfig); + // Step 3. Look-up the JMS queue + Queue queue = (Queue) initialContext.lookup("queue/exampleQueue"); - // Step 4. Configure the JMS Queue - JMSQueueConfiguration queueConfig = new JMSQueueConfigurationImpl().setName("queue1").setDurable(false).setBindings("queue/queue1"); - jmsConfig.getQueueConfigurations().add(queueConfig); + // Step 4. Look-up the JMS connection factory + ConnectionFactory cf = (ConnectionFactory) initialContext.lookup("ConnectionFactory"); - // Step 5. Start the JMS Server using the ActiveMQ Artemis core server and the JMS configuration - EmbeddedJMS jmsServer = new EmbeddedJMS().setConfiguration(configuration).setJmsConfiguration(jmsConfig).start(); - System.out.println("Started Embedded JMS Server"); - - // Step 6. Lookup JMS resources defined in the configuration - ConnectionFactory cf = (ConnectionFactory) jmsServer.lookup("cf"); - Queue queue = (Queue) jmsServer.lookup("queue/queue1"); - - // Step 7. Send and receive a message using JMS API + // Step 5. Send and receive a message using JMS API Connection connection = null; try { connection = cf.createConnection(); @@ -83,8 +72,8 @@ public final class EmbeddedExample { connection.close(); } - // Step 11. Stop the JMS server - jmsServer.stop(); + // Step 6. Stop the broker + server.stop(); System.out.println("Stopped the JMS Server"); } } diff --git a/examples/features/standard/embedded/src/main/resources/jndi.properties b/examples/features/standard/embedded/src/main/resources/jndi.properties new file mode 100644 index 0000000000..0f9a91f405 --- /dev/null +++ b/examples/features/standard/embedded/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=vm://0 +queue.queue/exampleQueue=exampleQueue