[hornetq-commits] JBoss hornetq SVN: r8551 - in trunk/examples/jms/transaction-failover: src/org/hornetq/jms/example and 1 other directory.
do-not-reply at jboss.org
do-not-reply at jboss.org
Fri Dec 4 09:24:27 EST 2009
Author: jmesnil
Date: 2009-12-04 09:24:27 -0500 (Fri, 04 Dec 2009)
New Revision: 8551
Modified:
trunk/examples/jms/transaction-failover/readme.html
trunk/examples/jms/transaction-failover/src/org/hornetq/jms/example/TransactionFailoverExample.java
Log:
transaction failover example
* rewrite example to have failover with in-flight transaction + retry the tx work
Modified: trunk/examples/jms/transaction-failover/readme.html
===================================================================
--- trunk/examples/jms/transaction-failover/readme.html 2009-12-04 13:53:58 UTC (rev 8550)
+++ trunk/examples/jms/transaction-failover/readme.html 2009-12-04 14:24:27 UTC (rev 8551)
@@ -63,60 +63,54 @@
MessageConsumer consumer = session.createConsumer(queue);
</pre>
- <li>Send some messages to server #1 and commit the session</li>
+ <li>Send half of the messages, kill the live server and send the remaining messages</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());
- }
- session.commit();
+ sendMessages(session, producer, numMessages, true);
</pre>
- <li>Crash server #1, the live server, and wait a little while to make sure
- it has really crashed.<br />
- When server #1 crashes, the client automatically detects the failure and automatically
+ <p>When server #1 crashes, the client automatically detects the failure and automatically
fails over from server #1 to server #0 (in your real program you wouldn't need to sleep).
- </li>
- <pre class="prettyprint">
- killServer(1); // This causes the live server to crash
- Thread.sleep(2000); // Just wait a little while to make sure the live server has really crashed.
- </pre>
+ </p>
- <li>The client is transparently reconnected to server #0 - the backup server.
- As no work has been done by the session during the failover, we can <em>transparently</em> consume messages
- from the session.</li>
+ <li>As failover occured during transaction, the session has been marked for <em>rollback only</em> and commit will fail</li>
<pre class="prettyprint">
- for (int i = 0; i < numMessages; i++)
+ try
{
- TextMessage message0 = (TextMessage)consumer.receive(5000);
- System.out.println("Got message: " + message0.getText());
- }
- session.commit();
+ session.commit();
+ } catch (TransactionRolledBackException e)
+ {
+ ...
+ }
</pre>
- <li>Send some more messages and commit the session</li>
+ <li>In the <code>catch (TransactionRolledBackException e)</code> block, we rollback the session
<pre class="prettyprint">
- for (int i = numMessages; i < numMessages * 2; i++)
- {
- TextMessage message = session.createTextMessage("This is text message " + i);
- producer.send(message);
- System.out.println("Sent message: " + message.getText());
- }
- session.commit();
+ session.rollback();
</pre>
- <li>Consume them</li>
+ <li>We resend all the messages</li>
<pre class="prettyprint">
- for (int i = 0; i < numMessages; i++)
- {
- TextMessage message0 = (TextMessage)consumer.receive(5000);
- System.out.println("Got message: " + message0.getText());
- }
+ sendMessages(session, producer, numMessages, false);
+ </pre>
+
+ <li>We commit the session succesfully: the messages will be all delivered to the activated backup server</li>
+ <pre class="prettyprint">
session.commit();
</pre>
+
+ <li>We are now transparently reconnected to server #0, the backup server.
+ We consume the messages sent before the crash of the live server, commit the session, and check there are no other message on the queue</li>
+ <pre class="prettyprint">
+ for (int i = 0; i < numMessages; i++)
+ {
+ TextMessage message0 = (TextMessage)consumer.receive(5000);
+ System.out.println("Got message: " + message0.getText());
+ }
+ session.commit();
+ System.out.println("Other message on the server? " + consumer.receive(5000));
+ </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">
Modified: trunk/examples/jms/transaction-failover/src/org/hornetq/jms/example/TransactionFailoverExample.java
===================================================================
--- trunk/examples/jms/transaction-failover/src/org/hornetq/jms/example/TransactionFailoverExample.java 2009-12-04 13:53:58 UTC (rev 8550)
+++ trunk/examples/jms/transaction-failover/src/org/hornetq/jms/example/TransactionFailoverExample.java 2009-12-04 14:24:27 UTC (rev 8551)
@@ -19,6 +19,7 @@
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;
+import javax.jms.TransactionRolledBackException;
import javax.naming.InitialContext;
import org.hornetq.common.example.HornetQExample;
@@ -68,57 +69,44 @@
// Step 7. We create a JMS MessageConsumer
MessageConsumer consumer = session.createConsumer(queue);
- // Step 8. We send some messages to server #1, the live server
- for (int i = 0; i < numMessages; i++)
- {
- TextMessage message = session.createTextMessage("This is text message " + i);
- producer.send(message);
- System.out.println("Sent message: " + message.getText());
- }
- session.commit();
+ // Step 8. We send half of the messages, kill the live server and send the remaining messages
+ sendMessages(session, producer, numMessages, true);
- // Step 9. We now cause server #1, the live server to crash, and wait a little while to make sure
- // it has really crashed
- killServer(1);
- Thread.sleep(2000);
-
- // Step 10. We are now transparently reconnected to server #0, the backup server.
- // We consume the messages sent before the crash of the live server and commit the session.
- for (int i = 0; i < numMessages; i++)
+ try
{
- TextMessage message0 = (TextMessage)consumer.receive(5000);
- System.out.println("Got message: " + message0.getText());
- }
- session.commit();
-
- // Step 11. We now send some more messages and commit the session
- for (int i = numMessages; i < numMessages * 2; i++)
+ // Step 9. As failover occured during transaction, the session has been marked for rollback only
+ session.commit();
+ } catch (TransactionRolledBackException e)
{
- TextMessage message = session.createTextMessage("This is text message " + i);
- producer.send(message);
- System.out.println("Sent message: " + message.getText());
+ // Step 10. We rollback the transaction
+ session.rollback();
}
+
+ // Step 11. We resend all the messages
+ sendMessages(session, producer, numMessages, false);
+ // Step 12. We commit the session succesfully: the messages will be all delivered to the activated backup server
session.commit();
- // Step 12. And consume them and commit the session
+
+ // Step 13. We are now transparently reconnected to server #0, the backup server.
+ // We consume the messages sent before the crash of the live server and commit the session.
for (int i = 0; i < numMessages; i++)
{
TextMessage message0 = (TextMessage)consumer.receive(5000);
System.out.println("Got message: " + message0.getText());
}
session.commit();
+ System.out.println("Other message on the server? " + consumer.receive(5000));
return true;
}
finally
{
- // Step 13. Be sure to close our resources!
+ // Step 14. Be sure to close our resources!
if (connection != null)
{
- System.out.println("CLOSING");
connection.close();
- System.out.println("CLOSED");
}
if (initialContext != null)
@@ -128,4 +116,27 @@
}
}
+ private void sendMessages(Session session, MessageProducer producer, int numMessages, boolean killServer) throws Exception
+ {
+ // We send half of messages
+ for (int i = 0; i < numMessages / 2; i++)
+ {
+ TextMessage message = session.createTextMessage("This is text message " + i);
+ producer.send(message);
+ System.out.println("Sent message: " + message.getText());
+ }
+ if (killServer)
+ {
+ killServer(1);
+ Thread.sleep(2000);
+ }
+ // We send the remaining half of messages
+ for (int i = numMessages / 2; i < numMessages; i++)
+ {
+ TextMessage message = session.createTextMessage("This is text message " + i);
+ producer.send(message);
+ System.out.println("Sent message: " + message.getText());
+ }
+ }
+
}
More information about the hornetq-commits
mailing list