Chapter 7. 运行HornetQ例子

在HornetQ的examples目录下有两组例子,它们是:

7.1. JMS例子

所有JMS都有统一的格式。每个例子都有自己的目录,它的内容包括:

  • build.xml

    ant的build文件,用来运行例子。

  • src 目录

    例子的代码。

  • server0配置目录

    它包含了运行服务器所需要的配置文件。有的集群的例子可能有多个这样的目录,如server0, server1等。

在运行每个例子时,都要先启动一个或多个单独的服务器。当例子运行完毕,再将它们停止。

做为演示我们这里运行queue例子。其它的例子可以参见用户手册。

首先打开一个命令行窗口(shell),进入到HornetQ的 examples/jms/queue目录。

敲入命令./build.sh (或者在Windows上build.bat),你 将看到如下的输出:

Buildfile: build.xml

run:

init:

compile:
     [echo] src.example.dir=/home/andy/projects/hornetq/trunk/examples/jms/queue/src
    [javac] Compiling 5 source files to /home/andy/projects/hornetq/trunk/examples/jms/queue
/build/classes

runExample:
     [java] 10:41:04,149 INFO  @main [JMSExample] hornetq.example.runServer is true
     [java] 10:41:04,149 INFO  @main [JMSExample] starting server with config 'server0'
 logServerOutput true
     [java] 10:41:04,149 INFO  @main [JMSExample] and vm args: -Xms512M,-Xmx512M,-XX:+U
seParallelGC,-XX:+AggressiveOpts,-XX:+UseFastAccessorMethods,-Djava.util.logging.config.
file=/home/andy/projects/hornetq/trunk/examples/jms/common/../../../src/config/stand-alone/no
n-clustered/logging.properties
     [java] org.hornetq.jms.example.SpawnedJMSServer out:10:41:08,437 INFO  @main [Journal
StorageManager] AIO journal selected
     [java] org.hornetq.jms.example.SpawnedJMSServer out:10:41:08,437 WARN  @main [Journal
StorageManager] AIO wasn't located on this platform, it will fall back to using pure Jav
a NIO. If your platform is Linux, install LibAIO to enable the AIO journal
     [java] org.hornetq.jms.example.SpawnedJMSServer out:10:41:08,437 WARN  @main [Securit
yStoreImpl] It has been detected that the cluster admin password which is used to replic
ate management operation from one node to the other has not had its password changed fro
m the installation default. Please see the HornetQ user guide for instructions o
n how to do this.
     [java] org.hornetq.jms.example.SpawnedJMSServer out:10:41:10,941 INFO  @main [HornetQCo
nnectionFactory] read only is false
     [java] org.hornetq.jms.example.SpawnedJMSServer out:10:41:10,941 INFO  @main [HornetQCo
nnectionFactory] read only is false
     [java] org.hornetq.jms.example.SpawnedJMSServer out:10:41:10,941 INFO  @main [HornetQCo
nnectionFactory] read only is false
     [java] org.hornetq.jms.example.SpawnedJMSServer out:10:41:10,991 WARN  @main [JMSServ
erManagerImpl] Binding for java:/ConnectionFactory already exists
     [java] org.hornetq.jms.example.SpawnedJMSServer out:10:41:10,991 WARN  @main [JMSServ
erManagerImpl] Binding for java:/XAConnectionFactory already exists
     [java] org.hornetq.jms.example.SpawnedJMSServer out:10:41:11,241 INFO  @main [Messagi
ngServerImpl] HornetQ Server version 2.0.0.BETA5 (buzz-buzz, 107) started
     [java] org.hornetq.jms.example.SpawnedJMSServer out:10:41:11,241 INFO  @main [HornetQBoot
strapServer] HornetQ server started
     [java] org.hornetq.jms.example.SpawnedJMSServer out:STARTED::
     [java] 10:41:11,276 INFO  @main [JMSExample] using server0/client-jndi.properties f
or jndi
     [java] Sent message: This is a text message
     [java] Received message: This is a text message
     [java] 
     [java] #####################
     [java] ###    SUCCESS!   ###
     [java] #####################

BUILD SUCCESSFUL
Total time: 13 seconds

恭喜!你已经成功地运行了你的第一个HornetQ例子。试着运行其它的例子。

7.2. Java EE例子

Java EE的例子需要首先运行一个JEE应用服务器。这些例子包括MDB,Servlet,EJB等等。你首先要安装JBoss应用服务 器。参见前面的章节进行安装。

这里我们将用MDB例子来说明。有关其它例子的详细信息参见用户手册。首先确认JBoss应用服务器已经启动。

如同其它Java EE例子一样,MDB需要部署到应用服务器。第一,将JBOSS_HOME环境变量 指向你的应用服务器安装目录。如在Linux下:

export JBOSS_HOME=/home/jbossas5.1/build/output/jboss-5.1.0.GA

然后就可以进行部署了。在examples/javaee/mdb目录下运行:

./build.sh deploy

它将创建一个“messaging"的AS配置,并用它来启动AS服务器。

在命令行窗口你会看到如下的输出:

Buildfile: build.xml

validate-jboss:

deploy:

deploy-resources:
     [copy] Copying 1 file to /home/andy/projects/jbossas5.1/build/output/jboss-5.1.0.GA
     /server/default-with-hornetq/deploy/hornetq.sar
     [copy] Copying 1 file to /home/andy/projects/jbossas5.1/build/output/jboss-5.1.0.GA
     /server/default-with-hornetq/deploy/hornetq.sar

init:

compile:

jar:
      [jar] Building jar: /home/andy/projects/hornetq/trunk/examples/javaee/mdb/build/mdb-exa
      mple.jar

ear:
      [jar] Building jar: /home/andy/projects/hornetq/trunk/examples/javaee/mdb/build/mdb-exa
      mple.ear

deploy-ear:
     [copy] Copying 1 file to /home/andy/projects/jbossas5.1/build/output/jboss-5.1.0.GA

     .......


     [exec] 16:01:04,607 INFO  [JBossASKernel] 	Class:javax.jms.MessageListener
     [exec] 16:01:04,607 INFO  [JBossASKernel] Added bean(jboss.j2ee:ear=mdb-example.ear,
     jar=mdb-example.jar,name=MessageMDBExample,service=EJB3) to KernelDeployment of: 
     mdb-example.jar
     [exec] 16:01:04,702 INFO  [EJBContainer] STARTED EJB: org.hornetq.javaee.example.server
     .MDBExample ejbName: MessageMDBExample
     [exec] 16:01:09,496 INFO  [TomcatDeployment] deploy, ctxPath=/mdb-example
     [exec] 16:01:09,812 WARNING [config] Unable to process deployment descriptor for cont
     ext '/mdb-example'
     [exec] 16:01:09,812 INFO  [config] Initializing Mojarra (1.2_12-b01-FCS) for context 
     '/mdb-example'
     [exec] 16:01:09,958 INFO  [Http11Protocol] Starting Coyote HTTP/1.1 on http-127.0.0.1-
     8080
     [exec] 16:01:09,990 INFO  [AjpProtocol] Starting Coyote AJP/1.3 on ajp-127.0.0.1-8009
     [exec] 16:01:10,001 INFO  [ServerImpl] JBoss (Microcontainer) [5.1.0.GA (build: SVNTag
     =JBoss_5_1_0_GA date=200905151005)] Started in 6m:21s:797ms

现在我们可以运行例子了。在mdb目录下运行下列命令:

./build.sh

你将看到如下输出:

Buildfile: build.xml

run:

init:

compile:

runExample:
     [java] Sent message: This is a text message

BUILD SUCCESSFUL
Total time: 7 seconds

在JBoss应用服务器端你还会看到如下的输出:

16:27:54,703 INFO  [STDOUT] message This is a text message received

然后在mdb目录下运行下面的命令可以将这个例子卸载。

./build.sh undeploy

你将会看到如下的输出:

Buildfile: build.xml

validate-jboss:

undeploy:

undeploy-misc:
   [delete] Deleting: /home/andy/projects/jbossas5.1/build/output/jboss-5.1.0.GA
   /server/default-with-hornetq/deploy/mdb-example.ear
   [delete] Deleting: /home/andy/projects/jbossas5.1/build/output/jboss-5.1.0.GA
   /server/default-with-hornetq/deploy/hornetq.sar/hornetq-queues.xml
   [delete] Deleting: /home/andy/projects/jbossas5.1/build/output/jboss-5.1.0.GA
   /server/default-with-hornetq/deploy/hornetq.sar/hornetq-jms.xml

BUILD SUCCESSFUL
Total time: 1 second

你还会在窗口输出中看到MDB的应用最終被删除了,如:

16:36:45,277 INFO  [EJBContainer] STOPPED EJB: org.jboss.javaee.example.server.MDBExample 
         ejbName: MessageMDBExample

恭喜!你已经成功地部署并运行了一个Java EE的例子。