[jboss-cvs] JBoss Messaging SVN: r6572 - in trunk/examples/javaee: mdb-remote and 2 other directories.
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Mon Apr 27 00:45:52 EDT 2009
Author: clebert.suconic at jboss.com
Date: 2009-04-27 00:45:52 -0400 (Mon, 27 Apr 2009)
New Revision: 6572
Modified:
trunk/examples/javaee/common/build.xml
trunk/examples/javaee/mdb-remote/build.xml
trunk/examples/javaee/mdb-remote/readme.html
trunk/examples/javaee/mdb-remote/src/org/jboss/javaee/example/MDBRemoteClientExample.java
trunk/examples/javaee/mdb-remote/src/org/jboss/javaee/example/server/MDBRemoteExample.java
trunk/examples/javaee/mdb-remote/src/org/jboss/javaee/example/server/StatelessSender.java
Log:
Changes on MDB Remote example
Modified: trunk/examples/javaee/common/build.xml
===================================================================
--- trunk/examples/javaee/common/build.xml 2009-04-27 03:21:51 UTC (rev 6571)
+++ trunk/examples/javaee/common/build.xml 2009-04-27 04:45:52 UTC (rev 6572)
@@ -112,7 +112,12 @@
</jar>
</target>
- <target name="deploy" depends="validate-jboss, ear">
+ <target name="deploy">
+ <antcall target="do-deploy"></antcall>
+ </target>
+
+ <target name="do-deploy" depends="validate-jboss, ear">
+ <!-- some examples may choose to do a little extra on deploy and call this target directly -->
<copy file="${build.dir}/mdb-example.ear" todir="${jboss.home}/server/jbm2_default/deploy"/>
</target>
Modified: trunk/examples/javaee/mdb-remote/build.xml
===================================================================
--- trunk/examples/javaee/mdb-remote/build.xml 2009-04-27 03:21:51 UTC (rev 6571)
+++ trunk/examples/javaee/mdb-remote/build.xml 2009-04-27 04:45:52 UTC (rev 6572)
@@ -29,9 +29,12 @@
<import file="../common/build.xml"/>
- <target name="install">
+ <target name="deploy">
<copy file="./server-config/jms-remote-ds.xml" todir="${jboss.home}/server/jbm2_default/deploy"/>
+ <antcall target="do-deploy"></antcall>
</target>
+
+
<target name="run">
<antcall target="runExample">
Modified: trunk/examples/javaee/mdb-remote/readme.html
===================================================================
--- trunk/examples/javaee/mdb-remote/readme.html 2009-04-27 03:21:51 UTC (rev 6571)
+++ trunk/examples/javaee/mdb-remote/readme.html 2009-04-27 04:45:52 UTC (rev 6572)
@@ -79,8 +79,6 @@
<?xml version="1.0" encoding="UTF-8"?>
-<!-- $Id: ra.xml 76819 2008-08-08 11:04:20Z jesper.pedersen $ -->
-
<connector xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
@@ -136,7 +134,7 @@
<li>Start the second server with <code>./run.sh -c jbm2_default_2 -Djboss.service.binding.set=ports-01</li>
</ol>
- <h2>Example step-by-step</h2>
+ <h2>Example part step-by-step</h2>
<p><i>To deploy the example, simply type <code>ant deploy</code> from this directory **</i></p>
<p><i>To run the example, simply type <code>ant</code> from this directory</i></p>
<p><i>To undeploy the example, simply type <code>ant undeploy</code> from this directory **</i></p>
@@ -148,73 +146,75 @@
<code>initialContext = new InitialContext();</code>
</pre>
- <li>We look up the JMS queue object from JNDI</li>
- <pre>
- <code>Queue queue = (Queue) initialContext.lookup("/queue/testQueue");</code>
- </pre>
+ <li>Getting a reference to the Stateless Bean</li>
+ <pre><code>
+ StatelessSenderService sender = (StatelessSenderService)initialContext.lookup("mdb-example/StatelessSender/remote");
+ </code></pre>
+
+ <li>Calling the Stateless Bean</li>
+ <pre><code>
+ sender.sendHello("Hello there MDB!");
+ </code></pre>
- <li>We look up the JMS connection factory object from JNDI</li>
- <pre>
- <code>ConnectionFactory cf = (ConnectionFactory) initialContext.lookup("/ConnectionFactory");</code>
- </pre>
- <li>We create a JMS connection</li>
- <pre>
- <code>connection = cf.createConnection();</code>
- </pre>
+ <li>On StatelessSender.java: Define the destination that will receive the message (instead of using JNDI to the remote server)</li>
+ <pre><code>
+ JBossQueue destQueue = new JBossQueue("testQueue");
+ </code></pre>
+
+ <li>Create a connection to a remote server using a connection-factory (look at the deployed file jms-remote-ds.xml). JCA will actually manage this thorugh a <i>Connection Pool</i></li>
+ <pre><code>
+ Connection conn = connectionFactory.createConnection("guest", "guest");
+ </code></pre>
+
+ <li>Send a message to a remote queue</li>
+ <pre><code>
+ Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
+ MessageProducer prod = sess.createProducer(destQueue);
+ prod.send(sess.createTextMessage(message));
+ </code></pre>
- <li>We create a JMS session. The session is created as non transacted and will auto acknowledge messages.</li>
- <pre>
- <code>Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);</code>
- </pre>
+ <li>Close the queue. (Since this is a JCA connection, this close will save the connection to a connection pool)</li>
+ <pre><code>
+ sess.close();
+ </code></pre>
+
+ <li>The message is received on the MDB, using the Remote Queue</li>
- <li>We create a JMS message producer on the session. This will be used to send the messages.</li>
- <pre>
- <code>MessageProducer messageProducer = session.createProducer(queue);</code>
- </pre>
- <li>We create a JMS text messages that we are going to send.</li>
- <pre>
- <code> TextMessage message = session.createTextMessage("This is a text message");</code>
- </pre>
- <li>We send messages to the queue</li>
- <pre>
- <code>messageProducer.send(message);</code>
- </pre>
-
- <li>The MDB receives the message<br />
- We know the message is a TextMessage so we cast to it.
- </li>
- <pre>
- <code>TextMessage tm = (TextMessage)message;</code>
- </pre>
-
- <li>The MDB gets the text and prints it
- </li>
- <pre>
- <code>String text = tm.getText();
- System.out.println("message " + text + " received");
- </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>
+ at MessageDriven(name = "MessageMDBExample",
+ activationConfig =
+ {
+ @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
+ @ActivationConfigProperty(propertyName = "destination", propertyValue = "queue/testQueue"),
+ @ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge"),
+ @ActivationConfigProperty(propertyName = "ConnectorClassName", propertyValue = "org.jboss.messaging.integration.transports.netty.NettyConnectorFactory"),
+ @ActivationConfigProperty(propertyName = "ConnectionParameters", propertyValue = "jbm.remoting.netty.port=5545")
+ })
+public class MDBRemoteExample implements MessageListener
+{
+ public void onMessage(Message message)
+ {
+ try
+ {
+ TextMessage tm = (TextMessage)message;
- <pre>
- <code>finally
- {
- if (initialContext != null)
- {
- initialContext.close();
- }
- if (connection != null)
- {
- connection.close();
- }
- }</code>
- </pre>
+ String text = tm.getText();
+ System.out.println("message " + text + " received");
+
+ }
+ catch (Exception e)
+ {
+ e.printStackTrace();
+ }
+ }
+}
+ </code></pre>
+
</ol>
</body>
Modified: trunk/examples/javaee/mdb-remote/src/org/jboss/javaee/example/MDBRemoteClientExample.java
===================================================================
--- trunk/examples/javaee/mdb-remote/src/org/jboss/javaee/example/MDBRemoteClientExample.java 2009-04-27 03:21:51 UTC (rev 6571)
+++ trunk/examples/javaee/mdb-remote/src/org/jboss/javaee/example/MDBRemoteClientExample.java 2009-04-27 04:45:52 UTC (rev 6572)
@@ -22,17 +22,15 @@
package org.jboss.javaee.example;
import javax.jms.Connection;
-import javax.jms.ConnectionFactory;
-import javax.jms.MessageProducer;
-import javax.jms.Queue;
-import javax.jms.Session;
-import javax.jms.TextMessage;
import javax.naming.InitialContext;
import org.jboss.javaee.example.server.StatelessSenderService;
/**
- * @author <a href="mailto:andy.taylor at jboss.org">Andy Taylor</a>
+ *
+ * MDB Remote & JCA Configuration Example.
+ *
+ * @author <a href="mailto:clebert.suconic at jboss.org">Clebert Suconic</a>
*/
public class MDBRemoteClientExample
{
@@ -45,10 +43,11 @@
//Step 1. Create an initial context to perform the JNDI lookup.
initialContext = new InitialContext();
-
+ //Step 2. Getting a reference to the Stateless Bean
StatelessSenderService sender = (StatelessSenderService)initialContext.lookup("mdb-example/StatelessSender/remote");
- sender.sendHello("Hi, It's friday and I'm still writing tests. What about a bear?");
+ //Step 3. Calling the Stateless Bean
+ sender.sendHello("Hello there MDB!");
initialContext.close();
}
Modified: trunk/examples/javaee/mdb-remote/src/org/jboss/javaee/example/server/MDBRemoteExample.java
===================================================================
--- trunk/examples/javaee/mdb-remote/src/org/jboss/javaee/example/server/MDBRemoteExample.java 2009-04-27 03:21:51 UTC (rev 6571)
+++ trunk/examples/javaee/mdb-remote/src/org/jboss/javaee/example/server/MDBRemoteExample.java 2009-04-27 04:45:52 UTC (rev 6572)
@@ -28,16 +28,19 @@
import javax.jms.TextMessage;
/**
- * @author <a href="mailto:andy.taylor at jboss.org">Andy Taylor</a>
+ * MDB that is connected to the remote queue.
+ * @author <a href="mailto:clebert.suconic at jboss.org">Clebert Suconic</a>
*/
+
+//Step 8. The message is received on the MDB, using a remote queue.
@MessageDriven(name = "MessageMDBExample",
activationConfig =
{
@ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
- @ActivationConfigProperty(propertyName = "destination", propertyValue = "queue/testQueue"),
+ @ActivationConfigProperty(propertyName = "destination", propertyValue = "queue/A"),
@ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge"),
@ActivationConfigProperty(propertyName = "ConnectorClassName", propertyValue = "org.jboss.messaging.integration.transports.netty.NettyConnectorFactory"),
- @ActivationConfigProperty(propertyName = "ConnectionParameters", propertyValue = "jbm.remoting.netty.port=5545")
+ @ActivationConfigProperty(propertyName = "ConnectionParameters", propertyValue = "jbm.remoting.netty.port=5545") // Port on the second server
})
public class MDBRemoteExample implements MessageListener
{
@@ -45,10 +48,8 @@
{
try
{
- //Step 9. We know the client is sending a text message so we cast
TextMessage tm = (TextMessage)message;
- //Step 10. get the text from the message.
String text = tm.getText();
System.out.println("message " + text + " received");
Modified: trunk/examples/javaee/mdb-remote/src/org/jboss/javaee/example/server/StatelessSender.java
===================================================================
--- trunk/examples/javaee/mdb-remote/src/org/jboss/javaee/example/server/StatelessSender.java 2009-04-27 03:21:51 UTC (rev 6571)
+++ trunk/examples/javaee/mdb-remote/src/org/jboss/javaee/example/server/StatelessSender.java 2009-04-27 04:45:52 UTC (rev 6572)
@@ -32,7 +32,7 @@
import javax.jms.*;
/**
- * A StatelessSender
+ * A Stateless Bean that will connect to a remote JBM.
*
* @author <a href="mailto:clebert.suconic at jboss.org">Clebert Suconic</a>
*
@@ -43,6 +43,9 @@
public class StatelessSender implements StatelessSenderService
{
+ /**
+ * Resource to be deployed by jms-remote-ds.xml
+ * */
@Resource(mappedName="java:RemoteJmsXA")
private ConnectionFactory connectionFactory;
@@ -52,16 +55,18 @@
*/
public void sendHello(String message) throws Exception
{
- JBossQueue destQueue = new JBossQueue("testQueue");
+ // Step 4. Define the destination that will receive the message (instead of using JNDI to the remote server)
+ JBossQueue destQueue = new JBossQueue("A");
+ // Step 5. Create a connection to a remote server using a connection-factory (look at the deployed file jms-remote-ds.xml)
Connection conn = connectionFactory.createConnection("guest", "guest");
+ // Step 6. Send a message to a remote queue
Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
-
MessageProducer prod = sess.createProducer(destQueue);
-
prod.send(sess.createTextMessage(message));
+ // Step 7. Close the queue. (Since this is a JCA connection, this will just place the connection back to a connection pool)
sess.close();
System.out.println("Sent message \"" + message + "\" on the Stateless");
More information about the jboss-cvs-commits
mailing list