[jboss-cvs] JBoss Messaging SVN: r6421 - in trunk: examples/jms and 7 other directories.
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Tue Apr 14 12:09:22 EDT 2009
Author: gaohoward
Date: 2009-04-14 12:09:22 -0400 (Tue, 14 Apr 2009)
New Revision: 6421
Added:
trunk/examples/jms/xa-heuristic/
trunk/examples/jms/xa-heuristic/build.xml
trunk/examples/jms/xa-heuristic/readme.html
trunk/examples/jms/xa-heuristic/server0/
trunk/examples/jms/xa-heuristic/server0/client-jndi.properties
trunk/examples/jms/xa-heuristic/server0/jbm-configuration.xml
trunk/examples/jms/xa-heuristic/server0/jbm-jms.xml
trunk/examples/jms/xa-heuristic/server0/jbm-queues.xml
trunk/examples/jms/xa-heuristic/server0/jbm-security.xml
trunk/examples/jms/xa-heuristic/server0/jbm-standalone-beans.xml
trunk/examples/jms/xa-heuristic/src/
trunk/examples/jms/xa-heuristic/src/org/
trunk/examples/jms/xa-heuristic/src/org/jboss/
trunk/examples/jms/xa-heuristic/src/org/jboss/jms/
trunk/examples/jms/xa-heuristic/src/org/jboss/jms/example/
trunk/examples/jms/xa-heuristic/src/org/jboss/jms/example/XAHeuristicExample.java
Modified:
trunk/.classpath
Log:
added xa-heuristic example
Modified: trunk/.classpath
===================================================================
--- trunk/.classpath 2009-04-14 16:08:07 UTC (rev 6420)
+++ trunk/.classpath 2009-04-14 16:09:22 UTC (rev 6421)
@@ -1,9 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry excluding="**/.svn/**/*" kind="src" path="src/main"/>
+ <classpathentry kind="src" path="examples/jms/xa-heuristic/src"/>
+ <classpathentry kind="src" path="build/src"/>
<classpathentry kind="src" path="examples/jms/xa-receive/src"/>
<classpathentry kind="src" path="examples/jms/xa-send/src"/>
- <classpathentry kind="src" path="build/src"/>
<classpathentry kind="src" path="tests/jms-tests/config"/>
<classpathentry kind="src" path="tests/config"/>
<classpathentry kind="src" path="src/config"/>
Added: trunk/examples/jms/xa-heuristic/build.xml
===================================================================
--- trunk/examples/jms/xa-heuristic/build.xml (rev 0)
+++ trunk/examples/jms/xa-heuristic/build.xml 2009-04-14 16:09:22 UTC (rev 6421)
@@ -0,0 +1,48 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE project [
+ <!ENTITY libraries SYSTEM "../../../thirdparty/libraries.ent">
+ ]>
+
+<!-- =========================================================================================== -->
+<!-- -->
+<!-- JBoss, Home of Professional Open Source -->
+<!-- Copyright 2005, JBoss Inc., and individual contributors as indicated -->
+<!-- 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. -->
+<!-- -->
+<!-- =========================================================================================== -->
+
+
+<project default="run" name="JBoss Messaging JMS XA Heuristic Example">
+
+ <import file="../common/build.xml"/>
+
+ <target name="run">
+ <antcall target="runExample">
+ <param name="example.classname" value="org.jboss.jms.example.XAHeuristicExample"/>
+ </antcall>
+ </target>
+
+ <target name="runRemote">
+ <antcall target="runExample">
+ <param name="example.classname" value="org.jboss.jms.example.XAHeuristicExample"/>
+ <param name="jbm.example.runServer" value="false"/>
+ </antcall>
+ </target>
+
+</project>
Added: trunk/examples/jms/xa-heuristic/readme.html
===================================================================
--- trunk/examples/jms/xa-heuristic/readme.html (rev 0)
+++ trunk/examples/jms/xa-heuristic/readme.html 2009-04-14 16:09:22 UTC (rev 6421)
@@ -0,0 +1,229 @@
+<html>
+ <head>
+ <title>JBoss Messaging JMS XA Heuristic Example</title>
+ <link rel="stylesheet" type="text/css" href="../common/common.css">
+ </head>
+ <body>
+ <h1>JMS XA Heuristic Example</h1>
+ <br>
+ <p>This example shows you how to make an XA heuristic decision through JBoss Messageing Management Interface.</p>
+
+ <p>A heuristic decision is a unilateral decision to commit or rollback an XA transaction branch after it has
+ been prepared. </p>
+
+ <p>In this example we simulate a transaction manager to control the transactions. First we create an XASession
+ and enlist it in a transaction through its XAResource. We then send a text message, 'hello' and end/prepare the transaction
+ on the XAResource, but neither commit nor roll back the transaction. Another transaction is created and
+ associated with the same XAResource, and a second message, 'world' is sent on behalf of the second transaction. Again we leave
+ the second transaction in prepare state.
+ Then we get the MBeanServerConnection object to manipulate the prepared transactions. To illustrate, we roll back the first
+ transaction but commit the second. This will result in that only the message 'world' is received. </p>
+
+ <p>This example uses JMX to manipulate transactions in a JBoss Messaging Server. For details on JMX facilities with JBoss
+ Messaging, please look at the JMX Example.</p>
+
+ <br>
+ <h2>Example step-by-step</h2>
+ <p><i>To run the example, simply type <code>ant</code> from this directory</i></p>
+ <br>
+ <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>
+ <code>InitialContext initialContext = getContext(0);</code>
+ </pre>
+
+ <li>We look-up the JMS queue object from JNDI</li>
+ <pre>
+ <code>Queue queue = (Queue) initialContext.lookup("/queue/exampleQueue");</code>
+ </pre>
+
+ <li>We perform a lookup on the XA Connection Factory</li>
+ <pre>
+ <code>XAConnectionFactory cf = (XAConnectionFactory) initialContext.lookup("/XAConnectionFactory");</code>
+ </pre>
+
+ <li>We create a JMS XAConnection</li>
+ <pre>
+ <code>connection = cf.createXAConnection();</code>
+ </pre>
+
+ <li>We Start the connection</li>
+ <pre>
+ <code>connection.start();</code>
+ </pre>
+
+ <li>We create a JMS XASession</li>
+ <pre>
+ <code>XASession xaSession = connection.createXASession();</code>
+ </pre>
+
+ <li>We create a normal session</li>
+ <pre>
+ <code>Session normalSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);</code>
+ </pre>
+
+ <li>We create a normal Message Consumer</li>
+ <pre>
+ <code>
+ MessageConsumer normalConsumer = normalSession.createConsumer(queue);
+ normalConsumer.setMessageListener(new SimpleMessageListener());
+ </code>
+ </pre>
+
+ <li>We get the JMS Session</li>
+ <pre>
+ <code>Session session = xaSession.getSession();</code>
+ </pre>
+
+ <li>We create a message producer</li>
+ <pre>
+ <code>MessageProducer producer = session.createProducer(queue); </code>
+ </pre>
+
+ <li>We create two Text Messages</li>
+ <pre>
+ <code>
+ TextMessage helloMessage = session.createTextMessage("hello");
+ TextMessage worldMessage = session.createTextMessage("world");
+ </code>
+ </pre>
+
+ <li>We create a transaction</li>
+ <pre>
+ <code>Xid xid1 = new XidImpl("xa-example1".getBytes(), 1, UUIDGenerator.getInstance().generateStringUUID().getBytes());</code>
+ </pre>
+
+ <li>We get the JMS XAResource</li>
+ <pre>
+ <code>XAResource xaRes = xaSession.getXAResource();</code>
+ </pre>
+
+ <li>We begin the Transaction work</li>
+ <pre>
+ <code>xaRes.start(xid1, XAResource.TMNOFLAGS);</code>
+ </pre>
+
+ <li>We do work, sending hello message.</li>
+ <pre>
+ <code>
+ normalProducer.send(helloMessage);
+ </code>
+ </pre>
+
+ <li>We stop the work for xid1</li>
+ <pre>
+ <code>xaRes.end(xid1, XAResource.TMSUCCESS);</code>
+ </pre>
+
+ <li>We prepare xid1</li>
+ <pre>
+ <code>xaRes.prepare(xid1);</code>
+ </pre>
+
+ <li>We check the none should be received.</li>
+ <pre>
+ <code>checkNoMessageReceived();</code>
+ </pre>
+
+ <li>We create another transaction </li>
+ <pre>
+ <code>Xid xid2 = new XidImpl("xa-example2".getBytes(), 1, UUIDGenerator.getInstance().generateStringUUID().getBytes());</code>
+ </pre>
+
+ <li>We begin the transaction work</li>
+ <pre>
+ <code>xaRes.start(xid2, XAResource.TMNOFLAGS);</code>
+ </pre>
+
+ <li>We send the second message</li>
+ <pre>
+ <code>producer.send(worldMessage);</code>
+ </pre>
+
+ <li>We stop the work for xid2</li>
+ <pre>
+ <code>xaRes.end(xid2, XAResource.TMSUCCESS);</code>
+ </pre>
+
+ <li>We prepare xid2</li>
+ <pre>
+ <code>xaRes.prepare(xid2);</code>
+ </pre>
+
+ <li>Again, no messages should be received!</li>
+ <pre>
+ <code>checkNoMessageReceived();</code>
+ </pre>
+
+ <li>We create JMX Connector to connect to the server's MBeanServer.</li>
+ <pre>
+ <code>
+ JMXConnector connector = JMXConnectorFactory.connect(new JMXServiceURL(JMX_URL), new HashMap<String, String>());
+ </code>
+ </pre>
+
+ <li>We retrieve the MBeanServerConnection</li>
+ <pre>
+ <code>MBeanServerConnection mbsc = connector.getMBeanServerConnection();</code>
+ </pre>
+
+ <li>We list the prepared transactions</li>
+ <pre>
+ <code>
+ ObjectName serverObject = ObjectNames.getMessagingServerObjectName();
+ String[] infos = (String[])mbsc.invoke(serverObject, "listPreparedTransactions", null, null);
+
+ System.out.println("Prepared transactions: ");
+ for (String i : infos)
+ {
+ System.out.println(i);
+ }
+ </code>
+ </pre>
+
+ <li>We roll back the first transaction</li>
+ <pre>
+ <code>mbsc.invoke(serverObject, "rollbackPreparedTransaction", new String[] {XidImpl.toBase64String(xid1)}, new String[]{"java.lang.String"});</code>
+ </pre>
+
+ <li>We commit the second one</li>
+ <pre>
+ <code>mbsc.invoke(serverObject, "commitPreparedTransaction", new String[] {XidImpl.toBase64String(xid2)}, new String[]{"java.lang.String"});</code>
+ </pre>
+
+ <li>We check the result, only the 'world' message received</li>
+ <pre>
+ <code>checkMessageReceived("world");</code>
+ </pre>
+
+ <li>We check the prepared transaction again, should have none.</li>
+ <pre>
+ <code>
+ infos = (String[])mbsc.invoke(serverObject, "listPreparedTransactions", null, null);
+ System.out.println("No. of prepared transactions now: " + infos.length);
+ </code>
+ </pre>
+
+ <li>We close the JMX connector.</li>
+ <pre>
+ <code>connector.close();</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>
+ <code>finally
+ {
+ if (initialContext != null)
+ {
+ initialContext.close();
+ }
+ if (connection != null)
+ {
+ connection.close();
+ }
+ }</code>
+ </pre>
+ </ol>
+ </body>
+</html>
Added: trunk/examples/jms/xa-heuristic/server0/client-jndi.properties
===================================================================
--- trunk/examples/jms/xa-heuristic/server0/client-jndi.properties (rev 0)
+++ trunk/examples/jms/xa-heuristic/server0/client-jndi.properties 2009-04-14 16:09:22 UTC (rev 6421)
@@ -0,0 +1,3 @@
+java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
+java.naming.provider.url=jnp://localhost:1099
+java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
Added: trunk/examples/jms/xa-heuristic/server0/jbm-configuration.xml
===================================================================
--- trunk/examples/jms/xa-heuristic/server0/jbm-configuration.xml (rev 0)
+++ trunk/examples/jms/xa-heuristic/server0/jbm-configuration.xml 2009-04-14 16:09:22 UTC (rev 6421)
@@ -0,0 +1,27 @@
+<deployment xmlns="urn:jboss:messaging"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="urn:jboss:messaging ../schemas/jbm-configuration.xsd">
+ <configuration>
+
+ <!-- Connectors -->
+
+ <connector name="netty-connector">
+ <factory-class>org.jboss.messaging.integration.transports.netty.NettyConnectorFactory</factory-class>
+ <param key="jbm.remoting.netty.port" value="5445" type="Integer"/>
+ </connector>
+
+ <!-- Acceptors -->
+
+ <acceptor name="netty-acceptor">
+ <factory-class>org.jboss.messaging.integration.transports.netty.NettyAcceptorFactory</factory-class>
+ <param key="jbm.remoting.netty.port" value="5445" type="Integer"/>
+ </acceptor>
+
+
+ <!-- Other config -->
+
+ <journal-min-files>2</journal-min-files>
+
+ </configuration>
+
+</deployment>
Added: trunk/examples/jms/xa-heuristic/server0/jbm-jms.xml
===================================================================
--- trunk/examples/jms/xa-heuristic/server0/jbm-jms.xml (rev 0)
+++ trunk/examples/jms/xa-heuristic/server0/jbm-jms.xml 2009-04-14 16:09:22 UTC (rev 6421)
@@ -0,0 +1,18 @@
+<deployment xmlns="urn:jboss:messaging"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="urn:jboss:messaging ../schemas/jbm-jms.xsd ">
+ <!--the connection factory used by the example-->
+ <connection-factory name="ConnectionFactory">
+ <connector-ref connector-name="netty-connector"/>
+ <entry name="ConnectionFactory"/>
+ <entry name="XAConnectionFactory"/>
+ <entry name="java:/ConnectionFactory"/>
+ <entry name="java:/XAConnectionFactory"/>
+ </connection-factory>
+
+ <!--the queue used by the example-->
+ <queue name="exampleQueue">
+ <entry name="/queue/exampleQueue"/>
+ </queue>
+
+</deployment>
\ No newline at end of file
Added: trunk/examples/jms/xa-heuristic/server0/jbm-queues.xml
===================================================================
--- trunk/examples/jms/xa-heuristic/server0/jbm-queues.xml (rev 0)
+++ trunk/examples/jms/xa-heuristic/server0/jbm-queues.xml 2009-04-14 16:09:22 UTC (rev 6421)
@@ -0,0 +1,15 @@
+<settings xmlns="urn:jboss:messaging"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="urn:jboss:messaging ../schemas/jbm-queues.xsd ">
+
+ <!--security for example queue-->
+ <security match="jms.queue.exampleQueue">
+ <permission type="createDurableQueue" roles="guest"/>
+ <permission type="deleteDurableQueue" roles="guest"/>
+ <permission type="createTempQueue" roles="guest"/>
+ <permission type="deleteTempQueue" roles="guest"/>
+ <permission type="consume" roles="guest"/>
+ <permission type="send" roles="guest"/>
+ </security>
+
+</settings>
Added: trunk/examples/jms/xa-heuristic/server0/jbm-security.xml
===================================================================
--- trunk/examples/jms/xa-heuristic/server0/jbm-security.xml (rev 0)
+++ trunk/examples/jms/xa-heuristic/server0/jbm-security.xml 2009-04-14 16:09:22 UTC (rev 6421)
@@ -0,0 +1,7 @@
+<deployment xmlns="urn:jboss:messaging" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="urn:jboss:messaging ../schemas/jbm-security.xsd ">
+ <!-- the default user. this is used where username is null-->
+ <defaultuser name="guest" password="guest">
+ <role name="guest"/>
+ </defaultuser>
+</deployment>
\ No newline at end of file
Added: trunk/examples/jms/xa-heuristic/server0/jbm-standalone-beans.xml
===================================================================
--- trunk/examples/jms/xa-heuristic/server0/jbm-standalone-beans.xml (rev 0)
+++ trunk/examples/jms/xa-heuristic/server0/jbm-standalone-beans.xml 2009-04-14 16:09:22 UTC (rev 6421)
@@ -0,0 +1,57 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<deployment xmlns="urn:jboss:bean-deployer:2.0">
+
+ <bean name="Naming" class="org.jnp.server.NamingBeanImpl"/>
+
+ <!-- JNDI server. Disable this if you don't want JNDI -->
+ <bean name="JNDIServer" class="org.jnp.server.Main">
+ <property name="namingInfo">
+ <inject bean="Naming"/>
+ </property>
+ <property name="port">1099</property>
+ <property name="bindAddress">localhost</property>
+ <property name="rmiPort">1098</property>
+ <property name="rmiBindAddress">localhost</property>
+ </bean>
+
+ <!-- MBean server -->
+ <bean name="MBeanServer" class="javax.management.MBeanServer">
+ <constructor factoryClass="java.lang.management.ManagementFactory"
+ factoryMethod="getPlatformMBeanServer"/>
+ </bean>
+
+ <!-- The core configuration -->
+ <bean name="Configuration" class="org.jboss.messaging.core.config.impl.FileConfiguration"/>
+
+ <!-- The security manager -->
+ <bean name="JBMSecurityManager" class="org.jboss.messaging.core.security.impl.JBMSecurityManagerImpl">
+ <start ignored="true"/>
+ <stop ignored="true"/>
+ </bean>
+
+ <!-- The core server -->
+ <bean name="MessagingServer" class="org.jboss.messaging.core.server.impl.MessagingServerImpl">
+ <constructor>
+ <parameter>
+ <inject bean="Configuration"/>
+ </parameter>
+ <parameter>
+ <inject bean="MBeanServer"/>
+ </parameter>
+ <parameter>
+ <inject bean="JBMSecurityManager"/>
+ </parameter>
+ </constructor>
+ </bean>
+
+ <!-- The JMS server -->
+ <bean name="JMSServerManager" class="org.jboss.messaging.jms.server.impl.JMSServerManagerImpl">
+ <constructor>
+ <parameter>
+ <inject bean="MessagingServer"/>
+ </parameter>
+ </constructor>
+ </bean>
+
+</deployment>
Added: trunk/examples/jms/xa-heuristic/src/org/jboss/jms/example/XAHeuristicExample.java
===================================================================
--- trunk/examples/jms/xa-heuristic/src/org/jboss/jms/example/XAHeuristicExample.java (rev 0)
+++ trunk/examples/jms/xa-heuristic/src/org/jboss/jms/example/XAHeuristicExample.java 2009-04-14 16:09:22 UTC (rev 6421)
@@ -0,0 +1,252 @@
+/*
+ * 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.jms.example;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+
+import javax.jms.JMSException;
+import javax.jms.Message;
+import javax.jms.MessageConsumer;
+import javax.jms.MessageListener;
+import javax.jms.MessageProducer;
+import javax.jms.Queue;
+import javax.jms.Session;
+import javax.jms.TextMessage;
+import javax.jms.XAConnection;
+import javax.jms.XAConnectionFactory;
+import javax.jms.XASession;
+import javax.management.MBeanServerConnection;
+import javax.management.ObjectName;
+import javax.management.remote.JMXConnector;
+import javax.management.remote.JMXConnectorFactory;
+import javax.management.remote.JMXServiceURL;
+import javax.naming.InitialContext;
+import javax.transaction.xa.XAResource;
+import javax.transaction.xa.Xid;
+
+import org.jboss.messaging.core.management.ObjectNames;
+import org.jboss.messaging.core.transaction.impl.XidImpl;
+import org.jboss.messaging.utils.UUIDGenerator;
+
+/**
+ * A simple JMS example showing how to administer un-finished transactions.
+ *
+ * @author <a href="hgao at redhat.com">Howard Gao</a>
+ */
+public class XAHeuristicExample extends JMSExample
+{
+ private volatile boolean result = true;
+ private ArrayList<String> receiveHolder = new ArrayList<String>();
+
+ private String JMX_URL = "service:jmx:rmi:///jndi/rmi://localhost:3001/jmxrmi";
+
+ public static void main(String[] args)
+ {
+ String[] serverJMXArgs = new String[] { "-Dcom.sun.management.jmxremote",
+ "-Dcom.sun.management.jmxremote.port=3001",
+ "-Dcom.sun.management.jmxremote.ssl=false",
+ "-Dcom.sun.management.jmxremote.authenticate=false"
+ };
+ new XAHeuristicExample().run(serverJMXArgs, args);
+ }
+
+ public boolean runExample() throws Exception
+ {
+ XAConnection connection = null;
+ InitialContext initialContext = null;
+ try
+ {
+ //Step 1. Create an initial context to perform the JNDI lookup.
+ initialContext = getContext(0);
+
+ //Step 2. Lookup on the queue
+ Queue queue = (Queue) initialContext.lookup("/queue/exampleQueue");
+
+ //Step 3. Perform a lookup on the XA Connection Factory
+ XAConnectionFactory cf = (XAConnectionFactory) initialContext.lookup("/XAConnectionFactory");
+
+ //Step 4.Create a JMS XAConnection
+ connection = cf.createXAConnection();
+
+ //Step 5. Start the connection
+ connection.start();
+
+ //Step 6. Create a JMS XASession
+ XASession xaSession = connection.createXASession();
+
+ //Step 7. Create a normal session
+ Session normalSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+
+ //Step 8. Create a normal Message Consumer
+ MessageConsumer normalConsumer = normalSession.createConsumer(queue);
+ normalConsumer.setMessageListener(new SimpleMessageListener());
+
+ //Step 9. Get the JMS Session
+ Session session = xaSession.getSession();
+
+ //Step 10. Create a message producer
+ MessageProducer producer = session.createProducer(queue);
+
+ //Step 11. Create two Text Messages
+ TextMessage helloMessage = session.createTextMessage("hello");
+ TextMessage worldMessage = session.createTextMessage("world");
+
+ //Step 12. create a transaction
+ Xid xid1 = new XidImpl("xa-example1".getBytes(), 1, UUIDGenerator.getInstance().generateStringUUID().getBytes());
+
+ //Step 13. Get the JMS XAResource
+ XAResource xaRes = xaSession.getXAResource();
+
+ //Step 14. Begin the Transaction work
+ xaRes.start(xid1, XAResource.TMNOFLAGS);
+
+ //Step 15. do work, sending hello message.
+ producer.send(helloMessage);
+
+ System.out.println("Sent message " + helloMessage.getText());
+
+ //Step 16. Stop the work for xid1
+ xaRes.end(xid1, XAResource.TMSUCCESS);
+
+ //Step 17. Prepare xid1
+ xaRes.prepare(xid1);
+
+ //Step 18. Check none should be received
+ checkNoMessageReceived();
+
+ //Step 19. Create another transaction.
+ Xid xid2 = new XidImpl("xa-example2".getBytes(), 1, UUIDGenerator.getInstance().generateStringUUID().getBytes());
+
+ //Step 20. Begin the transaction work
+ xaRes.start(xid2, XAResource.TMNOFLAGS);
+
+ //Step 21. Send the second message
+ producer.send(worldMessage);
+
+ System.out.println("Sent message " + worldMessage.getText());
+
+ //Step 22. Stop the work for xid2
+ xaRes.end(xid2, XAResource.TMSUCCESS);
+
+ //Step 23. prepare xid2
+ xaRes.prepare(xid2);
+
+ //Step 24. Again, no messages should be received!
+ checkNoMessageReceived();
+
+ //Step 25. Create JMX Connector to connect to the server's MBeanServer
+ JMXConnector connector = JMXConnectorFactory.connect(new JMXServiceURL(JMX_URL), new HashMap<String, String>());
+
+ //Step 26. Retrieve the MBeanServerConnection
+ MBeanServerConnection mbsc = connector.getMBeanServerConnection();
+
+ //Step 27. List the prepared transactions
+ ObjectName serverObject = ObjectNames.getMessagingServerObjectName();
+ String[] infos = (String[])mbsc.invoke(serverObject, "listPreparedTransactions", null, null);
+
+ System.out.println("Prepared transactions: ");
+ for (String i : infos)
+ {
+ System.out.println(i);
+ }
+
+ //Step 28. Roll back the first transaction
+ mbsc.invoke(serverObject, "rollbackPreparedTransaction", new String[] {XidImpl.toBase64String(xid1)}, new String[]{"java.lang.String"});
+
+ //Step 29. Commit the second one
+ mbsc.invoke(serverObject, "commitPreparedTransaction", new String[] {XidImpl.toBase64String(xid2)}, new String[]{"java.lang.String"});
+
+ Thread.sleep(2000);
+
+ //Step 30. Check the result, only the 'world' message received
+ checkMessageReceived("world");
+
+ //Step 31. Check the prepared transaction again, should have none.
+ infos = (String[])mbsc.invoke(serverObject, "listPreparedTransactions", null, null);
+ System.out.println("No. of prepared transactions now: " + infos.length);
+
+ //Step 32. Close the JMX Connector
+ connector.close();
+
+ return result;
+ }
+ finally
+ {
+ //Step 32. Be sure to close our JMS resources!
+ if (initialContext != null)
+ {
+ initialContext.close();
+ }
+ if(connection != null)
+ {
+ connection.close();
+ }
+ }
+ }
+
+ private void checkMessageReceived(String value)
+ {
+ if (receiveHolder.size() != 1)
+ {
+ System.out.println("Number of messages received not correct ! -- " + receiveHolder.size());
+ result = false;
+ }
+ String msg = receiveHolder.get(0);
+ if (!msg.equals(value))
+ {
+ System.out.println("Received message [" + msg + "], but we expect [" + value + "]");
+ result = false;
+ }
+ receiveHolder.clear();
+ }
+
+ private void checkNoMessageReceived()
+ {
+ if (receiveHolder.size() > 0)
+ {
+ System.out.println("Message received, wrong!");
+ result = false;
+ }
+ receiveHolder.clear();
+ }
+
+
+ public class SimpleMessageListener implements MessageListener
+ {
+ public void onMessage(Message message)
+ {
+ try
+ {
+ System.out.println("Message received: " + ((TextMessage)message).getText());
+ receiveHolder.add(((TextMessage)message).getText());
+ }
+ catch (JMSException e)
+ {
+ result = false;
+ e.printStackTrace();
+ }
+ }
+
+ }
+
+}
More information about the jboss-cvs-commits
mailing list