[jboss-cvs] JBoss Messaging SVN: r6426 - in trunk/examples/jms/client-side-load-balancing: src/org/jboss/jms/example and 1 other directory.
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Tue Apr 14 14:12:50 EDT 2009
Author: timfox
Date: 2009-04-14 14:12:50 -0400 (Tue, 14 Apr 2009)
New Revision: 6426
Modified:
trunk/examples/jms/client-side-load-balancing/readme.html
trunk/examples/jms/client-side-load-balancing/src/org/jboss/jms/example/ClientSideLoadBalancingExample.java
Log:
readme
Modified: trunk/examples/jms/client-side-load-balancing/readme.html
===================================================================
--- trunk/examples/jms/client-side-load-balancing/readme.html 2009-04-14 17:56:30 UTC (rev 6425)
+++ trunk/examples/jms/client-side-load-balancing/readme.html 2009-04-14 18:12:50 UTC (rev 6426)
@@ -1,32 +1,25 @@
<html>
<head>
- <title>JBoss Messaging JMS Load Balanced Queue Example/title>
+ <title>JBoss Messaging JMS Client Side Load-Balancing Example</title>
<link rel="stylesheet" type="text/css" href="../common/common.css">
</head>
<body>
- <h1>JBoss Messaging JMS Load Balanced Queue Example</h1>
+ <h1>JBoss Messaging JMS Client Side Load-Balancing Example</h1>
<br>
- <p>This example demonstrates a JMS queue deployed on two different nodes. The two nodes are configured to form a cluster.</p>
- <p>We then create a consumer on the queue on each node, and we create a producer on only one of the nodes.</p>
- <p>We then send some messages via the producer, and we verify that <b>both</b> consumers receive the sent messages
- in a round-robin fashion.</p>
- <p>In other words, JBoss Messaging <b>load balances</b> the sent messages across all consumers on the cluster</p>
- <p>This example uses JNDI to lookup the JMS Queue and ConnectionFactory objects. If you prefer not to use
- JNDI, these could be instantiated directly.</p>
- <p>Here's the relevant snippet from the server configuration, which tells the server to form a cluster between the two nodes
- and to load balance the messages between the nodes.</p>
- <pre>
- <code>
- <cluster-connection name="my-cluster">
- <address>jms</address>
- <retry-interval>500</retry-interval>
- <use-duplicate-detection>true</use-duplicate-detection>
- <forward-when-no-consumers>true</forward-when-no-consumers>
- <max-hops>1</max-hops>
- <discovery-group-ref discovery-group-name="my-discovery-group"/>
- </cluster-connection>
- </code>
- </pre>
+ <p>This example demonstrates how subsequent connections created from a JMS Connection Factory can be created
+ to different nodes of the cluster. In other words it demonstrates how JBoss Messaging does <b>client side load balancing</b> of
+ connections across the cluster.</p>
+ <p>The particular load-balancing policy can be chosen to be random, round-robin or user-defined. Please see the user
+ guide for more details of how to configure the specific load-balancing policy. In this example we will use
+ the default round-robin load balancing policy.</p>
+ <p>The list of servers over which JBoss Messaging will round-robin the connections can either be specified explicitly
+ in the connection factory when creating it, or deploying it on the server, or the factory can be configured
+ to use UDP discovery to discover the list of servers over which to round-robin. This example will use UDP
+ discovery to obtain the list.</p>
+ <p>This example starts three servers which all broadcast their location using UDP discovery. The UDP broadcast configuration
+ can be seen in the <code>jbm-configuration.xml</code> file.</p>
+ <p>A JMS ConnectionFactory is deployed on each server specifying the discovery group that will be used by that
+ connection factory.</p>
<p>For more information on JBoss Messaging load balancing, and clustering in general, please see the clustering
section of the user manual.</p>
<h2>Example step-by-step</h2>
@@ -36,120 +29,124 @@
<li> Get an initial context for looking up JNDI from server 0.</li>
<pre>
<code>
- ic0 = getContext(0);
+ initialContext = getContext(0);
</code>
</pre>
<li>Look-up the JMS Queue object from JNDI</li>
<pre>
- <code>Queue queue = (Queue)ic0.lookup("/queue/exampleQueue");</code>
+ <code>Queue queue = (Queue)initialContext.lookup("/queue/exampleQueue");</code>
</pre>
<li>Look-up a JMS Connection Factory object from JNDI on server 0</li>
<pre>
- <code>ConnectionFactory cf0 = (ConnectionFactory)ic0.lookup("/ConnectionFactory");</code>
+ <code>ConnectionFactory connectionFactory = (ConnectionFactory)initialContext.lookup("/ConnectionFactory");</code>
</pre>
- <li>Get an initial context for looking up JNDI from server 1.</li>
+ <li>We create three connections, since we are using round-robin load-balancing this should
+ result in each connection being connected to a different node of the cluster</li>
<pre>
- <code>ic1 = getContext(1);</code>
+ <code>
+ connectionA = connectionFactory.createConnection();
+
+ connectionB = connectionFactory.createConnection();
+
+ connectionC = connectionFactory.createConnection();
+ </code>
</pre>
- <li>Look-up a JMS Connection Factory object from JNDI on server 1</li>
+ <li>We create a JMS Session on each of those connections</li>
<pre>
- <code>ConnectionFactory cf1 = (ConnectionFactory)ic1.lookup("/ConnectionFactory");
+ <code>
+ Session sessionA = connectionA.createSession(false, Session.AUTO_ACKNOWLEDGE);
+
+ Session sessionB = connectionB.createSession(false, Session.AUTO_ACKNOWLEDGE);
+
+ Session sessionC = connectionC.createSession(false, Session.AUTO_ACKNOWLEDGE);
</code>
</pre>
- <li>We create a JMS Connection connection0 which is a connection to server 0</li>
+ <li>We start the connections to ensure delivery occurs on them</li>
<pre>
<code>
- connection0 = cf0.createConnection();
+ connectionA.start();
+
+ connectionB.start();
+
+ connectionC.start();
</code>
</pre>
- <li>We create a JMS Connection connection1 which is a connection to server 1</li>
+ <li>We create JMS MessageConsumer objects on the sessions</li>
<pre>
<code>
- connection1 = cf1.createConnection();
+ MessageConsumer consumerA = sessionA.createConsumer(queue);
+
+ MessageConsumer consumerB = sessionB.createConsumer(queue);
+
+ MessageConsumer consumerC = sessionC.createConsumer(queue);
</code>
</pre>
- <li>We create a JMS Session on server 0</li>
+ <li>We create JMS MessageProducer objects on the sessions</li>
<pre>
<code>
- Session session0 = connection0.createSession(false, Session.AUTO_ACKNOWLEDGE);
+ MessageProducer producerA = sessionA.createProducer(queue);
+
+ MessageProducer producerB = sessionB.createProducer(queue);
+
+ MessageProducer producerC = sessionC.createProducer(queue);
</code>
</pre>
- <li>We create a JMS Session on server 1</li>
+ <li>We send some messages on each producer</li>
<pre>
<code>
- Session session1 = connection1.createSession(false, Session.AUTO_ACKNOWLEDGE);
- </code>
- </pre>
+ final int numMessages = 10;
- <li>We start the connections to ensure delivery occurs on them</li>
- <pre>
- <code>
- connection0.start();
+ for (int i = 0; i < numMessages; i++)
+ {
+ TextMessage messageA = sessionA.createTextMessage("A:This is text message " + i);
- connection1.start();
- </code>
- </pre>
+ producerA.send(messageA);
- <li>We create JMS MessageConsumer objects on server 0 and server 1</li>
- <pre>
- <code>
- MessageConsumer consumer0 = session0.createConsumer(queue);
+ System.out.println("Sent message: " + messageA.getText());
+
+ TextMessage messageB = sessionB.createTextMessage("B:This is text message " + i);
- MessageConsumer consumer1 = session1.createConsumer(queue);
- </code>
- </pre>
+ producerB.send(messageB);
- <li>We create a JMS MessageProducer object on server 0.</li>
- <pre>
- <code>
- MessageProducer producer = session0.createProducer(queue);</code>
+ System.out.println("Sent message: " + messageB.getText());
+
+ TextMessage messageC = sessionC.createTextMessage("C:This is text message " + i);
+
+ producerC.send(messageC);
+
+ System.out.println("Sent message: " + messageC.getText());
+ }
+ </code>
</pre>
- <li>We send some messages to server 0.</li>
+ <li>We now consume the messages from each node. The connections must be on different nodes
+ since if they shared nodes then the consumers would receive the messages sent from different connections.</li>
<pre>
<code>
- final int numMessages = 10;
+ for (int i = 0; i < numMessages; i ++)
+ {
+ TextMessage messageA = (TextMessage)consumerA.receive(5000);
- for (int i = 0; i < numMessages; i++)
- {
- TextMessage message = session0.createTextMessage("This is text message " + i);
-
- producer.send(message);
-
- System.out.println("Sent message: " + message.getText());
- }
+ System.out.println("Got message: " + messageA.getText() + " from node A");
+
+ TextMessage messageB = (TextMessage)consumerB.receive(5000);
+
+ System.out.println("Got message: " + messageB.getText() + " from node B");
+
+ TextMessage messageC = (TextMessage)consumerC.receive(5000);
+
+ System.out.println("Got message: " + messageC.getText() + " from node C");
+ }
</code>
</pre>
-
- <li>We now consume those messages on *both* server 0 and server 1.
- We note the messages have been distributed between servers in a round robin fashion.
- JBoss Messaging has <b>load balanced</b> the messages between the available consumers on the different nodes.
- JBoss Messaging can be configured to always load balance messages to all nodes, or to only balance messages
- to nodes which have consumers with no or matching selectors. See the user manual for more details.</li>
- JMS Queues implement point-to-point message where each message is only ever consumed by a
- maximum of one consumer.
- <pre>
- <code>
- for (int i = 0; i < numMessages; i += 2)
- {
- TextMessage message0 = (TextMessage)consumer0.receive(5000);
-
- System.out.println("Got message: " + message0.getText() + " from node 0");
-
- TextMessage message1 = (TextMessage)consumer1.receive(5000);
-
- System.out.println("Got message: " + message1.getText() + " from node 1");
- }
- </code>
- </pre>
<li>And finally (no pun intended), <b>always</b> remember to close your JMS 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>
Modified: trunk/examples/jms/client-side-load-balancing/src/org/jboss/jms/example/ClientSideLoadBalancingExample.java
===================================================================
--- trunk/examples/jms/client-side-load-balancing/src/org/jboss/jms/example/ClientSideLoadBalancingExample.java 2009-04-14 17:56:30 UTC (rev 6425)
+++ trunk/examples/jms/client-side-load-balancing/src/org/jboss/jms/example/ClientSideLoadBalancingExample.java 2009-04-14 18:12:50 UTC (rev 6426)
@@ -82,38 +82,35 @@
connectionC = connectionFactory.createConnection();
- // Step 8. We create a JMS Session on each of those connections
+ // Step 5. We create a JMS Session on each of those connections
Session sessionA = connectionA.createSession(false, Session.AUTO_ACKNOWLEDGE);
Session sessionB = connectionB.createSession(false, Session.AUTO_ACKNOWLEDGE);
Session sessionC = connectionC.createSession(false, Session.AUTO_ACKNOWLEDGE);
- // Step 10. We start the connections to ensure delivery occurs on them
+ // Step 6. We start the connections to ensure delivery occurs on them
connectionA.start();
connectionB.start();
connectionC.start();
- // Step 11. We create JMS MessageConsumer objects on the sessions
+ // Step 7. We create JMS MessageConsumer objects on the sessions
MessageConsumer consumerA = sessionA.createConsumer(queue);
MessageConsumer consumerB = sessionB.createConsumer(queue);
MessageConsumer consumerC = sessionC.createConsumer(queue);
-
- Thread.sleep(1000);
-
- // Step 12. We create JMS MessageProducer objects on the sessions
+ // Step 8. We create JMS MessageProducer objects on the sessions
MessageProducer producerA = sessionA.createProducer(queue);
MessageProducer producerB = sessionB.createProducer(queue);
MessageProducer producerC = sessionC.createProducer(queue);
- // Step 13. We send some messages on each producer
+ // Step 9. We send some messages on each producer
final int numMessages = 10;
@@ -138,7 +135,7 @@
System.out.println("Sent message: " + messageC.getText());
}
- // Step 14. We now consume the messages from each node. The connections must be on different nodes
+ // Step 10. We now consume the messages from each node. The connections must be on different nodes
// since if they shared nodes then the consumers would receive the messages sent from different connections.
for (int i = 0; i < numMessages; i ++)
@@ -160,7 +157,7 @@
}
finally
{
- // Step 15. Be sure to close our resources!
+ // Step 11. Be sure to close our resources!
if (connectionA != null)
{
More information about the jboss-cvs-commits
mailing list