This closes #113 removing example profile

This commit is contained in:
Clebert Suconic 2015-08-07 15:30:55 -04:00
commit 4ed21a44c5
287 changed files with 7822 additions and 15763 deletions

View File

@ -29,9 +29,9 @@ public class ActionContext
this.err = err;
}
InputStream in;
PrintStream out;
PrintStream err;
public InputStream in;
public PrintStream out;
public PrintStream err;
public static ActionContext system()
{

View File

@ -24,6 +24,8 @@ import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.nio.file.Files;
import java.nio.file.attribute.PosixFilePermission;
import java.text.DecimalFormat;
@ -161,6 +163,12 @@ public class Create extends InputAbstract
@Option(name = "--no-web", description = "This will remove the web server definition from bootstrap.xml")
boolean noWeb;
@Option(name = "--queues", description = "comma separated list of jms queues.")
String queues;
@Option(name = "--topics", description = "comma separated list of jms topics ")
String topics;
boolean IS_WINDOWS;
boolean IS_CYGWIN;
@ -564,7 +572,6 @@ public class Create extends InputAbstract
filters.put("${password}", getPassword());
filters.put("${role}", getRole());
if (clustered)
{
filters.put("${host}", getHostForClustered());
@ -587,6 +594,7 @@ public class Create extends InputAbstract
filters.put("${cluster-password}", "");
}
applyJMSObjects(filters);
if (home != null)
{
@ -706,6 +714,24 @@ public class Create extends InputAbstract
return null;
}
/** It will create the jms configurations */
private void applyJMSObjects(HashMap<String, String> filters)
{
StringWriter writer = new StringWriter();
PrintWriter printWriter = new PrintWriter(writer);
printWriter.println();
for (String str : getQueueList())
{
printWriter.println(" <queue name=\"" + str + "\"/>");
}
for (String str : getTopicList())
{
printWriter.println(" <topic name=\"" + str + "\"/>");
}
filters.put("${jms-list.settings}", writer.toString());
}
private void performAutoTune(HashMap<String, String> filters, boolean aio, File dataFolder)
{
if (noAutoTune)
@ -793,6 +819,31 @@ public class Create extends InputAbstract
}
}
private String[] getQueueList()
{
if (queues == null)
{
return new String[0];
}
else
{
return queues.split(",");
}
}
private String[] getTopicList()
{
if (topics == null)
{
return new String[0];
}
else
{
return topics.split(",");
}
}
String path(String value, boolean unixPaths) throws IOException
{
return path(new File(value), unixPaths);

View File

@ -26,7 +26,7 @@ public class InputAbstract extends ActionAbstract
private Scanner scanner;
@Option(name = "--silent-input", description = "It will disable all the inputs, and it would make a best guess for any required input")
@Option(name = "--silent", description = "It will disable all the inputs, and it would make a best guess for any required input")
private boolean silentInput = false;
public boolean isSilentInput()

View File

@ -131,7 +131,7 @@ public final class XmlDataExporter extends DataAbstract implements Action
try
{
process(System.out, getBinding(), getJournal(), getPaging(), getLargeMessages());
process(context.out, getBinding(), getJournal(), getPaging(), getLargeMessages());
}
catch (Exception e)
{

View File

@ -24,7 +24,7 @@ under the License.
<jms xmlns="urn:activemq:jms">
<queue name="DLQ"/>
<queue name="ExpiryQueue"/>
<queue name="ExpiryQueue"/>${jms-list.settings}
</jms>
<core xmlns="urn:activemq:core">

View File

@ -23,9 +23,14 @@ import javax.jms.TextMessage;
import java.io.File;
import java.util.concurrent.TimeUnit;
import org.apache.activemq.artemis.api.core.SimpleString;
import org.apache.activemq.artemis.api.core.client.ClientSession;
import org.apache.activemq.artemis.api.core.client.ClientSessionFactory;
import org.apache.activemq.artemis.api.core.client.ServerLocator;
import org.apache.activemq.artemis.cli.Artemis;
import org.apache.activemq.artemis.cli.commands.Run;
import org.apache.activemq.artemis.cli.commands.util.SyncCalculation;
import org.apache.activemq.artemis.core.client.impl.ServerLocatorImpl;
import org.apache.activemq.artemis.jlibaio.LibaioContext;
import org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory;
import org.apache.activemq.artemis.jms.client.ActiveMQDestination;
@ -83,15 +88,35 @@ public class ArtemisTest
Assert.assertEquals(0, LibaioContext.getTotalMaxIO());
}
@Test
public void testSimpleRun() throws Exception
{
String queues = "q1,t2";
String topics = "t1,t2";
Run.setEmbedded(true);
Artemis.main("create", temporaryFolder.getRoot().getAbsolutePath(), "--force", "--silent-input", "--no-web");
Artemis.main("create", temporaryFolder.getRoot().getAbsolutePath(), "--force", "--silent", "--no-web", "--queues", queues, "--topics", topics);
System.setProperty("artemis.instance", temporaryFolder.getRoot().getAbsolutePath());
// Some exceptions may happen on the initialization, but they should be ok on start the basic core protocol
Artemis.execute("run");
try (ServerLocator locator = ServerLocatorImpl.newLocator("tcp://localhost:61616");
ClientSessionFactory factory = locator.createSessionFactory();
ClientSession coreSession = factory.createSession())
{
for (String str: queues.split(","))
{
ClientSession.QueueQuery queryResult = coreSession.queueQuery(SimpleString.toSimpleString("jms.queue." + str));
Assert.assertTrue("Couldn't find queue " + str, queryResult.isExists());
}
for (String str: topics.split(","))
{
ClientSession.QueueQuery queryResult = coreSession.queueQuery(SimpleString.toSimpleString("jms.topic." + str));
Assert.assertTrue("Couldn't find topic " + str, queryResult.isExists());
}
}
Assert.assertEquals(Integer.valueOf(1000), Artemis.execute("producer", "--message-count", "1000", "--verbose"));
Assert.assertEquals(Integer.valueOf(1000), Artemis.execute("consumer", "--verbose", "--break-on-null", "--receive-timeout", "100"));
@ -100,6 +125,7 @@ public class ArtemisTest
Session session = connection.createSession(true, Session.SESSION_TRANSACTED);
MessageProducer producer = session.createProducer(ActiveMQDestination.createDestination("queue://TEST", ActiveMQDestination.QUEUE_TYPE));
TextMessage message = session.createTextMessage("Banana");
message.setStringProperty("fruit", "banana");
producer.send(message);

View File

@ -181,7 +181,7 @@ public interface ActiveMQClientLogger extends BasicLogger
@Message(id = 212028, value = "error starting server locator", format = Message.Format.MESSAGE_FORMAT)
void errorStartingLocator(@Cause Exception e);
@LogMessage(level = Logger.Level.WARN)
@LogMessage(level = Logger.Level.DEBUG)
@Message(id = 212029,
value = "Closing a Server Locator left open. Please make sure you close all Server Locators explicitly before letting them go out of scope! {0}",
format = Message.Format.MESSAGE_FORMAT)

View File

@ -71,7 +71,7 @@ SYNOPSIS
[--clustered] [--data <data>] [--encoding <encoding>] [--force]
[--home <home>] [--host <host>] [--java-options <javaOptions>]
[--password <password>] [--port-offset <portOffset>] [--replicated]
[--role <role>] [--shared-store] [--silent-input] [--user <user>] [--]
[--role <role>] [--shared-store] [--silent] [--user <user>] [--]
<directory>
...
</PRE>

View File

@ -0,0 +1,48 @@
/**
* 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.maven;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Parameter;
public abstract class ArtemisAbstractPlugin extends AbstractMojo
{
/** It will ignore executioni if ignore has been set to true. This is useful as a property from the build. */
@Parameter(defaultValue = "")
private boolean ignore;
public void execute() throws MojoExecutionException, MojoFailureException
{
if (ignore)
{
getLog().debug("******************************************************************************************************");
getLog().debug("Execution of " + getClass().getSimpleName() + " is being ignored as ignore has been set to true");
getLog().debug("******************************************************************************************************");
}
else
{
doExecute();
}
}
protected abstract void doExecute() throws MojoExecutionException, MojoFailureException;
}

View File

@ -16,10 +16,14 @@
*/
package org.apache.activemq.artemis.maven;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import org.apache.activemq.artemis.cli.Artemis;
import org.apache.activemq.artemis.cli.commands.Run;
import org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugin.descriptor.PluginDescriptor;
@ -28,13 +32,8 @@ import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.Path;
@Mojo(name = "cli", defaultPhase = LifecyclePhase.VERIFY)
public class ArtemisCLIPlugin extends AbstractMojo
public class ArtemisCLIPlugin extends ArtemisAbstractPlugin
{
private PluginDescriptor descriptor;
@ -98,7 +97,8 @@ public class ArtemisCLIPlugin extends AbstractMojo
}
public void execute() throws MojoExecutionException, MojoFailureException
@Override
protected void doExecute() throws MojoExecutionException, MojoFailureException
{
// This is to avoid the Run issuing a kill at any point
Run.setEmbedded(true);

View File

@ -16,21 +16,20 @@
*/
package org.apache.activemq.artemis.maven;
import org.apache.maven.plugin.AbstractMojo;
import java.lang.reflect.Method;
import java.util.Properties;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import java.lang.reflect.Method;
import java.util.Properties;
/**
* Allows a Java Client to be run which must hve a static main(String[] args) method
*/
@Mojo(name = "runClient", defaultPhase = LifecyclePhase.VERIFY)
public class ActiveMQClientPlugin extends AbstractMojo
public class ArtemisClientPlugin extends ArtemisAbstractPlugin
{
@Parameter
@ -44,7 +43,8 @@ public class ActiveMQClientPlugin extends AbstractMojo
*/
private Properties systemProperties;
public void execute() throws MojoExecutionException, MojoFailureException
@Override
protected void doExecute() throws MojoExecutionException, MojoFailureException
{
try
{

View File

@ -28,7 +28,6 @@ import java.util.Map;
import java.util.Set;
import org.apache.activemq.artemis.cli.Artemis;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugin.descriptor.PluginDescriptor;
@ -47,7 +46,7 @@ import org.eclipse.aether.resolution.ArtifactResolutionException;
import org.eclipse.aether.resolution.ArtifactResult;
@Mojo(name = "create", defaultPhase = LifecyclePhase.VERIFY)
public class ActiveMQCreatePlugin extends AbstractMojo
public class ArtemisCreatePlugin extends ArtemisAbstractPlugin
{
@ -59,6 +58,7 @@ public class ActiveMQCreatePlugin extends AbstractMojo
*/
private PluginDescriptor descriptor;
/** Directory to replace the configuration with */
@Parameter(defaultValue = "${basedir}/target/classes/activemq/server0", required = true)
private File configuration;
@ -98,7 +98,7 @@ public class ActiveMQCreatePlugin extends AbstractMojo
@Parameter(defaultValue = "false")
private boolean sharedStore;
@Parameter(defaultValue = "true")
@Parameter(defaultValue = "false")
private boolean clustered;
@Parameter(defaultValue = "false")
@ -110,6 +110,10 @@ public class ActiveMQCreatePlugin extends AbstractMojo
@Parameter(defaultValue = "false")
private boolean failoverOnShutdown;
/** it will disable auto-tune*/
@Parameter(defaultValue = "true")
private boolean noAutoTune;
@Parameter(defaultValue = "ON_DEMAND")
private String messageLoadBalancing;
@ -122,6 +126,13 @@ public class ActiveMQCreatePlugin extends AbstractMojo
@Parameter(defaultValue = "${project.remoteProjectRepositories}")
private List<RemoteRepository> remoteRepos;
/**
* For extra stuff not covered by the properties
*/
@Parameter
ArrayList<String> args = new ArrayList<>();
@Parameter
private String[] libList;
@ -166,7 +177,8 @@ public class ActiveMQCreatePlugin extends AbstractMojo
}
}
public void execute() throws MojoExecutionException, MojoFailureException
@Override
protected void doExecute() throws MojoExecutionException, MojoFailureException
{
if (System.getProperty("bypassAddress") != null)
{
@ -205,7 +217,8 @@ public class ActiveMQCreatePlugin extends AbstractMojo
}
ArrayList<String> listCommands = new ArrayList<>();
add(listCommands, "create", "--allow-anonymous", "--silent-input", "--force", "--no-web", "--user", user, "--password", password,
add(listCommands, "create", "--allow-anonymous", "--silent", "--force", "--no-web", "--user", user, "--password", password,
"--role", role,
"--port-offset", "" + portOffset,
"--data", dataFolder);
@ -250,38 +263,46 @@ public class ActiveMQCreatePlugin extends AbstractMojo
add(listCommands, "--failover-on-shutdown");
}
add(listCommands, "--no-autotune");
if (noAutoTune)
{
add(listCommands, "--no-autotune");
}
add(listCommands, "--verbose");
for (String str : args)
{
add(listCommands, str);
}
add(listCommands, instance.getAbsolutePath());
getLog().info("************************************************");
getLog().info("Calling create server at " + instance + " home= " + home);
getLog().debug("***** Server created at " + instance + " with home=" + home + " *****");
try
{
Artemis.execute(home, null, listCommands);
String[] list = configuration.list();
if (list != null)
if (configuration != null)
{
getLog().debug("************************************************");
getLog().debug("Replacing configuration files:");
String[] list = configuration.list();
for (String file : configuration.list())
if (list != null)
{
Path target = instance.toPath().resolve("etc").resolve(file);
getLog().debug("Replacing " + file + " into " + target);
getLog().debug("************************************************");
getLog().debug("Replacing configuration files:");
for (String file : configuration.list())
{
Path target = instance.toPath().resolve("etc").resolve(file);
getLog().debug("Replacing " + file + " into " + target);
Files.copy(configuration.toPath().resolve(file), target, StandardCopyOption.REPLACE_EXISTING);
Files.copy(configuration.toPath().resolve(file), target, StandardCopyOption.REPLACE_EXISTING);
}
}
}
File projectLib = project.getArtifact().getFile();
copyToLib(projectLib);
if (libList != null)
{
for (int i = 0; i < libList.length; i++)

View File

@ -1,147 +0,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.
*/
package org.apache.activemq.artemis.maven;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.List;
import java.util.Map;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugin.descriptor.PluginDescriptor;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import org.eclipse.aether.RepositorySystem;
import org.eclipse.aether.RepositorySystemSession;
import org.eclipse.aether.artifact.Artifact;
import org.eclipse.aether.artifact.DefaultArtifact;
import org.eclipse.aether.repository.RemoteRepository;
import org.eclipse.aether.resolution.ArtifactRequest;
import org.eclipse.aether.resolution.ArtifactResolutionException;
import org.eclipse.aether.resolution.ArtifactResult;
@Mojo(name = "lib-install", defaultPhase = LifecyclePhase.VERIFY)
public class LibInstallPlugin extends AbstractMojo
{
@Parameter
String name;
/**
* The plugin descriptor
*/
private PluginDescriptor descriptor;
@Parameter(defaultValue = "${basedir}/target/server0", required = true)
private File instance;
@Component
private RepositorySystem repositorySystem;
@Parameter(defaultValue = "${repositorySystemSession}")
private RepositorySystemSession repoSession;
@Parameter(defaultValue = "${project.remoteProjectRepositories}")
private List<RemoteRepository> remoteRepos;
@Parameter
private String[] libList;
@Parameter(defaultValue = "${localRepository}")
private org.apache.maven.artifact.repository.ArtifactRepository localRepository;
public void execute() throws MojoExecutionException, MojoFailureException
{
MavenProject project = (MavenProject) getPluginContext().get("project");
Map properties = getPluginContext();
try
{
File projectLib = project.getArtifact().getFile();
copyToLib(projectLib);
if (libList != null)
{
for (int i = 0; i < libList.length; i++)
{
String[] splitString = libList[i].split(":");
getLog().info("********************" + splitString[0] + "/" + splitString[1] + "/" + splitString[2]);
Artifact artifact;
try
{
artifact = new DefaultArtifact(libList[i]);
}
catch (IllegalArgumentException e)
{
throw new MojoFailureException(e.getMessage(), e);
}
ArtifactRequest request = new ArtifactRequest();
request.setArtifact(artifact);
request.setRepositories(remoteRepos);
getLog().info("Resolving artifact " + artifact + " from " + remoteRepos);
ArtifactResult result;
try
{
result = repositorySystem.resolveArtifact(repoSession, request);
}
catch (ArtifactResolutionException e)
{
throw new MojoExecutionException(e.getMessage(), e);
}
File artifactFile = result.getArtifact().getFile();
getLog().info("Artifact:: " + artifact + " file = " + artifactFile);
copyToLib(artifactFile);
}
}
}
catch (Exception e)
{
getLog().error(e);
throw new MojoFailureException(e.getMessage());
}
}
private void copyToLib(File projectLib) throws IOException
{
Path target = instance.toPath().resolve("lib").resolve(projectLib.getName());
target.toFile().mkdirs();
getLog().info("Copying " + projectLib.getName() + " as " + target.toFile().getAbsolutePath());
Files.copy(projectLib.toPath(), target, StandardCopyOption.REPLACE_EXISTING);
}
}

View File

@ -20,9 +20,6 @@ import org.apache.activemq.artemis.core.server.ActiveMQServerLogger;
import org.apache.activemq.artemis.core.server.NodeManager;
import org.apache.activemq.artemis.core.server.cluster.ha.SharedStoreMasterPolicy;
/**
* Created by andy on 04/09/14.
*/
public final class SharedStoreLiveActivation extends LiveActivation
{
//this is how we act when we initially start as live

View File

@ -20,9 +20,6 @@ import org.apache.activemq.artemis.core.persistence.StorageManager;
import javax.transaction.xa.Xid;
/**
* Created by andy on 22/07/14.
*/
public interface TransactionFactory
{
Transaction newTransaction(Xid xid, StorageManager storageManager, int timeoutSeconds);

View File

@ -93,7 +93,7 @@ For a full list of updated properties always use:
[--clustered] [--data <data>] [--encoding <encoding>] [--force]
[--home <home>] [--host <host>] [--java-options <javaOptions>]
[--password <password>] [--port-offset <portOffset>] [--replicated]
[--role <role>] [--shared-store] [--silent-input] [--user <user>] [--]
[--role <role>] [--shared-store] [--silent] [--user <user>] [--]
<directory>
OPTIONS
@ -143,7 +143,7 @@ For a full list of updated properties always use:
--shared-store
Enable broker shared store
--silent-input
--silent
It will disable all the inputs, and it would make a best guess for
any required input

View File

@ -61,9 +61,6 @@ under the License.
</dependency>
</dependencies>
<profiles>
<profile>
<id>example</id>
<build>
<plugins>
<plugin>
@ -84,6 +81,4 @@ under the License.
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>

View File

@ -38,7 +38,7 @@ under the License.
<p>ActiveMQ Artemis Embedded could be used from very simple use cases with only InVM support to very complex cases with clustering, persistence and fail over.</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>
<p><i>To run the example, simply type <code>mvn verify</code> from this directory</i></p>
<p>In this we don't use any configuration files. (Everything is embedded). We simply instantiate ConfigurationImpl, ActiveMQServer, start it and operate on JMS regularly</p>
<ol>

View File

@ -23,13 +23,13 @@ under the License.
xsi:schemaLocation="urn:activemq /schema/artemis-server.xsd">
<core xmlns="urn:activemq:core">
<bindings-directory>${data.dir:./data}/bindings</bindings-directory>
<bindings-directory>./data/bindings</bindings-directory>
<journal-directory>${data.dir:./data}/journal</journal-directory>
<journal-directory>./data/journal</journal-directory>
<large-messages-directory>${data.dir:./data}/largemessages</large-messages-directory>
<large-messages-directory>./data/largemessages</large-messages-directory>
<paging-directory>${data.dir:./data}/paging</paging-directory>
<paging-directory>./data/paging</paging-directory>
<!-- Acceptors -->
<acceptors>

View File

@ -39,7 +39,8 @@ under the License.
<profiles>
<profile>
<id>release</id>
<!-- You need to activate -Pexamples to run all the examples -->
<id>examples</id>
<modules>
<module>embedded</module>
<module>embedded-remote</module>

View File

@ -3,22 +3,25 @@ Running the ActiveMQ Artemis Examples
To run an individual example firstly cd into the example directory and run
```sh
mvn verify
```
mvn verify -Pexample
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 -Pexample` in the examples
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"
```
To run the javaee examples follow the instructions in examples/javaee/README.md
### 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:

View File

@ -18,7 +18,8 @@ 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">
<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>
@ -34,7 +35,7 @@ under the License.
<activemq.basedir>${project.basedir}/../../..</activemq.basedir>
</properties>
<artifactId>artemis-jms-aerogear-example</artifactId>
<artifactId>aerogear</artifactId>
<packaging>jar</packaging>
<name>ActiveMQ Artemis JMS AeroGear Example</name>
@ -48,80 +49,86 @@ under the License.
<profiles>
<profile>
<id>example</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.activemq</groupId>
<artifactId>artemis-maven-plugin</artifactId>
<executions>
<execution>
<id>create</id>
<goals>
<goal>create</goal>
<!-- todo add integration layer -->
</goals>
<configuration>
<!-- this list was extracted from mvn dependency:tree on integration/aerogear -->
<libList>
<param>org.apache.activemq:artemis-aerogear-integration:${project.version}</param>
<param>org.jboss.aerogear:unifiedpush-java-client:1.0.0</param>
<param>net.iharder:base64:2.3.8</param>
<param>com.fasterxml.jackson.core:jackson-annotations:2.3.0</param>
<param>com.fasterxml.jackson.core:jackson-core:2.3.0</param>
<param>org.jboss.resteasy:resteasy-jackson-provider:2.3.2.Final</param>
<param>org.codehaus.jackson:jackson-core-asl:1.8.5</param>
<param>org.codehaus.jackson:jackson-mapper-asl:1.8.5</param>
<param>org.codehaus.jackson:jackson-jaxrs:1.8.5</param>
<param>org.codehaus.jackson:jackson-xc:1.8.5</param>
</libList>
</configuration>
</execution>
<execution>
<id>start</id>
<goals>
<goal>cli</goal>
</goals>
<configuration>
<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.AerogearExample</clientClass>
</configuration>
</execution>
<execution>
<id>stop</id>
<goals>
<goal>cli</goal>
</goals>
<configuration>
<args>
<param>stop</param>
</args>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.apache.activemq.examples.jms</groupId>
<artifactId>artemis-jms-aerogear-example</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
<!-- specify -PnoServer if you don't want to start the server -->
<id>noServer</id>
<properties>
<noServer>true</noServer>
</properties>
</profile>
</profiles>
<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>
<!-- this list was extracted from mvn dependency:tree on integration/aerogear -->
<libList>
<param>org.apache.activemq:artemis-aerogear-integration:${project.version}</param>
<param>org.jboss.aerogear:unifiedpush-java-client:1.0.0</param>
<param>net.iharder:base64:2.3.8</param>
<param>com.fasterxml.jackson.core:jackson-annotations:2.3.0</param>
<param>com.fasterxml.jackson.core:jackson-core:2.3.0</param>
<param>org.jboss.resteasy:resteasy-jackson-provider:2.3.2.Final</param>
<param>org.codehaus.jackson:jackson-core-asl:1.8.5</param>
<param>org.codehaus.jackson:jackson-mapper-asl:1.8.5</param>
<param>org.codehaus.jackson:jackson-jaxrs:1.8.5</param>
<param>org.codehaus.jackson:jackson-xc:1.8.5</param>
</libList>
</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.AerogearExample</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.jms</groupId>
<artifactId>aerogear</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>

View File

@ -27,6 +27,9 @@ under the License.
<body onload="prettyPrint()">
<h1>JMS AeroGear 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 you can send a message to a mobile device by leveraging <a href="http://aerogear.org/push/">AeroGears push</a> technology which
provides support for different push notification technologies like Google Cloud Messaging, Apple's APNs or
Mozilla's SimplePush.</p>

View File

@ -29,13 +29,13 @@ under the License.
<core xmlns="urn:activemq:core">
<bindings-directory>${data.dir:./data}/bindings</bindings-directory>
<bindings-directory>./data/bindings</bindings-directory>
<journal-directory>${data.dir:./data}/journal</journal-directory>
<journal-directory>./data/journal</journal-directory>
<large-messages-directory>${data.dir:./data}/largemessages</large-messages-directory>
<large-messages-directory>./data/largemessages</large-messages-directory>
<paging-directory>${data.dir:./data}/paging</paging-directory>
<paging-directory>./data/paging</paging-directory>
<!-- Acceptors -->
<acceptors>

View File

@ -18,7 +18,8 @@ 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">
<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>
@ -27,7 +28,7 @@ under the License.
<version>1.0.1-SNAPSHOT</version>
</parent>
<artifactId>artemis-jms-application-layer-failover-example</artifactId>
<artifactId>application-layer-failover</artifactId>
<packaging>jar</packaging>
<name>ActiveMQ Artemis JMS Application Layer Failover Example</name>
@ -42,64 +43,60 @@ under the License.
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-jms_2.0_spec</artifactId>
<groupId>org.apache.activemq</groupId>
<artifactId>artemis-jms-client</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
<profiles>
<profile>
<id>example</id>
<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>
</execution>
<execution>
<id>create1</id>
<goals>
<goal>create</goal>
</goals>
<configuration>
<instance>${basedir}/target/server1</instance>
<portOffset>1</portOffset>
</configuration>
</execution>
<execution>
<id>runClient</id>
<goals>
<goal>runClient</goal>
</goals>
<configuration>
<clientClass>org.apache.activemq.artemis.jms.example.ApplicationLayerFailoverExample</clientClass>
<args>
<param>${basedir}/target/server0</param>
<param>${basedir}/target/server1</param>
</args>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.apache.activemq.examples.jms</groupId>
<artifactId>artemis-jms-application-layer-failover-example</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</profile>
</profiles>
<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>
</execution>
<execution>
<id>create1</id>
<goals>
<goal>create</goal>
</goals>
<configuration>
<instance>${basedir}/target/server1</instance>
<portOffset>1</portOffset>
</configuration>
</execution>
<execution>
<id>runClient</id>
<goals>
<goal>runClient</goal>
</goals>
<configuration>
<clientClass>org.apache.activemq.artemis.jms.example.ApplicationLayerFailoverExample</clientClass>
<args>
<param>${basedir}/target/server0</param>
<param>${basedir}/target/server1</param>
</args>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.apache.activemq.examples.jms</groupId>
<artifactId>application-layer-failover</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>

View File

@ -27,6 +27,8 @@ under the License.
<body onload="prettyPrint()">
<h1>Application-Layer Failover Example</h1>
<pre>To run the example, simply type <b>mvn verify</b> from this directory. This example will always spawn and stop multiple servers.</pre>
<p>ActiveMQ Artemis implements fully transparent <b>automatic</b> failover of connections from a live node to a backup node which requires
no special coding. This is described in a different example and requires server replication.</p>
<p>However, ActiveMQ Artemis also supports <b>Application-Layer</b> failover which is useful in the case where replication is not enabled.</p>
@ -37,12 +39,11 @@ under the License.
<p>Application-Layer failover is an alternative approach to High Availabilty (HA).</p>
<p>Application-Layer failover differs from automatic failover in that some client side coding is required in order
to implement this. Also, with Application-Layer failover, since the old Session object dies and a new is created, any uncommitted
work in the old Session will be lost, and any unacknowledged messages might be redelivered.</p>
work in the old Session will be lost, and any unacknowledged messages might be redelivered.</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>
section of the user manual.</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>
<p>In this example, the live server is server 1, which will failover onto server 0.</p>
<p>The connection will initially be created to server1, server 1 will crash, and the client will carry on
on server 0, the new server. With Application-Layer failover the node that is failed over onto, does not need to
@ -87,13 +88,13 @@ under the License.
}
</code>
</pre>
<li>We now cause server 1, the live server to crash. After a little while the connection's
ExceptionListener will register the failure and reconnection will occur.</li>
<pre class="prettyprint">
<code>killServer(1);</code>
</pre>
<li>The connection's ExceptionListener gets called, and we lookup the JMS objects and
recreate the connection, session, etc on the other node 0.</li>
<pre class="prettyprint">
@ -121,7 +122,7 @@ under the License.
e.printStackTrace();
}
}
}
}
</code>
</pre>
@ -138,7 +139,7 @@ under the License.
}
</code>
</pre>
<li>And consume them.</li>
<pre class="prettyprint">
<code>

View File

@ -16,14 +16,6 @@
*/
package org.apache.activemq.artemis.jms.example;
import org.apache.activemq.artemis.util.ServerUtil;
import java.lang.Object;
import java.lang.String;
import java.util.Hashtable;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.ExceptionListener;
@ -35,6 +27,11 @@ import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory;
import org.apache.activemq.artemis.util.ServerUtil;
/**
* A simple example that demonstrates application-layer failover of the JMS connection from one node to another
@ -144,32 +141,25 @@ public class ApplicationLayerFailoverExample
private static void createJMSObjects(final int server) throws Exception
{
// Step 1. Get an initial context for looking up JNDI from the server
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 + server));
properties.put("queue.queue/exampleQueue", "exampleQueue");
initialContext = new InitialContext(properties);
// Step 1. Instantiate a JMS Connection Factory object from JNDI on server 1
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://127.0.0.1:" + (61616 + server));
// Step 2. Look-up the JMS Queue object from JNDI
Queue queue = (Queue)initialContext.lookup("queue/exampleQueue");
// Step 3. Look-up a JMS Connection Factory object from JNDI on server 1
ConnectionFactory connectionFactory = (ConnectionFactory)initialContext.lookup("ConnectionFactory");
// Step 4. We create a JMS Connection connection
// Step 2. We create a JMS Connection connection
connection = connectionFactory.createConnection();
// Step 6. We start the connection to ensure delivery occurs
// Step 3. We start the connection to ensure delivery occurs
connection.start();
// Step 5. We create a JMS Session
// Step 4. We create a JMS Session
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// Step 7. We create a JMS MessageConsumer object
// Step 5. Look-up the JMS Queue object from JNDI
Queue queue = session.createQueue("exampleQueue");
// Step 6. We create a JMS MessageConsumer object
consumer = session.createConsumer(queue);
// Step 8. We create a JMS MessageProducer object
// Step 7. We create a JMS MessageProducer object
producer = session.createProducer(queue);
}

View File

@ -18,7 +18,8 @@ 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">
<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>
@ -27,7 +28,7 @@ under the License.
<version>1.0.1-SNAPSHOT</version>
</parent>
<artifactId>artemis-jms-core-bridge-example</artifactId>
<artifactId>core-bridge</artifactId>
<packaging>jar</packaging>
<name>ActiveMQ Artemis Core Bridge Example</name>
@ -49,109 +50,127 @@ under the License.
<profiles>
<profile>
<id>example</id>
<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>
</configuration>
</execution>
<execution>
<id>create1</id>
<goals>
<goal>create</goal>
</goals>
<configuration>
<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>
<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>
<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>
<location>${basedir}/target/server0</location>
<args>
<param>stop</param>
</args>
</configuration>
</execution>
<execution>
<id>stop1</id>
<goals>
<goal>cli</goal>
</goals>
<configuration>
<location>${basedir}/target/server1</location>
<args>
<param>stop</param>
</args>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.apache.activemq.examples.jms</groupId>
<artifactId>artemis-jms-core-bridge-example</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
<!-- specify -PnoServer if you don't want to start the server -->
<id>noServer</id>
<properties>
<noServer>true</noServer>
</properties>
</profile>
</profiles>
<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.jms: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.jms: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.jms</groupId>
<artifactId>core-bridge</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>

View File

@ -26,6 +26,7 @@ under the License.
</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>
@ -69,166 +70,5 @@ under the License.
&lt;/bridge&gt;
</code>
</pre>
<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>We create an initial context for looking up JNDI on node 0</li>
<pre class="prettyprint">
<code>
ic0 = getContext(0);
</code>
</pre>
<li>We look up the sausage-factory queue from node 0</li>
<pre class="prettyprint">
<code>Queue sausageFactory = (Queue)ic0.lookup("/queue/sausage-factory");</code>
</pre>
<li>We look up a JMS ConnectionFactory object from node 0</li>
<pre class="prettyprint">
<code>ConnectionFactory cf0 = (ConnectionFactory)ic0.lookup("/ConnectionFactory");</code>
</pre>
<li>We create an initial context for looking up JNDI on node 1</li>
<pre class="prettyprint">
<code>ic1 = getContext(1);</code>
</pre>
<li>We look up the mincing-machine queue on node 1</li>
<pre class="prettyprint">
<code>Queue mincingMachine = (Queue)ic1.lookup("/queue/mincing-machine");
</code>
</pre>
<li>We look up a JMS ConnectionFactory object from node 1</li>
<pre class="prettyprint">
<code>
ConnectionFactory cf1 = (ConnectionFactory)ic1.lookup("/ConnectionFactory");
</code>
</pre>
<li>We create a JMS Connection connection0 which is a connection to server 0</li>
<pre class="prettyprint">
<code>
connection0 = cf0.createConnection();
</code>
</pre>
<li>We create a JMS Connection connection1 which is a connection to server 1</li>
<pre class="prettyprint">
<code>
connection1 = cf1.createConnection();
</code>
</pre>
<li>We create a JMS Session on server 0</li>
<pre class="prettyprint">
<code>
Session session0 = connection0.createSession(false, Session.AUTO_ACKNOWLEDGE);
</code>
</pre>
<li>We create a JMS Session on server 1</li>
<pre class="prettyprint">
<code>
Session session1 = connection1.createSession(false, Session.AUTO_ACKNOWLEDGE);
</code>
</pre>
<li>We start the connection to ensure delivery occurs on them</li>
<pre class="prettyprint">
<code>
connection1.start();
</code>
</pre>
<li>We create a JMS MessageConsumer object on server 1</li>
<pre class="prettyprint">
<code>
MessageConsumer consumer = session1.createConsumer(mincingMachine);
</code>
</pre>
<li>We create a JMS MessageProducer object on server 0.</li>
<pre class="prettyprint">
<code>
MessageProducer producer = session0.createProducer(sausageFactory);</code>
</pre>
<li>We create and send a message representing an aardvark with a green hat to the sausage-factory
on node 0</li>
<pre class="prettyprint">
<code>
Message message = session0.createMessage();
message.setStringProperty("name", "aardvark");
message.setStringProperty("hat", "green");
producer.send(message);
</code>
</pre>
<li>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!</li>
<pre class="prettyprint">
<code>
Message receivedMessage = consumer.receive(5000);
</code>
</pre>
<li>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.</li>
<pre class="prettyprint">
<code>
message = session0.createMessage();
message.setStringProperty("name", "sasquatch");
message.setStringProperty("hat", "mauve");
producer.send(message);
</code>
</pre>
<li>We don't receive the sasquatch message since it's not an aardvark!</li>
<pre class="prettyprint">
<code>
receivedMessage = (TextMessage)consumer.receive(1000);
</code>
</pre>
<li>And finally (no pun intended), <b>always</b> remember to close your JMS resources after use, in a <code>finally</code> block. Closing a JMS connection will automatically close all of its sessions, consumers, producer and browser objects</li>
<pre class="prettyprint">
<code>
finally
{
if (connection0 != null)
{
connection0.close();
}
if (connection1 != null)
{
connection1.close();
}
if (ic0 != null)
{
ic0.close();
}
if (ic1 != null)
{
ic1.close();
}
}
</code>
</pre>
</ol>
</body>
</html>

View File

@ -18,7 +18,8 @@ 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">
<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>
@ -27,7 +28,7 @@ under the License.
<version>1.0.1-SNAPSHOT</version>
</parent>
<artifactId>artemis-jms-browser-example</artifactId>
<artifactId>browser</artifactId>
<packaging>jar</packaging>
<name>ActiveMQ Artemis JMS Browser Example</name>
@ -37,72 +38,82 @@ under the License.
<dependencies>
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-jms_2.0_spec</artifactId>
<groupId>org.apache.activemq</groupId>
<artifactId>artemis-jms-client</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
<profiles>
<profile>
<id>example</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.activemq</groupId>
<artifactId>artemis-maven-plugin</artifactId>
<executions>
<execution>
<id>create</id>
<phase>verify</phase>
<goals>
<goal>create</goal>
</goals>
</execution>
<execution>
<id>start</id>
<goals>
<goal>cli</goal>
</goals>
<configuration>
<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.QueueBrowserExample</clientClass>
</configuration>
</execution>
<execution>
<id>stop</id>
<goals>
<goal>cli</goal>
</goals>
<configuration>
<args>
<param>stop</param>
</args>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.apache.activemq.examples.jms</groupId>
<artifactId>artemis-jms-browser-example</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
<!-- specify -PnoServer if you don't want to start the server -->
<id>noServer</id>
<properties>
<noServer>true</noServer>
</properties>
</profile>
</profiles>
<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.jms</groupId>
<artifactId>browser</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>

View File

@ -26,122 +26,15 @@ under the License.
</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.
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 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>
<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>First we need to get an initial context so we can look-up the JMS connection factory and destination objects from JNDI. This initial context will get it's properties from the <code>client-jndi.properties</code> file in the directory <code>../common/config</code></li>
<pre class="prettyprint">
<code>InitialContext initialContext = getContext();</code>
</pre>
<li>We look up the JMS queue object from JNDI</li>
<pre class="prettyprint">
<code>Queue queue = (Queue) initialContext.lookup("/queue/exampleQueue");</code>
</pre>
<li>We look up the JMS connection factory object from JNDI</li>
<pre class="prettyprint">
<code>ConnectionFactory cf = (ConnectionFactory) initialContext.lookup("/ConnectionFactory");</code>
</pre>
<li>We create a JMS connection</li>
<pre class="prettyprint">
<code>connection = cf.createConnection();</code>
</pre>
<li>We create a JMS session. The session is created as non transacted and will auto acknowledge messages.</li>
<pre class="prettyprint">
<code>Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);</code>
</pre>
<li>We create a JMS message producer on the session. This will be used to send the messages.</li>
<pre class="prettyprint">
<code>MessageProducer messageProducer = session.createProducer(topic);</code>
</pre>
<li>We create 2 JMS text messages that we are going to send.</li>
<pre class="prettyprint">
<code> TextMessage message_1 = session.createTextMessage("this is the 1st message");
TextMessage message_2 = session.createTextMessage("this is the 2nd message");</code>
</pre>
<li>We send messages to the queue</li>
<pre class="prettyprint">
<code>messageProducer.send(message_1);
messageProducer.send(message_2);</code>
</pre>
<li>We create a JMS QueueBrowser.<br />
We have not specified a message selector so the browser will enumerate the entire content of the queue.
</li>
<pre class="prettyprint">
<code>QueueBrowser browser = session.createBrowser(queue);</code>
</pre>
<li>We browse the queue and display all the messages' text
</li>
<pre class="prettyprint">
<code>Enumeration messageEnum = browser.getEnumeration();
while (messageEnum.hasMoreElements())
{
TextMessage message = (TextMessage)messageEnum.nextElement();
System.out.println("Browsing: " + message.getText());
}</code>
</pre>
<li>We close the browser once we have finished to use it</li>
<pre class="prettyprint">
<code>browser.close();</code>
</pre>
<p>The messages were browsed but they were not removed from the queue. We will now consume them.</p>
<li>We create a JMS Message Consumer to receive the messages.</li>
<pre class="prettyprint">
<code>MessageConsumer messageConsumer = session.createConsumer(queue);</code>
</pre>
<li>We start the connection. In order for delivery to occur on any consumers or subscribers on a connection, the connection must be started</li>
<pre class="prettyprint">
<code>connection.start();</code>
</pre>
<li>The 2 messages arrive at the consumer</li>
<pre class="prettyprint">
<code>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());</code>
</pre>
<li>And finally, <b>always</b> remember to close your JMS connections and resources after use, in a <code>finally</code> block. Closing a JMS connection will automatically close all of its sessions, consumers, producer and browser objects</li>
<pre class="prettyprint">
<code>finally
{
if (initialContext != null)
{
initialContext.close();
}
if (connection != null)
{
connection.close();
}
}</code>
</pre>
</ol>
</body>
</html>

View File

@ -45,8 +45,11 @@ public class QueueBrowserExample
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();

View File

@ -18,7 +18,8 @@ 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">
<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>
@ -27,7 +28,7 @@ under the License.
<version>1.0.1-SNAPSHOT</version>
</parent>
<artifactId>artemis-jms-client-kickoff-example</artifactId>
<artifactId>client-kickoff</artifactId>
<packaging>jar</packaging>
<name>ActiveMQ Artemis JMS Kick Off Example</name>
@ -38,79 +39,84 @@ under the License.
<dependencies>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>artemis-core-client</artifactId>
<artifactId>artemis-jms-client</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-jms_2.0_spec</artifactId>
</dependency>
</dependencies>
<profiles>
<profile>
<id>example</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.activemq</groupId>
<artifactId>artemis-maven-plugin</artifactId>
<executions>
<execution>
<id>create</id>
<goals>
<goal>create</goal>
</goals>
<configuration>
<!-- 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>
<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>
<args>
<param>stop</param>
</args>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.apache.activemq.examples.jms</groupId>
<artifactId>artemis-jms-client-kickoff-example</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
<!-- specify -PnoServer if you don't want to start the server -->
<id>noServer</id>
<properties>
<noServer>true</noServer>
</properties>
</profile>
</profiles>
<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.jms</groupId>
<artifactId>client-kickoff</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>

View File

@ -27,6 +27,8 @@ under the License.
<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>
@ -48,109 +50,5 @@ under the License.
(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>
<h2>Example step-by-step</h2>
<p><em>To run the example, simply type <code>mvn verify -Pexample</code> from this directory</em></p>
<ol>
<li>First we need to get an initial context so we can look-up the JMS connection factory and destination objects from JNDI. This initial context will get its properties from <a href="src/main/resources/activemq/server0/client-jndi.properties">client-jndi.properties</a></li>
<pre class="prettyprint">
<code>InitialContext initialContext = getContext(0);</code>
</pre>
<li>We look up the JMS connection factory object from JNDI</li>
<pre class="prettyprint">
<code>ConnectionFactory cf = (ConnectionFactory) initialContext.lookup("/ConnectionFactory");</code>
</pre>
<li>We create a JMS connection</li>
<pre class="prettyprint">
<code>connection = cf.createConnection();</code>
</pre>
<li>We set a <code>ExceptionListener</code> on the connection to be notified after a problem occurred</li>
<pre class="prettyprint">
<code>final AtomicReference&lt;JMSException&gt; exception = new AtomicReference&lt;JMSException&gt;();
connection.setExceptionListener(new ExceptionListener()
{
public void onException(JMSException e)
{
exception.set(e);
}
});</code>
</pre>
<li>We start the connection</li>
<pre class="prettyprint">
<code>connection.start();</code>
</pre>
<li>We create a MBean proxy to the ActiveMQServerControlMBean used to manage ActiveMQ Artemis server
(see <a href="../jmx/readme.html">JMX example</a> for a complete explanation of the different steps)</li>
<pre class="prettyprint">
<code>ObjectName on = ObjectNameBuilder.DEFAULT.getActiveMQServerObjectName();
JMXConnector connector = JMXConnectorFactory.connect(new JMXServiceURL(JMX_URL), new HashMap<String, String>());
MBeanServerConnection mbsc = connector.getMBeanServerConnection();
ActiveMQServerControlMBean serverControl = (ActiveMQServerControlMBean)MBeanServerInvocationHandler.newProxyInstance(mbsc,
on,
ActiveMQServerControlMBean.class,
false);
</code>
</pre>
<li>Using the server MBean, we list the remote address connected to the server</li>
<pre class="prettyprint">
<code>String[] remoteAddresses = serverControl.listRemoteAddresses();
for (String remoteAddress : remoteAddresses)
{
System.out.println(remoteAddress);
}
</code>
</pre>
<p>It will display a single address corresponding to the connection opened at step 3.</p>
<li>We close the connections corresponding to this remote address</li>
<pre class="prettyprint">
<code>serverControl.closeConnectionsForAddress(remoteAddresses[0]);</code>
</pre>
<p>Warnings be displayed on the server output:</p>
<pre class="prettyprint">
<code>org.apache.activemq.artemis.jms.example.SpawnedJMSServer out:11:22:33,034 WARN @RMI TCP Connection(3)-192.168.0.10 [RemotingConnectionImpl] Connection failure has been detected connections for /192.168.0.10:52707 closed by management:0
org.apache.activemq.artemis.jms.example.SpawnedJMSServer out:11:22:33,035 WARN @RMI TCP Connection(3)-192.168.0.10 [ServerSessionImpl] Client connection failed, clearing up resources for session 4646da35-2fe8-11de-9ce9-752ccc2b26e4
org.apache.activemq.artemis.jms.example.SpawnedJMSServer out:11:22:33,035 WARN @RMI TCP Connection(3)-192.168.0.10 [ServerSessionImpl] Cleared up resources for session 4646da35-2fe8-11de-9ce9-752ccc2b26e4
</code>
</pre>
<li>We display the exception received by the connection's ExceptionListener</li>
<pre class="prettyprint">
<code>exception.get().printStackTrace();</code>
</pre>
<p>When the connection was closed on the server-side by the call to <code>serverControl.closeConnectionsForAddress()</code>,
the client's connection was disconnected and its exception listener was notified.</p>
<li>And finally, <b>always</b> remember to close your JMS connections and resources after use, in a <code>finally</code> block. Closing a JMS connection will automatically close all of its sessions, consumers, producer and browser objects</li>
<pre class="prettyprint">
<code>finally
{
if (initialContext != null)
{
initialContext.close();
}
if (connection != null)
{
connection.close();
}
}</code>
</pre>
</ol>
<h2>More information</h2>
<ul>
<li><a href="http://java.sun.com/j2se/1.5.0/docs/guide/management/agent.html">Java 5 Management guide</a></li>
</ul>
</body>
</html>

View File

@ -18,7 +18,8 @@ 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">
<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>
@ -27,7 +28,7 @@ under the License.
<version>1.0.1-SNAPSHOT</version>
</parent>
<artifactId>artemis-jms-client-side-fileoverlistener-example</artifactId>
<artifactId>client-side-fileoverlistener</artifactId>
<packaging>jar</packaging>
<name>ActiveMQ Artemis JMS Client Side Failover listener Example</name>
@ -46,72 +47,63 @@ under the License.
<artifactId>artemis-jms-client</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-jms_2.0_spec</artifactId>
</dependency>
</dependencies>
<profiles>
<profile>
<id>example</id>
<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>
<dataFolder>../shared</dataFolder>
<sharedStore>true</sharedStore>
<slave>false</slave>
<failoverOnShutdown>true</failoverOnShutdown>
</configuration>
</execution>
<execution>
<id>create1</id>
<goals>
<goal>create</goal>
</goals>
<configuration>
<instance>${basedir}/target/server1</instance>
<dataFolder>../shared</dataFolder>
<sharedStore>true</sharedStore>
<slave>true</slave>
<portOffset>1</portOffset>
</configuration>
</execution>
<execution>
<id>runClient</id>
<goals>
<goal>runClient</goal>
</goals>
<configuration>
<clientClass>org.apache.activemq.artemis.jms.example.ClientSideFailoverListerExample</clientClass>
<args>
<param>${basedir}/target/server0</param>
<param>${basedir}/target/server1</param>
</args>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.apache.activemq.examples.jms</groupId>
<artifactId>artemis-jms-client-side-fileoverlistener-example</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</profile>
</profiles>
<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>
<dataFolder>../shared</dataFolder>
<sharedStore>true</sharedStore>
<slave>false</slave>
<failoverOnShutdown>true</failoverOnShutdown>
</configuration>
</execution>
<execution>
<id>create1</id>
<goals>
<goal>create</goal>
</goals>
<configuration>
<instance>${basedir}/target/server1</instance>
<dataFolder>../shared</dataFolder>
<sharedStore>true</sharedStore>
<slave>true</slave>
<portOffset>1</portOffset>
</configuration>
</execution>
<execution>
<id>runClient</id>
<goals>
<goal>runClient</goal>
</goals>
<configuration>
<clientClass>org.apache.activemq.artemis.jms.example.ClientSideFailoverListerExample</clientClass>
<args>
<param>${basedir}/target/server0</param>
<param>${basedir}/target/server1</param>
</args>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.apache.activemq.examples.jms</groupId>
<artifactId>client-side-fileoverlistener</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>

View File

@ -26,88 +26,12 @@ under the License.
</head>
<body onload="prettyPrint()">
<h1>Client Side Kickoff Example</h1>
<pre>To run the example, simply type <b>mvn verify</b> from this directory. This example will always spawn and stop multiple servers.</pre>
<p>This example demonstrates how you can listen on failover event on the client side.</p>
<p>In this example there are two nodes running in a cluster, both server will be running for start,
but after a while the first server will crash. This will trigger a fail-over event.</p>
<h2>Example step-by-step</h2>
<p><em>To run the example, simply type <code>mvn verify -Pexample</code> from this directory</em></p>
<ol>
<li>First we need to get an initial context so we can look-up the JMS connection factory and destination objects from JNDI. This initial context will get its properties from <a href="src/main/resources/activemq/server0/client-jndi.properties">client-jndi.properties</a></li>
<pre class="prettyprint">
<code>InitialContext initialContext = getContext(0);</code>
</pre>
<li>We look up the JMS Queue object from JNDI</li>
<pre class="prettyprint">
<code>Queue queue = (Queue)initialContext.lookup("/queue/exampleQueue");</code>
</pre>
<li>We look up a JMS Connection Factory object from JNDI on server 0</li>
<pre class="prettyprint">
<code>ConnectionFactory connectionFactory = (ConnectionFactory)initialContext.lookup("/ConnectionFactory");</code>
</pre>
<li>We create a JMS connection from the same connection factory, wait a little while to make sure broadcasts from all nodes have reached the client</li>
<pre class="prettyprint">
<code>
Thread.sleep(5000);
connectionA = connectionFactory.createConnection();
((ActiveMQConnection)connectionA).setFailoverListener(new FailoverListenerImpl());
</code>
</pre>
<li>We create JMS Sessions</li>
<pre class="prettyprint">
<code>Session sessionA = connectionA.createSession(false, Session.AUTO_ACKNOWLEDGE);</code>
</pre>
<li>We create JMS MessageProducer objects on the sessions</li>
<pre class="prettyprint">
<code>MessageProducer producerA = sessionA.createProducer(queue);</code>
</pre>
<li>We send some messages on each producer</li>
<pre class="prettyprint">
<code>
final int numMessages = 10;
for (int i = 0; i < numMessages; i++)
{
TextMessage messageA = sessionA.createTextMessage("A:This is text message " + i);
producerA.send(messageA);
System.out.println("Sent message: " + messageA.getText());
}
</code>
</pre>
<li>We start the connection to consume messages</li>
<pre class="prettyprint">
<code>connectionA.start();</code>
</pre>
<li>We consume messages from the session A, one at a time. We reached message no 5 the first server will crash</li>
<pre class="prettyprint">
<code>consume(sessionA, queue, numMessages, "A");</code>
</pre>
<li>And finally, <b>always</b> remember to close your JMS connections and resources after use, in a <code>finally</code> block. Closing a JMS connection will automatically close all of its sessions, consumers, producer and browser objects</li>
<pre class="prettyprint">
<code>
if (connectionA != null)
{
connectionA.close();
}
if (initialContext != null)
{
initialContext.close();
}
</code>
</pre>
</ol>
</body>
</html>

View File

@ -18,7 +18,8 @@ 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">
<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>
@ -27,7 +28,7 @@ under the License.
<version>1.0.1-SNAPSHOT</version>
</parent>
<artifactId>artemis-jms-client-side-load-balancing-example</artifactId>
<artifactId>client-side-load-balancing</artifactId>
<packaging>jar</packaging>
<name>ActiveMQ Artemis JMS Client Side Load Balancing Example</name>
@ -41,155 +42,164 @@ under the License.
<artifactId>artemis-jms-client</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-jms_2.0_spec</artifactId>
</dependency>
</dependencies>
<profiles>
<profile>
<id>example</id>
<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>
<clustered>true</clustered>
</configuration>
</execution>
<execution>
<id>create1</id>
<goals>
<goal>create</goal>
</goals>
<configuration>
<instance>${basedir}/target/server1</instance>
<clustered>true</clustered>
<portOffset>1</portOffset>
</configuration>
</execution>
<execution>
<id>create2</id>
<goals>
<goal>create</goal>
</goals>
<configuration>
<instance>${basedir}/target/server2</instance>
<clustered>true</clustered>
<portOffset>2</portOffset>
</configuration>
</execution>
<execution>
<id>start0</id>
<goals>
<goal>cli</goal>
</goals>
<configuration>
<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>
<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>start2</id>
<goals>
<goal>cli</goal>
</goals>
<configuration>
<spawn>true</spawn>
<location>${basedir}/target/server2</location>
<testURI>tcp://localhost:61618</testURI>
<args>
<param>run</param>
</args>
<name>server2</name>
</configuration>
</execution>
<execution>
<id>runClient</id>
<goals>
<goal>runClient</goal>
</goals>
<configuration>
<clientClass>org.apache.activemq.artemis.jms.example.ClientSideLoadBalancingExample</clientClass>
</configuration>
</execution>
<execution>
<id>stop0</id>
<goals>
<goal>cli</goal>
</goals>
<configuration>
<location>${basedir}/target/server0</location>
<args>
<param>stop</param>
</args>
</configuration>
</execution>
<execution>
<id>stop1</id>
<goals>
<goal>cli</goal>
</goals>
<configuration>
<location>${basedir}/target/server1</location>
<args>
<param>stop</param>
</args>
</configuration>
</execution>
<execution>
<id>stop2</id>
<goals>
<goal>cli</goal>
</goals>
<configuration>
<location>${basedir}/target/server2</location>
<args>
<param>stop</param>
</args>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.apache.activemq.examples.jms</groupId>
<artifactId>artemis-jms-client-side-load-balancing-example</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
<!-- specify -PnoServer if you don't want to start the server -->
<id>noServer</id>
<properties>
<noServer>true</noServer>
</properties>
</profile>
</profiles>
<build>
<plugins>
<plugin>
<groupId>org.apache.activemq</groupId>
<artifactId>artemis-maven-plugin</artifactId>
<executions>
<execution>
<id>create0</id>
<goals>
<goal>create</goal>
</goals>
<configuration>
<ignore>${noServer}</ignore>
<instance>${basedir}/target/server0</instance>
<clustered>true</clustered>
</configuration>
</execution>
<execution>
<id>create1</id>
<goals>
<goal>create</goal>
</goals>
<configuration>
<ignore>${noServer}</ignore>
<instance>${basedir}/target/server1</instance>
<clustered>true</clustered>
<portOffset>1</portOffset>
</configuration>
</execution>
<execution>
<id>create2</id>
<goals>
<goal>create</goal>
</goals>
<configuration>
<ignore>${noServer}</ignore>
<instance>${basedir}/target/server2</instance>
<clustered>true</clustered>
<portOffset>2</portOffset>
</configuration>
</execution>
<execution>
<id>start0</id>
<goals>
<goal>cli</goal>
</goals>
<configuration>
<spawn>true</spawn>
<ignore>${noServer}</ignore>
<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>start2</id>
<goals>
<goal>cli</goal>
</goals>
<configuration>
<ignore>${noServer}</ignore>
<spawn>true</spawn>
<location>${basedir}/target/server2</location>
<testURI>tcp://localhost:61618</testURI>
<args>
<param>run</param>
</args>
<name>server2</name>
</configuration>
</execution>
<execution>
<id>runClient</id>
<goals>
<goal>runClient</goal>
</goals>
<configuration>
<clientClass>org.apache.activemq.artemis.jms.example.ClientSideLoadBalancingExample</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>
<execution>
<id>stop2</id>
<goals>
<goal>cli</goal>
</goals>
<configuration>
<ignore>${noServer}</ignore>
<location>${basedir}/target/server2</location>
<args>
<param>stop</param>
</args>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.apache.activemq.examples.jms</groupId>
<artifactId>client-side-load-balancing</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>

View File

@ -26,6 +26,7 @@ under the License.
</head>
<body onload="prettyPrint()">
<h1>JMS Client-Side Load-Balancing 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 how connnections created from a single JMS Connection factory can be created
to different nodes of the cluster. In other words it demonstrates how ActiveMQ Artemis does <b>client side load balancing</b> of
@ -40,129 +41,9 @@ under the License.
<p>This example starts three servers which all broadcast their location using UDP discovery. The UDP broadcast configuration
can be seen in the <code>broker.xml</code> file.</p>
<p>A JMS ConnectionFactory is deployed on each server specifying the discovery group that will be used by that
connection factory.</p>
connection factory.</p>
<p>For more information on ActiveMQ Artemis load balancing, and clustering in general, please see the clustering
section of the user manual.</p>
section of the user manual.</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> Get an initial context for looking up JNDI from server 0.</li>
<pre class="prettyprint">
<code>initialContext = getContext(0);</code>
</pre>
<li>Look-up the JMS Queue object from JNDI</li>
<pre class="prettyprint">
<code>Queue queue = (Queue)initialContext.lookup("/queue/exampleQueue");</code>
</pre>
<li>Look-up a JMS Connection Factory object from JNDI on server 0</li>
<pre class="prettyprint">
<code>ConnectionFactory connectionFactory = (ConnectionFactory)initialContext.lookup("/ConnectionFactory");</code>
</pre>
<li> We create 3 JMS connections from the same connection factory. Since we are using round-robin
load-balancing this should result in each sessions being connected to a different node of the cluster</li>
<pre class="prettyprint">
<code>
Connection conn = connectionFactory.createConnection();
connectionA = connectionFactory.createConnection();
connectionB = connectionFactory.createConnection();
connectionC = connectionFactory.createConnection();
conn.close();
</code>
</pre>
<li>We create JMS Sessions</li>
<pre class="prettyprint">
<code>
Session sessionA = connectionA.createSession(false, Session.AUTO_ACKNOWLEDGE);
Session sessionB = connectionB.createSession(false, Session.AUTO_ACKNOWLEDGE);
Session sessionC = connectionC.createSession(false, Session.AUTO_ACKNOWLEDGE);
</code>
</pre>
<li>We create JMS MessageProducer objects on the sessions</li>
<pre class="prettyprint">
<code>
MessageProducer producerA = sessionA.createProducer(queue);
MessageProducer producerB = sessionB.createProducer(queue);
MessageProducer producerC = sessionC.createProducer(queue);
</code>
</pre>
<li>We send some messages on each producer</li>
<pre class="prettyprint">
<code>
final int numMessages = 10;
for (int i = 0; i < numMessages; i++)
{
TextMessage messageA = sessionA.createTextMessage("A:This is text message " + i);
producerA.send(messageA);
System.out.println("Sent message: " + messageA.getText());
TextMessage messageB = sessionB.createTextMessage("B:This is text message " + i);
producerB.send(messageB);
System.out.println("Sent message: " + messageB.getText());
TextMessage messageC = sessionC.createTextMessage("C:This is text message " + i);
producerC.send(messageC);
System.out.println("Sent message: " + messageC.getText());
}
</code>
</pre>
<li>We start the connection to consume messages</li>
<pre class="prettyprint">
<code>
connectionA.start();
connectionB.start();
connectionC.start();
</code>
</pre>
<li>We consume messages from the 3 session, one at a time.<br>
We try to consume one more message than expected from each session. If
the session were not properly load-balanced, we would be missing a
message from one of the sessions at the end.</li>
<pre class="prettyprint">
<code>
consume(sessionA, queue, numMessages, "A");
consume(sessionB, queue, numMessages, "B");
consume(sessionC, queue, numMessages, "C");
</code>
</pre>
<li>And finally (no pun intended), <b>always</b> remember to close your JMS resources after use, in a <code>finally</code> block. Closing a JMS connection will automatically close all of its sessions, consumers, producer and browser objects</li>
<pre class="prettyprint">
<code>
finally
{
if (connectionA != null)
{
connectionA.close();
}
if (connectionB != null)
{
connectionB.close();
}
if (connectionC != null)
{
connectionC.close();
}
if (initialContext != null)
{
initialContext.close();
}
}
</code>
</pre>
</ol>
</body>
</body>
</html>

View File

@ -18,7 +18,8 @@ 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">
<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>
@ -27,7 +28,7 @@ under the License.
<version>1.0.1-SNAPSHOT</version>
</parent>
<artifactId>artemis-jms-clustered-durable-subscription-example</artifactId>
<artifactId>clustered-durable-subscription</artifactId>
<packaging>jar</packaging>
<name>ActiveMQ Artemis JMS Clustered Durable Subscription Example</name>
@ -37,116 +38,130 @@ under the License.
<dependencies>
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-jms_2.0_spec</artifactId>
<groupId>org.apache.activemq</groupId>
<artifactId>artemis-jms-client</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
<profiles>
<profile>
<id>example</id>
<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>
</configuration>
</execution>
<execution>
<id>create1</id>
<goals>
<goal>create</goal>
</goals>
<configuration>
<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>
<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>
<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.ClusteredDurableSubscriptionExample</clientClass>
</configuration>
</execution>
<execution>
<id>stop0</id>
<goals>
<goal>cli</goal>
</goals>
<configuration>
<location>${basedir}/target/server0</location>
<args>
<param>stop</param>
</args>
</configuration>
</execution>
<execution>
<id>stop1</id>
<goals>
<goal>cli</goal>
</goals>
<configuration>
<location>${basedir}/target/server1</location>
<args>
<param>stop</param>
</args>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.apache.activemq.examples.jms</groupId>
<artifactId>artemis-jms-clustered-durable-subscription-example</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
<!-- specify -PnoServer if you don't want to start the server -->
<id>noServer</id>
<properties>
<noServer>true</noServer>
</properties>
</profile>
</profiles>
<build>
<plugins>
<plugin>
<groupId>org.apache.activemq</groupId>
<artifactId>artemis-maven-plugin</artifactId>
<executions>
<execution>
<id>create0</id>
<goals>
<goal>create</goal>
</goals>
<configuration>
<ignore>${noServer}</ignore>
<clustered>true</clustered>
<instance>${basedir}/target/server0</instance>
<configuration>${basedir}/target/classes/activemq/server0</configuration>
</configuration>
</execution>
<execution>
<id>create1</id>
<goals>
<goal>create</goal>
</goals>
<configuration>
<ignore>${noServer}</ignore>
<clustered>true</clustered>
<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.ClusteredDurableSubscriptionExample
</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.jms</groupId>
<artifactId>clustered-durable-subscription</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>

View File

@ -27,6 +27,8 @@ under the License.
<body onload="prettyPrint()">
<h1>JMS Durable Subscription 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 clustered JMS durable subscription.
Normally durable subscriptions exist on a single node and can only have one subscriber at any one time,
however, with ActiveMQ Artemis it's possible to create durable subscription instances with the same name and client-id
@ -45,6 +47,7 @@ under the License.
JNDI, these could be instantiated directly.
<p>Here's the relevant snippet from the server configuration, which tells the server to form a cluster between the two nodes
and to load balance the messages between the nodes.</p>
<p>The cli create method will define this section by default if you use --clustered as a parameter</p>
<pre class="prettyprint">
<code>&lt;cluster-connection name="my-cluster"&gt;
&lt;address&gt;jms&lt;/address&gt;
@ -59,155 +62,5 @@ under the License.
<p>For more information on ActiveMQ Artemis load balancing, and clustering in general, please see the clustering
section of the user manual.</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> Get an initial context for looking up JNDI from server 0.</li>
<pre class="prettyprint">
<code>
ic0 = getContext(0);
</code>
</pre>
<li>Look-up the JMS Topic object from JNDI</li>
<pre class="prettyprint">
<code>Topic topic = (Topic)ic0.lookup("/topic/exampleTopic");</code>
</pre>
<li>Look-up a JMS Connection Factory object from JNDI on server 0</li>
<pre class="prettyprint">
<code>ConnectionFactory cf0 = (ConnectionFactory)ic0.lookup("/ConnectionFactory");</code>
</pre>
<li>Get an initial context for looking up JNDI from server 1.</li>
<pre class="prettyprint">
<code>ic1 = getContext(1);</code>
</pre>
<li>Look-up a JMS Connection Factory object from JNDI on server 1</li>
<pre class="prettyprint">
<code>ConnectionFactory cf1 = (ConnectionFactory)ic1.lookup("/ConnectionFactory");
</code>
</pre>
<li>We create a JMS Connection connection0 which is a connection to server 0
and set the same client-id.</li>
<pre class="prettyprint">
<code>
connection0 = cf0.createConnection();
final String clientID = "my-client-id";
connection0.setClientID(clientID);
</code>
</pre>
<li>We create a JMS Connection connection1 which is a connection to server 1
and set the same client-id.</li>
<pre class="prettyprint">
<code>
connection1 = cf1.createConnection();
connection1.setClientID(clientID);
</code>
</pre>
<li>We create a JMS Session on server 0</li>
<pre class="prettyprint">
<code>
Session session0 = connection0.createSession(false, Session.AUTO_ACKNOWLEDGE);
</code>
</pre>
<li>We create a JMS Session on server 1</li>
<pre class="prettyprint">
<code>
Session session1 = connection1.createSession(false, Session.AUTO_ACKNOWLEDGE);
</code>
</pre>
<li>We start the connections to ensure delivery occurs on them</li>
<pre class="prettyprint">
<code>
connection0.start();
connection1.start();
</code>
</pre>
<li>We create JMS durable subscriptions with the same name and client-id on both nodes
of the cluster
</li>
<pre class="prettyprint">
<code>
final String subscriptionName = "my-subscription";
MessageConsumer subscriber0 = session0.createDurableSubscriber(topic, subscriptionName);
MessageConsumer subscriber1 = session1.createDurableSubscriber(topic, subscriptionName);
</code>
</pre>
<li>We create a JMS MessageProducer object on server 0.</li>
<pre class="prettyprint">
<code>
MessageProducer producer = session0.createProducer(topic);</code>
</pre>
<li>We send some messages to server 0.</li>
<pre class="prettyprint">
<code>
final int numMessages = 10;
for (int i = 0; i < numMessages; i++)
{
TextMessage message = session0.createTextMessage("This is text message " + i);
producer.send(message);
System.out.println("Sent message: " + message.getText());
}
</code>
</pre>
<li>
We now consume those messages on *both* server 0 and server 1.
Note that the messages have been load-balanced between the two nodes, with some
messages on node 0 and others on node 1.
The "logical" subscription is distributed across the cluster and contains exactly one copy of all the messages sent.
</li>
<pre class="prettyprint">
<code>
for (int i = 0; i < numMessages; i += 2)
{
TextMessage message0 = (TextMessage)consumer0.receive(5000);
System.out.println("Got message: " + message0.getText() + " from node 0");
TextMessage message1 = (TextMessage)consumer1.receive(5000);
System.out.println("Got message: " + message1.getText() + " from node 1");
}
</code>
</pre>
<li>And finally (no pun intended), <b>always</b> remember to close your JMS resources after use, in a <code>finally</code> block. Closing a JMS connection will automatically close all of its sessions, consumers, producer and browser objects</li>
<pre class="prettyprint">
<code>
finally
{
if (connection0 != null)
{
connection0.close();
}
if (connection1 != null)
{
connection1.close();
}
}
</code>
</pre>
</ol>
</body>
</body>
</html>

View File

@ -26,6 +26,8 @@ import javax.jms.Topic;
import javax.naming.InitialContext;
import java.util.Hashtable;
import org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory;
/**
* A simple example that shows a JMS Durable Subscription across two nodes of a cluster.
*
@ -40,36 +42,15 @@ public class ClusteredDurableSubscriptionExample
Connection connection1 = null;
InitialContext ic0 = null;
InitialContext ic1 = null;
try
{
// Step 1. Get an initial context for looking up JNDI from server 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://localhost:61616");
properties.put("topic.topic/exampleTopic", "exampleTopic");
ic0 = new InitialContext(properties);
// Step 1. Instantiate the connection factory on server 0
ConnectionFactory cf0 = new ActiveMQConnectionFactory("tcp://localhost:61616");
// Step 2. Look-up the JMS Topic object from JNDI
Topic topic = (Topic)ic0.lookup("topic/exampleTopic");
// Step 2. nstantiate the connection factory on server 1
ConnectionFactory cf1 = new ActiveMQConnectionFactory("tcp://localhost:61617");
// Step 3. Look-up a JMS Connection Factory object from JNDI on server 0
ConnectionFactory cf0 = (ConnectionFactory)ic0.lookup("ConnectionFactory");
// Step 4. Get an initial context for looking up JNDI from server 1
properties = new Hashtable<String, Object>();
properties.put("java.naming.factory.initial", "org.apache.activemq.artemis.jndi.ActiveMQInitialContextFactory");
properties.put("connectionFactory.ConnectionFactory", "tcp://localhost:61617");
ic1 = new InitialContext(properties);
// Step 5. Look-up a JMS Connection Factory object from JNDI on server 1
ConnectionFactory cf1 = (ConnectionFactory)ic1.lookup("ConnectionFactory");
// Step 6. We create a JMS Connection connection0 which is a connection to server 0
// Step 3. We create a JMS Connection connection0 which is a connection to server 0
// and set the client-id
connection0 = cf0.createConnection();
@ -77,38 +58,41 @@ public class ClusteredDurableSubscriptionExample
connection0.setClientID(clientID);
// Step 7. We create a JMS Connection connection1 which is a connection to server 1
// Step 4. We create a JMS Connection connection1 which is a connection to server 1
// and set the same client-id
connection1 = cf1.createConnection();
connection1.setClientID(clientID);
// Step 8. We create a JMS Session on server 0
// Step 5. We create a JMS Session on server 0
Session session0 = connection0.createSession(false, Session.AUTO_ACKNOWLEDGE);
// Step 9. We create a JMS Session on server 1
// Step 6. We create a JMS Session on server 1
Session session1 = connection1.createSession(false, Session.AUTO_ACKNOWLEDGE);
// Step 10. We start the connections to ensure delivery occurs on them
// Step 7. We start the connections to ensure delivery occurs on them
connection0.start();
connection1.start();
// Step 11. We create JMS durable subscriptions with the same name and client-id on both nodes
// Step 8. We create JMS durable subscriptions with the same name and client-id on both nodes
// of the cluster
final String subscriptionName = "my-subscription";
// Step 9. lookup the topic
Topic topic = session0.createTopic("exampleTopic");
MessageConsumer subscriber0 = session0.createDurableSubscriber(topic, subscriptionName);
MessageConsumer subscriber1 = session1.createDurableSubscriber(topic, subscriptionName);
Thread.sleep(1000);
// Step 12. We create a JMS MessageProducer object on server 0
// Step 10. We create a JMS MessageProducer object on server 0
MessageProducer producer = session0.createProducer(topic);
// Step 13. We send some messages to server 0
// Step 11. We send some messages to server 0
final int numMessages = 10;
@ -121,7 +105,7 @@ public class ClusteredDurableSubscriptionExample
System.out.println("Sent message: " + message.getText());
}
// Step 14. We now consume those messages on *both* server 0 and server 1.
// Step 12. We now consume those messages on *both* server 0 and server 1.
// Note that the messages have been load-balanced between the two nodes, with some
// messages on node 0 and others on node 1.
// The "logical" subscription is distributed across the cluster and contains exactly one copy of all the
@ -150,16 +134,6 @@ public class ClusteredDurableSubscriptionExample
{
connection1.close();
}
if (ic0 != null)
{
ic0.close();
}
if (ic1 != null)
{
ic1.close();
}
}
}
}

View File

@ -29,13 +29,13 @@ under the License.
<core xmlns="urn:activemq:core">
<bindings-directory>${data.dir:./data}/bindings</bindings-directory>
<bindings-directory>./data/bindings</bindings-directory>
<journal-directory>${data.dir:./data}/journal</journal-directory>
<journal-directory>./data/journal</journal-directory>
<large-messages-directory>${data.dir:./data}/largemessages</large-messages-directory>
<large-messages-directory>./data/largemessages</large-messages-directory>
<paging-directory>${data.dir:./data}/paging</paging-directory>
<paging-directory>./data/paging</paging-directory>
<!-- Connectors -->

View File

@ -29,13 +29,13 @@ under the License.
<core xmlns="urn:activemq:core">
<bindings-directory>${data.dir:./data}/bindings</bindings-directory>
<bindings-directory>./data/bindings</bindings-directory>
<journal-directory>${data.dir:./data}/journal</journal-directory>
<journal-directory>./data/journal</journal-directory>
<large-messages-directory>${data.dir:./data}/largemessages</large-messages-directory>
<large-messages-directory>./data/largemessages</large-messages-directory>
<paging-directory>${data.dir:./data}/paging</paging-directory>
<paging-directory>./data/paging</paging-directory>
<!-- Connectors -->
<connectors>

View File

@ -18,7 +18,8 @@ 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">
<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>
@ -27,7 +28,7 @@ under the License.
<version>1.0.1-SNAPSHOT</version>
</parent>
<artifactId>artemis-jms-clustered-grouping-example</artifactId>
<artifactId>clustered-grouping</artifactId>
<packaging>jar</packaging>
<name>ActiveMQ Artemis JMS CLustered Grouping Example</name>
@ -37,152 +38,166 @@ under the License.
<dependencies>
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-jms_2.0_spec</artifactId>
<groupId>org.apache.activemq</groupId>
<artifactId>artemis-jms-client</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
<profiles>
<profile>
<id>example</id>
<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>
</configuration>
</execution>
<execution>
<id>create1</id>
<goals>
<goal>create</goal>
</goals>
<configuration>
<instance>${basedir}/target/server1</instance>
<configuration>${basedir}/target/classes/activemq/server1</configuration>
</configuration>
</execution>
<execution>
<id>create2</id>
<goals>
<goal>create</goal>
</goals>
<configuration>
<instance>${basedir}/target/server2</instance>
<configuration>${basedir}/target/classes/activemq/server2</configuration>
</configuration>
</execution>
<execution>
<id>start0</id>
<goals>
<goal>cli</goal>
</goals>
<configuration>
<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>
<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>start2</id>
<goals>
<goal>cli</goal>
</goals>
<configuration>
<spawn>true</spawn>
<location>${basedir}/target/server2</location>
<testURI>tcp://localhost:61618</testURI>
<args>
<param>run</param>
</args>
<name>server2</name>
</configuration>
</execution>
<execution>
<id>runClient</id>
<goals>
<goal>runClient</goal>
</goals>
<configuration>
<clientClass>org.apache.activemq.artemis.jms.example.ClusteredGroupingExample</clientClass>
</configuration>
</execution>
<execution>
<id>stop0</id>
<goals>
<goal>cli</goal>
</goals>
<configuration>
<location>${basedir}/target/server0</location>
<args>
<param>stop</param>
</args>
</configuration>
</execution>
<execution>
<id>stop1</id>
<goals>
<goal>cli</goal>
</goals>
<configuration>
<location>${basedir}/target/server1</location>
<args>
<param>stop</param>
</args>
</configuration>
</execution>
<execution>
<id>stop2</id>
<goals>
<goal>cli</goal>
</goals>
<configuration>
<location>${basedir}/target/server2</location>
<args>
<param>stop</param>
</args>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.apache.activemq.examples.jms</groupId>
<artifactId>artemis-jms-clustered-grouping-example</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
<!-- specify -PnoServer if you don't want to start the server -->
<id>noServer</id>
<properties>
<noServer>true</noServer>
</properties>
</profile>
</profiles>
<build>
<plugins>
<plugin>
<groupId>org.apache.activemq</groupId>
<artifactId>artemis-maven-plugin</artifactId>
<executions>
<execution>
<id>create0</id>
<goals>
<goal>create</goal>
</goals>
<configuration>
<ignore>${noServer}</ignore>
<instance>${basedir}/target/server0</instance>
<configuration>${basedir}/target/classes/activemq/server0</configuration>
</configuration>
</execution>
<execution>
<id>create1</id>
<goals>
<goal>create</goal>
</goals>
<configuration>
<ignore>${noServer}</ignore>
<instance>${basedir}/target/server1</instance>
<configuration>${basedir}/target/classes/activemq/server1</configuration>
</configuration>
</execution>
<execution>
<id>create2</id>
<goals>
<goal>create</goal>
</goals>
<configuration>
<ignore>${noServer}</ignore>
<instance>${basedir}/target/server2</instance>
<configuration>${basedir}/target/classes/activemq/server2</configuration>
</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>start2</id>
<goals>
<goal>cli</goal>
</goals>
<configuration>
<ignore>${noServer}</ignore>
<spawn>true</spawn>
<location>${basedir}/target/server2</location>
<testURI>tcp://localhost:61618</testURI>
<args>
<param>run</param>
</args>
<name>server2</name>
</configuration>
</execution>
<execution>
<id>runClient</id>
<goals>
<goal>runClient</goal>
</goals>
<configuration>
<clientClass>org.apache.activemq.artemis.jms.example.ClusteredGroupingExample</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>
<execution>
<id>stop2</id>
<goals>
<goal>cli</goal>
</goals>
<configuration>
<ignore>${noServer}</ignore>
<location>${basedir}/target/server2</location>
<args>
<param>stop</param>
</args>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.apache.activemq.examples.jms</groupId>
<artifactId>clustered-grouping</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>

View File

@ -25,6 +25,8 @@ under the License.
<body>
<h1>JMS Clustered Grouping 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 how to ensure strict ordering across a cluster using clustered message grouping</p>
<p>We create 3 nodes each with a grouping message handler, one with a Local handler and 2 with a Remote handler.</p>
<p>The local handler acts as an arbitrator for the 2 remote handlers, holding the information on routes and communicating
@ -75,191 +77,5 @@ under the License.
</code>
</pre>
<p>For more information on ActiveMQ Artemis clustering and grouping see the clustering and grouping
section of the user manual.</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> Get an initial context for looking up JNDI from server 0.</li>
<pre class="prettyprint">
<code>ic0 = getContext(0);</code>
</pre>
<li>Look-up the JMS Queue object from JNDI</li>
<pre class="prettyprint">
<code>Queue queue = (Queue)ic0.lookup("/queue/exampleQueue");</code>
</pre>
<li>Look-up a JMS Connection Factory object from JNDI on server 0</li>
<pre class="prettyprint">
<code>ConnectionFactory cf0 = (ConnectionFactory)ic0.lookup("/ConnectionFactory");</code>
</pre>
<li>Get an initial context for looking up JNDI from server 1.</li>
<pre class="prettyprint">
<code>ic1 = getContext(1);</code>
</pre>
<li>Look-up a JMS Connection Factory object from JNDI on server 1</li>
<pre class="prettyprint">
<code>ConnectionFactory cf1 = (ConnectionFactory)ic1.lookup("/ConnectionFactory");
</code>
</pre>
<li>Get an initial context for looking up JNDI from server 2.</li>
<pre class="prettyprint">
<code>ic2 = getContext(2);</code>
</pre>
<li>Look-up a JMS Connection Factory object from JNDI on server 2</li>
<pre class="prettyprint">
<code>ConnectionFactory cf2 = (ConnectionFactory)ic2.lookup("/ConnectionFactory");
</code>
</pre>
<li>We create a JMS Connection connection0 which is a connection to server 0</li>
<pre class="prettyprint">
<code>connection0 = cf0.createConnection();</code>
</pre>
<li>We create a JMS Connection connection0 which is a connection to server 1</li>
<pre class="prettyprint">
<code>connection1 = cf1.createConnection();</code>
</pre>
<li>We create a JMS Connection connection0 which is a connection to server 2</li>
<pre class="prettyprint">
<code>connection2 = cf2.createConnection();</code>
</pre>
<li>We create a JMS Session on server 0</li>
<pre class="prettyprint">
<code>Session session0 = connection0.createSession(false, Session.AUTO_ACKNOWLEDGE);</code>
</pre>
<li>We create a JMS Session on server 1</li>
<pre class="prettyprint">
<code>Session session1 = connection1.createSession(false, Session.AUTO_ACKNOWLEDGE);</code>
</pre>
<li>We create a JMS Session on server 2</li>
<pre class="prettyprint">
<code>Session session2 = connection2.createSession(false, Session.AUTO_ACKNOWLEDGE);</code>
</pre>
<li>We start the connections to ensure delivery occurs on them</li>
<pre class="prettyprint">
<code>
connection0.start();
connection1.start();
connection2.start();</code>
</pre>
<li>We create JMS MessageConsumer objects on server 0</li>
<pre class="prettyprint">
<code>MessageConsumer consumer = session0.createConsumer(queue);</code>
</pre>
<li>We create a JMS MessageProducer object on server 0, 1 and 2</li>
<pre class="prettyprint">
<code>
MessageProducer producer0 = session0.createProducer(queue);
MessageProducer producer1 = session1.createProducer(queue);
MessageProducer producer2 = session2.createProducer(queue);</code>
</pre>
<li>We send some messages to server 0, 1 and 2 with the same groupid set</li>
<pre class="prettyprint">
<code>
final int numMessages = 10;
for (int i = 0; i < numMessages; i++)
{
TextMessage message = session0.createTextMessage("This is text message " + i);
message.setStringProperty(ActiveMQMessage.JMSXGROUPID, "Group-0");
producer0.send(message);
System.out.println("Sent messages: " + message.getText() + " to node 0");
}
for (int i = 0; i < numMessages; i++)
{
TextMessage message = session1.createTextMessage("This is text message " + (i + 10));
message.setStringProperty(ActiveMQMessage.JMSXGROUPID, "Group-0");
producer1.send(message);
System.out.println("Sent messages: " + message.getText() + " to node 1");
}
for (int i = 0; i < numMessages; i++)
{
TextMessage message = session2.createTextMessage("This is text message " + (i + 20));
message.setStringProperty(ActiveMQMessage.JMSXGROUPID, "Group-0");
producer2.send(message);
System.out.println("Sent messages: " + message.getText() + " to node 2");
}
</code>
</pre>
<li>We now consume those messages from server 0. We note the messages have all been sent to the same consumer on the same node</li>
<pre class="prettyprint">
<code>
for (int i = 0; i < numMessages * 3; i++)
{
TextMessage message0 = (TextMessage)consumer.receive(5000);
System.out.println("Got message: " + message0.getText() + " from node 0");
}
</code>
</pre>
<li>Finally, Be sure to close our resources!</li>
<pre class="prettyprint">
<code>
if (connection0 != null)
{
connection0.close();
}
if (connection1 != null)
{
connection1.close();
}
if (connection2 != null)
{
connection2.close();
}
if (ic0 != null)
{
ic0.close();
}
if (ic1 != null)
{
ic1.close();
}
if (ic2 != null)
{
ic2.close();
}</code>
</pre>
</ol>
</body>
</body>
</html>

View File

@ -23,8 +23,10 @@ import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.InitialContext;
import java.util.Hashtable;
import org.apache.activemq.artemis.api.jms.ActiveMQJMSClient;
import org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory;
import org.apache.activemq.artemis.jms.client.ActiveMQQueue;
/**
* A simple example that demonstrates server side load-balancing of messages between the queue instances on different
@ -40,82 +42,58 @@ public class ClusteredGroupingExample
Connection connection2 = null;
InitialContext ic0 = null;
InitialContext ic1 = null;
InitialContext ic2 = null;
try
{
// Step 1. Get an initial context for looking up JNDI from server 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://localhost:61616");
properties.put("queue.queue/exampleQueue", "exampleQueue");
ic0 = new InitialContext(properties);
// Step 1. We will instantiate the queue object directly on this example
// This could be done through JNDI or JMSession.createQueue
Queue queue = ActiveMQJMSClient.createQueue("exampleQueue");
// Step 2. Look-up the JMS Queue object from JNDI
Queue queue = (Queue)ic0.lookup("queue/exampleQueue");
// Step 2. create a connection factory towards server 0.
ConnectionFactory cf0 = new ActiveMQConnectionFactory("tcp://localhost:61616");
// Step 3. Look-up a JMS Connection Factory object from JNDI on server 0
ConnectionFactory cf0 = (ConnectionFactory)ic0.lookup("ConnectionFactory");
// Step 3. create a connection factory towards server 1.
ConnectionFactory cf1 = new ActiveMQConnectionFactory("tcp://localhost:61617");
// Step 4. Get an initial context for looking up JNDI from server 1
properties = new Hashtable<String, Object>();
properties.put("java.naming.factory.initial", "org.apache.activemq.artemis.jndi.ActiveMQInitialContextFactory");
properties.put("connectionFactory.ConnectionFactory", "tcp://localhost:61617");
ic1 = new InitialContext(properties);
// Step 4. create a connection factory towards server 2.
ConnectionFactory cf2 = new ActiveMQConnectionFactory("tcp://localhost:61618");
// Step 5. Look-up a JMS Connection Factory object from JNDI on server 1
ConnectionFactory cf1 = (ConnectionFactory)ic1.lookup("ConnectionFactory");
// Step 4. Get an initial context for looking up JNDI from server 2
properties = new Hashtable<String, Object>();
properties.put("java.naming.factory.initial", "org.apache.activemq.artemis.jndi.ActiveMQInitialContextFactory");
properties.put("connectionFactory.ConnectionFactory", "tcp://localhost:61618");
ic2 = new InitialContext(properties);
// Step 5. Look-up a JMS Connection Factory object from JNDI on server 2
ConnectionFactory cf2 = (ConnectionFactory)ic2.lookup("ConnectionFactory");
// Step 6. We create a JMS Connection connection0 which is a connection to server 0
// Step 5. We create a JMS Connection connection0 which is a connection to server 0
connection0 = cf0.createConnection();
// Step 7. We create a JMS Connection connection1 which is a connection to server 1
// Step 6. We create a JMS Connection connection1 which is a connection to server 1
connection1 = cf1.createConnection();
// Step 8. We create a JMS Connection connection2 which is a connection to server 2
// Step 7. We create a JMS Connection connection2 which is a connection to server 2
connection2 = cf2.createConnection();
// Step 9. We create a JMS Session on server 0
// Step 8. 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
// Step 9. We create a JMS Session on server 1
Session session1 = connection1.createSession(false, Session.AUTO_ACKNOWLEDGE);
// Step 11. We create a JMS Session on server 2
// Step 10. We create a JMS Session on server 2
Session session2 = connection1.createSession(false, Session.AUTO_ACKNOWLEDGE);
// Step 12. We start the connections to ensure delivery occurs on them
// Step 11. We start the connections to ensure delivery occurs on them
connection0.start();
connection1.start();
connection2.start();
// Step 13. We create JMS MessageConsumer objects on server 0
// Step 12. We create JMS MessageConsumer objects on server 0
MessageConsumer consumer = session0.createConsumer(queue);
// Step 14. We create a JMS MessageProducer object on server 0, 1 and 2
// Step 13. We create a JMS MessageProducer object on server 0, 1 and 2
MessageProducer producer0 = session0.createProducer(queue);
MessageProducer producer1 = session1.createProducer(queue);
MessageProducer producer2 = session2.createProducer(queue);
// Step 15. We send some messages to server 0, 1 and 2 with the same groupid set
// Step 14. We send some messages to server 0, 1 and 2 with the same groupid set
final int numMessages = 10;
@ -153,7 +131,7 @@ public class ClusteredGroupingExample
System.out.println("Sent messages: " + message.getText() + " to node 2");
}
// Step 16. We now consume those messages from server 0
// Step 15. We now consume those messages from server 0
// We note the messages have all been sent to the same consumer on the same node
for (int i = 0; i < numMessages * 3; i++)
@ -182,21 +160,6 @@ public class ClusteredGroupingExample
{
connection2.close();
}
if (ic0 != null)
{
ic0.close();
}
if (ic1 != null)
{
ic1.close();
}
if (ic2 != null)
{
ic2.close();
}
}
}
}

View File

@ -29,13 +29,13 @@ under the License.
<core xmlns="urn:activemq:core">
<bindings-directory>${data.dir:./data}/bindings</bindings-directory>
<bindings-directory>./data/bindings</bindings-directory>
<journal-directory>${data.dir:./data}/journal</journal-directory>
<journal-directory>./data/journal</journal-directory>
<large-messages-directory>${data.dir:./data}/largemessages</large-messages-directory>
<large-messages-directory>./data/largemessages</large-messages-directory>
<paging-directory>${data.dir:./data}/paging</paging-directory>
<paging-directory>./data/paging</paging-directory>
<!-- Connectors -->

View File

@ -29,13 +29,13 @@ under the License.
<core xmlns="urn:activemq:core">
<bindings-directory>${data.dir:./data}/bindings</bindings-directory>
<bindings-directory>./data/bindings</bindings-directory>
<journal-directory>${data.dir:./data}/journal</journal-directory>
<journal-directory>./data/journal</journal-directory>
<large-messages-directory>${data.dir:./data}/largemessages</large-messages-directory>
<large-messages-directory>./data/largemessages</large-messages-directory>
<paging-directory>${data.dir:./data}/paging</paging-directory>
<paging-directory>./data/paging</paging-directory>
<!-- Connectors -->

View File

@ -29,13 +29,13 @@ under the License.
<core xmlns="urn:activemq:core">
<bindings-directory>${data.dir:./data}/bindings</bindings-directory>
<bindings-directory>./data/bindings</bindings-directory>
<journal-directory>${data.dir:./data}/journal</journal-directory>
<journal-directory>./data/journal</journal-directory>
<large-messages-directory>${data.dir:./data}/largemessages</large-messages-directory>
<large-messages-directory>./data/largemessages</large-messages-directory>
<paging-directory>${data.dir:./data}/paging</paging-directory>
<paging-directory>./data/paging</paging-directory>
<!-- Connectors -->

View File

@ -18,7 +18,8 @@ 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">
<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>
@ -37,123 +38,133 @@ under the License.
<dependencies>
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-jms_2.0_spec</artifactId>
<groupId>org.apache.activemq</groupId>
<artifactId>artemis-jms-client</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
<profiles>
<profile>
<id>example</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.activemq</groupId>
<artifactId>artemis-maven-plugin</artifactId>
<executions>
<execution>
<id>create</id>
<goals>
<goal>create</goal>
</goals>
<configuration>
<libList>
<!-- You need to add jgroups.jar to the server's lib -->
<arg>org.jgroups:jgroups:3.6.0.Final</arg>
</libList>
<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>
<!-- You need to add jgroups.jar to the server's lib -->
<arg>org.jgroups:jgroups:3.6.0.Final</arg>
</libList>
<instance>${basedir}/target/server1</instance>
<configuration>${basedir}/target/classes/activemq/server1</configuration>
</configuration>
</execution>
<execution>
<id>start0</id>
<goals>
<goal>cli</goal>
</goals>
<configuration>
<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>
<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.ClusteredJgroupsExample</clientClass>
</configuration>
</execution>
<execution>
<id>stop0</id>
<goals>
<goal>cli</goal>
</goals>
<configuration>
<location>${basedir}/target/server0</location>
<args>
<param>stop</param>
</args>
</configuration>
</execution>
<execution>
<id>stop1</id>
<goals>
<goal>cli</goal>
</goals>
<configuration>
<location>${basedir}/target/server1</location>
<args>
<param>stop</param>
</args>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.apache.activemq.examples.jms</groupId>
<artifactId>clustered-jgroups</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
<!-- specify -PnoServer if you don't want to start the server -->
<id>noServer</id>
<properties>
<noServer>true</noServer>
</properties>
</profile>
</profiles>
<build>
<plugins>
<plugin>
<groupId>org.apache.activemq</groupId>
<artifactId>artemis-maven-plugin</artifactId>
<executions>
<execution>
<id>create</id>
<goals>
<goal>create</goal>
</goals>
<configuration>
<libList>
<!-- You need to add jgroups.jar to the server's lib -->
<arg>org.jgroups:jgroups:3.6.0.Final</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>
<!-- You need to add jgroups.jar to the server's lib -->
<arg>org.jgroups:jgroups:3.6.0.Final</arg>
</libList>
<ignore>${noServer}</ignore>
<instance>${basedir}/target/server1</instance>
<configuration>${basedir}/target/classes/activemq/server1</configuration>
</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.ClusteredJgroupsExample</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>
<location>${basedir}/target/server1</location>
<args>
<param>stop</param>
</args>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.apache.activemq.examples.jms</groupId>
<artifactId>clustered-jgroups</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>

View File

@ -27,7 +27,9 @@ under the License.
<body onload="prettyPrint()">
<h1>ActiveMQ Artemis Clustering with JGroups Example</h1>
<p>This example demonstrates the working of a two node cluster using JGroups as the underlying topology broadcasting/discovery
<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 the working of a two node cluster using JGroups as the underlying topology broadcasting/discovery
technique.</p>
<p>We deploy a queue on to the cluster, then create a consumer on the queue on each node, and we create a producer on only one of the nodes.</p>
<p>We then send some messages via the producer, and we verify that <b>both</b> consumers receive the sent messages
@ -58,150 +60,8 @@ under the License.
&lt;/discovery-group>
&lt;/discovery-groups&gt;
</code>
</pre>
</pre>
<p>For more information on ActiveMQ Artemis clustering in general, please see the clustering
section of the user manual.</p>
<h2>Example step-by-step</h2>
<p><i>To run the example, simply type <code>./build.sh</code> (or <code>build.bat</code> on windows) from this directory</i></p>
<ol>
<li> Get an initial context for looking up JNDI from server 0.</li>
<pre class="prettyprint">
<code>
ic0 = getContext(0);
</code>
</pre>
<li>Look-up the JMS Queue object from JNDI</li>
<pre class="prettyprint">
<code>Queue queue = (Queue)ic0.lookup("/queue/exampleQueue");</code>
</pre>
<li>Look-up a JMS Connection Factory object from JNDI on server 0</li>
<pre class="prettyprint">
<code>ConnectionFactory cf0 = (ConnectionFactory)ic0.lookup("/ConnectionFactory");</code>
</pre>
<li>Get an initial context for looking up JNDI from server 1.</li>
<pre class="prettyprint">
<code>ic1 = getContext(1);</code>
</pre>
<li>Look-up a JMS Connection Factory object from JNDI on server 1</li>
<pre class="prettyprint">
<code>ConnectionFactory cf1 = (ConnectionFactory)ic1.lookup("/ConnectionFactory");
</code>
</pre>
<li>We create a JMS Connection connection0 which is a connection to server 0</li>
<pre class="prettyprint">
<code>
connection0 = cf0.createConnection();
</code>
</pre>
<li>We create a JMS Connection connection1 which is a connection to server 1</li>
<pre class="prettyprint">
<code>
connection1 = cf1.createConnection();
</code>
</pre>
<li>We create a JMS Session on server 0</li>
<pre class="prettyprint">
<code>
Session session0 = connection0.createSession(false, Session.AUTO_ACKNOWLEDGE);
</code>
</pre>
<li>We create a JMS Session on server 1</li>
<pre class="prettyprint">
<code>
Session session1 = connection1.createSession(false, Session.AUTO_ACKNOWLEDGE);
</code>
</pre>
<li>We start the connections to ensure delivery occurs on them</li>
<pre class="prettyprint">
<code>
connection0.start();
connection1.start();
</code>
</pre>
<li>We create JMS MessageConsumer objects on server 0 and server 1</li>
<pre class="prettyprint">
<code>
MessageConsumer consumer0 = session0.createConsumer(queue);
MessageConsumer consumer1 = session1.createConsumer(queue);
</code>
</pre>
<li>We create a JMS MessageProducer object on server 0.</li>
<pre class="prettyprint">
<code>
MessageProducer producer = session0.createProducer(queue);</code>
</pre>
<li>We send some messages to server 0.</li>
<pre class="prettyprint">
<code>
final int numMessages = 10;
for (int i = 0; i < numMessages; i++)
{
TextMessage message = session0.createTextMessage("This is text message " + i);
producer.send(message);
System.out.println("Sent message: " + message.getText());
}
</code>
</pre>
<li>We now consume those messages on *both* server 0 and server 1.
We note the messages have been distributed between servers in a round robin fashion.
ActiveMQ Artemis has <b>load balanced</b> the messages between the available consumers on the different nodes.
ActiveMQ Artemis can be configured to always load balance messages to all nodes, or to only balance messages
to nodes which have consumers with no or matching selectors. See the user manual for more details.</li>
JMS Queues implement point-to-point message where each message is only ever consumed by a
maximum of one consumer.
<pre class="prettyprint">
<code>
for (int i = 0; i < numMessages; i += 2)
{
TextMessage message0 = (TextMessage)consumer0.receive(5000);
System.out.println("Got message: " + message0.getText() + " from node 0");
TextMessage message1 = (TextMessage)consumer1.receive(5000);
System.out.println("Got message: " + message1.getText() + " from node 1");
}
</code>
</pre>
<li>And finally (no pun intended), <b>always</b> remember to close your JMS resources after use, in a <code>finally</code> block. Closing a JMS connection will automatically close all of its sessions, consumers, producer and browser objects</li>
<pre class="prettyprint">
<code>
finally
{
if (connection0 != null)
{
connection0.close();
}
if (connection1 != null)
{
connection1.close();
}
}
</code>
</pre>
</ol>
section of the user manual.</p>
</body>
</html>

View File

@ -29,13 +29,13 @@ under the License.
<core xmlns="urn:activemq:core">
<bindings-directory>${data.dir:./data}/bindings</bindings-directory>
<bindings-directory>./data/bindings</bindings-directory>
<journal-directory>${data.dir:./data}/journal</journal-directory>
<journal-directory>./data/journal</journal-directory>
<large-messages-directory>${data.dir:./data}/largemessages</large-messages-directory>
<large-messages-directory>./data/largemessages</large-messages-directory>
<paging-directory>${data.dir:./data}/paging</paging-directory>
<paging-directory>./data/paging</paging-directory>
<!-- Connectors -->

View File

@ -29,13 +29,13 @@ under the License.
<core xmlns="urn:activemq:core">
<bindings-directory>${data.dir:./data}/bindings</bindings-directory>
<bindings-directory>./data/bindings</bindings-directory>
<journal-directory>${data.dir:./data}/journal</journal-directory>
<journal-directory>./data/journal</journal-directory>
<large-messages-directory>${data.dir:./data}/largemessages</large-messages-directory>
<large-messages-directory>./data/largemessages</large-messages-directory>
<paging-directory>${data.dir:./data}/paging</paging-directory>
<paging-directory>./data/paging</paging-directory>
<!-- Connectors -->
<connectors>

View File

@ -18,7 +18,8 @@ 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">
<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>
@ -37,115 +38,126 @@ under the License.
<dependencies>
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-jms_2.0_spec</artifactId>
<groupId>org.apache.activemq</groupId>
<artifactId>artemis-jms-client</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
<profiles>
<profile>
<id>example</id>
<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>
</configuration>
</execution>
<execution>
<id>create1</id>
<goals>
<goal>create</goal>
</goals>
<configuration>
<instance>${basedir}/target/server1</instance>
<configuration>${basedir}/target/classes/activemq/server1</configuration>
</configuration>
</execution>
<execution>
<id>start0</id>
<goals>
<goal>cli</goal>
</goals>
<configuration>
<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>
<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.ClusteredQueueExample</clientClass>
</configuration>
</execution>
<execution>
<id>stop0</id>
<goals>
<goal>cli</goal>
</goals>
<configuration>
<location>${basedir}/target/server0</location>
<args>
<param>stop</param>
</args>
</configuration>
</execution>
<execution>
<id>stop1</id>
<goals>
<goal>cli</goal>
</goals>
<configuration>
<location>${basedir}/target/server1</location>
<args>
<param>stop</param>
</args>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.apache.activemq.examples.jms</groupId>
<artifactId>clustered-queue</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
<!-- specify -PnoServer if you don't want to start the server -->
<id>noServer</id>
<properties>
<noServer>true</noServer>
</properties>
</profile>
</profiles>
<build>
<plugins>
<plugin>
<groupId>org.apache.activemq</groupId>
<artifactId>artemis-maven-plugin</artifactId>
<executions>
<execution>
<id>create0</id>
<goals>
<goal>create</goal>
</goals>
<configuration>
<ignore>${noServer}</ignore>
<instance>${basedir}/target/server0</instance>
<configuration>${basedir}/target/classes/activemq/server0</configuration>
</configuration>
</execution>
<execution>
<id>create1</id>
<goals>
<goal>create</goal>
</goals>
<configuration>
<ignore>${noServer}</ignore>
<instance>${basedir}/target/server1</instance>
<configuration>${basedir}/target/classes/activemq/server1</configuration>
</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.ClusteredQueueExample</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.jms</groupId>
<artifactId>clustered-queue</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>

View File

@ -27,6 +27,8 @@ under the License.
<body onload="prettyPrint()">
<h1>JMS Load Balanced Clustered Queue 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 JMS queue deployed on two different nodes. The two nodes are configured to form a cluster.</p>
<p>We then create a consumer on the queue on each node, and we create a producer on only one of the nodes.</p>
<p>We then send some messages via the producer, and we verify that <b>both</b> consumers receive the sent messages
@ -50,147 +52,5 @@ under the License.
</pre>
<p>For more information on ActiveMQ Artemis load balancing, and clustering in general, please see the clustering
section of the user manual.</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> Get an initial context for looking up JNDI from server 0.</li>
<pre class="prettyprint">
<code>
ic0 = getContext(0);
</code>
</pre>
<li>Look-up the JMS Queue object from JNDI</li>
<pre class="prettyprint">
<code>Queue queue = (Queue)ic0.lookup("/queue/exampleQueue");</code>
</pre>
<li>Look-up a JMS Connection Factory object from JNDI on server 0</li>
<pre class="prettyprint">
<code>ConnectionFactory cf0 = (ConnectionFactory)ic0.lookup("/ConnectionFactory");</code>
</pre>
<li>Get an initial context for looking up JNDI from server 1.</li>
<pre class="prettyprint">
<code>ic1 = getContext(1);</code>
</pre>
<li>Look-up a JMS Connection Factory object from JNDI on server 1</li>
<pre class="prettyprint">
<code>ConnectionFactory cf1 = (ConnectionFactory)ic1.lookup("/ConnectionFactory");
</code>
</pre>
<li>We create a JMS Connection connection0 which is a connection to server 0</li>
<pre class="prettyprint">
<code>
connection0 = cf0.createConnection();
</code>
</pre>
<li>We create a JMS Connection connection1 which is a connection to server 1</li>
<pre class="prettyprint">
<code>
connection1 = cf1.createConnection();
</code>
</pre>
<li>We create a JMS Session on server 0</li>
<pre class="prettyprint">
<code>
Session session0 = connection0.createSession(false, Session.AUTO_ACKNOWLEDGE);
</code>
</pre>
<li>We create a JMS Session on server 1</li>
<pre class="prettyprint">
<code>
Session session1 = connection1.createSession(false, Session.AUTO_ACKNOWLEDGE);
</code>
</pre>
<li>We start the connections to ensure delivery occurs on them</li>
<pre class="prettyprint">
<code>
connection0.start();
connection1.start();
</code>
</pre>
<li>We create JMS MessageConsumer objects on server 0 and server 1</li>
<pre class="prettyprint">
<code>
MessageConsumer consumer0 = session0.createConsumer(queue);
MessageConsumer consumer1 = session1.createConsumer(queue);
</code>
</pre>
<li>We create a JMS MessageProducer object on server 0.</li>
<pre class="prettyprint">
<code>
MessageProducer producer = session0.createProducer(queue);</code>
</pre>
<li>We send some messages to server 0.</li>
<pre class="prettyprint">
<code>
final int numMessages = 10;
for (int i = 0; i < numMessages; i++)
{
TextMessage message = session0.createTextMessage("This is text message " + i);
producer.send(message);
System.out.println("Sent message: " + message.getText());
}
</code>
</pre>
<li>We now consume those messages on *both* server 0 and server 1.
We note the messages have been distributed between servers in a round robin fashion.
ActiveMQ Artemis has <b>load balanced</b> the messages between the available consumers on the different nodes.
ActiveMQ Artemis can be configured to always load balance messages to all nodes, or to only balance messages
to nodes which have consumers with no or matching selectors. See the user manual for more details.</li>
JMS Queues implement point-to-point message where each message is only ever consumed by a
maximum of one consumer.
<pre class="prettyprint">
<code>
for (int i = 0; i < numMessages; i += 2)
{
TextMessage message0 = (TextMessage)consumer0.receive(5000);
System.out.println("Got message: " + message0.getText() + " from node 0");
TextMessage message1 = (TextMessage)consumer1.receive(5000);
System.out.println("Got message: " + message1.getText() + " from node 1");
}
</code>
</pre>
<li>And finally (no pun intended), <b>always</b> remember to close your JMS resources after use, in a <code>finally</code> block. Closing a JMS connection will automatically close all of its sessions, consumers, producer and browser objects</li>
<pre class="prettyprint">
<code>
finally
{
if (connection0 != null)
{
connection0.close();
}
if (connection1 != null)
{
connection1.close();
}
}
</code>
</pre>
</ol>
</body>
</html>

View File

@ -23,8 +23,10 @@ import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.InitialContext;
import java.util.Hashtable;
import org.apache.activemq.artemis.api.jms.ActiveMQJMSClient;
import org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory;
import org.apache.activemq.artemis.jms.client.ActiveMQQueue;
/**
* A simple example that demonstrates server side load-balancing of messages between the queue instances on different
@ -38,33 +40,16 @@ public class ClusteredQueueExample
Connection connection1 = null;
InitialContext ic0 = null;
InitialContext ic1 = null;
try
{
// Step 1. Get an initial context for looking up JNDI from server 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://localhost:61616");
properties.put("queue.queue/exampleQueue", "exampleQueue");
ic0 = new InitialContext(properties);
// Step 2. Instantiate the Queue
Queue queue = ActiveMQJMSClient.createQueue("exampleQueue");
// Step 2. Look-up the JMS Queue object from JNDI
Queue queue = (Queue)ic0.lookup("queue/exampleQueue");
// Step 3. Look-up a JMS Connection Factory object from JNDI on server 0
ConnectionFactory cf0 = (ConnectionFactory)ic0.lookup("ConnectionFactory");
// Step 4. Get an initial context for looking up JNDI from server 1
properties = new Hashtable<String, Object>();
properties.put("java.naming.factory.initial", "org.apache.activemq.artemis.jndi.ActiveMQInitialContextFactory");
properties.put("connectionFactory.ConnectionFactory", "tcp://localhost:61617");
ic1 = new InitialContext(properties);
// Instantiate connection towards server 0
ConnectionFactory cf0 = new ActiveMQConnectionFactory("tcp://localhost:61616");
// Step 5. Look-up a JMS Connection Factory object from JNDI on server 1
ConnectionFactory cf1 = (ConnectionFactory)ic1.lookup("ConnectionFactory");
ConnectionFactory cf1 = new ActiveMQConnectionFactory("tcp://localhost:61617");
// Step 6. We create a JMS Connection connection0 which is a connection to server 0
connection0 = cf0.createConnection();
@ -135,16 +120,6 @@ public class ClusteredQueueExample
{
connection1.close();
}
if (ic0 != null)
{
ic0.close();
}
if (ic1 != null)
{
ic1.close();
}
}
}
}

View File

@ -29,13 +29,13 @@ under the License.
<core xmlns="urn:activemq:core">
<bindings-directory>${data.dir:./data}/bindings</bindings-directory>
<bindings-directory>./data/bindings</bindings-directory>
<journal-directory>${data.dir:./data}/journal</journal-directory>
<journal-directory>./data/journal</journal-directory>
<large-messages-directory>${data.dir:./data}/largemessages</large-messages-directory>
<large-messages-directory>./data/largemessages</large-messages-directory>
<paging-directory>${data.dir:./data}/paging</paging-directory>
<paging-directory>./data/paging</paging-directory>
<!-- Connectors -->
<connectors>

View File

@ -1,188 +0,0 @@
<?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.jms</groupId>
<artifactId>jms-examples</artifactId>
<version>1.0.1-SNAPSHOT</version>
</parent>
<artifactId>artemis-jms-clustered-standalone-example</artifactId>
<packaging>jar</packaging>
<name>ActiveMQ Artemis JMS Clustered Standalone Example</name>
<properties>
<activemq.basedir>${project.basedir}/../../..</activemq.basedir>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-jms_2.0_spec</artifactId>
</dependency>
</dependencies>
<profiles>
<profile>
<id>example</id>
<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>
</configuration>
</execution>
<execution>
<id>create1</id>
<goals>
<goal>create</goal>
</goals>
<configuration>
<instance>${basedir}/target/server1</instance>
<configuration>${basedir}/target/classes/activemq/server1</configuration>
</configuration>
</execution>
<execution>
<id>create2</id>
<goals>
<goal>create</goal>
</goals>
<configuration>
<instance>${basedir}/target/server2</instance>
<configuration>${basedir}/target/classes/activemq/server2</configuration>
</configuration>
</execution>
<execution>
<id>start0</id>
<goals>
<goal>cli</goal>
</goals>
<configuration>
<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>
<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>start2</id>
<goals>
<goal>cli</goal>
</goals>
<configuration>
<spawn>true</spawn>
<location>${basedir}/target/server2</location>
<testURI>tcp://localhost:61618</testURI>
<args>
<param>run</param>
</args>
<name>server2</name>
</configuration>
</execution>
<execution>
<id>runClient</id>
<goals>
<goal>runClient</goal>
</goals>
<configuration>
<clientClass>org.apache.activemq.artemis.jms.example.ClusteredStandaloneExample</clientClass>
</configuration>
</execution>
<execution>
<id>stop0</id>
<goals>
<goal>cli</goal>
</goals>
<configuration>
<location>${basedir}/target/server0</location>
<args>
<param>stop</param>
</args>
</configuration>
</execution>
<execution>
<id>stop1</id>
<goals>
<goal>cli</goal>
</goals>
<configuration>
<location>${basedir}/target/server1</location>
<args>
<param>stop</param>
</args>
</configuration>
</execution>
<execution>
<id>stop2</id>
<goals>
<goal>cli</goal>
</goals>
<configuration>
<location>${basedir}/target/server2</location>
<args>
<param>stop</param>
</args>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.apache.activemq.examples.jms</groupId>
<artifactId>artemis-jms-clustered-standalone-example</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>

View File

@ -1,70 +0,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.
-->
<html>
<head>
<title>JMS Clustered Stand-alone 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 Clustered Stand-alone Example</h1>
<p>This example demonstrates a JMS Topic deployed on three different nodes.
The three nodes are configured to form a cluster.</p>
<p>Subscribers for the topic are created on each node, and a producer is created on only one of the nodes.</p>
<p>Some messages are sent by the producer, and we verify that <strong>all</strong> subscribers receive all the
sent messages.</p>
<p>This example uses ActiveMQ's default stand-alone clustered configuration.
The relevant snippet from the server configuration, which tells the servers to form a cluster between the three nodes
and to load balance the messages between the nodes is:</p>
<pre class="prettyprint">
<code>&lt;cluster-connection name="my-cluster"&gt;
&lt;address&gt;jms&lt;/address&gt;
&lt;discovery-group-ref discovery-group-name="dg-group1"/&gt;
&lt;/cluster-connection&gt;
</code>
</pre>
<h2>Example step-by-step</h2>
<p><i>To run the example, simply type <code>mvn verify -Pexample</code> from this directory</i>. This will
automatically start the 3 cluster nodes, each with its specific configuration.</p>
<p>To start the tests <em>manually</em>, the following steps are:</p>
<ul>
<li>create 4 terminals (3 for the servers and 1 for the example client)</li>
<li>in the first terminal, go to the <code>bin</code> directory and start the first server (with default configuration):
<pre class="prettyprint"><code>./run.sh ../config/stand-alone/clustered</code></pre>
<li>in the second terminal, start the second server:
<pre class="prettyprint"><code>export CLUSTER_PROPS="-Ddata.dir=../data-server2 -Djnp.port=2099 -Djnp.rmiPort=2098 -Dactivemq.remoting.netty.port=6445 -Dactivemq.remoting.netty.batch.port=6455"
./run.sh ../config/stand-alone/clustered</code></pre>
<li>in the third terminal, start the third server (with default configuration):
<pre class="prettyprint"><code>export CLUSTER_PROPS="-Ddata.dir=../data-server3 -Djnp.port=3099 -Djnp.rmiPort=3098 -Dactivemq.remoting.netty.port=7445 -Dactivemq.remoting.netty.batch.port=7455"
./run.sh ../config/stand-alone/clustered</code></pre>
<li>finally, in the fourth terminal, start the example
<pre class="prettyprint"><code>./build.sh runRemote</code> (or <code>build.bat runRemote</code> on windows)</pre>
<p>The example connects to the three cluster nodes using JNDI (which are retrieved from
<a href="server0/client-jndi.properties">server0</a>, <a href="server1/client-jndi.properties">server1</a>, and
<a href="server2/client-jndi.properties">server2</a>' s JNDI properties file). The JNDI ports were specified
using the environment property <code>jnp.port</code> (or 1098 by default) when starting the 3 cluster nodes.</p>
<p>For a description of the example code, please read the <a href="../clustered-topic/readme.html">
clustered-topic example</a> which is very similar (it has 2 nodes while this example has 3 nodes).
</body>
</html>

View File

@ -1,181 +0,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.
*/
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.Session;
import javax.jms.TextMessage;
import javax.jms.Topic;
import javax.naming.InitialContext;
import java.util.Hashtable;
public class ClusteredStandaloneExample
{
public static void main(final String[] args) throws Exception
{
Connection connection0 = null;
Connection connection1 = null;
Connection connection2 = null;
InitialContext initialContext0 = null;
InitialContext initialContext1 = null;
InitialContext initialContext2 = null;
try
{
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://localhost:61616");
properties.put("topic.topic/exampleTopic", "exampleTopic");
initialContext0 = new InitialContext(properties);
properties = new Hashtable<String, Object>();
properties.put("java.naming.factory.initial", "org.apache.activemq.artemis.jndi.ActiveMQInitialContextFactory");
properties.put("connectionFactory.ConnectionFactory", "tcp://localhost:61617");
initialContext1 = new InitialContext(properties);
properties = new Hashtable<String, Object>();
properties.put("java.naming.factory.initial", "org.apache.activemq.artemis.jndi.ActiveMQInitialContextFactory");
properties.put("connectionFactory.ConnectionFactory", "tcp://localhost:61618");
initialContext2 = new InitialContext(properties);
// First we demonstrate a distributed topic.
// We create a connection on each node, create a consumer on each connection and send some
// messages at a node and verify they are all received by all consumers
ConnectionFactory cf0 = (ConnectionFactory)initialContext0.lookup("ConnectionFactory");
System.out.println("Got cf " + cf0);
ConnectionFactory cf1 = (ConnectionFactory)initialContext1.lookup("ConnectionFactory");
System.out.println("Got cf " + cf1);
ConnectionFactory cf2 = (ConnectionFactory)initialContext2.lookup("ConnectionFactory");
System.out.println("Got cf " + cf2);
Topic topic = (Topic)initialContext0.lookup("topic/exampleTopic");
connection0 = cf0.createConnection();
connection1 = cf1.createConnection();
connection2 = cf2.createConnection();
connection0.start();
connection1.start();
connection2.start();
Session session0 = connection0.createSession(false, Session.AUTO_ACKNOWLEDGE);
Session session1 = connection1.createSession(false, Session.AUTO_ACKNOWLEDGE);
Session session2 = connection2.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageConsumer messageConsumer0 = session0.createConsumer(topic);
MessageConsumer messageConsumer1 = session1.createConsumer(topic);
MessageConsumer messageConsumer2 = session2.createConsumer(topic);
MessageProducer producer = session0.createProducer(topic);
final int numMessages = 10;
for (int i = 0; i < numMessages; i++)
{
TextMessage message = session0.createTextMessage("Message " + i);
producer.send(message);
}
for (int i = 0; i < numMessages; i++)
{
TextMessage message0 = (TextMessage)messageConsumer0.receive(2000);
if (message0 == null)
{
throw new IllegalStateException();
}
// System.out.println("Received message " + message0.getText());
TextMessage message1 = (TextMessage)messageConsumer1.receive(2000);
if (message1 == null)
{
throw new IllegalStateException();
}
// System.out.println("Received message " + message1.getText());
TextMessage message2 = (TextMessage)messageConsumer2.receive(2000);
if (message2 == null)
{
throw new IllegalStateException();
}
System.out.println("Received message " + message2.getText());
}
producer.close();
messageConsumer0.close();
messageConsumer1.close();
messageConsumer2.close();
}
finally
{
// Step 12. Be sure to close our JMS resources!
if (initialContext0 != null)
{
initialContext0.close();
}
if (initialContext1 != null)
{
initialContext1.close();
}
if (initialContext2 != null)
{
initialContext2.close();
}
if (connection0 != null)
{
connection0.close();
}
if (connection1 != null)
{
connection1.close();
}
if (connection2 != null)
{
connection2.close();
}
}
}
}

View File

@ -1,95 +0,0 @@
<?xml version='1.0'?>
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->
<configuration xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="urn:activemq"
xsi:schemaLocation="urn:activemq /schema/artemis-server.xsd">
<jms xmlns="urn:activemq:jms">
<!--the topic used by the example-->
<topic name="exampleTopic"/>
</jms>
<core xmlns="urn:activemq:core">
<bindings-directory>${data.dir:./data}/bindings</bindings-directory>
<journal-directory>${data.dir:./data}/journal</journal-directory>
<large-messages-directory>${data.dir:./data}/largemessages</large-messages-directory>
<paging-directory>${data.dir:./data}/paging</paging-directory>
<!-- Connectors -->
<connectors>
<connector name="netty-connector">tcp://localhost:61616</connector>
</connectors>
<!-- Acceptors -->
<acceptors>
<acceptor name="netty-acceptor">tcp://localhost:61616</acceptor>
</acceptors>
<!-- Clustering configuration -->
<broadcast-groups>
<broadcast-group name="my-broadcast-group">
<group-address>${udp-address:231.7.7.7}</group-address>
<group-port>9876</group-port>
<broadcast-period>100</broadcast-period>
<connector-ref>netty-connector</connector-ref>
</broadcast-group>
</broadcast-groups>
<discovery-groups>
<discovery-group name="my-discovery-group">
<group-address>${udp-address:231.7.7.7}</group-address>
<group-port>9876</group-port>
<refresh-timeout>10000</refresh-timeout>
</discovery-group>
</discovery-groups>
<cluster-connections>
<cluster-connection name="my-cluster">
<address>jms</address>
<connector-ref>netty-connector</connector-ref>
<retry-interval>500</retry-interval>
<use-duplicate-detection>true</use-duplicate-detection>
<message-load-balancing>STRICT</message-load-balancing>
<max-hops>1</max-hops>
<discovery-group-ref discovery-group-name="my-discovery-group"/>
</cluster-connection>
</cluster-connections>
<!-- Other config -->
<security-settings>
<!--security for example queue-->
<security-setting match="jms.topic.exampleTopic">
<permission type="createDurableQueue" roles="guest"/>
<permission type="deleteDurableQueue" roles="guest"/>
<permission type="createNonDurableQueue" roles="guest"/>
<permission type="deleteNonDurableQueue" roles="guest"/>
<permission type="consume" roles="guest"/>
<permission type="send" roles="guest"/>
</security-setting>
</security-settings>
</core>
</configuration>

View File

@ -1,95 +0,0 @@
<?xml version='1.0'?>
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->
<configuration xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="urn:activemq"
xsi:schemaLocation="urn:activemq /schema/artemis-server.xsd">
<jms xmlns="urn:activemq:jms">
<!--the topic used by the example-->
<topic name="exampleTopic"/>
</jms>
<core xmlns="urn:activemq:core">
<bindings-directory>${data.dir:./data}/bindings</bindings-directory>
<journal-directory>${data.dir:./data}/journal</journal-directory>
<large-messages-directory>${data.dir:./data}/largemessages</large-messages-directory>
<paging-directory>${data.dir:./data}/paging</paging-directory>
<!-- Connectors -->
<connectors>
<connector name="netty-connector">tcp://localhost:61617</connector>
</connectors>
<!-- Acceptors -->
<acceptors>
<acceptor name="netty-acceptor">tcp://localhost:61617</acceptor>
</acceptors>
<!-- Clustering configuration -->
<broadcast-groups>
<broadcast-group name="my-broadcast-group">
<group-address>${udp-address:231.7.7.7}</group-address>
<group-port>9876</group-port>
<broadcast-period>100</broadcast-period>
<connector-ref>netty-connector</connector-ref>
</broadcast-group>
</broadcast-groups>
<discovery-groups>
<discovery-group name="my-discovery-group">
<group-address>${udp-address:231.7.7.7}</group-address>
<group-port>9876</group-port>
<refresh-timeout>10000</refresh-timeout>
</discovery-group>
</discovery-groups>
<cluster-connections>
<cluster-connection name="my-cluster">
<address>jms</address>
<connector-ref>netty-connector</connector-ref>
<retry-interval>500</retry-interval>
<use-duplicate-detection>true</use-duplicate-detection>
<message-load-balancing>STRICT</message-load-balancing>
<max-hops>1</max-hops>
<discovery-group-ref discovery-group-name="my-discovery-group"/>
</cluster-connection>
</cluster-connections>
<!-- Other config -->
<security-settings>
<!--security for example queue-->
<security-setting match="jms.topic.exampleTopic">
<permission type="createDurableQueue" roles="guest"/>
<permission type="deleteDurableQueue" roles="guest"/>
<permission type="createNonDurableQueue" roles="guest"/>
<permission type="deleteNonDurableQueue" roles="guest"/>
<permission type="consume" roles="guest"/>
<permission type="send" roles="guest"/>
</security-setting>
</security-settings>
</core>
</configuration>

View File

@ -1,95 +0,0 @@
<?xml version='1.0'?>
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->
<configuration xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="urn:activemq"
xsi:schemaLocation="urn:activemq /schema/artemis-server.xsd">
<jms xmlns="urn:activemq:jms">
<!--the topic used by the example-->
<topic name="exampleTopic"/>
</jms>
<core xmlns="urn:activemq:core">
<bindings-directory>${data.dir:./data}/bindings</bindings-directory>
<journal-directory>${data.dir:./data}/journal</journal-directory>
<large-messages-directory>${data.dir:./data}/largemessages</large-messages-directory>
<paging-directory>${data.dir:./data}/paging</paging-directory>
<!-- Connectors -->
<connectors>
<connector name="netty-connector">tcp://localhost:61618</connector>
</connectors>
<!-- Acceptors -->
<acceptors>
<acceptor name="netty-acceptor">tcp://localhost:61618</acceptor>
</acceptors>
<!-- Clustering configuration -->
<broadcast-groups>
<broadcast-group name="my-broadcast-group">
<group-address>${udp-address:231.7.7.7}</group-address>
<group-port>9876</group-port>
<broadcast-period>100</broadcast-period>
<connector-ref>netty-connector</connector-ref>
</broadcast-group>
</broadcast-groups>
<discovery-groups>
<discovery-group name="my-discovery-group">
<group-address>${udp-address:231.7.7.7}</group-address>
<group-port>9876</group-port>
<refresh-timeout>10000</refresh-timeout>
</discovery-group>
</discovery-groups>
<cluster-connections>
<cluster-connection name="my-cluster">
<address>jms</address>
<connector-ref>netty-connector</connector-ref>
<retry-interval>500</retry-interval>
<use-duplicate-detection>true</use-duplicate-detection>
<message-load-balancing>STRICT</message-load-balancing>
<max-hops>1</max-hops>
<discovery-group-ref discovery-group-name="my-discovery-group"/>
</cluster-connection>
</cluster-connections>
<!-- Other config -->
<security-settings>
<!--security for example queue-->
<security-setting match="jms.topic.exampleTopic">
<permission type="createDurableQueue" roles="guest"/>
<permission type="deleteDurableQueue" roles="guest"/>
<permission type="createNonDurableQueue" roles="guest"/>
<permission type="deleteNonDurableQueue" roles="guest"/>
<permission type="consume" roles="guest"/>
<permission type="send" roles="guest"/>
</security-setting>
</security-settings>
</core>
</configuration>

View File

@ -18,7 +18,8 @@ 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">
<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>
@ -27,7 +28,7 @@ under the License.
<version>1.0.1-SNAPSHOT</version>
</parent>
<artifactId>artemis-jms-clustered-static-discovery-example</artifactId>
<artifactId>clustered-static-discovery</artifactId>
<packaging>jar</packaging>
<name>ActiveMQ Artemis JMS Clustered Static Discovery Example</name>
@ -42,189 +43,206 @@ under the License.
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-jms_2.0_spec</artifactId>
<groupId>org.apache.activemq</groupId>
<artifactId>artemis-jms-client</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
<profiles>
<profile>
<id>example</id>
<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>
</configuration>
</execution>
<execution>
<id>create1</id>
<goals>
<goal>create</goal>
</goals>
<configuration>
<instance>${basedir}/target/server1</instance>
<configuration>${basedir}/target/classes/activemq/server1</configuration>
</configuration>
</execution>
<execution>
<id>create2</id>
<goals>
<goal>create</goal>
</goals>
<configuration>
<instance>${basedir}/target/server2</instance>
<configuration>${basedir}/target/classes/activemq/server2</configuration>
</configuration>
</execution>
<execution>
<id>create3</id>
<goals>
<goal>create</goal>
</goals>
<configuration>
<instance>${basedir}/target/server3</instance>
<configuration>${basedir}/target/classes/activemq/server3</configuration>
</configuration>
</execution>
<execution>
<id>start0</id>
<goals>
<goal>cli</goal>
</goals>
<configuration>
<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>
<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>start2</id>
<goals>
<goal>cli</goal>
</goals>
<configuration>
<spawn>true</spawn>
<location>${basedir}/target/server2</location>
<testURI>tcp://localhost:61618</testURI>
<args>
<param>run</param>
</args>
<name>server2</name>
</configuration>
</execution>
<execution>
<id>start3</id>
<goals>
<goal>cli</goal>
</goals>
<configuration>
<spawn>true</spawn>
<location>${basedir}/target/server3</location>
<testURI>tcp://localhost:61619</testURI>
<args>
<param>run</param>
</args>
<name>server3</name>
</configuration>
</execution>
<execution>
<id>runClient</id>
<goals>
<goal>runClient</goal>
</goals>
<configuration>
<clientClass>org.apache.activemq.artemis.jms.example.StaticClusteredQueueExample</clientClass>
</configuration>
</execution>
<execution>
<id>stop0</id>
<goals>
<goal>cli</goal>
</goals>
<configuration>
<location>${basedir}/target/server0</location>
<args>
<param>stop</param>
</args>
</configuration>
</execution>
<execution>
<id>stop1</id>
<goals>
<goal>cli</goal>
</goals>
<configuration>
<location>${basedir}/target/server1</location>
<args>
<param>stop</param>
</args>
</configuration>
</execution>
<execution>
<id>stop2</id>
<goals>
<goal>cli</goal>
</goals>
<configuration>
<location>${basedir}/target/server2</location>
<args>
<param>stop</param>
</args>
</configuration>
</execution>
<execution>
<id>stop3</id>
<goals>
<goal>cli</goal>
</goals>
<configuration>
<location>${basedir}/target/server3</location>
<args>
<param>stop</param>
</args>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.apache.activemq.examples.jms</groupId>
<artifactId>artemis-jms-clustered-static-discovery-example</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
<!-- specify -PnoServer if you don't want to start the server -->
<id>noServer</id>
<properties>
<noServer>true</noServer>
</properties>
</profile>
</profiles>
<build>
<plugins>
<plugin>
<groupId>org.apache.activemq</groupId>
<artifactId>artemis-maven-plugin</artifactId>
<executions>
<execution>
<id>create0</id>
<goals>
<goal>create</goal>
</goals>
<configuration>
<ignore>${noServer}</ignore>
<instance>${basedir}/target/server0</instance>
<configuration>${basedir}/target/classes/activemq/server0</configuration>
</configuration>
</execution>
<execution>
<id>create1</id>
<goals>
<goal>create</goal>
</goals>
<configuration>
<ignore>${noServer}</ignore>
<instance>${basedir}/target/server1</instance>
<configuration>${basedir}/target/classes/activemq/server1</configuration>
</configuration>
</execution>
<execution>
<id>create2</id>
<goals>
<goal>create</goal>
</goals>
<configuration>
<ignore>${noServer}</ignore>
<instance>${basedir}/target/server2</instance>
<configuration>${basedir}/target/classes/activemq/server2</configuration>
</configuration>
</execution>
<execution>
<id>create3</id>
<goals>
<goal>create</goal>
</goals>
<configuration>
<ignore>${noServer}</ignore>
<instance>${basedir}/target/server3</instance>
<configuration>${basedir}/target/classes/activemq/server3</configuration>
</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>
<spawn>true</spawn>
<ignore>${noServer}</ignore>
<location>${basedir}/target/server1</location>
<testURI>tcp://localhost:61617</testURI>
<args>
<param>run</param>
</args>
<name>server1</name>
</configuration>
</execution>
<execution>
<id>start2</id>
<goals>
<goal>cli</goal>
</goals>
<configuration>
<ignore>${noServer}</ignore>
<spawn>true</spawn>
<location>${basedir}/target/server2</location>
<testURI>tcp://localhost:61618</testURI>
<args>
<param>run</param>
</args>
<name>server2</name>
</configuration>
</execution>
<execution>
<id>start3</id>
<goals>
<goal>cli</goal>
</goals>
<configuration>
<ignore>${noServer}</ignore>
<spawn>true</spawn>
<location>${basedir}/target/server3</location>
<testURI>tcp://localhost:61619</testURI>
<args>
<param>run</param>
</args>
<name>server3</name>
</configuration>
</execution>
<execution>
<id>runClient</id>
<goals>
<goal>runClient</goal>
</goals>
<configuration>
<clientClass>org.apache.activemq.artemis.jms.example.StaticClusteredQueueExample</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>
<execution>
<id>stop2</id>
<goals>
<goal>cli</goal>
</goals>
<configuration>
<ignore>${noServer}</ignore>
<location>${basedir}/target/server2</location>
<args>
<param>stop</param>
</args>
</configuration>
</execution>
<execution>
<id>stop3</id>
<goals>
<goal>cli</goal>
</goals>
<configuration>
<ignore>${noServer}</ignore>
<location>${basedir}/target/server3</location>
<args>
<param>stop</param>
</args>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.apache.activemq.examples.jms</groupId>
<artifactId>clustered-static-discovery</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>

View File

@ -26,6 +26,7 @@ under the License.
</head>
<body onload="prettyPrint()">
<h1>JMS Load Balanced Static Clustered Queue 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 JMS queue deployed on two different nodes. The two nodes are configured to form a cluster
from a <em>static</em> list of nodes.</p>
@ -53,147 +54,5 @@ under the License.
</pre>
<p>For more information on ActiveMQ Artemis load balancing, and clustering in general, please see the clustering
section of the user manual.</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> Get an initial context for looking up JNDI from server 0.</li>
<pre class="prettyprint">
<code>
ic0 = getContext(0);
</code>
</pre>
<li>Look-up the JMS Queue object from JNDI</li>
<pre class="prettyprint">
<code>Queue queue = (Queue)ic0.lookup("/queue/exampleQueue");</code>
</pre>
<li>Look-up a JMS Connection Factory object from JNDI on server 0</li>
<pre class="prettyprint">
<code>ConnectionFactory cf0 = (ConnectionFactory)ic0.lookup("/ConnectionFactory");</code>
</pre>
<li>Get an initial context for looking up JNDI from server 1.</li>
<pre class="prettyprint">
<code>ic1 = getContext(1);</code>
</pre>
<li>Look-up a JMS Connection Factory object from JNDI on server 1</li>
<pre class="prettyprint">
<code>ConnectionFactory cf1 = (ConnectionFactory)ic1.lookup("/ConnectionFactory");
</code>
</pre>
<li>We create a JMS Connection connection0 which is a connection to server 0</li>
<pre class="prettyprint">
<code>
connection0 = cf0.createConnection();
</code>
</pre>
<li>We create a JMS Connection connection1 which is a connection to server 1</li>
<pre class="prettyprint">
<code>
connection1 = cf1.createConnection();
</code>
</pre>
<li>We create a JMS Session on server 0</li>
<pre class="prettyprint">
<code>
Session session0 = connection0.createSession(false, Session.AUTO_ACKNOWLEDGE);
</code>
</pre>
<li>We create a JMS Session on server 1</li>
<pre class="prettyprint">
<code>
Session session1 = connection1.createSession(false, Session.AUTO_ACKNOWLEDGE);
</code>
</pre>
<li>We start the connections to ensure delivery occurs on them</li>
<pre class="prettyprint">
<code>
connection0.start();
connection1.start();
</code>
</pre>
<li>We create JMS MessageConsumer objects on server 0 and server 1</li>
<pre class="prettyprint">
<code>
MessageConsumer consumer0 = session0.createConsumer(queue);
MessageConsumer consumer1 = session1.createConsumer(queue);
</code>
</pre>
<li>We create a JMS MessageProducer object on server 0.</li>
<pre class="prettyprint">
<code>
MessageProducer producer = session0.createProducer(queue);</code>
</pre>
<li>We send some messages to server 0.</li>
<pre class="prettyprint">
<code>
final int numMessages = 10;
for (int i = 0; i < numMessages; i++)
{
TextMessage message = session0.createTextMessage("This is text message " + i);
producer.send(message);
System.out.println("Sent message: " + message.getText());
}
</code>
</pre>
<li>We now consume those messages on *both* server 0 and server 1.
We note the messages have been distributed between servers in a round robin fashion.
ActiveMQ Artemis has <b>load balanced</b> the messages between the available consumers on the different nodes.
ActiveMQ Artemis can be configured to always load balance messages to all nodes, or to only balance messages
to nodes which have consumers with no or matching selectors. See the user manual for more details.</li>
JMS Queues implement point-to-point message where each message is only ever consumed by a
maximum of one consumer.
<pre class="prettyprint">
<code>
for (int i = 0; i < numMessages; i += 2)
{
TextMessage message0 = (TextMessage)consumer0.receive(5000);
System.out.println("Got message: " + message0.getText() + " from node 0");
TextMessage message1 = (TextMessage)consumer1.receive(5000);
System.out.println("Got message: " + message1.getText() + " from node 1");
}
</code>
</pre>
<li>And finally (no pun intended), <b>always</b> remember to close your JMS resources after use, in a <code>finally</code> block. Closing a JMS connection will automatically close all of its sessions, consumers, producer and browser objects</li>
<pre class="prettyprint">
<code>
finally
{
if (connection0 != null)
{
connection0.close();
}
if (connection1 != null)
{
connection1.close();
}
}
</code>
</pre>
</ol>
</body>
</html>

View File

@ -16,8 +16,6 @@
*/
package org.apache.activemq.artemis.jms.example;
import org.apache.activemq.artemis.util.ServerUtil;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.MessageConsumer;
@ -25,8 +23,10 @@ import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.InitialContext;
import java.util.Hashtable;
import org.apache.activemq.artemis.api.jms.ActiveMQJMSClient;
import org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory;
import org.apache.activemq.artemis.util.ServerUtil;
/**
* A simple example that demonstrates server side load-balancing of messages between the queue instances on different
@ -46,22 +46,13 @@ public class StaticClusteredQueueExample
Connection connection3 = null;
InitialContext ic0 = null;
try
{
// Step 1. Get an initial context for looking up JNDI from server 3
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://localhost:61619");
properties.put("queue.queue/exampleQueue", "exampleQueue");
ic0 = new InitialContext(properties);
// Step 2. Use direct instantiation (or JNDI if you like)
Queue queue = ActiveMQJMSClient.createQueue("exampleQueue");
// Step 2. Look-up the JMS Queue object from JNDI
Queue queue = (Queue)ic0.lookup("queue/exampleQueue");
// Step 3. Look-up a JMS Connection Factory object from JNDI on server 0
ConnectionFactory cf0 = (ConnectionFactory)ic0.lookup("ConnectionFactory");
// Step 3. new JMS Connection Factory object from JNDI on server 3
ConnectionFactory cf0 = new ActiveMQConnectionFactory("tcp://localhost:61619");
//grab an initial connection and wait, in reality you wouldn't do it this way but since we want to ensure an
// equal load balance we do this and then create 4 connections round robined
@ -189,11 +180,6 @@ public class StaticClusteredQueueExample
{
connection3.close();
}
if (ic0 != null)
{
ic0.close();
}
}
}
}

View File

@ -29,13 +29,13 @@ under the License.
<core xmlns="urn:activemq:core">
<bindings-directory>${data.dir:./data}/bindings</bindings-directory>
<bindings-directory>./data/bindings</bindings-directory>
<journal-directory>${data.dir:./data}/journal</journal-directory>
<journal-directory>./data/journal</journal-directory>
<large-messages-directory>${data.dir:./data}/largemessages</large-messages-directory>
<large-messages-directory>./data/largemessages</large-messages-directory>
<paging-directory>${data.dir:./data}/paging</paging-directory>
<paging-directory>./data/paging</paging-directory>
<!-- Connectors -->

View File

@ -29,13 +29,13 @@ under the License.
<core xmlns="urn:activemq:core">
<bindings-directory>${data.dir:./data}/bindings</bindings-directory>
<bindings-directory>./data/bindings</bindings-directory>
<journal-directory>${data.dir:./data}/journal</journal-directory>
<journal-directory>./data/journal</journal-directory>
<large-messages-directory>${data.dir:./data}/largemessages</large-messages-directory>
<large-messages-directory>./data/largemessages</large-messages-directory>
<paging-directory>${data.dir:./data}/paging</paging-directory>
<paging-directory>./data/paging</paging-directory>
<!-- Connectors -->
<connectors>

View File

@ -26,13 +26,13 @@
<core xmlns="urn:activemq:core">
<bindings-directory>${data.dir:./data}/bindings</bindings-directory>
<bindings-directory>./data/bindings</bindings-directory>
<journal-directory>${data.dir:./data}/journal</journal-directory>
<journal-directory>./data/journal</journal-directory>
<large-messages-directory>${data.dir:./data}/largemessages</large-messages-directory>
<large-messages-directory>./data/largemessages</large-messages-directory>
<paging-directory>${data.dir:./data}/paging</paging-directory>
<paging-directory>./data/paging</paging-directory>
<!-- Connectors -->
<connectors>

View File

@ -26,13 +26,13 @@
<core xmlns="urn:activemq:core">
<bindings-directory>${data.dir:./data}/bindings</bindings-directory>
<bindings-directory>./data/bindings</bindings-directory>
<journal-directory>${data.dir:./data}/journal</journal-directory>
<journal-directory>./data/journal</journal-directory>
<large-messages-directory>${data.dir:./data}/largemessages</large-messages-directory>
<large-messages-directory>./data/largemessages</large-messages-directory>
<paging-directory>${data.dir:./data}/paging</paging-directory>
<paging-directory>./data/paging</paging-directory>
<!-- Connectors -->
<connectors>

View File

@ -18,7 +18,8 @@ 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">
<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>
@ -27,7 +28,7 @@ under the License.
<version>1.0.1-SNAPSHOT</version>
</parent>
<artifactId>artemis-jms-clustered-static-oneway-example</artifactId>
<artifactId>clustered-static-oneway</artifactId>
<packaging>jar</packaging>
<name>ActiveMQ Artemis JMS Clustered Static One Way Example</name>
@ -42,151 +43,165 @@ under the License.
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-jms_2.0_spec</artifactId>
<groupId>org.apache.activemq</groupId>
<artifactId>artemis-jms-client</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
<profiles>
<profile>
<id>example</id>
<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>
</configuration>
</execution>
<execution>
<id>create1</id>
<goals>
<goal>create</goal>
</goals>
<configuration>
<instance>${basedir}/target/server1</instance>
<configuration>${basedir}/target/classes/activemq/server1</configuration>
</configuration>
</execution>
<execution>
<id>create2</id>
<goals>
<goal>create</goal>
</goals>
<configuration>
<instance>${basedir}/target/server2</instance>
<configuration>${basedir}/target/classes/activemq/server2</configuration>
</configuration>
</execution>
<execution>
<id>start0</id>
<goals>
<goal>cli</goal>
</goals>
<configuration>
<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>
<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>start2</id>
<goals>
<goal>cli</goal>
</goals>
<configuration>
<spawn>true</spawn>
<location>${basedir}/target/server2</location>
<testURI>tcp://localhost:61618</testURI>
<args>
<param>run</param>
</args>
<name>server2</name>
</configuration>
</execution>
<execution>
<id>runClient</id>
<goals>
<goal>runClient</goal>
</goals>
<configuration>
<clientClass>org.apache.activemq.artemis.jms.example.ClusterStaticOnewayExample</clientClass>
</configuration>
</execution>
<execution>
<id>stop0</id>
<goals>
<goal>cli</goal>
</goals>
<configuration>
<location>${basedir}/target/server0</location>
<args>
<param>stop</param>
</args>
</configuration>
</execution>
<execution>
<id>stop1</id>
<goals>
<goal>cli</goal>
</goals>
<configuration>
<location>${basedir}/target/server1</location>
<args>
<param>stop</param>
</args>
</configuration>
</execution>
<execution>
<id>stop2</id>
<goals>
<goal>cli</goal>
</goals>
<configuration>
<location>${basedir}/target/server2</location>
<args>
<param>stop</param>
</args>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.apache.activemq.examples.jms</groupId>
<artifactId>artemis-jms-clustered-static-oneway-example</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
<!-- specify -PnoServer if you don't want to start the server -->
<id>noServer</id>
<properties>
<noServer>true</noServer>
</properties>
</profile>
</profiles>
<build>
<plugins>
<plugin>
<groupId>org.apache.activemq</groupId>
<artifactId>artemis-maven-plugin</artifactId>
<executions>
<execution>
<id>create0</id>
<goals>
<goal>create</goal>
</goals>
<configuration>
<ignore>${noServer}</ignore>
<instance>${basedir}/target/server0</instance>
<configuration>${basedir}/target/classes/activemq/server0</configuration>
</configuration>
</execution>
<execution>
<id>create1</id>
<goals>
<goal>create</goal>
</goals>
<configuration>
<ignore>${noServer}</ignore>
<instance>${basedir}/target/server1</instance>
<configuration>${basedir}/target/classes/activemq/server1</configuration>
</configuration>
</execution>
<execution>
<id>create2</id>
<goals>
<goal>create</goal>
</goals>
<configuration>
<ignore>${noServer}</ignore>
<instance>${basedir}/target/server2</instance>
<configuration>${basedir}/target/classes/activemq/server2</configuration>
</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>start2</id>
<goals>
<goal>cli</goal>
</goals>
<configuration>
<ignore>${noServer}</ignore>
<spawn>true</spawn>
<location>${basedir}/target/server2</location>
<testURI>tcp://localhost:61618</testURI>
<args>
<param>run</param>
</args>
<name>server2</name>
</configuration>
</execution>
<execution>
<id>runClient</id>
<goals>
<goal>runClient</goal>
</goals>
<configuration>
<clientClass>org.apache.activemq.artemis.jms.example.ClusterStaticOnewayExample</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>
<execution>
<id>stop2</id>
<goals>
<goal>cli</goal>
</goals>
<configuration>
<ignore>${noServer}</ignore>
<location>${basedir}/target/server2</location>
<args>
<param>stop</param>
</args>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.apache.activemq.examples.jms</groupId>
<artifactId>clustered-static-oneway</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>

View File

@ -26,6 +26,7 @@ under the License.
</head>
<body onload="prettyPrint()">
<h1>JMS Load Balanced Static Clustered One Way Queue 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 JMS queue deployed on three different nodes. The three nodes are configured to form a one way cluster
from a <em>static</em> list of nodes. </p>
@ -59,190 +60,5 @@ under the License.
</pre>
<p>For more information on ActiveMQ Artemis load balancing, and clustering in general, please see the clustering
section of the user manual.</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> Get an initial context for looking up JNDI from server 0.</li>
<pre class="prettyprint">
<code>
ic0 = getContext(0);
</code>
</pre>
<li>Look-up the JMS Queue object from JNDI</li>
<pre class="prettyprint">
<code>
Queue queue = (Queue)ic0.lookup("/queue/exampleQueue");
</code>
</pre>
<li>Look-up a JMS Connection Factory object from JNDI on server 0</li>
<pre class="prettyprint">
<code>
ConnectionFactory cf0 = (ConnectionFactory)ic0.lookup("/ConnectionFactory");
</code>
</pre>
<li>grab an initial connection and wait, in reality you wouldn't do it this way but since we want to ensure an
equal load balance we do this and then create 4 connections round robined</li>
<pre class="prettyprint">
<code>
initialConnection = cf0.createConnection();
</code>
</pre>
<li>We create a JMS Connection connection0 which is a connection to server 0</li>
<pre class="prettyprint">
<code>
connection0 = cf0.createConnection()
</code>
</pre>
<li>We create a JMS Connection connection0 which is a connection to server 1</li>
<pre class="prettyprint">
<code>
connection1 = cf0.createConnection()
</code>
</pre>
<li>We create a JMS Connection connection0 which is a connection to server 2</li>
<pre class="prettyprint">
<code>
connection2 = cf0.createConnection()
</code>
</pre>
<li>We create a JMS Session on server 0</li>
<pre class="prettyprint">
<code>
Session session0 = connection0.createSession(false, Session.AUTO_ACKNOWLEDGE);
</code>
</pre>
<li>We create a JMS Session on server 1</li>
<pre class="prettyprint">
<code>
Session session1 = connection1.createSession(false, Session.AUTO_ACKNOWLEDGE);
</code>
</pre>
<li>We create a JMS Session on server 1</li>
<pre class="prettyprint">
<code>
Session session2 = connection2.createSession(false, Session.AUTO_ACKNOWLEDGE);
</code>
</pre>
<li>We start the connections to ensure delivery occurs on them</li>
<pre class="prettyprint">
<code>
connection0.start();
connection1.start();
connection2.start();
</code>
</pre>
<li>We create JMS MessageConsumer objects on server 0 and server 1</li>
<pre class="prettyprint">
<code>
MessageConsumer consumer0 = session0.createConsumer(queue);
MessageConsumer consumer2 = session2.createConsumer(queue);
MessageConsumer consumer3 = session3.createConsumer(queue);
</code>
</pre>
<li>We create a JMS MessageProducer object on server 0.</li>
<pre class="prettyprint">
<code>
Session sendSession = getServerConnection(0, connection0, connection1, connection2).createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer producer = sendSession.createProducer(queue);
</code>
</pre>
<li>We send some messages to server 0.</li>
<pre class="prettyprint">
<code>
final int numMessages = 18;
for (int i = 0; i < numMessages; i++)
{
TextMessage message = session0.createTextMessage("This is text message " + i);
producer.send(message);
System.out.println("Sent message: " + message.getText());
}
</code>
</pre>
<li>We now consume those messages on *both* server 0 and server 1.
We note the messages have been distributed between servers in a round robin fashion.
ActiveMQ Artemis has <b>load balanced</b> the messages between the available consumers on the different nodes.
ActiveMQ Artemis can be configured to always load balance messages to all nodes, or to only balance messages
to nodes which have consumers with no or matching selectors. See the user manual for more details.</li>
JMS Queues implement point-to-point message where each message is only ever consumed by a
maximum of one consumer.
<pre class="prettyprint">
<code>
for (int i = 0; i < numMessages; i += 2)
{
TextMessage message0 = (TextMessage)consumer0.receive(5000);
System.out.println("Got message: " + message0.getText() + " from node 0");
TextMessage message1 = (TextMessage)consumer1.receive(5000);
System.out.println("Got message: " + message1.getText() + " from node 1");
TextMessage message2 = (TextMessage)consumer2.receive(5000);
System.out.println("Got message: " + message2.getText() + " from node " + con2Node);
}
</code>
</pre>
<li>And finally (no pun intended), <b>always</b> remember to close your JMS resources after use, in a <code>finally</code> block. Closing a JMS connection will automatically close all of its sessions, consumers, producer and browser objects</li>
<pre class="prettyprint">
<code>
finally
{
if (initialConnection != null)
{
initialConnection.close();
}
if (connection0 != null)
{
connection0.close();
}
if (connection1 != null)
{
connection1.close();
}
if (connection2 != null)
{
connection2.close();
}
if (ic0 != null)
{
ic0.close();
}
}
</code>
</pre>
</ol>
</body>
</html>

View File

@ -16,8 +16,6 @@
*/
package org.apache.activemq.artemis.jms.example;
import org.apache.activemq.artemis.util.ServerUtil;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.MessageConsumer;
@ -25,8 +23,10 @@ import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.InitialContext;
import java.util.Hashtable;
import org.apache.activemq.artemis.api.jms.ActiveMQJMSClient;
import org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory;
import org.apache.activemq.artemis.util.ServerUtil;
/**
* A simple example that demonstrates server side load-balancing of messages between the queue instances on different
@ -44,22 +44,13 @@ public class ClusterStaticOnewayExample
Connection connection2 = null;
InitialContext ic0 = null;
try
{
// Step 1. Get an initial context for looking up JNDI from server 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://localhost:61616");
properties.put("queue.queue/exampleQueue", "exampleQueue");
ic0 = new InitialContext(properties);
// Step 2. Look-up the JMS Queue object from JNDI
Queue queue = (Queue)ic0.lookup("queue/exampleQueue");
// Step 2. Instantiate Queue
Queue queue = ActiveMQJMSClient.createQueue("exampleQueue");
// Step 3. Look-up a JMS Connection Factory object from JNDI on server 0
ConnectionFactory cf0 = (ConnectionFactory)ic0.lookup("ConnectionFactory");
ConnectionFactory cf0 = new ActiveMQConnectionFactory("tcp://localhost:61616");
//step 4. grab an initial connection and wait, in reality you wouldn't do it this way but since we want to ensure an
// equal load balance we do this and then create 4 connections round robined
@ -173,11 +164,6 @@ public class ClusterStaticOnewayExample
{
connection2.close();
}
if (ic0 != null)
{
ic0.close();
}
}
}
}

View File

@ -26,13 +26,13 @@
<core xmlns="urn:activemq:core">
<bindings-directory>${data.dir:./data}/bindings</bindings-directory>
<bindings-directory>./data/bindings</bindings-directory>
<journal-directory>${data.dir:./data}/journal</journal-directory>
<journal-directory>./data/journal</journal-directory>
<large-messages-directory>${data.dir:./data}/largemessages</large-messages-directory>
<large-messages-directory>./data/largemessages</large-messages-directory>
<paging-directory>${data.dir:./data}/paging</paging-directory>
<paging-directory>./data/paging</paging-directory>
<!-- Connectors -->

View File

@ -26,13 +26,13 @@
<core xmlns="urn:activemq:core">
<bindings-directory>${data.dir:./data}/bindings</bindings-directory>
<bindings-directory>./data/bindings</bindings-directory>
<journal-directory>${data.dir:./data}/journal</journal-directory>
<journal-directory>./data/journal</journal-directory>
<large-messages-directory>${data.dir:./data}/largemessages</large-messages-directory>
<large-messages-directory>./data/largemessages</large-messages-directory>
<paging-directory>${data.dir:./data}/paging</paging-directory>
<paging-directory>./data/paging</paging-directory>
<!-- Connectors -->
<connectors>

View File

@ -26,13 +26,13 @@
<core xmlns="urn:activemq:core">
<bindings-directory>${data.dir:./data}/bindings</bindings-directory>
<bindings-directory>./data/bindings</bindings-directory>
<journal-directory>${data.dir:./data}/journal</journal-directory>
<journal-directory>./data/journal</journal-directory>
<large-messages-directory>${data.dir:./data}/largemessages</large-messages-directory>
<large-messages-directory>./data/largemessages</large-messages-directory>
<paging-directory>${data.dir:./data}/paging</paging-directory>
<paging-directory>./data/paging</paging-directory>
<!-- Connectors -->
<connectors>

View File

@ -18,7 +18,8 @@ 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">
<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>
@ -27,7 +28,7 @@ under the License.
<version>1.0.1-SNAPSHOT</version>
</parent>
<artifactId>artemis-jms-clustered-topic-example</artifactId>
<artifactId>clustered-topic</artifactId>
<packaging>jar</packaging>
<name>ActiveMQ Artemis JMS Clustered Topic Example</name>
@ -37,115 +38,125 @@ under the License.
<dependencies>
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-jms_2.0_spec</artifactId>
<groupId>org.apache.activemq</groupId>
<artifactId>artemis-jms-client</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
<profiles>
<profile>
<id>example</id>
<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>
</configuration>
</execution>
<execution>
<id>create1</id>
<goals>
<goal>create</goal>
</goals>
<configuration>
<instance>${basedir}/target/server1</instance>
<configuration>${basedir}/target/classes/activemq/server1</configuration>
</configuration>
</execution>
<execution>
<id>start0</id>
<goals>
<goal>cli</goal>
</goals>
<configuration>
<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>
<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.ClusteredTopicExample</clientClass>
</configuration>
</execution>
<execution>
<id>stop0</id>
<goals>
<goal>cli</goal>
</goals>
<configuration>
<location>${basedir}/target/server0</location>
<args>
<param>stop</param>
</args>
</configuration>
</execution>
<execution>
<id>stop1</id>
<goals>
<goal>cli</goal>
</goals>
<configuration>
<location>${basedir}/target/server1</location>
<args>
<param>stop</param>
</args>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.apache.activemq.examples.jms</groupId>
<artifactId>artemis-jms-clustered-topic-example</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
<!-- specify -PnoServer if you don't want to start the server -->
<id>noServer</id>
<properties>
<noServer>true</noServer>
</properties>
</profile>
</profiles>
<build>
<plugins>
<plugin>
<groupId>org.apache.activemq</groupId>
<artifactId>artemis-maven-plugin</artifactId>
<executions>
<execution>
<id>create0</id>
<goals>
<goal>create</goal>
</goals>
<configuration>
<ignore>${noServer}</ignore>
<instance>${basedir}/target/server0</instance>
<configuration>${basedir}/target/classes/activemq/server0</configuration>
</configuration>
</execution>
<execution>
<id>create1</id>
<goals>
<goal>create</goal>
</goals>
<configuration>
<ignore>${noServer}</ignore>
<instance>${basedir}/target/server1</instance>
<configuration>${basedir}/target/classes/activemq/server1</configuration>
</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.ClusteredTopicExample</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.jms</groupId>
<artifactId>clustered-topic</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>

View File

@ -26,6 +26,7 @@ under the License.
</head>
<body onload="prettyPrint()">
<h1>JMS Clustered Topic 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 JMS Topic deployed on two different nodes. The two nodes are configured to form a cluster.</p>
<p>We then create a subscriber on the topic on each node, and we create a producer on only one of the nodes.</p>
@ -50,144 +51,5 @@ under the License.
</pre>
<p>For more information on ActiveMQ Artemis load balancing, and clustering in general, please see the clustering
section of the user manual.</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> Get an initial context for looking up JNDI from server 0.</li>
<pre class="prettyprint">
<code>
ic0 = getContext(0);
</code>
</pre>
<li>Look-up the JMS Topic object from JNDI</li>
<pre class="prettyprint">
<code>Topic topic = (Topic)ic0.lookup("/topic/exampleTopic");</code>
</pre>
<li>Look-up a JMS Connection Factory object from JNDI on server 0</li>
<pre class="prettyprint">
<code>ConnectionFactory cf0 = (ConnectionFactory)ic0.lookup("/ConnectionFactory");</code>
</pre>
<li>Get an initial context for looking up JNDI from server 1.</li>
<pre class="prettyprint">
<code>ic1 = getContext(1);</code>
</pre>
<li>Look-up a JMS Connection Factory object from JNDI on server 1</li>
<pre class="prettyprint">
<code>ConnectionFactory cf1 = (ConnectionFactory)ic1.lookup("/ConnectionFactory");
</code>
</pre>
<li>We create a JMS Connection connection0 which is a connection to server 0</li>
<pre class="prettyprint">
<code>
connection0 = cf0.createConnection();
</code>
</pre>
<li>We create a JMS Connection connection1 which is a connection to server 1</li>
<pre class="prettyprint">
<code>
connection1 = cf1.createConnection();
</code>
</pre>
<li>We create a JMS Session on server 0</li>
<pre class="prettyprint">
<code>
Session session0 = connection0.createSession(false, Session.AUTO_ACKNOWLEDGE);
</code>
</pre>
<li>We create a JMS Session on server 1</li>
<pre class="prettyprint">
<code>
Session session1 = connection1.createSession(false, Session.AUTO_ACKNOWLEDGE);
</code>
</pre>
<li>We start the connections to ensure delivery occurs on them</li>
<pre class="prettyprint">
<code>
connection0.start();
connection1.start();
</code>
</pre>
<li>We create JMS MessageConsumer (Topic subscriber) objects on server 0 and server 1</li>
<pre class="prettyprint">
<code>
MessageConsumer consumer0 = session0.createConsumer(topic);
MessageConsumer consumer1 = session1.createConsumer(topic);
</code>
</pre>
<li>We create a JMS MessageProducer object on server 0.</li>
<pre class="prettyprint">
<code>
MessageProducer producer = session0.createProducer(topic);</code>
</pre>
<li>We send some messages to server 0.</li>
<pre class="prettyprint">
<code>
final int numMessages = 10;
for (int i = 0; i < numMessages; i++)
{
TextMessage message = session0.createTextMessage("This is text message " + i);
producer.send(message);
System.out.println("Sent message: " + message.getText());
}
</code>
</pre>
<li>
We now consume those messages on <b>both</b> server 0 and server 1.
We note that all messages have been consumed by <b>both</b> consumers.
JMS Topics implement <b>publish-subscribe</b> messaging where all consumers get a copy of all messages.
<pre class="prettyprint">
<code>
for (int i = 0; i < numMessages; i ++)
{
TextMessage message0 = (TextMessage)consumer0.receive(5000);
System.out.println("Got message: " + message0.getText() + " from node 0");
TextMessage message1 = (TextMessage)consumer1.receive(5000);
System.out.println("Got message: " + message1.getText() + " from node 1");
}
</code>
</pre>
<li>And finally (no pun intended), <b>always</b> remember to close your JMS resources after use, in a <code>finally</code> block. Closing a JMS connection will automatically close all of its sessions, consumers, producer and browser objects</li>
<pre class="prettyprint">
<code>
finally
{
if (connection0 != null)
{
connection0.close();
}
if (connection1 != null)
{
connection1.close();
}
}
</code>
</pre>
</ol>
</body>
</html>

View File

@ -24,7 +24,9 @@ import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.Topic;
import javax.naming.InitialContext;
import java.util.Hashtable;
import org.apache.activemq.artemis.api.jms.ActiveMQJMSClient;
import org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory;
/**
* A simple example that shows a JMS Topic clustered across two nodes of a cluster.
@ -44,56 +46,44 @@ public class ClusteredTopicExample
try
{
// Step 1. Get an initial context for looking up JNDI from server 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://localhost:61616");
properties.put("topic.topic/exampleTopic", "exampleTopic");
ic0 = new InitialContext(properties);
// Step 2. Look-up the JMS Topic object from JNDI
Topic topic = (Topic)ic0.lookup("topic/exampleTopic");
// Step 1. Instantiate topic
Topic topic = ActiveMQJMSClient.createTopic("exampleTopic");
// Step 3. Look-up a JMS Connection Factory object from JNDI on server 0
ConnectionFactory cf0 = (ConnectionFactory)ic0.lookup("ConnectionFactory");
// Step 2. Look-up a JMS Connection Factory object from JNDI on server 0
ConnectionFactory cf0 = new ActiveMQConnectionFactory("tcp://localhost:61616");
// Step 4. Get an initial context for looking up JNDI from server 1
properties = new Hashtable<String, Object>();
properties.put("java.naming.factory.initial", "org.apache.activemq.artemis.jndi.ActiveMQInitialContextFactory");
properties.put("connectionFactory.ConnectionFactory", "tcp://localhost:61617");
ic1 = new InitialContext(properties);
// Step 3. Look-up a JMS Connection Factory object from JNDI on server 1
ConnectionFactory cf1 = new ActiveMQConnectionFactory("tcp://localhost:61617");
// Step 5. Look-up a JMS Connection Factory object from JNDI on server 1
ConnectionFactory cf1 = (ConnectionFactory)ic1.lookup("ConnectionFactory");
// Step 6. We create a JMS Connection connection0 which is a connection to server 0
// Step 4. We create a JMS Connection connection0 which is a connection to server 0
connection0 = cf0.createConnection();
// Step 7. We create a JMS Connection connection1 which is a connection to server 1
// Step 5. We create a JMS Connection connection1 which is a connection to server 1
connection1 = cf1.createConnection();
// Step 8. We create a JMS Session on server 0
// Step 6. We create a JMS Session on server 0
Session session0 = connection0.createSession(false, Session.AUTO_ACKNOWLEDGE);
// Step 9. We create a JMS Session on server 1
// Step 7. We create a JMS Session on server 1
Session session1 = connection1.createSession(false, Session.AUTO_ACKNOWLEDGE);
// Step 10. We start the connections to ensure delivery occurs on them
// Step 8. We start the connections to ensure delivery occurs on them
connection0.start();
connection1.start();
// Step 11. We create JMS MessageConsumer objects on server 0 and server 1
// Step 9. We create JMS MessageConsumer objects on server 0 and server 1
MessageConsumer consumer0 = session0.createConsumer(topic);
MessageConsumer consumer1 = session1.createConsumer(topic);
Thread.sleep(1000);
// Step 12. We create a JMS MessageProducer object on server 0
// Step 10. We create a JMS MessageProducer object on server 0
MessageProducer producer = session0.createProducer(topic);
// Step 13. We send some messages to server 0
// Step 11. We send some messages to server 0
final int numMessages = 10;
@ -106,7 +96,7 @@ public class ClusteredTopicExample
System.out.println("Sent message: " + message.getText());
}
// Step 14. We now consume those messages on *both* server 0 and server 1.
// Step 12. We now consume those messages on *both* server 0 and server 1.
// We note that all messages have been consumed by *both* consumers.
// JMS Topics implement *publish-subscribe* messaging where all consumers get a copy of all messages

View File

@ -29,13 +29,13 @@ under the License.
<core xmlns="urn:activemq:core">
<bindings-directory>${data.dir:./data}/bindings</bindings-directory>
<bindings-directory>./data/bindings</bindings-directory>
<journal-directory>${data.dir:./data}/journal</journal-directory>
<journal-directory>./data/journal</journal-directory>
<large-messages-directory>${data.dir:./data}/largemessages</large-messages-directory>
<large-messages-directory>./data/largemessages</large-messages-directory>
<paging-directory>${data.dir:./data}/paging</paging-directory>
<paging-directory>./data/paging</paging-directory>
<!-- Connectors -->

View File

@ -29,13 +29,13 @@ under the License.
<core xmlns="urn:activemq:core">
<bindings-directory>${data.dir:./data}/bindings</bindings-directory>
<bindings-directory>./data/bindings</bindings-directory>
<journal-directory>${data.dir:./data}/journal</journal-directory>
<journal-directory>./data/journal</journal-directory>
<large-messages-directory>${data.dir:./data}/largemessages</large-messages-directory>
<large-messages-directory>./data/largemessages</large-messages-directory>
<paging-directory>${data.dir:./data}/paging</paging-directory>
<paging-directory>./data/paging</paging-directory>
<!-- Connectors -->
<connectors>

View File

@ -18,7 +18,8 @@ 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">
<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>
@ -42,65 +43,61 @@ under the License.
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-jms_2.0_spec</artifactId>
<groupId>org.apache.activemq</groupId>
<artifactId>artemis-jms-client</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
<profiles>
<profile>
<id>example</id>
<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>
</configuration>
</execution>
<execution>
<id>create1</id>
<goals>
<goal>create</goal>
</goals>
<configuration>
<instance>${basedir}/target/server1</instance>
<configuration>${basedir}/target/classes/activemq/server1</configuration>
</configuration>
</execution>
<execution>
<id>runClient</id>
<goals>
<goal>runClient</goal>
</goals>
<configuration>
<clientClass>org.apache.activemq.artemis.jms.example.ColocatedFailoverScaleDownExample</clientClass>
<args>
<param>${basedir}/target/server0</param>
<param>${basedir}/target/server1</param>
</args>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.apache.activemq.examples.jms</groupId>
<artifactId>colocated-failover-scale-down</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</profile>
</profiles>
<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>
</configuration>
</execution>
<execution>
<id>create1</id>
<goals>
<goal>create</goal>
</goals>
<configuration>
<instance>${basedir}/target/server1</instance>
<configuration>${basedir}/target/classes/activemq/server1</configuration>
</configuration>
</execution>
<execution>
<id>runClient</id>
<goals>
<goal>runClient</goal>
</goals>
<configuration>
<clientClass>org.apache.activemq.artemis.jms.example.ColocatedFailoverScaleDownExample
</clientClass>
<args>
<param>${basedir}/target/server0</param>
<param>${basedir}/target/server1</param>
</args>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.apache.activemq.examples.jms</groupId>
<artifactId>colocated-failover-scale-down</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>

View File

@ -26,6 +26,8 @@ under the License.
</head>
<body onload="prettyPrint()">
<h1>JMS Colocated Failover Recover Only Example</h1>
<pre>To run the example, simply type <b>mvn verify</b> from this directory. This example will always spawn and stop multiple servers.</pre>
<p>This example demonstrates how you can colocate live and backup servers in the same VM. We do this by creating an
HA Policy that is colocated. colocated means that backup servers can be created and maintained by live servers on behalf
@ -59,133 +61,5 @@ under the License.
from the list of available connectors which in this case is the first INVM connector</p>
<p> One other thing to notice is that the cluster connection has its reconnect attempts set to 5, this is so it will
disconnect instead of trying to reconnect to a backup that doesn't exist.</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> Get an initial context for looking up JNDI for both servers</li>
<pre class="prettyprint">
<code>
initialContext1 = getContext(1);
initialContext = getContext(0);
</code>
</pre>
<li>Look up the JMS resources from JNDI</li>
<pre class="prettyprint">
<code>
Queue queue = (Queue)initialContext.lookup("/queue/exampleQueue");
ConnectionFactory connectionFactory = (ConnectionFactory)initialContext.lookup("/ConnectionFactory");
ConnectionFactory connectionFactory1 = (ConnectionFactory)initialContext1.lookup("/ConnectionFactory");
</code>
</pre>
<li>Create a JMS Connections</li>
<pre class="prettyprint">
<code>
connection = connectionFactory.createConnection();
connection1 = connectionFactory1.createConnection();
</code>
</pre>
<li>Create a *non-transacted* JMS Session with client acknowledgement</li>
<pre class="prettyprint">
<code>
Session session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
Session session1 = connection1.createSession(false, Session.CLIENT_ACKNOWLEDGE);
</code>
</pre>
<li>Create a JMS MessageProducer</li>
<pre class="prettyprint">
<code>
MessageProducer producer = session.createProducer(queue);
MessageProducer producer1 = session1.createProducer(queue);
</code>
</pre>
<li>Send some messages to both servers</li>
<pre class="prettyprint">
<code>
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());
message = session1.createTextMessage("This is another text message " + i);
producer1.send(message);
System.out.println("Sent message: " + message.getText());
}
</code>
</pre>
<li>Crash server #0, the live server</li>
<pre class="prettyprint">
<code>
killServer(0);
</code>
</pre>
<li>start the connection ready to receive messages</li>
<pre class="prettyprint">
<code>
connection1.start();
</code>
</pre>
<li>create a consumer</li>
<pre class="prettyprint">
<code>
MessageConsumer consumer = session1.createConsumer(queue);
</code>
</pre>
<li>Receive and acknowledge all of the sent messages, notice that they will be out of order, this is
because they were initially round robined to both nodes then when the server failed were reloaded into the
live server.</li>
<pre class="prettyprint">
<code>
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();
</code>
</pre>
</pre>
<li>And finally (no pun intended), <b>always</b> remember to close your JMS resources after use, in a <code>finally</code> block. Closing a JMS connection will automatically close all of its sessions, consumers, producer and browser objects</li>
<pre class="prettyprint">
<code>
finally
{
if (connection != null)
{
connection.close();
}
if (initialContext != null)
{
initialContext.close();
}
if (connection1 != null)
{
connection1.close();
}
if (initialContext1 != null)
{
initialContext1.close();
}
}
</code>
</pre>
</ol>
</body>
</html>

View File

@ -18,7 +18,8 @@ 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">
<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>
@ -42,64 +43,60 @@ under the License.
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-jms_2.0_spec</artifactId>
<groupId>org.apache.activemq</groupId>
<artifactId>artemis-jms-client</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
<profiles>
<profile>
<id>example</id>
<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>
</configuration>
</execution>
<execution>
<id>create1</id>
<goals>
<goal>create</goal>
</goals>
<configuration>
<instance>${basedir}/target/server1</instance>
<configuration>${basedir}/target/classes/activemq/server1</configuration>
</configuration>
</execution>
<execution>
<id>runClient</id>
<goals>
<goal>runClient</goal>
</goals>
<configuration>
<clientClass>org.apache.activemq.artemis.jms.example.ColocatedFailoverExample</clientClass>
<args>
<param>${basedir}/target/server0</param>
<param>${basedir}/target/server1</param>
</args>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.apache.activemq.examples.jms</groupId>
<artifactId>colocated-failover</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</profile>
</profiles>
<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>
</configuration>
</execution>
<execution>
<id>create1</id>
<goals>
<goal>create</goal>
</goals>
<configuration>
<instance>${basedir}/target/server1</instance>
<configuration>${basedir}/target/classes/activemq/server1</configuration>
</configuration>
</execution>
<execution>
<id>runClient</id>
<goals>
<goal>runClient</goal>
</goals>
<configuration>
<clientClass>org.apache.activemq.artemis.jms.example.ColocatedFailoverExample</clientClass>
<args>
<param>${basedir}/target/server0</param>
<param>${basedir}/target/server1</param>
</args>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.apache.activemq.examples.jms</groupId>
<artifactId>colocated-failover</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>

View File

@ -26,6 +26,7 @@ under the License.
</head>
<body onload="prettyPrint()">
<h1>JMS Colocated Failover Shared Store Example</h1>
<pre>To run the example, simply type <b>mvn verify</b> from this directory. This example will always spawn and stop multiple servers.</pre>
<p>This example demonstrates how you can colocate live and backup servers in the same VM. We do this by creating an
HA Policy that is colocated. colocated means that backup servers can be created and maintained by live servers on behalf
@ -51,143 +52,5 @@ under the License.
</pre>
<p>notice that we have used a template to set some sensible defaults but overridden the backup strategy so back ups
are full servers</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> Get an initial context for looking up JNDI for both servers</li>
<pre class="prettyprint">
<code>
initialContext1 = getContext(1);
initialContext = getContext(0);
</code>
</pre>
<li>Look up the JMS resources from JNDI</li>
<pre class="prettyprint">
<code>
Queue queue = (Queue)initialContext.lookup("/queue/exampleQueue");
ConnectionFactory connectionFactory = (ConnectionFactory)initialContext.lookup("/ConnectionFactory");
ConnectionFactory connectionFactory1 = (ConnectionFactory)initialContext1.lookup("/ConnectionFactory");
</code>
</pre>
<li>Create a JMS Connections</li>
<pre class="prettyprint">
<code>
connection = connectionFactory.createConnection();
connection1 = connectionFactory1.createConnection();
</code>
</pre>
<li>Create a *non-transacted* JMS Session with client acknowledgement</li>
<pre class="prettyprint">
<code>
Session session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
Session session1 = connection1.createSession(false, Session.CLIENT_ACKNOWLEDGE);
</code>
</pre>
<li>Create a JMS MessageProducer</li>
<pre class="prettyprint">
<code>
MessageProducer producer = session.createProducer(queue);
MessageProducer producer1 = session1.createProducer(queue);
</code>
</pre>
<li>Send some messages to both servers</li>
<pre class="prettyprint">
<code>
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());
message = session1.createTextMessage("This is another text message " + i);
producer1.send(message);
System.out.println("Sent message: " + message.getText());
}
</code>
</pre>
<li>Crash server #0, the live server</li>
<pre class="prettyprint">
<code>
killServer(0);
</code>
</pre>
<li>start the connection ready to receive messages</li>
<pre class="prettyprint">
<code>
connection1.start();
</code>
</pre>
<li>create a consumer</li>
<pre class="prettyprint">
<code>
MessageConsumer consumer = session1.createConsumer(queue);
</code>
</pre>
<li>Receive and acknowledge all of the sent messages, the backup server that is colocated with server 1
will have become live and is now handling messages for server 0.</li>
<pre class="prettyprint">
<code>
TextMessage message0 = null;
for (int i = 0; i < numMessages; i++)
{
message0 = (TextMessage)consumer.receive(5000);
System.out.println("Got message: " + message0.getText());
}
message0.acknowledge();
</code>
</pre>
<li>Receive and acknowledge the rest of the sent messages from server 1.</li>
<pre class="prettyprint">
<code>
for (int i = 0; i < numMessages; i++)
{
message0 = (TextMessage)consumer1.receive(5000);
System.out.println("Got message: " + message0.getText());
}
message0.acknowledge();
</code>
</pre>
</pre>
<li>And finally (no pun intended), <b>always</b> remember to close your JMS resources after use, in a <code>finally</code> block. Closing a JMS connection will automatically close all of its sessions, consumers, producer and browser objects</li>
<pre class="prettyprint">
<code>
finally
{
if (connection != null)
{
connection.close();
}
if (initialContext != null)
{
initialContext.close();
}
if (connection1 != null)
{
connection1.close();
}
if (initialContext1 != null)
{
initialContext1.close();
}
}
</code>
</pre>
</ol>
</body>
</html>

View File

@ -18,7 +18,8 @@ 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">
<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>
@ -27,7 +28,7 @@ under the License.
<version>1.0.1-SNAPSHOT</version>
</parent>
<artifactId>artemis-jms-consumer-rate-limit-example</artifactId>
<artifactId>consumer-rate-limit</artifactId>
<packaging>jar</packaging>
<name>ActiveMQ Artemis JMS Consumer Rate Limit Example</name>
@ -37,71 +38,81 @@ under the License.
<dependencies>
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-jms_2.0_spec</artifactId>
<groupId>org.apache.activemq</groupId>
<artifactId>artemis-jms-client</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
<profiles>
<profile>
<id>example</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.activemq</groupId>
<artifactId>artemis-maven-plugin</artifactId>
<executions>
<execution>
<id>create</id>
<goals>
<goal>create</goal>
</goals>
</execution>
<execution>
<id>start</id>
<goals>
<goal>cli</goal>
</goals>
<configuration>
<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.ConsumerRateLimitExample</clientClass>
</configuration>
</execution>
<execution>
<id>stop</id>
<goals>
<goal>cli</goal>
</goals>
<configuration>
<args>
<param>stop</param>
</args>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.apache.activemq.examples.jms</groupId>
<artifactId>artemis-jms-consumer-rate-limit-example</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
<!-- specify -PnoServer if you don't want to start the server -->
<id>noServer</id>
<properties>
<noServer>true</noServer>
</properties>
</profile>
</profiles>
<build>
<plugins>
<plugin>
<groupId>org.apache.activemq</groupId>
<artifactId>artemis-maven-plugin</artifactId>
<executions>
<execution>
<id>create</id>
<goals>
<goal>create</goal>
</goals>
<configuration>
<ignore>${noServer}</ignore>
</configuration>
</execution>
<execution>
<id>start</id>
<goals>
<goal>cli</goal>
</goals>
<configuration>
<ignore>${noServer}</ignore>
<spawn>true</spawn>
<testURI>tcp://localhost:61616</testURI>
<args>
<param>run</param>
</args>
</configuration>
</execution>
<execution>
<id>runClient</id>
<goals>
<goal>runClient</goal>
</goals>
<configuration>
<clientClass>org.apache.activemq.artemis.jms.example.ConsumerRateLimitExample</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.jms</groupId>
<artifactId>consumer-rate-limit</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>

View File

@ -26,6 +26,7 @@ under the License.
</head>
<body onload="prettyPrint()">
<h1>JMS Message Consumer Rate Limiting</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>With ActiveMQ Artemis you can specify a maximum consume rate at which a JMS MessageConsumer will consume messages.
This can be specified when creating or configuring the connection factory. See <code>jndi.properties</code>.</p>
@ -42,131 +43,5 @@ connectionFactory.ConnectionFactory=tcp://localhost:61616?consumerMaxRate=10
<p>We then simply consume as many messages as we can in 10 seconds and note how many messages are actually consumed.</p>
<p>We note that the number of messages consumed per second never exceeds the specified value of <code>10</code> messages per second.</p>
<p><i>To run the example, simply type <code>mvn verify -Pexample</code> from this directory</i></p>
<ol>
<li>Create an initial context to perform the JNDI lookup.</li>
<pre class="prettyprint">
<code>initialContext = getContext(0);</code>
</pre>
<li>Perfom a lookup on the queue</li>
<pre class="prettyprint">
<code>Queue queue = (Queue)initialContext.lookup("/queue/exampleQueue");</code>
</pre>
<li>Perform a lookup on the Connection Factory</li>
<pre class="prettyprint">
<code>ConnectionFactory cf = (ConnectionFactory)initialContext.lookup("/ConnectionFactory");</code>
</pre>
<li>Create a JMS Connection</li>
<pre class="prettyprint">
<code>connection = cf.createConnection();</code>
</pre>
<li>Create a JMS Session</li>
<pre class="prettyprint">
<code>Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);</code>
</pre>
<li>Create a JMS MessageProducer</li>
<pre class="prettyprint">
<code>MessageProducer producer = session.createProducer(queue);</code>
</pre>
<li>Create a JMS MessageConsumer</li>
<pre class="prettyprint">
<code>MessageConsumer consumer = session.createConsumer(queue);</code>
</pre>
<li>Start the connection</li>
<pre class="prettyprint">
<code>
connection.start();
</code>
</pre>
<li>Send a bunch of messages</li>
<pre class="prettyprint">
<code>
final int numMessages = 150;
for (int i = 0; i < numMessages; i++)
{
TextMessage message = session.createTextMessage("This is text message: " + i);
producer.send(message);
}
</code>
</pre>
<li>Consume as many messages as we can in 10 seconds</li>
<pre class="prettyprint">
<code>
final long duration = 10000;
int i = 0;
long start = System.currentTimeMillis();
while (System.currentTimeMillis() - start <= duration)
{
TextMessage message = (TextMessage)consumer.receive(2000);
if (message == null)
{
return false;
}
i++;
}
long end = System.currentTimeMillis();
double rate = 1000 * (double)i / (end - start);
System.out.println("We consumed " + i + " messages in " + (end - start) + " milliseconds");
System.out.println("Actual consume rate was " + rate + " messages per second");
</code>
</pre>
<li>This should produce output something like:</li>
<pre class="prettyprint">
<code>
[java] Sent messages
[java] Will now try and consume as many as we can in 10 seconds ...
[java] We consumed 100 messages in 10001 milliseconds
[java] Actual consume rate was 9.99900009999 messages per second
</code>
</pre>
<li>Be sure to close our resources!</li>
<pre class="prettyprint">
<code>
finally
{
if (initialContext != null)
{
initialContext.close();
}
if (connection != null)
{
connection.close();
}
}</code>
</pre>
</ol>
</body>
</html>

View File

@ -18,7 +18,8 @@ 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">
<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>
@ -27,7 +28,7 @@ under the License.
<version>1.0.1-SNAPSHOT</version>
</parent>
<artifactId>artemis-jms-dead-letter-example</artifactId>
<artifactId>dead-letter</artifactId>
<packaging>jar</packaging>
<name>ActiveMQ Artemis JMS Dead Letter Example</name>
@ -37,71 +38,82 @@ under the License.
<dependencies>
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-jms_2.0_spec</artifactId>
<groupId>org.apache.activemq</groupId>
<artifactId>artemis-jms-client</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
<profiles>
<profile>
<id>example</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.activemq</groupId>
<artifactId>artemis-maven-plugin</artifactId>
<executions>
<execution>
<id>create</id>
<goals>
<goal>create</goal>
</goals>
</execution>
<execution>
<id>start</id>
<goals>
<goal>cli</goal>
</goals>
<configuration>
<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.DeadLetterExample</clientClass>
</configuration>
</execution>
<execution>
<id>stop</id>
<goals>
<goal>cli</goal>
</goals>
<configuration>
<args>
<param>stop</param>
</args>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.apache.activemq.examples.jms</groupId>
<artifactId>artemis-jms-dead-letter-example</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
<!-- specify -PnoServer if you don't want to start the server -->
<id>noServer</id>
<properties>
<noServer>true</noServer>
</properties>
</profile>
</profiles>
<build>
<plugins>
<plugin>
<groupId>org.apache.activemq</groupId>
<artifactId>artemis-maven-plugin</artifactId>
<executions>
<execution>
<id>create</id>
<goals>
<goal>create</goal>
</goals>
<configuration>
<configuration>${basedir}/target/classes/activemq/server0</configuration>
<ignore>${noServer}</ignore>
</configuration>
</execution>
<execution>
<id>start</id>
<goals>
<goal>cli</goal>
</goals>
<configuration>
<ignore>${noServer}</ignore>
<spawn>true</spawn>
<testURI>tcp://localhost:61616</testURI>
<args>
<param>run</param>
</args>
</configuration>
</execution>
<execution>
<id>runClient</id>
<goals>
<goal>runClient</goal>
</goals>
<configuration>
<clientClass>org.apache.activemq.artemis.jms.example.DeadLetterExample</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.jms</groupId>
<artifactId>dead-letter</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>

View File

@ -26,9 +26,10 @@ under the License.
</head>
<body onload="prettyPrint()">
<h1>Dead Letter 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 define and deal with dead letter messages.</p>
<p>Messages can be delivered unsuccessfully (e.g. if the transacted session used to consume them is rolled back).
<p>Messages can be delivered unsuccessfully (e.g. if the transacted session used to consume them is rolled back).
Such a message goes back to the JMS destination ready to be redelivered.
However, this means it is possible for a message to be delivered again and again without any success and remain in the destination, clogging the system.</p>
<p>To prevent this, messaging systems define dead letter messages: after a specified unsuccessful delivery attempts, the message is removed from the destination
@ -47,12 +48,12 @@ under the License.
&lt;max-delivery-attempts&gt;3&lt;/max-delivery-attempts&gt;
&lt;/address-setting&gt;
</code>
</pre>
</pre>
<p>This configuration will moved dead letter messages from <code>exampleQueue</code> to the <code>deadLetterQueue</code>.</p>
<p>ActiveMQ Artemis allows to specify either a <code>Queue</code> by prefixing the <code>dead-letter-address</code> with <code>jms.queue.</code>
or a <code>Topic</code> by prefixing with <code>jms.topic.</code>.<br />
In this example, we will use a <code>Queue</code> to hold the dead letter messages.</p>
<p>The maximum attempts of delivery is <code>3</code>. Once this figure is reached, a message is considered a dead letter message and is moved to
<p>The maximum attempts of delivery is <code>3</code>. Once this figure is reached, a message is considered a dead letter message and is moved to
the <code>deadLetterQueue</code>.
<p>Since we want to consume messages from this deadLetterQueue, we also need to add a JNDI binding to perform a lookup.
This is configured in <a href="src/main/resources/activemq/server0/activemq-jms.xml">activemq-jms.xml</a></p>
@ -61,166 +62,5 @@ under the License.
&lt;entry name="/queue/deadLetterQueue"/&gt;
&lt;/queue&gt;</code>
</pre>
</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>First we need to get an initial context so we can look-up the JMS connection factory and destination objects from JNDI. This initial context will get it's properties from the <code>client-jndi.properties</code> file in the directory <code>../common/config</code></li>
<pre class="prettyprint">
<code>InitialContext initialContext = getContext();</code>
</pre>
<li>We look up the JMS queue object from JNDI</li>
<pre class="prettyprint">
<code>Queue queue = (Queue) initialContext.lookup("/queue/exampleQueue");</code>
</pre>
<li>We look up the JMS connection factory object from JNDI</li>
<pre class="prettyprint">
<code>ConnectionFactory cf = (ConnectionFactory) initialContext.lookup("/ConnectionFactory");</code>
</pre>
<li>We create a JMS connection</li>
<pre class="prettyprint">
<code>connection = cf.createConnection();</code>
</pre>
<li>We create a JMS <em>transacted</em> session
<pre class="prettyprint">
<code>Session session = connection.createSession(true, 0);</code>
</pre>
<li>We create a JMS message producer on the session. This will be used to send the messages</li>
<pre class="prettyprint">
<code>MessageProducer messageProducer = session.createProducer(topic);</code>
</pre>
<li>We create a text messages</li>
<pre class="prettyprint">
<code>TextMessage message = session.createTextMessage("this is a text message");</code>
</pre>
<li>We send the message to the queue</li>
<pre class="prettyprint">
<code>producer.send(message);</code>
</pre>
<li>We commit the session to effectively send the message to the queue</li>
<pre class="prettyprint">
<code>session.commit();</code>
</pre>
<p>We will now consume the message from the queue 3 times and roll back the session every time</p>
<li>We create a JMS message consumer on the queue</li>
<pre class="prettyprint">
<code>MessageConsumer messageConsumer = session.createConsumer(queue);</code>
</pre>
<li>We start the connection. In order for delivery to occur on any consumers or subscribers on a connection, the connection must be started</li>
<pre class="prettyprint">
<code>connection.start();</code>
</pre>
<li>We receive the message a 1<sup>st</sup> time</li>
<pre class="prettyprint">
<code>TextMessage messageReceived = (TextMessage)messageConsumer.receive(5000);
System.out.println("1st delivery from " + queue.getQueueName() + ": " + messageReceived.getText());</code>
</pre>
<li>We roll back the session. The message we received is undelivered and goes back to the queue</li>
<pre class="prettyprint">
<code>session.rollback();</code>
</pre>
<li>We receive a message and roll back the session a 2<sup>nd</sup> time
<pre class="prettyprint">
<code>messageReceived = (TextMessage)messageConsumer.receive(5000);
System.out.println("2nd delivery from " + queue.getQueueName() + ": " + messageReceived.getText());
session.rollback();</code>
</pre>
<li>We do it againt a 3<sup>rd</sup> time
<pre class="prettyprint">
<code>messageReceived = (TextMessage)messageConsumer.receive(5000);
System.out.println("3rd delivery from " + queue.getQueueName() + ": " + messageReceived.getText());
session.rollback();</code>
</pre>
<p>Since the queue was configured to move messages to the <code>deadLetterQueue</code> after <code>3</code> unsuccessful delivery attempts,
the message won't be in the <code>queue</code> anymore</p>
<li>We try to receive a message from the queue for a 4<sup>th</sup>. Since there is none, the call will timeout after 5000ms and <code>messageReceived</code> will be <code>null</code>
<pre class="prettyprint">
<code>messageReceived = (TextMessage)messageConsumer.receive(5000);
System.out.println("4th delivery from " + queue.getQueueName() + ": " + messageReceived);</code>
</pre>
<p>We have configured ActiveMQ Artemis to send any dead letter messages to the <code>deadLetterQueue</code>.
We will now consume messages from this queue and receives the <em>dead letter messages</em>.</p>
<li>We look up the JMS <em>dead letter queue</em> object from JNDI</li>
<pre class="prettyprint">
<code>Queue deadLetterQueue = (Queue)initialContext.lookup("/queue/deadLetterQueue");</code>
</pre>
<li>We create a JMS message consumer on the dead letter queue</li>
<pre class="prettyprint">
<code>MessageConsumer deadLetterConsumer = session.createConsumer(expiryQueue);</code>
</pre>
<li>We consume a message from the dead letter queue:</li>
<pre class="prettyprint">
<code>messageReceived = (TextMessage)deadLetterConsumer.receive(5000);</code>
</pre>
<li>The message consumed from the <em>dead letter queue</em> has the <em>same content</em> than the message which was sent to the <em>queue</em>
<pre class="prettyprint">
<code>System.out.println("Received message from " + deadLetterQueue.getQueueName() + ": " + messageReceived.getText());</code>
</pre>
<p>JMS does not specify the notion of dead letter destinations and messages. From JMS point of view, the message received from the dead letter queue
is a <strong>different</strong> message than the message removed from the queue after the unsuccessful delivery attempts:
the messages have the same content (properties and body) but their JMS headers differ.<br />
ActiveMQ Artemis defines additional properties for messages received from a dead letter destination</p>
<li>The message's destination is the dead letter queue</li>
<pre class="prettyprint">
<code>System.out.println("Destination of the message: " + ((Queue)messageReceived.getJMSDestination()).getQueueName());</code>
</pre>
<li>The <strong>origin destination</strong> is stored in the <code>_AMQ_ORIG_ADDRESS</code> property
<pre class="prettyprint">
<code>System.out.println("*Origin destination* of the message: " + messageReceived.getStringProperty("_AMQ_ORIG_ADDRESS"));</code>
</pre>
<li>We do not forget to commit the session to acknowledge that we have received the message from the dead letter queue</li>
<pre class="prettyprint">
<code>session.commit();</code>
</pre>
</p>
<li>And finally, <b>always</b> remember to close your JMS connections and resources after use, in a <code>finally</code> block. Closing a JMS connection will automatically close all of its sessions, consumers, producer and browser objects</li>
<pre class="prettyprint">
<code>finally
{
if (initialContext != null)
{
initialContext.close();
}
if (connection != null)
{
connection.close();
}
}</code>
</pre>
</ol>
<h2>More information</h2>
<ul>
<li>User Manual's <a href="../../../docs/user-manual/en/html_single/index.html#undelivered-messages">Undelivered Messages chapter</a></li>
</ul>
</body>
</html>

View File

@ -18,7 +18,8 @@ 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">
<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>
@ -27,7 +28,7 @@ under the License.
<version>1.0.1-SNAPSHOT</version>
</parent>
<artifactId>artemis-jms-delayed-redelivery-example</artifactId>
<artifactId>delayed-redelivery</artifactId>
<packaging>jar</packaging>
<name>ActiveMQ Artemis JMS Delayed Redelivery Example</name>
@ -37,71 +38,82 @@ under the License.
<dependencies>
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-jms_2.0_spec</artifactId>
<groupId>org.apache.activemq</groupId>
<artifactId>artemis-jms-client</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
<profiles>
<profile>
<id>example</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.activemq</groupId>
<artifactId>artemis-maven-plugin</artifactId>
<executions>
<execution>
<id>create</id>
<goals>
<goal>create</goal>
</goals>
</execution>
<execution>
<id>start</id>
<goals>
<goal>cli</goal>
</goals>
<configuration>
<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.DelayedRedeliveryExample</clientClass>
</configuration>
</execution>
<execution>
<id>stop</id>
<goals>
<goal>cli</goal>
</goals>
<configuration>
<args>
<param>stop</param>
</args>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.apache.activemq.examples.jms</groupId>
<artifactId>artemis-jms-delayed-redelivery-example</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
<!-- specify -PnoServer if you don't want to start the server -->
<id>noServer</id>
<properties>
<noServer>true</noServer>
</properties>
</profile>
</profiles>
<build>
<plugins>
<plugin>
<groupId>org.apache.activemq</groupId>
<artifactId>artemis-maven-plugin</artifactId>
<executions>
<execution>
<id>create</id>
<goals>
<goal>create</goal>
</goals>
<configuration>
<configuration>${basedir}/target/classes/activemq/server0</configuration>
<ignore>${noServer}</ignore>
</configuration>
</execution>
<execution>
<id>start</id>
<goals>
<goal>cli</goal>
</goals>
<configuration>
<ignore>${noServer}</ignore>
<spawn>true</spawn>
<testURI>tcp://localhost:61616</testURI>
<args>
<param>run</param>
</args>
</configuration>
</execution>
<execution>
<id>runClient</id>
<goals>
<goal>runClient</goal>
</goals>
<configuration>
<clientClass>org.apache.activemq.artemis.jms.example.DelayedRedeliveryExample</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.jms</groupId>
<artifactId>delayed-redelivery</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>

View File

@ -26,7 +26,8 @@ under the License.
</head>
<body onload="prettyPrint()">
<h1>Delayed Redelivery 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 how ActiveMQ Artemis can be configured to provide a delayed redelivery in the case
where a message needs to be redelivered.</p>
<p>Delaying redelivery can often be useful in the case that clients regularly fail or roll-back. Without a delayed
@ -46,142 +47,10 @@ under the License.
<p>We then consume a message in a transacted session, and rollback, and note that the message is not redelivered until
after 5 seconds.</p>
<pre class="prettyprint">
<code>&lt;address-setting match="jms.queue.exampleQueue"&gt;
<code>&lt;address-setting match="jms.queue.exampleQueue"&gt;
&lt;redelivery-delay&gt;5000&lt;/redelivery-delay&gt;
&lt;/address-setting&gt;
</code>
</pre>
<p><i>To run the example, simply type <code>mvn verify -Pexample</code> from this directory</i></p>
<ol>
<li>Create an initial context to perform the JNDI lookup.</li>
<pre class="prettyprint">
<code>initialContext = getContext(0);</code>
</pre>
<li>Perform a lookup on the queue</li>
<pre class="prettyprint">
<code>Queue queue = (Queue)initialContext.lookup("/queue/exampleQueue");</code>
</pre>
<li>Perform a lookup on the Connection Factory</li>
<pre class="prettyprint">
<code>ConnectionFactory cf = (ConnectionFactory)initialContext.lookup("/ConnectionFactory");</code>
</pre>
<li>Create a JMS Connection</li>
<pre class="prettyprint">
<code>connection = cf.createConnection();</code>
</pre>
<li>Create a transacted JMS Session
<pre class="prettyprint">
<code>Session session = connection.createSession(true, 0);</code>
</pre>
<li>Create a JMS Message Producer</li>
<pre class="prettyprint">
<code>MessageProducer producer = session.createProducer(queue);</code>
</pre>
<li>Create a Text Message</li>
<pre class="prettyprint">
<code>TextMessage message = session.createTextMessage("this is a text message");</code>
</pre>
<li>Send the Message</li>
<pre class="prettyprint">
<code>producer.send(message);</code>
</pre>
<li>We commit the session to effectively send the message to the queue</li>
<pre class="prettyprint">
<code>session.commit();</code>
</pre>
<li>We create a JMS message consumer on the queue</li>
<pre class="prettyprint">
<code>MessageConsumer messageConsumer = session.createConsumer(queue);</code>
</pre>
<li>We start the connection. In order for delivery to occur on any consumers or subscribers on a connection, the connection must be started</li>
<pre class="prettyprint">
<code>connection.start();</code>
</pre>
<li>We receive the message...</li>
<pre class="prettyprint">
<code>TextMessage messageReceived = (TextMessage)messageConsumer.receive(5000);
System.out.println("1st delivery from " + queue.getQueueName() + ": " + messageReceived.getText());</code>
</pre>
<li>...but we roll back the session. the message returns to the queue, but only after a
5 second delay</li>
<pre class="prettyprint">
<code>session.rollback();</code>
</pre>
<li>We try to receive the message but it's being delayed</li>
<pre class="prettyprint">
<code>
messageReceived = (TextMessage)messageConsumer.receive(3000);
if (messageReceived != null)
{
return false;
}
System.out.println("Redelivery has been delayed so received message is " + messageReceived);
</code>
</pre>
<li>We try and receive the message again, this time we should get it</li>
<pre class="prettyprint">
<code>
messageReceived = (TextMessage)messageConsumer.receive(3000);
System.out.println("2nd delivery from " + queue.getQueueName() + ": " + messageReceived.getText());
</code>
</pre>
<li>We rollback the session again to cause another redelivery, and we time how long this one takes</code>
<pre class="prettyprint">
<code>
long start = System.currentTimeMillis();
session.rollback();
messageReceived = (TextMessage)messageConsumer.receive(8000);
long end = System.currentTimeMillis();
System.out.println("3nd delivery from " + queue.getQueueName() + ": " + messageReceived.getText() +
" after " + (end - start) + " milliseconds.");
</code>
</pre>
<li>And finally, <b>always</b> remember to close your JMS connections and resources after use, in a <code>finally</code> block. Closing a JMS connection will automatically close all of its sessions, consumers, producer and browser objects</li>
<pre class="prettyprint">
<code>finally
{
if (initialContext != null)
{
initialContext.close();
}
if (connection != null)
{
connection.close();
}
}</code>
</pre>
</ol>
<h2>More information</h2>
<ul>
<li>User Manual's <a href="../../../docs/user-manual/en/html_single/index.html#undelivered-messages">Undelivered Messages chapter</a></li>
</ul>
</pre>
</body>
</html>

View File

@ -18,7 +18,8 @@ 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">
<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>
@ -26,8 +27,7 @@ under the License.
<artifactId>jms-examples</artifactId>
<version>1.0.1-SNAPSHOT</version>
</parent>
<artifactId>artemis-jms-divert-example</artifactId>
<artifactId>divert</artifactId>
<packaging>jar</packaging>
<name>ActiveMQ Artemis JMS Divert Example</name>
@ -42,114 +42,127 @@ under the License.
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-jms_2.0_spec</artifactId>
<groupId>org.apache.activemq</groupId>
<artifactId>artemis-jms-client</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
<profiles>
<profile>
<id>example</id>
<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>
</configuration>
</execution>
<execution>
<id>create1</id>
<goals>
<goal>create</goal>
</goals>
<configuration>
<instance>${basedir}/target/server1</instance>
<configuration>${basedir}/target/classes/activemq/server1</configuration>
</configuration>
</execution>
<execution>
<id>start0</id>
<goals>
<goal>cli</goal>
</goals>
<configuration>
<spawn>true</spawn>
<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>
<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.DivertExample</clientClass>
</configuration>
</execution>
<execution>
<id>stop0</id>
<goals>
<goal>cli</goal>
</goals>
<configuration>
<location>${basedir}/target/server0</location>
<args>
<param>stop</param>
</args>
</configuration>
</execution>
<execution>
<id>stop1</id>
<goals>
<goal>cli</goal>
</goals>
<configuration>
<location>${basedir}/target/server1</location>
<args>
<param>stop</param>
</args>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.apache.activemq.examples.jms</groupId>
<artifactId>artemis-jms-divert-example</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
<!-- specify -PnoServer if you don't want to start the server -->
<id>noServer</id>
<properties>
<noServer>true</noServer>
</properties>
</profile>
</profiles>
<build>
<plugins>
<plugin>
<groupId>org.apache.activemq</groupId>
<artifactId>artemis-maven-plugin</artifactId>
<executions>
<execution>
<id>create0</id>
<goals>
<goal>create</goal>
</goals>
<configuration>
<ignore>${noServer}</ignore>
<libList><arg>org.apache.activemq.examples.jms:divert:${project.version}</arg></libList>
<instance>${basedir}/target/server0</instance>
<configuration>${basedir}/target/classes/activemq/server0</configuration>
</configuration>
</execution>
<execution>
<id>create1</id>
<goals>
<goal>create</goal>
</goals>
<configuration>
<ignore>${noServer}</ignore>
<libList><arg>org.apache.activemq.examples.jms:divert:${project.version}</arg></libList>
<instance>${basedir}/target/server1</instance>
<configuration>${basedir}/target/classes/activemq/server1</configuration>
</configuration>
</execution>
<execution>
<id>start0</id>
<goals>
<goal>cli</goal>
</goals>
<configuration>
<ignore>${noServer}</ignore>
<spawn>true</spawn>
<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.DivertExample</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.jms</groupId>
<artifactId>divert</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>

View File

@ -26,6 +26,7 @@ under the License.
</head>
<body onload="prettyPrint()">
<h1>Divert 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>ActiveMQ Artemis diverts allow messages to be transparently "diverted" from one address to another
with just some simple configuration defined on the server side.</p>
@ -114,325 +115,5 @@ under the License.
&lt;/bridges&gt;
</code>
</pre>
<p><i>To run the example, simply type <code>mvn verify -Pexample</code> from this directory</i></p>
<ol>
<li>Create an initial context to perform the JNDI lookup on the London server</li>
<pre class="prettyprint">
<code>
initialContext0 = getContext(0);
</code>
</pre>
<li>Look-up the queue orderQueue on the London server - this is the queue any orders are sent to</li>
<pre class="prettyprint">
<code>
Queue queue = (Queue) initialContext.lookup("/queue/exampleQueue");
</code>
</pre>
<li>Look-up the topic priceUpdates on the London server- this is the topic that any price updates are sent to</li>
<pre class="prettyprint">
<code>
Topic priceUpdates = (Topic)initialContextLondon.lookup("/topic/priceUpdates");
</code>
</pre>
<li>Look-up the spy topic on the London server- this is what we will use to snoop on any orders</li>
<pre class="prettyprint">
<code>
Topic spyTopic = (Topic)initialContextLondon.lookup("/topic/spyTopic");
</code>
</pre>
<li>Create an initial context to perform the JNDI lookup on the New York server.</li>
<pre class="prettyprint">
<code>
initialContextNewYork = getContext(1);
</code>
</pre>
<li>Look-up the topic newYorkPriceUpdates on the New York server - any price updates sent to priceUpdates on the London server will
be diverted to the queue priceForward on the London server, and a bridge will consume from that queue and forward
them to the address newYorkPriceUpdates on the New York server where they will be distributed to the topic subscribers on
the New York server.
</li>
<pre class="prettyprint">
<code>
Topic newYorkPriceUpdates = (Topic)initialContextNewYork.lookup("/topic/newYorkPriceUpdates");
</code>
</pre>
<li>Perform a lookup on the Connection Factory on the London server</li>
<pre class="prettyprint">
<code>
ConnectionFactory cfLondon = (ConnectionFactory)initialContextLondon.lookup("/ConnectionFactory");
</code>
</pre>
<li>Perform a lookup on the Connection Factory on the New York server.</li>
<pre class="prettyprint">
<code>
ConnectionFactory cfNewYork = (ConnectionFactory)initialContextNewYork.lookup("/ConnectionFactory");
</code>
</pre>
<li>Create a JMS Connection on the London server</li>
<pre class="prettyprint">
<code>
connectionLondon = cfLondon.createConnection();
</code>
</pre>
<li>Create a JMS Connection on the New York server</li>
<pre class="prettyprint">
<code>
connectionNewYork = cfNewYork.createConnection();
</code>
</pre>
<li>Create a JMS Session on the London server.</li>
<pre class="prettyprint">
<code>
Session sessionLondon = connectionLondon.createSession(false, Session.AUTO_ACKNOWLEDGE);
</code>
</pre>
<li>Create a JMS Session on the New York server.</li>
<pre class="prettyprint">
<code>
Session sessionNewYork = connectionNewYork.createSession(false, Session.AUTO_ACKNOWLEDGE);
</code>
</pre>
<li>Create a JMS MessageProducer orderProducer that sends to the queue orderQueue on the London server.</li>
<pre class="prettyprint">
<code>
MessageProducer orderProducer = sessionLondon.createProducer(orderQueue);
/code>
</pre>
<li>Create a JMS MessageProducer priceProducer that sends to the topic priceUpdates on the London server.</li>
<pre class="prettyprint">
<code>
MessageProducer priceProducer = sessionLondon.createProducer(priceUpdates);
/code>
</pre>
<li>Create a JMS subscriber which subscribes to the spyTopic on the London server</li>
<pre class="prettyprint">
<code>
MessageConsumer spySubscriberA = sessionLondon.createConsumer(spyTopic);
</code>
</pre>
<li>Create another JMS subscriber which also subscribes to the spyTopic on the London server</li>
<pre class="prettyprint">
<code>
MessageConsumer spySubscriberB = sessionLondon.createConsumer(spyTopic);
</code>
</pre>
<li>Create a JMS MessageConsumer which consumes orders from the order queue on the London server</li>
<pre class="prettyprint">
<code>
MessageConsumer orderConsumer = sessionLondon.createConsumer(orderQueue);
</code>
</pre>
<li>Create a JMS subscriber which subscribes to the priceUpdates topic on the London server</li>
<pre class="prettyprint">
<code>
MessageConsumer priceUpdatesSubscriberLondon = sessionLondon.createConsumer(priceUpdates);
</code>
</pre>
<li>Create a JMS subscriber which subscribes to the newYorkPriceUpdates topic on the New York server</li>
<pre class="prettyprint">
<code>
MessageConsumer newYorkPriceUpdatesSubscriberA = sessionNewYork.createConsumer(newYorkPriceUpdates);
</code>
</pre>
<li>Create another JMS subscriber which also subscribes to the newYorkPriceUpdates topic on the New York server</li>
<pre class="prettyprint">
<code>
MessageConsumer newYorkPriceUpdatesSubscriberB = sessionNewYork.createConsumer(newYorkPriceUpdates);
</code>
</pre>
<li>Start the connections</li>
<pre class="prettyprint">
<code>
connectionLondon.start();
connectionNewYork.start();
</code>
</pre>
<li>Create an order message</li>
<pre class="prettyprint">
<code>
TextMessage orderMessage = sessionLondon.createTextMessage("This is an order");
</code>
</pre>
<li>Send the order message to the order queue on the London server</li>
<pre class="prettyprint">
<code>
orderProducer.send(orderMessage);
System.out.println("Sent message: " + orderMessage.getText());
</code>
</pre>
<li>The order message is consumed by the orderConsumer on the London server</li>
<pre class="prettyprint">
<code>
TextMessage receivedOrder = (TextMessage)orderConsumer.receive(5000);
System.out.println("Received order: " + receivedOrder.getText());
</code>
</pre>
<li>A copy of the order is also received by the spyTopic subscribers on the London server</li>
<pre class="prettyprint">
<code>
TextMessage spiedOrder1 = (TextMessage)spySubscriberA.receive(5000);
System.out.println("Snooped on order: " + spiedOrder1.getText());
TextMessage spiedOrder2 = (TextMessage)spySubscriberB.receive(5000);
System.out.println("Snooped on order: " + spiedOrder2.getText());
</code>
</pre>
<li>Create and send a price update message, destined for London</li>
<pre class="prettyprint">
<code>
TextMessage priceUpdateMessageLondon = sessionLondon.createTextMessage("This is a price update for London");
priceUpdateMessageLondon.setStringProperty("office", "London");
priceProducer.send(priceUpdateMessageLondon);
</code>
</pre>
<li>The price update *should* be received by the local subscriber since we only divert messages
where office = New York</li>
<pre class="prettyprint">
<code>
TextMessage receivedUpdate = (TextMessage)priceUpdatesSubscriberLondon.receive(2000);
System.out.println("Received price update locally: " + receivedUpdate.getText());
</code>
</pre>
<li>The price update *should not* be received in New York</li>
<pre class="prettyprint">
<code>
TextMessage priceUpdate1 = (TextMessage)newYorkPriceUpdatesSubscriberA.receive(1000);
if (priceUpdate1 != null)
{
return false;
}
System.out.println("Did not received price update in New York, look it's: " + priceUpdate1);
TextMessage priceUpdate2 = (TextMessage)newYorkPriceUpdatesSubscriberB.receive(1000);
if (priceUpdate2 != null)
{
return false;
}
System.out.println("Did not received price update in New York, look it's: " + priceUpdate2);
</code>
</pre>
<li>Create a price update message, destined for New York</li>
<pre class="prettyprint">
<code>
TextMessage priceUpdateMessageNewYork = sessionLondon.createTextMessage("This is a price update for New York");
priceUpdateMessageNewYork.setStringProperty("office", "New York");
</code>
</pre>
<li>Send the price update message to the priceUpdates topic on the London server</li>
<pre class="prettyprint">
<code>
priceProducer.send(priceUpdateMessageNewYork);
</code>
</pre>
<li>The price update *should not* be received by the local subscriber to the priceUpdates topic
since it has been *exclusively* diverted to the priceForward queue, because it has a header saying
it is destined for the New York office</li>
<pre class="prettyprint">
<code>
Message message = priceUpdatesSubscriberLondon.receive(1000);
if (message != null)
{
return false;
}
System.out.println("Didn't receive local price update, look, it's: " + message);
</code>
</pre>
<li>The remote subscribers on server 1 *should* receive a copy of the price update since
it has been diverted to a local priceForward queue which has a bridge consuming from it and which
forwards it to the same address on server 1.
We notice how the forwarded messages have had a special header added by our custom transformer that
we told the divert to use</li>
<pre class="prettyprint">
<code>
priceUpdate1 = (TextMessage)newYorkPriceUpdatesSubscriberA.receive(5000);
System.out.println("Received forwarded price update on server 1: " + priceUpdate1.getText());
System.out.println("Time of forward: " + priceUpdate1.getLongProperty("time_of_forward"));
priceUpdate2 = (TextMessage)newYorkPriceUpdatesSubscriberB.receive(5000);
System.out.println("Received forwarded price update on server 2: " + priceUpdate2.getText());
System.out.println("Time of forward: " + priceUpdate2.getLongProperty("time_of_forward"));
</code>
</pre>
<li>And finally, <b>always</b> remember to close your resources after use, in a <code>finally</code> block.</li>
<pre class="prettyprint">
<code>
finally
{
if (initialContextLondon != null)
{
initialContextLondon.close();
}
if (initialContextNewYork != null)
{
initialContextNewYork.close();
}
if (connectionLondon != null)
{
connectionLondon.close();
}
if (connectionNewYork != null)
{
connectionNewYork.close();
}
}
</code>
</pre>
</ol>
</body>
</html>

View File

@ -25,8 +25,9 @@ import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.Topic;
import javax.naming.InitialContext;
import java.util.Hashtable;
import org.apache.activemq.artemis.api.jms.ActiveMQJMSClient;
import org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory;
/**
* This examples demonstrates the use of ActiveMQ Artemis "Diverts" to transparently divert or copy messages
@ -41,37 +42,17 @@ public class DivertExample
Connection connectionLondon = null;
Connection connectionNewYork = null;
InitialContext initialContextLondon = null;
InitialContext initialContextNewYork = null;
try
{
// Step 1. Create an initial context to perform the JNDI lookup on the London server
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://localhost:61616");
properties.put("queue.queue/orders", "orders");
properties.put("topic.topic/priceUpdates", "priceUpdates");
properties.put("topic.topic/spyTopic", "spyTopic");
initialContextLondon = new InitialContext(properties);
// Step 2. Look-up the queue orderQueue on the London server - this is the queue any orders are sent to
Queue orderQueue = (Queue)initialContextLondon.lookup("queue/orders");
Queue orderQueue = ActiveMQJMSClient.createQueue("orders");
// Step 3. Look-up the topic priceUpdates on the London server- this is the topic that any price updates are
// sent to
Topic priceUpdates = (Topic)initialContextLondon.lookup("topic/priceUpdates");
Topic priceUpdates = ActiveMQJMSClient.createTopic("priceUpdates");
// Step 4. Look-up the spy topic on the London server- this is what we will use to snoop on any orders
Topic spyTopic = (Topic)initialContextLondon.lookup("topic/spyTopic");
// Step 6. Create an initial context to perform the JNDI lookup on the New York server
properties = new Hashtable<String, Object>();
properties.put("java.naming.factory.initial", "org.apache.activemq.artemis.jndi.ActiveMQInitialContextFactory");
properties.put("connectionFactory.ConnectionFactory2", "tcp://localhost:61617");
properties.put("topic.topic/newYorkPriceUpdates", "newYorkPriceUpdates");
initialContextNewYork = new InitialContext(properties);
Topic spyTopic = ActiveMQJMSClient.createTopic("spyTopic");
// Step 7. Look-up the topic newYorkPriceUpdates on the New York server - any price updates sent to
// priceUpdates on the London server will
@ -80,13 +61,13 @@ public class DivertExample
// them to the address newYorkPriceUpdates on the New York server where they will be distributed to the topic
// subscribers on
// the New York server
Topic newYorkPriceUpdates = (Topic)initialContextNewYork.lookup("topic/newYorkPriceUpdates");
Topic newYorkPriceUpdates = ActiveMQJMSClient.createTopic("newYorkPriceUpdates");
// Step 8. Perform a lookup on the Connection Factory on the London server
ConnectionFactory cfLondon = (ConnectionFactory)initialContextLondon.lookup("ConnectionFactory");
ConnectionFactory cfLondon = new ActiveMQConnectionFactory("tcp://localhost:61616");
// Step 9. Perform a lookup on the Connection Factory on the New York server
ConnectionFactory cfNewYork = (ConnectionFactory)initialContextNewYork.lookup("ConnectionFactory2");
ConnectionFactory cfNewYork = new ActiveMQConnectionFactory("tcp://localhost:61617");
// Step 10. Create a JMS Connection on the London server
connectionLondon = cfLondon.createConnection();
@ -226,15 +207,6 @@ public class DivertExample
}
finally
{
// Step 12. Be sure to close our resources!
if (initialContextLondon != null)
{
initialContextLondon.close();
}
if (initialContextNewYork != null)
{
initialContextNewYork.close();
}
if (connectionLondon != null)
{
connectionLondon.close();

View File

@ -18,7 +18,8 @@ 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">
<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>
@ -27,7 +28,7 @@ under the License.
<version>1.0.1-SNAPSHOT</version>
</parent>
<artifactId>artemis-jms-durable-subscription-example</artifactId>
<artifactId>durable-subscription</artifactId>
<packaging>jar</packaging>
<name>ActiveMQ Artemis JMS Durable Subscription Example</name>
@ -37,71 +38,82 @@ under the License.
<dependencies>
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-jms_2.0_spec</artifactId>
<groupId>org.apache.activemq</groupId>
<artifactId>artemis-jms-client</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
<profiles>
<profile>
<id>example</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.activemq</groupId>
<artifactId>artemis-maven-plugin</artifactId>
<executions>
<execution>
<id>create</id>
<goals>
<goal>create</goal>
</goals>
</execution>
<execution>
<id>start</id>
<goals>
<goal>cli</goal>
</goals>
<configuration>
<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.DurableSubscriptionExample</clientClass>
</configuration>
</execution>
<execution>
<id>stop</id>
<goals>
<goal>cli</goal>
</goals>
<configuration>
<args>
<param>stop</param>
</args>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.apache.activemq.examples.jms</groupId>
<artifactId>artemis-jms-durable-subscription-example</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
<!-- specify -PnoServer if you don't want to start the server -->
<id>noServer</id>
<properties>
<noServer>true</noServer>
</properties>
</profile>
</profiles>
<build>
<plugins>
<plugin>
<groupId>org.apache.activemq</groupId>
<artifactId>artemis-maven-plugin</artifactId>
<executions>
<execution>
<id>create</id>
<goals>
<goal>create</goal>
</goals>
<configuration>
<ignore>${noServer}</ignore>
<configuration>${basedir}/target/classes/activemq/server0</configuration>
</configuration>
</execution>
<execution>
<id>start</id>
<goals>
<goal>cli</goal>
</goals>
<configuration>
<ignore>${noServer}</ignore>
<spawn>true</spawn>
<testURI>tcp://localhost:61616</testURI>
<args>
<param>run</param>
</args>
</configuration>
</execution>
<execution>
<id>runClient</id>
<goals>
<goal>runClient</goal>
</goals>
<configuration>
<clientClass>org.apache.activemq.artemis.jms.example.DurableSubscriptionExample</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.jms</groupId>
<artifactId>durable-subscription</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>

View File

@ -27,133 +27,13 @@ under the License.
<body onload="prettyPrint()">
<h1>JMS Durable Subscription 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 how to use a durable subscription with ActiveMQ Artemis.</p>
<p>Durable subscriptions are a standard part of JMS, please consult the JMS 1.1 specification for full details.</p>
<p>Unlike non durable subscriptions, the key function of durable subscriptions is that the messages contained in them
persist longer than the lifetime of the subscriber - i.e. they will accumulate messages sent to the topic even
if the subscriber is not currently connected. They will also survive server restarts. Note that for the messages to
if the subscriber is not currently connected. They will also survive server restarts. Note that for the messages to
be persisted, the messages sent to them must be marked as persistent messages.</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>First we need to get an initial context so we can look-up the JMS connection factory and destination objects from JNDI. This initial context will get it's properties from the <code>client-jndi.properties</code> file in the directory <code>../common/config</code></li>
<pre class="prettyprint">
<code>initialContext = getContext();</code>
</pre>
<li>We look-up the JMS topic object from JNDI</li>
<pre class="prettyprint">
<code>Topic topic = (Topic) initialContext.lookup("/topic/exampleTopic");</code>
</pre>
<li>We look-up the JMS connection factory object from JNDI</li>
<pre class="prettyprint">
<code>ConnectionFactory cf = (ConnectionFactory) initialContext.lookup("/ConnectionFactory");</code>
</pre>
<li>We create a JMS connection</li>
<pre class="prettyprint">
<code>connection = cf.createConnection();</code>
</pre>
<li>We set the client-id on the connection. This must be the <b>first operation</b> performed on the connection object.
The combination of client-id and durable subscription name uniquely identifies the durable subscription. Maybe different durable subscritions can have the same name if they belong to different client-id values</li>
<pre class="prettyprint">
<code>connection.setClientID("durable-client");</code>
</pre>
<li>We start the connection. In order for delivery to occur on any consumers or subscribers on a connection, the connection must be started</li>
<pre class="prettyprint">
<code>connection.start();</code>
</pre>
<li>We create a JMS session. The session is created as non transacted and will auto acknowledge messages.</li>
<pre class="prettyprint">
<code>Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);</code>
</pre>
<li>We create a JMS message producer on the session. This will be used to send the messages.</li>
<pre class="prettyprint">
<code>MessageProducer messageProducer = session.createProducer(topic);</code>
</pre>
<li>We create the durable subscriber on the topic, specifying it's name. Since this is the first time the subscriber is created and a subscription with that name and for this client-id does not already exist, then the underlying durable subscription will be created, and a subscriber will be created and returned for that subscription.</li>
<pre class="prettyprint">
<code>TopicSubscriber subscriber = session.createDurableSubscriber(topic, "subscriber-1");</code>
</pre>
<li>We create a JMS text message, message 1, that we are going to send. Note that it must be a persistent message in order to survive server restart.</li>
<pre class="prettyprint">
<code>TextMessage message1 = session.createTextMessage("This is a text message 1");</code>
</pre>
<li>We send message 1 to the topic</li>
<pre class="prettyprint">
<code>messageProducer.send(message1);</code>
</pre>
<li>The message arrives in the subscription, and we consume the message from the subscription.</li>
<pre class="prettyprint">
<code>TextMessage messageReceived = (TextMessage)subscriber.receive();</code>
</pre>
<li>We create and send another text message, message 2, to the same topic</li>
<pre class="prettyprint">
<code>TextMessage message2 = session.createTextMessage("This is a text message 2");
messageProducer.send(message2);</code>
</pre>
<li>Now we close the subscriber. Since the subscription is durable it will continue to survive even though there is no subscriber attached to it. At this point you could even stop and restart the server and the subscription would survive!</li>
<pre class="prettyprint">
<code>subscriber.close();</code>
</pre>
<li>We now create another durable subscriber, with the same name and same client-id on the same topic. Since the durable subscrition already exists, it will simply return a new subscriber consuming from the <i>same</i> durable subscription instance as before</li>
<pre class="prettyprint">
<code>subscriber = session.createDurableSubscriber(topic, "subscriber-1");</code>
</pre>
<li>We consume message 2 which was sent before the first subscriber was closed.</li>
<pre class="prettyprint">
<code>messageReceived = (TextMessage)subscriber.receive();</code>
</pre>
<li>We close the second subscriber.</li>
<pre class="prettyprint">
<code>subscriber.close();</code>
</pre>
<li>Now we <i>delete</i> the underlying durable subscription. This will delete any remaining unacknowledged messages in the subscription and a new subscriber will not be able to access them</li>
<pre class="prettyprint">
<code>session.unsubscribe("subscriber-1");</code>
</pre>
<li>And finally, <b>always</b> remember to close your JMS connections and resources after use, in a <code>finally</code> block. Closing a JMS connection will automatically close all of its sessions, consumers, producer and browser objects</li>
<pre class="prettyprint">
<code>finally
{
if (initialContext != null)
{
initialContext.close();
}
if (connection != null)
{
connection.close();
}
}</code>
</pre>
</ol>
</body>
</html>

View File

@ -18,7 +18,8 @@ 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">
<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>
@ -27,7 +28,7 @@ under the License.
<version>1.0.1-SNAPSHOT</version>
</parent>
<artifactId>artemis-jms-embedded-simple-example</artifactId>
<artifactId>embedded-simple</artifactId>
<packaging>jar</packaging>
<name>ActiveMQ Artemis JMS Simple Embedded Example</name>
@ -47,41 +48,37 @@ under the License.
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-jms_2.0_spec</artifactId>
<groupId>org.apache.activemq</groupId>
<artifactId>artemis-jms-client</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
<profiles>
<profile>
<id>example</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.activemq</groupId>
<artifactId>artemis-maven-plugin</artifactId>
<executions>
<execution>
<id>runClient</id>
<goals>
<goal>runClient</goal>
</goals>
<configuration>
<clientClass>org.apache.activemq.artemis.jms.example.EmbeddedExample</clientClass>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.apache.activemq.examples.jms</groupId>
<artifactId>artemis-jms-embedded-simple-example</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</profile>
</profiles>
<build>
<plugins>
<plugin>
<groupId>org.apache.activemq</groupId>
<artifactId>artemis-maven-plugin</artifactId>
<executions>
<execution>
<id>runClient</id>
<goals>
<goal>runClient</goal>
</goals>
<configuration>
<clientClass>org.apache.activemq.artemis.jms.example.EmbeddedExample</clientClass>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.apache.activemq.examples.jms</groupId>
<artifactId>embedded-simple</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>

View File

@ -18,7 +18,8 @@ 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">
<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>
@ -27,7 +28,7 @@ under the License.
<version>1.0.1-SNAPSHOT</version>
</parent>
<artifactId>artemis-jms-embedded-example</artifactId>
<artifactId>embedded</artifactId>
<packaging>jar</packaging>
<name>ActiveMQ Artemis JMS Embedded Example</name>
@ -47,41 +48,37 @@ under the License.
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-jms_2.0_spec</artifactId>
<groupId>org.apache.activemq</groupId>
<artifactId>artemis-jms-client</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
<profiles>
<profile>
<id>example</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.activemq</groupId>
<artifactId>artemis-maven-plugin</artifactId>
<executions>
<execution>
<id>runClient</id>
<goals>
<goal>runClient</goal>
</goals>
<configuration>
<clientClass>org.apache.activemq.artemis.jms.example.EmbeddedExample</clientClass>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.apache.activemq.examples.jms</groupId>
<artifactId>artemis-jms-embedded-example</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</profile>
</profiles>
<build>
<plugins>
<plugin>
<groupId>org.apache.activemq</groupId>
<artifactId>artemis-maven-plugin</artifactId>
<executions>
<execution>
<id>runClient</id>
<goals>
<goal>runClient</goal>
</goals>
<configuration>
<clientClass>org.apache.activemq.artemis.jms.example.EmbeddedExample</clientClass>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.apache.activemq.examples.jms</groupId>
<artifactId>embedded</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>

View File

@ -26,78 +26,13 @@ under the License.
</head>
<body onload="prettyPrint()">
<h1>Embedded JMS Server 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>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 />
<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>
<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, and set the properties accordingly</li>
<pre class="prettyprint">
<code>Configuration configuration = new ConfigurationImpl();
configuration.setPersistenceEnabled(false);
configuration.setSecurityEnabled(false);
configuration.getAcceptorConfigurations().add(new TransportConfiguration(NettyAcceptorFactory.class.getName()));</code>
Configuration configuration = new ConfigurationImpl();</pre>
<li>Create the ActiveMQ Artemis core server</li>
<pre class="prettyprint">
<code>ActiveMQServer activemqServer = ActiveMQ Artemis.newActiveMQServer(configuration);</code>
</pre>
<li>Create the JMS configuration</li>
<pre class="prettyprint">
<code>JMSConfiguration jmsConfig = new JMSConfigurationImpl();</code>
</pre>
<li>Configure the JMS ConnectionFactory</li>
<pre class="prettyprint">
<code>TransportConfiguration connectorConfig = new TransportConfiguration(NettyConnectorFactory.class.getName());
ConnectionFactoryConfiguration cfConfig = new ConnectionFactoryConfigurationImpl("cf", connectorConfig, "/cf");
jmsConfig.getConnectionFactoryConfigurations().add(cfConfig);</code>
</pre>
<li>Configure the JMS Queue</li>
<pre class="prettyprint">
<code>QueueConfiguration queueConfig = new QueueConfigurationImpl("queue1", null, false, "/queue/queue1");
jmsConfig.getQueueConfigurations().add(queueConfig);</code>
</pre>
<li>Start the JMS Server using the ActiveMQ Artemis core server and the JMS configuration</li>
<pre class="prettyprint">
<code>JMSServerManager jmsServer = new JMSServerManagerImpl(activemqServer, jmsConfig);
jmsServer.start();</code>
</pre>
<p>At this point the JMS server is started and any JMS clients can look up JMS resources from 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)context.lookup("/cf");
Queue queue = (Queue)context.lookup("/queue/queue1");</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>Stop the JMS server</li>
<pre class="prettyprint">
<code>jmsServer.stop();</code>
</pre>
<li>Stop the JNDI server</li>
<pre class="prettyprint">
<code>naming.stop();</code>
</pre>
</ol>
</body>
</html>

View File

@ -18,7 +18,8 @@ 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">
<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>
@ -27,7 +28,7 @@ under the License.
<version>1.0.1-SNAPSHOT</version>
</parent>
<artifactId>artemis-jms-expiry-example</artifactId>
<artifactId>expiry</artifactId>
<packaging>jar</packaging>
<name>ActiveMQ Artemis JMS Expiry Example</name>
@ -37,71 +38,82 @@ under the License.
<dependencies>
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-jms_2.0_spec</artifactId>
<groupId>org.apache.activemq</groupId>
<artifactId>artemis-jms-client</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
<profiles>
<profile>
<id>example</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.activemq</groupId>
<artifactId>artemis-maven-plugin</artifactId>
<executions>
<execution>
<id>create</id>
<goals>
<goal>create</goal>
</goals>
</execution>
<execution>
<id>start</id>
<goals>
<goal>cli</goal>
</goals>
<configuration>
<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.ExpiryExample</clientClass>
</configuration>
</execution>
<execution>
<id>stop</id>
<goals>
<goal>cli</goal>
</goals>
<configuration>
<args>
<param>stop</param>
</args>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.apache.activemq.examples.jms</groupId>
<artifactId>artemis-jms-expiry-example</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
<!-- specify -PnoServer if you don't want to start the server -->
<id>noServer</id>
<properties>
<noServer>true</noServer>
</properties>
</profile>
</profiles>
<build>
<plugins>
<plugin>
<groupId>org.apache.activemq</groupId>
<artifactId>artemis-maven-plugin</artifactId>
<executions>
<execution>
<id>create</id>
<goals>
<goal>create</goal>
</goals>
<configuration>
<ignore>${noServer}</ignore>
<configuration>${basedir}/target/classes/activemq/server0</configuration>
</configuration>
</execution>
<execution>
<id>start</id>
<goals>
<goal>cli</goal>
</goals>
<configuration>
<ignore>${noServer}</ignore>
<spawn>true</spawn>
<testURI>tcp://localhost:61616</testURI>
<args>
<param>run</param>
</args>
</configuration>
</execution>
<execution>
<id>runClient</id>
<goals>
<goal>runClient</goal>
</goals>
<configuration>
<clientClass>org.apache.activemq.artemis.jms.example.ExpiryExample</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.jms</groupId>
<artifactId>expiry</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>

Some files were not shown because too many files have changed in this diff Show More