8.4跟Apache Geronimo集成

8.4和Apache Geronimo集成
Apache Geronimo应用服务器是在应用服务器许多方面都使用了许多领先开源项目的一个fully certified Java EE 5 runtime。事实上,Geronimo更大程度上是用来创建一个定制的只包含您需要的组件的应用程序分布式服务器的工具包。

默认的,有三个取决于您的需求的不同发行版的Geronimo:
1 Java EE Certified--一个完全认证的Java EE 5分布,使用两个主要的开源web容器:
■Geronimo with Jetty 7
■Geronimo with Tomcat6
2 Little-G--一个提供一个web容器和Geronimo模块的一个子集的distribution,子集包括:
■Geronimo with Jetty 7
■Geronimo with Tomcat6
3 Micro-G--一个允许你from the ground up建立你自己定制的应用服务器。你通过Geronimo deployer决定包含哪些组件。

Geronimo 提供了大量宽广的选择性,但是我们将为本章节使用Little-G 2.2 with Tomcat 6因为它还没有安装ActiveMQ(而Java EE certified runtime确实包括ActiveMQ)。所以从Geronimo下载页面(http://mng.bz/DaoR)下载Little-G 2.2 with Tomcat 6并将它解压到你的电脑上。

在本章节中,我们将为Geronimo部署ActiveMQ插件并使用Geronimo web console来注册ActiveMQ JMS 资源到Geronimo JNDI provider。在完成了这些步骤以后,Geronimo 示例应用(jms-webapp-geronimo项目)能被部署。

8.4.1安装Geronimo并在Geronimo中配置ActiveMQ插件

在archive展开之后,启动到建立的目录并启动Geronimo,如下所示。
Listing 8.10 Start up Geronimo
$ cd ./geronimo-tomcat6-minimal-2.2
$ ./bin/start-server
Launching Geronimo Server...
Booting Geronimo Kernel (in Java 1.6.0_15)...
...
Startup completed in 6.431s seconds
Listening on Ports:
1099 0.0.0.0 RMI Naming
8009 0.0.0.0 Tomcat Connector AJP TomcatAJPConnector
8080 0.0.0.0 Tomcat Connector HTTP BIO TomcatWebConnector
8443 0.0.0.0 Tomcat Connector HTTPS BIO TomcatWebSSLConnector
9999 0.0.0.0 JMX Remoting Connector
Geronimo Server started in 0:00:08.787
Started Application Modules:
WAR: org.apache.geronimo.configs/remote-deploy-tomcat/2.2/car
Web Applications:
/remote-deploy
Geronimo Application Server started

当Geronimo启动了,它的输出显示了哪些组件被启动了。默认的所有组件都包含在Little-G中。注意ActiveMQ没有包含在输出中。现在该是使用Geronimo deployer安装ActiveMQ的时候了。

Geronimo deployer是一个用于查询和安装Geronimo插件的命令行工具。启动Geronimo deployer并使用它的search-plugins功能查看所有可用的插件的列表。
$ ./bin/deploy.sh --user system --password manager \
search-plugins http://geronimo.apache.org/plugins/geronimo-2.2
Using GERONIMO_HOME: /opt/geronimo-tomcat6-minimal-2.2
Using GERONIMO_TMPDIR: var/temp
Using JRE_HOME:
<no category>
1: ActiveMQ web console on Jetty (2.2)
2: ActiveMQ web console on Tomcat (2.2)
3: Geronimo Plugins, Clustering :: Plugin Farm Datasource (2.2)
4: Geronimo Plugins, UDDI : Database (2.2)
Administration
5: Geronimo Plugins, Console :: Debug Views (Jetty) (2.2)
...
12: Geronimo Plugins, Console :: System Database (Jetty) (2.2)
13: Geronimo Plugins, Console :: System Database (Tomcat) (2.2)
14: Geronimo Plugins, Console :: Tomcat (2.2)
15: Geronimo Plugins, OpenEJB :: Jetty (2.2)
JMS
[b]88: Geronimo Plugins, ActiveMQ v5 :: Broker (2.2)
89: Geronimo Plugins, ActiveMQ v5 :: Console (Jetty) (2.2)
90: Geronimo Plugins, ActiveMQ v5 :: Console (Tomcat) (2.2)
91: Geronimo Plugins, ActiveMQ v5 :: Resource Adapter (2.2)[/b]
JavaEE
92: Geronimo Framework, Configs :: JavaEE Specs (2.2)
...
Install Services [enter a comma separated list of numbers or 'q' to quit]:


注意  当Geronimo deployer启动了,它需要一个username和一个密码。默认的username是system并且默认的密码是manager。注意这些将作为参数传给deployer。

虽然deployer上的许多输出已被elided来节省空间,你能通过142个不同插件的列表看到这里有很大的选择空间。但是在本章需要插件仅用粗体标示--插件 14,88和91。插件14是Geronimo控制台,用于管理Geronimo的基于web的控制台。Geronimo控制台将使它更容易建立JMS资源。插件88是ActiveMQ代理。插件91是ActiveMQ 资源适配器,它将允许ActiveMQ和Java EE服务器集成。

注意deployer上输出的最后一行是允许你以逗号为间隔输入要按章的插件(通过number)的prompt。在这个prompt上,输入14,88,91并按回车。当你这么做后你将看到如下的输出:
...
Install Services [enter a comma separated list of numbers or
'q' to quit]: 14,88,91
Checking for status every 1000ms:
Downloading org.apache.geronimo.plugins/console-tomcat/2.2/car (40%)
Downloading org.apache.pluto/pluto-portal-driver/1.1.6/jar
Downloading org.apache.pluto/pluto-container/1.1.6/jar
Downloading org.apache.pluto/pluto-descriptor-impl/1.1.6/jar
Downloading commons-beanutils/commons-beanutils/1.7.0/jar
Downloading org.apache.portals/portlet-api_1.0_spec/1.0/jar
Downloading org.springframework/spring-core/2.5.6/jar
Downloading org.springframework/spring-context/2.5.6/jar
...
**** Installation Complete!
...
Downloaded 22983 kB in 52s (441 kB/s)

输出已经被truncated,但是你一旦看到了Installation Complete!消息,就意味着三个ActiveMQ插件已经成功安装了。去检验的最简答的方法时打开浏览器并访问http://localhost:8080/控制台来查看Geronimo控制台。你应该能看到如图8.5所示的页面。

请登录,使用Geronimo deployer相同的credentials(默认username是system,密码是manager)。登录到Geronimo控制台后,你将看到如图8.6显示的页面。

注意在图8.6右侧的链接。名为JMS Server的图允许你管理ActiveMQ配置,如8.7图中显示的。

虽然Geronimo控制台使管理ActiveMQ配置很简单,但是对于本章最简单的是使用对代理的默认配置。但是在GeronimoJMS资源的默认配置是一个不同的。

8.4.2在Geronimo中配置ActiveMQ JMS资源
ActiveMQ的JMS资源(JMS connection和JMS destination)需要定制来匹配在示例应用中配置的资源。要做到这一点,在web控制台点击名为JMS Resources的链接则你将看到图8.8中的页面。

如你所见,一个默认的JMS资源组已被配置。Geronimo中默认的JMS资源没有被命名为和示例应用兼容的方式,所以你将要建立一个新的资源组。要做到这一点,点击名为ActiveMQ的链接。你将看到如图8.9的页面。为这个资源组键入一个名字。对于这个示例,资源组名为FooGroup。然后滑动到页面的底部并点击下一步按键。

下一个页面允许你建立JMS连接工厂和JMS destinations。该页面如图8.10所示。

点击添加Connection Facotry按钮。在下一个页面,如图8.11所示leave the default type of javax.jms.ConnectionFactory并点击下一步按钮。

下一个页面允许一些JMS connection factory定制,如图8.12所示。

确保name了连接工厂为ConnectionFactory并设置transaction suppor为None。接着点击下一步按钮。这将使你返回那个允许你建立JMS资源的页面,但是现在新的连接工厂应该已被列出,如图8.13所示。

现在点击添加Destination按钮建立一个JMSdestination。这件显示一个页面,它允许选择destination类型,如图8.14所示。确保javax.jms.Queue被选择,并点击下一步按钮来定制queue。

在下一个页面,确保在Message Destination Name字段输入FooQueue并在Physical Name字段输入FOO.QUEUE,如图8.15所示。

该queue将作为FooQueue从JNDI context中fetched,但是该queue被实力web应用使用的的实际名字是FOO.QUEUE。现在点击下一步按钮。这会带你回到允许建立JMS资源的页面。现在新的连接工厂和新的queue才应被列出,如图8.16所示。

现在JMS资源组已经准备好被部署了。要做到这一点,点击Deploy Now按钮。Geronimo将会发布ActiveMQ资源适配器并将你带回到初始JMS资源组页面。不同的是FooGroup资源组已被列出,如图8.17所示。

现在JMS资源已经被建立,你能进一步来发布示例web应用来测试它们。

8.4.3为在Geronimo中部署准备示例应用

为了部署示例,一个jms-webapp-local项目的拷贝将被使用,命名为jms-webapp-geronimo。Java EE应用服务器提供了不同的环境,在其中去发布应用,所以一些tweaks来支持该环境是必要的:

第一步 建立一个名为src/main/webapp/WEB-INF/geronimo-web.xml的文件,如下所示。
Listing 8.11 The geronimo-web.xml file
<web-app xmlns="http://geronimo.apache.org/xml/ns/j2ee/web-2.0.1">
	<environment>
		<moduleId>
			<groupId>org.apache.activemq.book</groupId>
			<artifactId>jms-webapp</artifactId>
			<version>1.0-SNAPSHOT</version>
			<type>war</type>
		</moduleId>
		<dependencies>
			<!-- Depend upon the custom JMS resources group -->
			<dependency>
				<groupId>console.jms</groupId>
				<artifactId>FooGroup</artifactId>
				<version>1.0</version>
				<type>car</type>
			</dependency>
		</dependencies>
		<!-- Filter out the following items from the parent classloader -->
		<hidden-classes>
			<filter>org.springframework.</filter>
			<filter>META-INF/spring</filter>
		</hidden-classes>
	</environment>
	<context-root>/jms-webapp</context-root>
</web-app>

geronimo-web.xml文件对Geronimo是特殊的。它为Geronimo提供了一些instructions。<moduleId>元素被用来识别发布到Geronimo的WAR文件。<dependency>元素告诉Geronimo它需要在该章节先前建立的JMS资源组。若没有作为资源组一部分的连接工厂和destination,示例应用将不能正确地运行。<hidden-classes>元素告诉Geronimo不要在列出的packages中expose任何classes,因为它们已经存在于exposed到web应用的应用server的classloader中。This is something that should happen automatically and will probably be corrected in the near term based on a JIRA issue that was created to point this out.一旦这被fixed了,<hidden-classes>元素将不再需要来隐藏activemq-broker Geronimo插件的Spring classes。
第二步 取代在web.xml文件中使用<resource-ref>元素定义JMS queue,Geronimo需要使用<message-destination-ref>。<message-destination-ref>元素为web.xml定义在DTD。下面的列表显示了jms-webapp-geronimo/src/main/webapp/WEB-INF/web.xml文件中必要的修改。
Listing 8.12 Change to the web.xml file for Geronimo
...
	<message-destination-ref>
		<message-destination-ref-name>jms/FooQueue</message-destination-ref-name>
		<message-destination-type>javax.jms.Queue</message-destination-type>
		<message-destination-usage>Produces</message-destination-usage>
		<message-destination-link>jms/FooQueue</message-destination-link>
	</message-destination-ref>
	<!--
	<resource-ref>
		<res-ref-name>jms/FooQueue</res-ref-name>
		<res-type>javax.jms.Queue</res-type>
		<res-auth>Container</res-auth>
		<res-sharing-scope>Shareable</res-sharing-scope>
	</resource-ref>
	-->
...

第三步 因为ActiveMQ通过activemq-broker Geronimo 代理而可用,jms-webapp-geronimo不在WAR文件中package ActiveMQ JAR或它的dependencies。为了ActiveMQ依赖被Maven编译过程包含在WAR文件中,在pom.xml文件中添加<scope>provided</scope>元素到ActiveMQ依赖,XBean依赖和Log4J依赖,如下面类表中粗体显示。
Listing 8.13 Maven dependency changes for Geronimo
...
	<dependency>
		<groupId>org.apache.activemq</groupId>
		<artifactId>activemq-core</artifactId>
		<version>${activemq-version}</version>
		<scope>provided</scope>
		<exclusions>
			<exclusion>
				<groupId>org.apache.activemq</groupId>
				<artifactId>activeio-core</artifactId>
			</exclusion>
			<exclusion>
				<groupId>org.apache.camel</groupId>
				<artifactId>camel-core</artifactId>
			</exclusion>
			<exclusion>
				<artifactId>activemq-protobuf</artifactId>
				<groupId>org.apache.activemq.protobuf</groupId>
			</exclusion>
			<exclusion>
				<artifactId>commons-net</artifactId>
				<groupId>commons-net</groupId>
			</exclusion>
		</exclusions>
	</dependency>
	<dependency>
		<groupId>org.apache.xbean</groupId>
		<artifactId>xbean-spring</artifactId>
		<version>3.4.3</version>
		<scope>provided</scope>
	</dependency>
	<dependency>
		<groupId>log4j</groupId>
		<artifactId>log4j</artifactId>
		<version>1.2.14</version>
		<scope>provided</scope>
	</dependency>
...

注意在表8.13中粗体显示的<scope>provided</scope>元素。不包含这些JAR在WAR文件的inclusion中避免了任何classloader的*es,如果dependency被包含将引起*es。
第四步 你需要在在这个示例web应用中做的最后一件事是在Spring配置文件中关于连接工厂的类型。编辑jmswebapp-geronimo/src/main/webapp/WEB-INF/spring/jms-context.xml文件来修改连接工厂类型,从org.apache.activemq.ActiveMQConnectionFactory改为org.apache.activemq.ra.ActiveMQConnectionFactory。这是必要的因为在这里使用的连接工厂是ActiveMQ资源适配器的,而不是基本的连接工厂。

所有的application-specific的修改已经存在于jms-webapp-geronimo项目。确保下载本书的示例源码来查看示例项目。

8.4.4在Geronimo中部署和修改示例应用

使用jms-webapp-geronimo项目在Geronimo中激活JNDI资源的定义,使用下面的步骤:

第一步 编译项目并建立WAR文件,在命令行,运行下面的Maven命令:
$ cd jms-webapp-geronimo
$ mvn clean install
...
[INFO] Scanning for projects...
[INFO] ----------------------------
-----
[INFO] Building jms-webapp-global
[INFO] task-segment: [clean, install]
[INFO] ----------------------------
-----
...
[INFO] ----------------------------
-----
[INFO] BUILD SUCCESSFUL
[INFO] ----------------------------
-----
...

第二步 在一个控制台,使用如下命令启动Geronimo:
$ cd $GERONIMO_HOME
$ ./bin/start-server
Launching Geronimo Server...
Booting Geronimo Kernel (in Java 1.6.0_15)...
...
Module 33/36 org.apache.geronimo.configs/activemq-broker/2.2/car
started in 1.248s
Module 34/36 org.apache.geronimo.configs/activemq-ra/2.2/car
started in .350s
Module 35/36 org.apache.geronimo.plugins/activemq-console-tomcat/2.2/car
started in .264s
Module 36/36 console.jms/FooGroup/1.0/car
started in .111s
Startup completed in 17.753s seconds
Listening on Ports:
1099 0.0.0.0 RMI Naming
1527 0.0.0.0 Derby Connector
8009 0.0.0.0 Tomcat Connector AJP TomcatAJPConnector
8080 0.0.0.0 Tomcat Connector HTTP BIO TomcatWebConnector
8443 0.0.0.0 Tomcat Connector HTTPS BIO TomcatWebSSLConnector
9999 0.0.0.0 JMX Remoting Connector
61616 0.0.0.0 ActiveMQ Transport Connector
Started Application Modules:
EAR: org.apache.geronimo.plugins/console-tomcat/2.2/car
RAR: console.jms/FooGroup/1.0/car
RAR: org.apache.geronimo.configs/activemq-ra/2.2/car
RAR: org.apache.geronimo.configs/system-database/2.2/car
WAR: org.apache.geronimo.configs/remote-deploy-tomcat/2.2/car
Web Applications:
/console
/console-base
/remote-deploy
Geronimo Application Server started
Geronimo Server started in 0:00:21.997

虽然一些输出已经被eliminated来节省空间,hold JMS资源的FooGroup被部署了,Geronimo必要的ActiveMQ插件被已被部署。

第三步 在第二个控制台