Skip to content

Commit

Permalink
Merge pull request hornetq#354 from andytaylor/master
Browse files Browse the repository at this point in the history
added replication example
  • Loading branch information
clebertsuconic committed Jul 13, 2012
2 parents 3ecab6e + cc69e20 commit 1af3b0a
Show file tree
Hide file tree
Showing 10 changed files with 717 additions and 1 deletion.
158 changes: 158 additions & 0 deletions examples/jms/replicated-failback/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
<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.hornetq.examples.jms</groupId>
<artifactId>jms-examples</artifactId>
<version>2.3.0.Alpha</version>
</parent>

<artifactId>hornetq-jms-replicated-failback-example</artifactId>
<packaging>jar</packaging>
<name>HornetQ JMS Replicated Failback Example</name>

<dependencies>
<dependency>
<groupId>org.hornetq.examples.jms</groupId>
<artifactId>common</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.jboss.spec.javax.jms</groupId>
<artifactId>jboss-jms-api_1.1_spec</artifactId>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.hornetq</groupId>
<artifactId>hornetq-maven-plugin</artifactId>
<version>1.0.0</version>
<executions>
<execution>
<id>start0</id>
<goals>
<goal>start</goal>
</goals>
<configuration>
<hornetqConfigurationDir>${basedir}/target/classes/hornetq/server0</hornetqConfigurationDir>
<systemProperties>
<property>
<name>build.directory</name>
<value>${basedir}/target/</value>
</property>
<property>
<name>udp-address</name>
<value>${udp-address}</value>
</property>
</systemProperties>
</configuration>
</execution>
<execution>
<id>start1</id>
<goals>
<goal>start</goal>
</goals>
<configuration>
<jndiPort>1199</jndiPort>
<jndiRmiPort>1198</jndiRmiPort>
<hornetqConfigurationDir>${basedir}/target/classes/hornetq/server1</hornetqConfigurationDir>
<fork>true</fork>
<systemProperties>
<property>
<name>build.directory</name>
<value>${basedir}/target/</value>
</property>
<property>
<name>udp-address</name>
<value>${udp-address}</value>
</property>
</systemProperties>
</configuration>
</execution>
<execution>
<id>runClient</id>
<goals>
<goal>runClient</goal>
</goals>
<configuration>
<clientClass>org.hornetq.jms.example.ReplicatedFailbackExample</clientClass>
<args>
<param>jnp://localhost:1099</param>
<param>jnp://localhost:1199</param>
</args>
<systemProperties>
<property>
<name>exampleConfigDir</name>
<value>${basedir}/target/classes/hornetq</value>
</property>
</systemProperties>
</configuration>
</execution>
<execution>
<id>stop0</id>
<goals>
<goal>stop</goal>
</goals>
<configuration>
<hornetqConfigurationDir>${basedir}/target/classes/hornetq/server0</hornetqConfigurationDir>
</configuration>
</execution>
<execution>
<id>stop1</id>
<goals>
<goal>stop</goal>
</goals>
<configuration>
<hornetqConfigurationDir>${basedir}/target/classes/hornetq/server1</hornetqConfigurationDir>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.hornetq.examples.jms</groupId>
<artifactId>hornetq-jms-replicated-failback-example</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.hornetq</groupId>
<artifactId>hornetq-core</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.hornetq</groupId>
<artifactId>hornetq-jms</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty</artifactId>
<version>3.4.5.Final</version>
</dependency>
<dependency>
<groupId>org.jboss.javaee</groupId>
<artifactId>jboss-jms-api</artifactId>
<version>1.1.0.GA</version>
</dependency>
<dependency>
<groupId>org.jboss.naming</groupId>
<artifactId>jnpserver</artifactId>
<version>5.0.3.GA</version>
</dependency>
</dependencies>
<configuration>
<waitOnStart>false</waitOnStart>
<systemProperties>
<property>
<name>build.directory</name>
<value>${basedir}/target/</value>
</property>
</systemProperties>
</configuration>
</plugin>
</plugins>
</build>

</project>
153 changes: 153 additions & 0 deletions examples/jms/replicated-failback/readme.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
<html>
<head>
<title>HornetQ JMS Multiple Failover using Replication 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 Multiple Failover using Replication Example</h1>

<b>Note that failback example is not working in 2.3.0.Alpha but will be fully functional in the next release.</b>

<p>This example demonstrates three servers coupled as a live-backup-backup group for high availability (HA) using replication, and a client
connection failing over from live to backup when the live server is crashed and then to the second backup once the new live fails.</p>

<p>For more information on HornetQ failover and HA, 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</code> from this directory</i></p>
<p>In this example, the live server is server 1, and the backup server is server 0</p>
<p>The connection will initially be created to server1, server 1 will crash, and the client will carry on
seamlessly on server 0, the backup server.</p>

<ol>
<li>Get an initial context for looking up JNDI from server #0.</li>
<pre class="prettyprint">
initialContext = getContext(0);
</pre>

<li>Look up the JMS resources from JNDI on server #0.</li>
<pre class="prettyprint">
Queue queue = (Queue)initialContext.lookup("/queue/exampleQueue");
ConnectionFactory connectionFactory = (ConnectionFactory)initialContext.lookup("/ConnectionFactory");
</pre>

<li>Create a JMS Connection</li>
<pre class="prettyprint">
connection = connectionFactory.createConnection();
</pre>

<li>Create a client acknowledged Session</li>
<pre class="prettyprint">
Session session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
</pre>

<li>Start the connection to ensure delivery occurs</li>
<pre class="prettyprint">
connection.start();
</pre>

<li>Create a JMS MessageProducer</li>
<pre class="prettyprint">
MessageProducer producer = session.createProducer(queue);
</pre>

<li>Create a JMS MessageConsumer</li>
<pre class="prettyprint">
MessageConsumer consumer = session.createConsumer(queue);
</pre>

<li>Send some messages to server #0, the live server</li>
<pre class="prettyprint">
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());
}
</pre>

<li>Receive and acknowledge a third of the sent messages</li>
<pre class="prettyprint">
TextMessage message0 = null;
for (int i = 0; i < numMessages / 3; i++)
{
message0 = (TextMessage)consumer.receive(5000);
System.out.println("Got message: " + message0.getText());
}
message0.acknowledge();
</pre>

<p>When server #0 crashes, the client automatically detects the failure and automatically
fails over from server #0 to server #1 (in your real program you wouldn't need to sleep).
</p>

<li>Acknowledging the 2nd third of the sent messages will fail as failover to the backup server has occurred</li>
<pre class="prettyprint">
try
{
message0.acknowledge();
}
catch (JMSException e)
{
System.err.println("Got exception while acknowledging message: " + e.getMessage());
}
</pre>

<li>Consume again the 2nd third of the messages again. Note that they are not considered as redelivered.</li>
<pre class="prettyprint">
for (int i = numMessages / 3; i < (numMessages / 3) * 2; i++)
{
message0 = (TextMessage)consumer.receive(5000);
System.out.printf("Got message: %s (redelivered?: %s)\n", message0.getText(), message0.getJMSRedelivered());
}
message0.acknowledge();
</pre>

<p>now we Kill server #1 and wait for the server #2 to take over the responsibilities.</p>

<li>Acknowledging the 3rd third of the sent messages will fail as failover to the backup server has occurred</li>
<pre class="prettyprint">
try
{
message0.acknowledge();
}
catch (JMSException e)
{
System.err.println("Got exception while acknowledging message: " + e.getMessage());
}
</pre>


<li>Consume again the 3rd third of the messages again. Note that they are not considered as redelivered</li>
<pre class="prettyprint">
for (int i = (numMessages / 3) * 2; i < numMessages; i++)
{
message0 = (TextMessage)consumer.receive(5000);
System.out.printf("Got message: %s (redelivered?: %s)\n", message0.getText(), message0.getJMSRedelivered());
}
message0.acknowledge();
</pre>

<li>And finally, <strong>always</strong> remember to close your 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">
finally
{
if (connection != null)
{
connection.close();
}

if (initialContext != null)
{
initialContext.close();
}
}
</pre>

</ol>
</body>
</html>
Loading

0 comments on commit 1af3b0a

Please sign in to comment.