This commit is contained in:
Clebert Suconic 2017-11-27 13:03:26 -05:00
commit bce766c392
9 changed files with 109 additions and 134 deletions

View File

@ -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) {

View File

@ -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
------------------

View File

@ -19,78 +19,17 @@ under the License.
<html>
<head>
<title>ActiveMQ Artemis Embedded JMS Server Example</title>
<title>ActiveMQ Artemis Embedded Broker with External Config 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>Embedded JMS Server Example</h1>
<h1>Embedded Broker with External Config Example</h1>
<pre>To run the example, simply type <b>mvn verify</b> from this directory</pre>
<p>This examples shows how to setup and run an embedded JMS server using ActiveMQ Artemis along with ActiveMQ Artemis configuration files.</p>
<h2>Example step-by-step</h2>
<p><i>To run the example, simply type <code>mvn verify -Pexample</code> from this directory</i></p>
<ol>
<li>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".</li>
<li>Create an embedded ActiveMQ Artemis JMS server</li>
<pre class="prettyprint">
<code>EmbeddedJMS jmsServer = new EmbeddedJMS();</code>
</pre>
<li>Setup security configurations</li>
<pre class="prettyprint">
<code>SecurityConfiguration securityConfig = new SecurityConfiguration();
securityConfig.addUser("guest", "guest");
securityConfig.addRole("guest", "guest");
securityConfig.setDefaultUser("guest");
jmsServer.setSecurityManager(new ActiveMQSecurityManagerImpl(securityConfig));</code>
</pre>
<li>Start the embedded ActiveMQ Artemis JMS server</li>
<pre class="prettyprint">
<code>jmsServer.start()</code>
</pre>
<li>Create JMS resources (connection factory and queue) for the example</li>
<pre class="prettyprint">
<code>JMSServerManager jmsServerManager = jmsServer.getJMSServerManager();
List<String> connectors = new ArrayList<String>();
connectors.add("in-vm");
jmsServerManager.createConnectionFactory("ConnectionFactory", false, JMSFactoryType.CF, connectors, "ConnectionFactory");
jmsServerManager.createQueue(false, "exampleQueue", null, false, "queue/exampleQueue");</code>
</pre>
<p>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.</p>
<li>Lookup JMS resources defined in the configuration </li>
<pre class="prettyprint">
<code>ConnectionFactory cf = (ConnectionFactory)jmsServer.lookup("ConnectionFactory");
Queue queue = (Queue)jmsServer.lookup("queue/exampleQueue");</code>
</pre>
<li>Send and receive a message using JMS API</li>
<p>See the <a href="../queue/readme.html">Queue Example</a> for detailed steps to send and receive a JMS message</p>
<p>Finally, we stop the JMS server and its associated resources.</p>
<li>Close the connection</li>
<pre class="prettyprint">
<code>if (connection != null)
{
connection.close();
}</code>
</pre>
<li>Stop the JMS server</li>
<pre class="prettyprint">
<code>jmsServer.stop();</code>
</pre>
</ol>
<p>This examples shows how to setup and run an embedded broker using ActiveMQ Artemis.</p>
<p>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.</p>
<p>This example uses an external configuration file (i.e. broker.xml).</p>
</body>
</html>

View File

@ -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<String> 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");
}
}
}

View File

@ -26,10 +26,6 @@ under the License.
<persistence-enabled>false</persistence-enabled>
<connectors>
<connector name="in-vm">vm://0</connector>
</connectors>
<acceptors>
<acceptor name="in-vm">vm://0</acceptor>
</acceptors>

View File

@ -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

View File

@ -19,20 +19,18 @@ under the License.
<html>
<head>
<title>ActiveMQ Artemis Embedded JMS Server Example</title>
<title>ActiveMQ Artemis Embedded Broker with Programmatic Config 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>Embedded JMS Server Example</h1>
<h1>Embedded Broker with Programmatic Config Example</h1>
<pre>To run the example, simply type <b>mvn verify</b> from this directory</pre>
<p>This examples shows how to setup and run an embedded JMS server using ActiveMQ Artemis.</p>
<p>This examples shows how to setup and run an embedded broker using ActiveMQ Artemis.</p>
<p>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.</p>
<p>This example does not use any configuration files. The server is configured using POJOs and can be easily ported to any dependency injection framework.<br />
We will setup and run a full-fledged JMS server which binds its JMS resources to JNDI and can be accessed by remote clients.</p>
<p>This example does not use any configuration files. The server is configured using POJOs and can be easily ported to any dependency injection framework.</p>
</body>
</html>

View File

@ -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");
}
}

View File

@ -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