Author: jmesnil
Date: 2009-10-02 08:43:21 -0400 (Fri, 02 Oct 2009)
New Revision: 8034
Modified:
trunk/examples/jms/embedded/build.xml
trunk/examples/jms/embedded/readme.html
trunk/examples/jms/embedded/src/org/hornetq/jms/example/EmbeddedExample.java
Log:
HORNETQ-149: Update JMS embedded example to actually start a JMSServerManager
* rewrote the emebedded example to use a JMSServerManager
* used JMSConfiguration API to configure the JMS server (see HORNETQ-161)
Modified: trunk/examples/jms/embedded/build.xml
===================================================================
--- trunk/examples/jms/embedded/build.xml 2009-10-02 12:41:40 UTC (rev 8033)
+++ trunk/examples/jms/embedded/build.xml 2009-10-02 12:43:21 UTC (rev 8034)
@@ -20,10 +20,20 @@
<import file="../../common/build.xml"/>
<!-- We use a minimal classpath on this example -->
+ <path id="extra.classpath">
+ <fileset dir="${jars.dir}">
+ <include name="**/jnpserver.jar"/>
+ <include name="**/jboss-logging-spi.jar"/>
+ <include name="**/jboss-common-core.jar"/>
+ <include name="**/netty.jar"/>
+ </fileset>
+ </path>
+
<path id="local.classpath">
<fileset dir="${hornetq.jars.dir}">
<include name="hornetq-core.jar"/>
<include name="hornetq-jms.jar"/>
+ <include name="hornetq-transports.jar"/>
</fileset>
<fileset dir="${jars.dir}">
@@ -31,10 +41,10 @@
</fileset>
<pathelement location="${classes.dir}"/>
+ <path refid="extra.classpath"/>
</path>
<target name="run" depends="compile">
-
<java classname="org.hornetq.jms.example.EmbeddedExample"
fork="true" resultproperty="example-result">
<jvmarg value="-Xms50M"/>
<jvmarg value="-Xmx50M"/>
@@ -42,7 +52,7 @@
</java>
<!-- if the example exited with a result value != 0, we fail the build -->
- <fail message="Example ${example.classname} failed">
+ <fail message="Example EmbeddedExample failed">
<condition>
<not>
<equals arg1="${example-result}" arg2="0"/>
Modified: trunk/examples/jms/embedded/readme.html
===================================================================
--- trunk/examples/jms/embedded/readme.html 2009-10-02 12:41:40 UTC (rev 8033)
+++ trunk/examples/jms/embedded/readme.html 2009-10-02 12:43:21 UTC (rev 8034)
@@ -1,92 +1,106 @@
<html>
- <head>
- <title>HornetQ Embedded Example</title>
- <link rel="stylesheet" type="text/css"
href="../../common/common.css">
- </head>
- <body>
- <h1>Embedded Example</h1>
- <br>
- <p>This examples shows how to setup and run HornetQ embedded.</p>
- <p>HornetQ was designed to use POJOs (Plain Old Java Objects), what makes
embedding HornetQ as simple as instantiating a few objects.</p>
- <p>On this example, we only one jars (hornetq-core.jar, hornetq-jms.jar and
jboss-javaee.jar).</p>
+ <head>
+ <title>HornetQ Embedded JMS Server Example</title>
+ <link rel="stylesheet" type="text/css"
href="../../common/common.css">
+ </head>
+ <body>
+ <h1>Embedded JMS Server Example</h1>
+
+ <p>This examples shows how to setup and run an embedded JMS server with
HornetQ.</p>
+ <p>HornetQ was designed to use POJOs (Plain Old Java Objects) so that
embedding HornetQ is as simple as instantiating a few objects.</p>
+ <p>This example does not use any configuration files. The server is
configured using POJOs and can be easily ported to any dependency injection
framework.<br />
+ We will setup and run a full-fledged JMS server which binds its JMS resources to
JNDI and can be accessed by remote clients.</p>
- <p>HornetQ Embedded could be used from very simple use cases with only InVM
support to very complex cases with clustering, persistence and fail over.</p>
- <br>
- <h2>Example step-by-step</h2>
- <p><i>To run the example, simply type <code>ant</code> from
this directory</i></p>
- <p>In this we don't use any configuration files. (Everything is embedded).
We simply instantiate ConfigurationImpl, HornetQServer, start it and operate on JMS
regularly</p>
- <br>
- <ol>
- <li>Create the Configuration, and set the properties
accordingly</li>
- <pre>
- <code>Configuration configuration = new ConfigurationImpl();</code>
- <code>configuration.setEnablePersistence(false);</code>
- <code>configuration.setSecurityEnabled(false);</code>
- <code>configuration.getAcceptorConfigurations().add(new
TransportConfiguration(InVMAcceptorFactory.class.getName()));</code></pre>
-
- <li>Create and start the server</li>
- <pre>
- <code>HornetQServer server =
HornetQ.newHornetQServer(configuration);</code>
- <code>server.start();</code></pre>
+ <h2>Example step-by-step</h2>
+ <p><i>To run the example, simply type <code>ant</code> from
this directory</i></p>
- <li>As we are not using a JNDI environment we instantiate the objects
directly</li>
- <pre>
- <code>Queue queue = new
HornetQQueue("exampleQueue");</code>
- <code>HornetQConnectionFactory cf = new HornetQConnectionFactory (new
TransportConfiguration(InVMConnectorFactory.class.getName()));</code>
- </pre>
+ <ol>
+ <li>Create HornetQ core configuration, and set the properties
accordingly</li>
+ <pre>
+ <code>Configuration configuration = new ConfigurationImpl();
+ configuration.setPersistenceEnabled(false);
+ configuration.setSecurityEnabled(false);
+ configuration.getAcceptorConfigurations().add(new
TransportConfiguration(NettyAcceptorFactory.class.getName()));</code>
+ Configuration configuration = new ConfigurationImpl();</pre>
- <li>Create a JMS Destination by using the Core API</li>
- <pre>
- <code>
- ClientSession coreSession = cf.getCoreFactory().createSession(false, false,
false);
- coreSession.createQueue("jms.queue.exampleQueue",
"jms.queue.exampleQueue", true);
- coreSession.close();</code>
- </pre>
+ <li>Create the HornetQ core server</li>
+ <pre>
+ <code>HornetQServer hornetqServer =
HornetQ.newHornetQServer(configuration);</code>
+ </pre>
+
+ <li>Create and start the JNDI server (using JBoss JNDI
implementation)</li>
+ <pre class="prettyprint">
+ <code>System.setProperty("java.naming.factory.initial",
"org.jnp.interfaces.NamingContextFactory");
+ NamingBeanImpl naming = new NamingBeanImpl();
+ naming.start();
+ Main jndiServer = new Main();
+ jndiServer.setNamingInfo(naming);
+ jndiServer.setPort(1099);
+ jndiServer.setBindAddress("localhost");
+ jndiServer.setRmiPort(1098);
+ jndiServer.setRmiBindAddress("localhost");
+ jndiServer.start();</code>
+ </pre>
- <li>Create the JMS objects</li>
- <pre>
- <code>
- connection = cf.createConnection();
- Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
- MessageProducer producer = session.createProducer(queue);</code>
- </pre>
+ <li>Create the JMS configuration</li>
+ <pre>
+ <code>JMSConfiguration jmsConfig = new
JMSConfigurationImpl();</code>
+ </pre>
- <li>Create and send a TextMessage</li>
- <pre><code>
- TextMessage message = session.createTextMessage("Hello sent at " +
new Date());
- System.out.println("Sending the message.");
- producer.send(message);</code></pre>
+ <li>Configure context used to bind the JMS resources to JNDI</li>
+ <pre>
+ <code>Hashtable<String, String> env = new
Hashtable<String, String>();
+ env.put("java.naming.factory.initial",
"org.jnp.interfaces.NamingContextFactory");
+ env.put("java.naming.provider.url",
"jnp://localhost:1099");
+ env.put("java.naming.factory.url.pkgs",
"org.jboss.naming:org.jnp.interfaces");
+ Context context = new InitialContext(env);
+ jmsConfig.setContext(context);</code>
+ </pre>
- <li>Create the message consumer and start the connection</li>
- <pre><code>
- MessageConsumer messageConsumer = session.createConsumer(queue);
- connection.start();</code></pre>
+ <li>Configure the JMS ConnectionFactory</li>
+ <pre>
+ <code>TransportConfiguration connectorConfig = new
TransportConfiguration(NettyConnectorFactory.class.getName());
+ ConnectionFactoryConfiguration cfConfig = new
ConnectionFactoryConfigurationImpl("cf", connectorConfig, "/cf");
+ jmsConfig.getConnectionFactoryConfigurations().add(cfConfig);</code>
+ </pre>
+
+ <li>Configure the JMS Queue</li>
+ <pre>
+ <code>QueueConfiguration queueConfig = new
QueueConfigurationImpl("queue1", null, false, "/queue/queue1");
+ jmsConfig.getQueueConfigurations().add(queueConfig);</code>
+ </pre>
+
+ <li>Start the JMS Server using the HornetQ core server and the JMS
configuration</li>
+ <pre>
+ <code>JMSServerManager jmsServer = new
JMSServerManagerImpl(hornetqServer, jmsConfig);
+ jmsServer.start();</code>
+ </pre>
- <li>Receive the message</li>
- <pre><code>
- TextMessage messageReceived = (TextMessage)messageConsumer.receive(1000);
- System.out.println("Received TextMessage:" +
messageReceived.getText());</code></pre>
-
- <li>Be sure to close our resources!</li>
-
- <pre>
- <code>
- finally
- {
- if (connection != null)
- {
- connection.close();
- }
- }</code>
- </pre>
-
- <li>Stop the server</li>
-
- <pre>
- <code>
- server.stop();
- </code>
- </pre>
- </ol>
- </body>
+ <p>At this point the JMS server is started and any JMS clients can look up
JMS resources from JNDI to send/receive
+ messages from the server. To keep the example simple, we will send and
receive a JMS message from the same JVM
+ used to run the JMS server.</p>
+
+ <li>Lookup JMS resources defined in the configuration </li>
+ <pre>
+ <code>ConnectionFactory cf =
(ConnectionFactory)context.lookup("/cf");
+ Queue queue = (Queue)context.lookup("/queue/queue1");</code>
+ </pre>
+
+ <li>Send and receive a message using JMS API</li>
+ <p>See the <a href="../queue/readme.html">Queue
Example</a> for detailed steps to send and receive a JMS message</p>
+
+ <p>Finally, we stop the JMS server and its associated
resources.</p>
+
+ <li>Stop the JMS server</li>
+ <pre>
+ <code>jmsServer.stop();</code>
+ </pre>
+
+ <li>Stop the JNDI server</li>
+ <pre>
+ <code>naming.stop();
+ jndiServer.stop();</code>
+ </pre>
+ </ol>
+ </body>
</html>
\ No newline at end of file
Modified: trunk/examples/jms/embedded/src/org/hornetq/jms/example/EmbeddedExample.java
===================================================================
---
trunk/examples/jms/embedded/src/org/hornetq/jms/example/EmbeddedExample.java 2009-10-02
12:41:40 UTC (rev 8033)
+++
trunk/examples/jms/embedded/src/org/hornetq/jms/example/EmbeddedExample.java 2009-10-02
12:43:21 UTC (rev 8034)
@@ -13,29 +13,41 @@
package org.hornetq.jms.example;
import java.util.Date;
+import java.util.Hashtable;
import javax.jms.Connection;
+import javax.jms.ConnectionFactory;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;
+import javax.naming.Context;
+import javax.naming.InitialContext;
-import org.hornetq.core.client.ClientSession;
import org.hornetq.core.config.Configuration;
import org.hornetq.core.config.TransportConfiguration;
import org.hornetq.core.config.impl.ConfigurationImpl;
-import org.hornetq.core.remoting.impl.invm.InVMAcceptorFactory;
-import org.hornetq.core.remoting.impl.invm.InVMConnectorFactory;
import org.hornetq.core.server.HornetQ;
import org.hornetq.core.server.HornetQServer;
-import org.hornetq.jms.HornetQQueue;
-import org.hornetq.jms.client.HornetQConnectionFactory;
+import org.hornetq.integration.transports.netty.NettyAcceptorFactory;
+import org.hornetq.integration.transports.netty.NettyConnectorFactory;
+import org.hornetq.jms.server.JMSServerManager;
+import org.hornetq.jms.server.config.ConnectionFactoryConfiguration;
+import org.hornetq.jms.server.config.JMSConfiguration;
+import org.hornetq.jms.server.config.QueueConfiguration;
+import org.hornetq.jms.server.config.impl.ConnectionFactoryConfigurationImpl;
+import org.hornetq.jms.server.config.impl.JMSConfigurationImpl;
+import org.hornetq.jms.server.config.impl.QueueConfigurationImpl;
+import org.hornetq.jms.server.impl.JMSServerManagerImpl;
+import org.jnp.server.Main;
+import org.jnp.server.NamingBeanImpl;
/**
* This example demonstrates how to run a HornetQ embedded with JMS
*
* @author <a href="clebert.suconic(a)jboss.com">Clebert Suconic</a>
+ * @author <a href="mailto:jmesnil@redhat.com">Jeff Mesnil</a>
*/
public class EmbeddedExample
{
@@ -45,60 +57,86 @@
try
{
- // Step 1. Create the Configuration, and set the properties accordingly
+ // Step 1. Create HornetQ core configuration, and set the properties
accordingly
Configuration configuration = new ConfigurationImpl();
configuration.setPersistenceEnabled(false);
configuration.setSecurityEnabled(false);
- configuration.getAcceptorConfigurations().add(new
TransportConfiguration(InVMAcceptorFactory.class.getName()));
+ configuration.getAcceptorConfigurations().add(new
TransportConfiguration(NettyAcceptorFactory.class.getName()));
- // Step 2. Create and start the server
- HornetQServer server = HornetQ.newHornetQServer(configuration);
- server.start();
+ // Step 2. Create HornetQ core server
+ HornetQServer hornetqServer = HornetQ.newHornetQServer(configuration);
+ // Step 3. Create and start the JNDI server
+ System.setProperty("java.naming.factory.initial",
"org.jnp.interfaces.NamingContextFactory");
+ NamingBeanImpl naming = new NamingBeanImpl();
+ naming.start();
+ Main jndiServer = new Main();
+ jndiServer.setNamingInfo(naming);
+ jndiServer.setPort(1099);
+ jndiServer.setBindAddress("localhost");
+ jndiServer.setRmiPort(1098);
+ jndiServer.setRmiBindAddress("localhost");
+ jndiServer.start();
- // Step 3. As we are not using a JNDI environment we instantiate the objects
directly
- Queue queue = new HornetQQueue("exampleQueue");
- HornetQConnectionFactory cf = new HornetQConnectionFactory (new
TransportConfiguration(InVMConnectorFactory.class.getName()));
+ // Step 4. Create the JMS configuration
+ JMSConfiguration jmsConfig = new JMSConfigurationImpl();
- // Step 4. Create a JMS Destination by using the Core API
- ClientSession coreSession = cf.getCoreFactory().createSession(false, false,
false);
- coreSession.createQueue("jms.queue.exampleQueue",
"jms.queue.exampleQueue", true);
- coreSession.close();
+ // Step 5. Configure context used to bind the JMS resources to JNDI
+ Hashtable<String, String> env = new Hashtable<String, String>();
+ env.put("java.naming.factory.initial",
"org.jnp.interfaces.NamingContextFactory");
+ env.put("java.naming.provider.url",
"jnp://localhost:1099");
+ env.put("java.naming.factory.url.pkgs",
"org.jboss.naming:org.jnp.interfaces");
+ Context context = new InitialContext(env);
+ jmsConfig.setContext(context);
+ // Step 6. Configure the JMS ConnectionFactory
+ TransportConfiguration connectorConfig = new
TransportConfiguration(NettyConnectorFactory.class.getName());
+ ConnectionFactoryConfiguration cfConfig = new
ConnectionFactoryConfigurationImpl("cf", connectorConfig, "/cf");
+ jmsConfig.getConnectionFactoryConfigurations().add(cfConfig);
- Connection connection = null;
-
+ // Step 7. Configure the JMS Queue
+ QueueConfiguration queueConfig = new QueueConfigurationImpl("queue1",
null, false, "/queue/queue1");
+ jmsConfig.getQueueConfigurations().add(queueConfig);
+
+ // Step 8. Start the JMS Server using the HornetQ core server and the JMS
configuration
+ JMSServerManager jmsServer = new JMSServerManagerImpl(hornetqServer,
jmsConfig);
+ jmsServer.start();
+ System.out.println("Started Embedded JMS Server");
+
+ // Step 9. Lookup JMS resources defined in the configuration
+ ConnectionFactory cf = (ConnectionFactory)context.lookup("/cf");
+ Queue queue = (Queue)context.lookup("/queue/queue1");
+
+ // Step 10. Send and receive a message using JMS API
+ Connection connection = null;
try
{
-
- // Step 5. Create the JMS objects
connection = cf.createConnection();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer producer = session.createProducer(queue);
-
- // Step 6. Create and send a TextMessage
TextMessage message = session.createTextMessage("Hello sent at " +
new Date());
- System.out.println("Sending the message.");
+ System.out.println("Sending message: " + message.getText());
producer.send(message);
-
- // Step 7. Create the message consumer and start the connection
MessageConsumer messageConsumer = session.createConsumer(queue);
connection.start();
-
- // Step 8. Receive the message.
TextMessage messageReceived = (TextMessage)messageConsumer.receive(1000);
- System.out.println("Received TextMessage:" +
messageReceived.getText());
+ System.out.println("Received message:" +
messageReceived.getText());
}
finally
{
- // Step 9. Be sure to close our resources!
if (connection != null)
{
connection.close();
}
- // Step 10. Stop the server
- server.stop();
+ // Step 11. Stop the JMS server
+ jmsServer.stop();
+ System.out.println("Stopped the JMS Server");
+
+ // Step 12. Stop the JNDI server
+ naming.stop();
+ jndiServer.stop();
+ System.exit(0);
}
}
catch (Exception e)