`
enjiex
  • 浏览: 67216 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

axis2学习——开发自定义的axis2服务

阅读更多
经过前面两部分的学习,我们对axis2的安装及axis2的消息处理有了一定的了解,今天我开始着手定义自己的axis2服务,并打包成axis2可识别的.aar服务包并部署到axis2服务器中,以提供给axis2 client的调用。axis2 client的开发会在后面介绍。

axis2提供了很多方式来创建服务,例如:
  • 创建一个服务并从通过脚本进行打包。通过这种方式,你把你的服务类构建到特定的可读取的AXIOM OMElement对象中,然后创建services.xml文件,最后一并打包成可部署的.aar文件
  • 把POJO对象作为服务部署
  • 通过WSDL生成服务。你可以通过WSDL生成client,同时也能生成服务端skeleton。

下面首先以第一种方式来构建我们自己的axis2服务,这种方式虽然不是最便捷的,但对于开发者来说却可以拥有更多的控制权。这种方式构建自定义的服务过程如下:
  1. 在类中为每个服务定义一个方法,此方法接收一个org.apache.axiom.om.OMElement作为参数(OMElement定义了Axis2的对象模型(AXIOM: AXis2 Object Model)如何表示一个XML元素)
  2. 创建服务描述文件:services.xml,并定义服务使用到的类以及恰当的消息接收者
  3. 创建.aar文件,包括classes下的包文件及META-INF目录下的services.xml
  4. 把.aar文件部署到axis2服务中。

“工欲善其事,必先利其器”,所以在开发之前还是需要先搭建自己的开发环境。我这里使用Eclipse作为开发工具,并使用Maven来构建项目。所以首先随便进入一个目录(自己的工作目录),然后执行如下命令:
mvn archetype:create -DgroupId=com.nuc.axis2first -DartifactId=axis2first

执行完上面命令之后会看到当前目录下多了一个axis2first目录,进入此目录只有src目录和一个pom.xml文件。

创建好了项目之后,还需要引入axis2所使用到的jar文件,所以修改pom.xml文件如下:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.nuc.axis2first</groupId>
	<artifactId>axis2first</artifactId>
	<packaging>war</packaging>
	<version>1.0-SNAPSHOT</version>
	<name>axisfirst Maven Webapp</name>
	<url>http://maven.apache.org</url>
	<dependencies>
		<dependency>
			<groupId>org.apache.axis2</groupId>
			<artifactId>axis2</artifactId>
			<version>1.6.0</version>
		</dependency>
		<dependency>
			<groupId>org.apache.ws.commons.axiom</groupId>
			<artifactId>axiom-dom</artifactId>
			<version>1.2.11</version>
		</dependency>
		
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>
	</dependencies>
	<build>
		<finalName>axis2first</finalName>
	</build>
</project>

从上面看,我们引入了axis2包和axiom-dom包,这应该是开发axis2服务所需要引入的最少的包了。其中的axis2提供了是axis2所必须的包,axiom-dom包提供了解析AXIOM对象模型的功能。

然后在axis2first目录下执行如下命令,用于把创建的项目构建成Eclipse可识别的工程:
mvn eclipse:clean eclipse:eclipse -DdownloadSources

然后就可以开发自己的服务类了,假设我们定义了一个服务类SampleService并提供了一个服务接口sayHello,该服务服务接收client传入的用户名personToGreet,然后做一定的处理即返回了"hello " + personToGreet的数据给client,如下:
package com.nuc.axis2first;

import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;

public class SampleService {

	public OMElement sayHello(OMElement element){
		
		element.build();
		element.detach();
		
		String rootName = element.getLocalName();
		System.out.println("Reading " + rootName + " element");
		
		OMElement childElement = element.getFirstElement();
		String personToGreet = childElement.getText();
		
		OMFactory factory = OMAbstractFactory.getOMFactory();
		OMNamespace omNamespace = factory.createOMNamespace("http://example1.org/example1", "example1");
	
		OMElement method = factory.createOMElement("sayHelloResponse", omNamespace);
		OMElement value = factory.createOMElement("greeting", omNamespace);
		value.addChild(factory.createOMText(value, "Hello, " + personToGreet));
		method.addChild(value);
		
		return method;
	}
}

Axis2使用AXIOM(AXIs Object Model),一个类DOM(Document Object Model)结构的基于StAX API(Streaming API for XML)的数据模型。作为services使用的方法必须接收一个OMElement类型的参数,它表示了传入的SOAP消息(一个OMElement是以AXIOM的方式表示一个XML元素,类似于DOM Element对象)。使用这种方式,开发者可以抽取加载的OMElement元素的节点的内容,添加新的文本,并把它作为返回的OMElement对象。除非这是"in only"场景的服务(即不需要返回结果的service),否则这些方法必须返回OMElement类型的结果,因为这个结果会转化成返回的SOAP消息。

为了描述上面的服务,我们还需要定义一个文件services.xml,这个文件位于同服务类的根目录同一目录下的META-INF目录下,对于上面的包com.nuc.axis2first来说,META-INF与com在同一目录。services.xml文件内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<service name="UserGuideSampleService">
	<description>
		This is a sample service created in the Axis2 User's Guide
	</description>
	<parameter name="ServiceClass">
		com.nuc.axis2first.SampleService
	</parameter>
	<operation name="sayHello">
		<messageReceiver class="org.apache.axis2.receivers.RawXMLINOutMessageReceiver" />
	</operation>
</service>

在这个文件中,定义了服务的名称“UserGuideSampleService,通过参数ServiceClass指定服务类,通过operation元素指定服务接口,同时通过messageReceiver指定消息接收者及接收方式,这里是RawXMLINOutMessageReceiver,说明该服务可以接收请求并返回结果。

好了,下面就可以把这个项目打包成服务了。这里打包还是使用maven,因为maven提供了用于构建.aar文件的插件:axis2-aar-maven-plugin。所以还要修改上面的pom文件,在build标签下添加plugins标签,用于添加插件,如下:
<build>
		<finalName>axis2first</finalName>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<configuration>
					<source>1.4</source>
					<target>1.4</target>
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.apache.axis2</groupId>
				<artifactId>axis2-aar-maven-plugin</artifactId>
				<extensions>true</extensions>
				<version>1.6.0</version>
				<configuration>
					<servicesXmlFile>${basedir}/src/main/java/META-INF/services.xml</servicesXmlFile>
					<includeDependencies>true</includeDependencies>
					<fileSets>
						<fileSet>
							<directory>${basedir}/src/main/java</directory>
							<includes>
								<include>**/*.xml</include>
								<include>**/*.class</include>
							</includes>
						</fileSet>
					</fileSets>
				</configuration>
				<executions>
					<execution>
						<goals>
							<goal>aar</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
		</plugins>
	</build>


然后在axis2first目录下执行以下命令,就能打成.aar服务包了:
mvn clean compile axis2-aar:aar

这条命令依次完成了清理target目录,编译project,并打包的过程,其中clean操作不是必须的。如果成功的话,就可以在axis2first/target目录下看到axis2first.aar包文件了。

到这里还不算完,因为还没有把服务发布出去。发布的话,可以参考axis2学习——axis2的安装中关于上传服务那部分的介绍,以及查看已上传的服务。

1
0
分享到:
评论
1 楼 120076566 2013-09-12  
这个源码工程不对

相关推荐

Global site tag (gtag.js) - Google Analytics