1. Service的java代码
D:/mysamples/sample2/src/SampleService.java
import org.apache.axiom.om.OMNamespace;
public class SampleService {
public OMElement sayHello(OMElement element) throws XMLStreamException {
element.build();
element.detach();
String rootName = element.getLocalName();
System.out.println("Reading "+rootName+" element");
OMElement childElement = element.getFirstElement();
String personToGreet = childElement.getText();
OMFactory fac = OMAbstractFactory.getOMFactory();
OMNamespace omNs = fac.createOMNamespace("http://example1.org/example1", "example1");
OMElement method = fac.createOMElement("sayHelloResponse", omNs);
OMElement value = fac.createOMElement("greeting", omNs);
value.addChild(fac.createOMText(value, "Hello," + personToGreet));
method.addChild(value);
return method;
}
private void ping(){
}
}
2. services.xml
D:/mysamples/sample2/resources/services.xml
<service name="UserGuideSampleService">
<description>
This is a sample service created in the Axis2 User's Guide
</description>
<parameter name="ServiceClass"
>org.apache.axis2.axis2userguide.SampleService
</parameter>
<operation name="sayHello">
<messageReceiver
class="org.apache.axis2.receivers.RawXMLINOutMessageReceiver"/>
</operation>
<operation name="ping">
<messageReceiver
class="org.apache.axis2.receivers.RawXMLINOnlyMessageReceiver"/>
</operation>
</service>
3. Client的JAVA代码
D:/mysamples/sample2/src/SampleClient.java
package org.apache.axis2.axis2userguide;
import javax.xml.stream.XMLStreamException;
import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.Constants;
import org.apache.axis2.client.ServiceClient;
public class SampleClient {
private static EndpointReference targetEPR =
new EndpointReference(
"http://localhost:8080/axis2/services/UserGuideSampleService");
public static OMElement greetUserPayload(String personToGreet) {
OMFactory fac = OMAbstractFactory.getOMFactory();
OMNamespace omNs = fac.createOMNamespace(
"http://example1.org/example1", "example1");
OMElement method = fac.createOMElement("sayHello", omNs);
OMElement value = fac.createOMElement("personToGreet",
omNs);
value.addChild(fac.createOMText(value, personToGreet));
method.addChild(value);
return method;
}
public static void main(String[] args) {
try {
OMElement payload =
SampleClient.greetUserPayload("John");
Options options = new Options();
options.setTo(targetEPR);
options.setTransportInProtocol(Constants.TRANSPORT_HTTP);
ServiceClient sender = new ServiceClient();
sender.setOptions(options);
OMElement result = sender.sendReceive(payload);
String response = result.getFirstElement().getText();
System.out.println(response);
} catch (Exception e) { //(XMLStreamException e) {
System.out.println(e.toString());
}
}
}
4. build
D:/mysamples/sample2/build.xml
<?xml version="1.0" encoding="UTF-8"?>
<project basedir="." default="jar.client">
<!--Auto generated ant build file-->
<property environment="env"/>
<property name="axis2.home" value="${env.AXIS2_HOME}"/>
<property name="project.base.dir" value="."/>
<property name="maven.class.path" value=""/>
<property name="name" value="SampleService"/>
<property name="src" value="${project.base.dir}/src"/>
<property name="build" value="${project.base.dir}/build"/>
<property name="classes" value="${build}/classes"/>
<property name="lib" value="${build}/lib"/>
<property name="resources" value="${project.base.dir}/resources"/>
<property value="" name="jars.ok"/>
<path id="axis2.class.path">
<pathelement path="${java.class.path}"/>
<pathelement path="${maven.class.path}"/>
<fileset dir="${axis2.home}">
<include name="lib/*.jar"/>
</fileset>
</path>
<target name="init">
<mkdir dir="${build}"/>
<mkdir dir="${classes}"/>
<mkdir dir="${lib}"/>
</target>
<target depends="init" name="pre.compile.test">
<!--Test the classpath for the availability of necesary classes-->
<available classpathref="axis2.class.path" property="stax.available" classname="javax.xml.stream.XMLStreamReader"/>
<available classpathref="axis2.class.path" property="axis2.available" classname="org.apache.axis2.engine.AxisEngine"/>
<condition property="jars.ok">
<and>
<isset property="stax.available"/>
<isset property="axis2.available"/>
</and>
</condition>
<!--Print out the availabilities-->
<echo message="Stax Availability= ${stax.available}"/>
<echo message="Axis2 Availability= ${axis2.available}"/>
</target>
<target depends="pre.compile.test" name="compile.src" if="jars.ok">
<javac debug="on" memoryMaximumSize="256m" memoryInitialSize="256m" fork="true" destdir="${classes}" srcdir="${src}">
<classpath refid="axis2.class.path"/>
</javac>
</target>
<target depends="compile.src" name="compile.test" if="jars.ok">
<javac debug="on" memoryMaximumSize="256m" memoryInitialSize="256m" fork="true" destdir="${classes}">
<src path="${test}"/>
<classpath refid="axis2.class.path"/>
</javac>
</target>
<target depends="pre.compile.test" name="echo.classpath.problem" unless="jars.ok">
<echo message="The class path is not set right! Please make sure the following classes are in the classpath 1. XmlBeans 2. Stax 3. Axis2 "/>
</target>
<target depends="jar.server, jar.client" name="jar.all"/>
<target depends="compile.src,echo.classpath.problem" name="jar.server" if="jars.ok">
<copy toDir="${classes}/META-INF" failοnerrοr="false">
<fileset dir="${resources}">
<include name="*.xml"/>
<include name="*.wsdl"/>
<include name="*.xsd"/>
</fileset>
</copy>
<jar destfile="${lib}/${name}.aar">
<fileset excludes="**/Test.class" dir="${classes}"/>
</jar>
</target>
<target if="jars.ok" name="jar.client" depends="compile.src">
<jar destfile="${lib}/${name}-test-client.jar">
<fileset dir="${classes}">
<exclude name="**/META-INF/*.*"/>
<exclude name="**/lib/*.*"/>
<exclude name="**/*MessageReceiver.class"/>
<exclude name="**/*Skeleton.class"/>
</fileset>
</jar>
</target>
<target if="jars.ok" depends="jar.server" name="make.repo">
<mkdir dir="${build}/repo/"/>
<mkdir dir="${build}/repo/services"/>
<copy file="${build}/lib/${name}.aar" toDir="${build}/repo/services/"/>
</target>
<target if="jars.ok" depends="make.repo" name="start.server">
<java fork="true" classname="org.apache.axis2.transport.http.SimpleHTTPServer">
<arg value="${build}/repo"/>
<classpath refid="axis2.class.path"/>
</java>
</target>
<target if="jars.ok" depends="compile.test" name="run.test">
<path id="test.class.path">
<pathelement location="${lib}/${name}-test-client.jar"/>
<path refid="axis2.class.path"/>
<pathelement location="${classes}"/>
</path>
<mkdir dir="${build}/test-reports/"/>
<junit haltonfailure="yes" printsummary="yes">
<classpath refid="test.class.path"/>
<formatter type="plain"/>
<batchtest fork="yes" toDir="${build}/test-reports/">
<fileset dir="${test}">
<include name="**/*Test*.java"/>
</fileset>
</batchtest>
</junit>
</target>
<target if="jars.ok" name="run.client">
<java classname="org.apache.axis2.axis2userguide.SampleClient">
<classpath refid="client.class.path" />
</java>
<path id="client.class.path">
<pathelement location="${lib}/${name}-test-client.jar"/>
<path refid="axis2.class.path"/>
<pathelement location="${classes}"/>
</path>
</target>
<target name="clean">
<delete dir="${build}"/>
</target>
</project>
5. 测试
step1: 形成service的.aar文件
D:/mysamples/sample2/ant jar.server
results:
D:/mysamples/sample2/build/lib/SampleService.aar, SampleService-test-client.jar
step2: 将.aar文件部署到tomcat的服务中
将SampleService.aar复制到:e:/tomcat/webapps/axis2/WEB-INF/services/
step3: 形成客户端的.jar文件
D:/mysamples/sample2/ant jar.client
step4: 测试客户端
D:/mysamples/sample2/ant run.client