[jboss-cvs] JBoss Messaging SVN: r6886 - in trunk/examples/javaee/xarecovery: src/org/jboss/javaee/example and 1 other directories.
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Tue May 19 10:07:17 EDT 2009
Author: jmesnil
Date: 2009-05-19 10:07:17 -0400 (Tue, 19 May 2009)
New Revision: 6886
Added:
trunk/examples/javaee/xarecovery/src/org/jboss/javaee/example/XARecoveryExample.java
Removed:
trunk/examples/javaee/xarecovery/src/org/jboss/javaee/example/XARecoveryReceiverExample.java
trunk/examples/javaee/xarecovery/src/org/jboss/javaee/example/XARecoverySenderExample.java
Modified:
trunk/examples/javaee/xarecovery/build.xml
trunk/examples/javaee/xarecovery/readme.html
trunk/examples/javaee/xarecovery/src/org/jboss/javaee/example/server/XARecoveryExampleBean.java
Log:
XARecovery example
* refactored example, the client will now invoke the EJB, wait for
the server to restart and receive the JMS message
* XARecovery configuration and server restart are manual operations
Modified: trunk/examples/javaee/xarecovery/build.xml
===================================================================
--- trunk/examples/javaee/xarecovery/build.xml 2009-05-19 13:54:19 UTC (rev 6885)
+++ trunk/examples/javaee/xarecovery/build.xml 2009-05-19 14:07:17 UTC (rev 6886)
@@ -25,20 +25,23 @@
<!-- =========================================================================================== -->
-<project default="sender" name="JBoss Messaging XA Recovery Example">
+<project default="run" name="JBoss Messaging XA Recovery Example">
<import file="../common/build.xml"/>
- <target name="sender">
+ <target name="run">
<antcall target="runExample">
- <param name="example.classname" value="org.jboss.javaee.example.XARecoverySenderExample"/>
+ <param name="example.classname" value="org.jboss.javaee.example.XARecoveryExample"/>
</antcall>
</target>
- <target name="receiver">
- <antcall target="runExample">
- <param name="example.classname" value="org.jboss.javaee.example.XARecoveryReceiverExample"/>
- </antcall>
+ <target name="restart">
+ <echo>==============================</echo>
+ <echo>Restart the server and recover</echo>
+ <echo>==============================</echo>
+ <exec dir="${jboss.home}/bin" executable="sh">
+ <arg line="run.sh -c ${example.name}-example-profile"/>
+ </exec>
+
</target>
-
</project>
\ No newline at end of file
Modified: trunk/examples/javaee/xarecovery/readme.html
===================================================================
--- trunk/examples/javaee/xarecovery/readme.html 2009-05-19 13:54:19 UTC (rev 6885)
+++ trunk/examples/javaee/xarecovery/readme.html 2009-05-19 14:07:17 UTC (rev 6886)
@@ -37,26 +37,24 @@
<h2>Example step-by-step</h2>
+ <p><strong>Make sure you have configured XA recovery in <code>$JBOSS_HOME/server/default-with-jbm2/conf/jbossts-properties.xml</code>.</strong></p>
<p><em>You need to deploy the example <em>before starting the server</em>, type <code>ant deploy</code> from this directory<br />
- You need to start JBoss AS 5 with the <code>default-with-jbm2</code> configuration.<br />
- Once the example is deployed in JBoss AS 5, type <code>ant sender</code> to start the example.
- This will also crash the server, you will need to restart it.<br />
- Once it is restarted, type <code>ant receiver</code> to receive the JMS messages which was recovered.<br />
+ Once the example is deployed in JBoss AS 5, type <code>ant run</code> to start the example. <br />
+ This will crash the server: when informed, type <code>ant restart</code> in the terminal where you deployed the example
+ to restart the server.<br />
Type <code>ant undeploy</code> to undeploy the example from JBoss AS 5.</em></p>
- The example code is composed of 3 main classes:
+ The example code is composed of 2 main classes:
<dl>
- <dt><code>XARecoverySenderExample</code></dt>
- <dd>the client application to invoke the EJB</dd>
- <dt><code>XARecoveryReceiverExample</code></dt>
- <dd>the client application to receive a JMS message</dd>
+ <dt><code>XARecoveryExample</code></dt>
+ <dd>the client application to invoke the EJB and receive the message</dd>
<dt><code>XARecoveryExampleBean</code></dt>
<dd>a Stateless EJB</dd>
</dl>
<h3>Example Application</h3>
- <p>Let's take a look at XARecoverySenderExample first.</p>
+ <p>Let's take a look at XARecoveryExample first.</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 <a href="config/jndi.properties">jndi.properties</a></li>
@@ -73,8 +71,42 @@
<li>We invoke the EJB's <code>send</code> method. This method will send a JMS text message (with the text passed in parameter)
and crash the server when committing the transaction</li>
<pre>
- <code>service.send("This is a text message");</code>
+ <code>String message = "This is a text message sent at " + new Date();
+ System.out.println("invoking the EJB service with text: " + message);
+ try
+ {
+ service.send(message);
+ }
+ catch (Exception e)
+ {
+ System.out.println("#########################");
+ System.out.println("The server crashed: " + e.getMessage());
+ System.out.println("#########################");
+ }</code>
</pre>
+
+ <p><em>At that time, the server is crashed and must be restarted by typing <code>ant restart</code>
+ in the terminal where you typed <code>ant deploy</code></em></p>
+
+ <li>We will try to receive a message. Once the server is restarted, the message will be recovered and the consumer will receive it
+ <pre>
+ <code>boolean received = false;
+ while (!received)
+ {
+ try
+ {
+ Thread.sleep(15000);
+ receiveMessage();
+ received = true;
+ }
+ catch (Exception e)
+ {
+ System.out.println(".");
+ }
+ }</code>
+ </pre>
+ <p>The <code>receiveMessage()</code> method contains code to receive a text message from the
+ JMS Queue and display it.</p>
<li>And finally, <b>always</b> remember to close your resources after use, in a <code>finally</code> block.</li>
Copied: trunk/examples/javaee/xarecovery/src/org/jboss/javaee/example/XARecoveryExample.java (from rev 6875, trunk/examples/javaee/xarecovery/src/org/jboss/javaee/example/XARecoverySenderExample.java)
===================================================================
--- trunk/examples/javaee/xarecovery/src/org/jboss/javaee/example/XARecoveryExample.java (rev 0)
+++ trunk/examples/javaee/xarecovery/src/org/jboss/javaee/example/XARecoveryExample.java 2009-05-19 14:07:17 UTC (rev 6886)
@@ -0,0 +1,145 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2005-2008, Red Hat Middleware LLC, and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.javaee.example;
+
+import java.util.Date;
+
+import javax.jms.Connection;
+import javax.jms.ConnectionFactory;
+import javax.jms.MessageConsumer;
+import javax.jms.Queue;
+import javax.jms.Session;
+import javax.jms.TextMessage;
+import javax.naming.InitialContext;
+
+import org.jboss.javaee.example.server.XARecoveryExampleService;
+
+/**
+ * An example which invokes an EJB. The EJB will be involved in a
+ * transaction with a "buggy" XAResource to crash the server.
+ * When the server is restarted, the recovery manager will recover the message
+ * so that the consumer can receive it.
+ *
+ * @author <a href="mailto:andy.taylor at jboss.org">Andy Taylor</a>
+ * @author <a href="mailto:jmesnil at redhat.com">Jeff Mesnil</a>
+ */
+public class XARecoveryExample
+{
+
+ public static void main(String[] args) throws Exception
+ {
+ InitialContext initialContext = null;
+ try
+ {
+ // Step 1. Obtain an Initial Context
+ initialContext = new InitialContext();
+
+ // Step 2. Lookup the EJB
+ XARecoveryExampleService service = (XARecoveryExampleService)initialContext.lookup("xarecovery-example/XARecoveryExampleBean/remote");
+
+ // Step 3. Invoke the send method. This will crash the server
+ String message = "This is a text message sent at " + new Date();
+ System.out.println("invoking the EJB service with text: " + message);
+ try
+ {
+ service.send(message);
+ }
+ catch (Exception e)
+ {
+ System.out.println("#########################");
+ System.out.println("The server crashed: " + e.getMessage());
+ System.out.println("#########################");
+ }
+
+ System.out.println("\n\n\nRestart the server by running 'ant restart' in a terminal");
+
+ // Step 4. We will try to receive a message. Once the server is restarted, the message
+ // will be recovered and the consumer will receive it
+ System.out.println("Waiting for the server to restart and recover before receiving a message");
+
+ boolean received = false;
+ while (!received)
+ {
+ try
+ {
+ Thread.sleep(15000);
+ receiveMessage();
+ received = true;
+ }
+ catch (Exception e)
+ {
+ System.out.println(".");
+ }
+ }
+ }
+ finally
+ {
+ // Step 4. Be sure to close the resources!
+ if (initialContext != null)
+ {
+ initialContext.close();
+ }
+ }
+ }
+
+ private static void receiveMessage() throws Exception
+ {
+ InitialContext initialContext = null;
+ Connection connection = null;
+ try
+ {
+ // Step 1. Obtain an Initial Context
+ initialContext = new InitialContext();
+
+ // Step 2. Lookup the JMS connection factory
+ ConnectionFactory cf = (ConnectionFactory)initialContext.lookup("/ConnectionFactory");
+
+ // Step 3. Lookup the queue
+ Queue queue = (Queue)initialContext.lookup("queue/testQueue");
+
+ // Step 4. Create a connection, a session and a message consumer for the queue
+ connection = cf.createConnection();
+ Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+ MessageConsumer consumer = session.createConsumer(queue);
+
+ // Step 5. Start the connection
+ connection.start();
+
+ // Step 6. Receive the message sent by the EJB
+ System.out.println("\nwaiting to receive a message...");
+ TextMessage messageReceived = (TextMessage)consumer.receive(3600 * 1000);
+ System.out.format("Received message: %s \n\t(JMS MessageID: %s)\n", messageReceived.getText(), messageReceived.getJMSMessageID());
+ }
+ finally
+ {
+ // Step 7. Be sure to close the resources!
+ if (initialContext != null)
+ {
+ initialContext.close();
+ }
+ if (connection != null)
+ {
+ connection.close();
+ }
+ }
+ }
+}
Deleted: trunk/examples/javaee/xarecovery/src/org/jboss/javaee/example/XARecoveryReceiverExample.java
===================================================================
--- trunk/examples/javaee/xarecovery/src/org/jboss/javaee/example/XARecoveryReceiverExample.java 2009-05-19 13:54:19 UTC (rev 6885)
+++ trunk/examples/javaee/xarecovery/src/org/jboss/javaee/example/XARecoveryReceiverExample.java 2009-05-19 14:07:17 UTC (rev 6886)
@@ -1,83 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source
- * Copyright 2005-2008, Red Hat Middleware LLC, and individual contributors
- * by the @authors tag. See the copyright.txt in the distribution for a
- * full listing of individual contributors.
- *
- * This is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * This software is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
- */
-package org.jboss.javaee.example;
-
-import javax.jms.Connection;
-import javax.jms.ConnectionFactory;
-import javax.jms.MessageConsumer;
-import javax.jms.Queue;
-import javax.jms.Session;
-import javax.jms.TextMessage;
-import javax.naming.InitialContext;
-
-/**
- * An example showing how to receive a JMS message.
- * The JMS message was recovered after the server was crashed and restarted.
- *
- * @author <a href="mailto:andy.taylor at jboss.org">Andy Taylor</a>
- * @author <a href="mailto:jmesnil at redhat.com">Jeff Mesnil</a>
- */
-public class XARecoveryReceiverExample
-{
- public static void main(String[] args) throws Exception
- {
- InitialContext initialContext = null;
- Connection connection = null;
- try
- {
- // Step 1. Obtain an Initial Context
- initialContext = new InitialContext();
-
- // Step 2. Lookup the JMS connection factory
- ConnectionFactory cf = (ConnectionFactory)initialContext.lookup("/ConnectionFactory");
-
- // Step 3. Lookup the queue
- Queue queue = (Queue)initialContext.lookup("queue/testQueue");
-
- // Step 4. Create a connection, a session and a message consumer for the queue
- connection = cf.createConnection();
- Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
- MessageConsumer consumer = session.createConsumer(queue);
-
- // Step 5. Start the connection
- connection.start();
-
- System.out.println("waiting to receive a message...");
-
- // Step 6. Receive the message sent by the EJB
- TextMessage messageReceived = (TextMessage)consumer.receive(3600 * 1000);
- System.out.format("Received message: %s (%s)\n", messageReceived.getText(), messageReceived.getJMSMessageID());
- }
- finally
- {
- // Step 7. Be sure to close the resources!
- if (initialContext != null)
- {
- initialContext.close();
- }
- if (connection != null)
- {
- connection.close();
- }
- }
- }
-}
Deleted: trunk/examples/javaee/xarecovery/src/org/jboss/javaee/example/XARecoverySenderExample.java
===================================================================
--- trunk/examples/javaee/xarecovery/src/org/jboss/javaee/example/XARecoverySenderExample.java 2009-05-19 13:54:19 UTC (rev 6885)
+++ trunk/examples/javaee/xarecovery/src/org/jboss/javaee/example/XARecoverySenderExample.java 2009-05-19 14:07:17 UTC (rev 6886)
@@ -1,61 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source
- * Copyright 2005-2008, Red Hat Middleware LLC, and individual contributors
- * by the @authors tag. See the copyright.txt in the distribution for a
- * full listing of individual contributors.
- *
- * This is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * This software is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
- */
-package org.jboss.javaee.example;
-
-import org.jboss.javaee.example.server.XARecoveryExampleService;
-
-import javax.naming.InitialContext;
-
-/**
- * An example which invokes an EJB. The EJB will "pause" the server so that it
- * can be "crashed" to show how recovery works when the server is restarted.
- *
- * @author <a href="mailto:andy.taylor at jboss.org">Andy Taylor</a>
- * @author <a href="mailto:jmesnil at redhat.com">Jeff Mesnil</a>
- */
-public class XARecoverySenderExample
-{
- public static void main(String[] args) throws Exception
- {
- InitialContext initialContext = null;
- try
- {
- // Step 1. Obtain an Initial Context
- initialContext = new InitialContext();
-
- // Step 2. Lookup the EJB
- XARecoveryExampleService service = (XARecoveryExampleService)initialContext.lookup("xarecovery-example/XARecoveryExampleBean/remote");
-
- // Step 3. Invoke the sendAndUpdate method
- service.send("This is a text message");
- System.out.println("invoked the EJB service");
- }
- finally
- {
- // Step 4. Be sure to close the resources!
- if (initialContext != null)
- {
- initialContext.close();
- }
- }
- }
-}
Modified: trunk/examples/javaee/xarecovery/src/org/jboss/javaee/example/server/XARecoveryExampleBean.java
===================================================================
--- trunk/examples/javaee/xarecovery/src/org/jboss/javaee/example/server/XARecoveryExampleBean.java 2009-05-19 13:54:19 UTC (rev 6885)
+++ trunk/examples/javaee/xarecovery/src/org/jboss/javaee/example/server/XARecoveryExampleBean.java 2009-05-19 14:07:17 UTC (rev 6886)
@@ -91,7 +91,7 @@
// Step 10. Send The Text Message
TextMessage message = session.createTextMessage(text);
messageProducer.send(message);
- System.out.format("Sent message: %s (%s)\n", message.getText(), message.getJMSMessageID());
+ System.out.format("Sent message: %s\n\t(JMS MessageID: %s)\n", message.getText(), message.getJMSMessageID());
// Step 12. Delist the failing XAResource
tx.delistResource(failingXAResource, XAResource.TMSUCCESS);
More information about the jboss-cvs-commits
mailing list