`
stone02111
  • 浏览: 212574 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Xfire实现webservice实例

    博客分类:
  • Java
 
阅读更多

 

本实例是以简单的加减法(a+ba-b)运算实现用xfire开发webservice服务端和客户端。


一、开发环境

运行环境: Tomcat 6.0.14,jdk 1.5

开发工具:Eclipse3.2 + MyEclipse 5.5


二、开发service(服务端)

开发之前请先到官方(http://xfire.codehaus.org/Download)下载xfire-distribution-1.2.6.zip并解压缩

 


 

<!--[if !supportLists]-->1.       <!--[endif]-->MyEclipse 建立一个web工程,命名为:XFireWebService,src目录下新建一个META-INF文件夹里包含 xfire/service.xml,把xfire-1.2.6lib所有jarxfire-all-1.2.6.jar拷贝到工程lib里,工程具 体信息如附件图


2.修改WebRoot/Web-INF/web.xml文件

<?xml version="1.0" encoding="UTF-8"?> 
<web-app> 
<welcome-file-list> 
<welcome-file>index.jsp</welcome-file> 
</welcome-file-list> 

<context-param> 
     <param-name>log4jConfigLocation</param-name> 
     <param-value>/WEB-INF/log4j.properties</param-value> 
</context-param> 

<servlet> 
<servlet-name>XFireServlet</servlet-name> 
<servlet-class> 
   org.codehaus.xfire.transport.http.XFireConfigurableServlet 
</servlet-class> 
<load-on-startup>0</load-on-startup> 
</servlet> 

<servlet-mapping> 
     <servlet-name>XFireServlet</servlet-name> 
     <url-pattern>/servlet/XFireServlet/*</url-pattern> 
</servlet-mapping> 

<servlet-mapping> 
<servlet-name>XFireServlet</servlet-name> 
<url-pattern>/services/*</url-pattern> 
</servlet-mapping> 
</web-app> 

 

3.修改src/META-INF/xfire/services.xml文件

<?xml version="1.0" encoding="UTF-8"?> 

<beans xmlns="http://xfire.codehaus.org/config/1.0"> 
<service> 
<name>MathService</name> 
<namespace> 
   http://localhost:8080/XFireWebService/MathService 
</namespace> 
<serviceClass>com.test.ws.MathServiceImpl</serviceClass> 
</service> 
</beans> 

 

 4.MathService接口类和MathServicImpl实现类 MathService接口类

package com.test.ws; 

public interface MathService { 
public long add(int p1, int p2); 
public long minus(int p3, int p4); 
} 

MathServiceImpl实现类 
package com.test.ws; 

public class MathServiceImpl implements MathService{ 

public long add(int p1, int p2) { 
System.out.println(p1 + p2); 
     return p1 + p2; 
} 

public long minus(int p3, int p4) { 
System.out.println(p3 - p4); 
return p3 - p4; 
} 
}


 5.MyEclipse会自动编译src文件,请到MyEclipseworkspace里找到刚才建的XFireWebService工程, 打开工程并copy WebRoot文件夹到%TOMCAT_HOME%/webapps里并把WebRoot改名为xfire,参照图如附件:


三、开发client(客户端

    开发client有几种模式 

1. 纯POJO模式 
需要service端提供MathService接口类

package com.test.client; 

import java.net.MalformedURLException; 

import org.codehaus.xfire.XFire; 
import org.codehaus.xfire.XFireFactory; 
import org.codehaus.xfire.client.XFireProxyFactory; 
import org.codehaus.xfire.service.Service; 
import org.codehaus.xfire.service.binding.ObjectServiceFactory; 

import com.test.ws.MathService; 

public class CallXfireWebService_POJO { 

/** 
* @param args 
*/ 
public static void main(String[] args) { 
Service serviceModel = new ObjectServiceFactory().create(MathService.class); 

XFire xfire = XFireFactory.newInstance().getXFire(); 
XFireProxyFactory factory = new XFireProxyFactory(xfire); 
String serviceUrl = "http://localhost:8080/xfire/services/MathService"; 

int p1 = 115; 
int p2 = 4; 

int p3 = 9; 
int p4 = 240; 

MathService client = null; 
try { 
client = (MathService) factory.create(serviceModel, serviceUrl); 
} catch (MalformedURLException e) { 
System.out.println( "Client call webservice has exception: " + e.toString()); 
} 

int result1 = (int) client.add(p1, p2); 
int result2 = (int) client.minus(p3, p4); 
System.out.println(result1); 
System.out.println(result2); 

} 
} 
 

      返回自定义类型必须把webservice中的自定义类型也拿过来,同时拿过来的包路径必须一致



2 JSR181模式 
Server端开发组需要向Client提供MathService的实现类MathServiceImpl,而不止于接口,有点危险,不提倡用这个模式。暂不研究。



3 SpringSide 
暂空着,下轮用到再详解。


4. 动态模式 
   动态模式不需要服务端的class,但在性能上有所欠佳。


package com.test.client; 

import java.net.MalformedURLException; 
import java.net.URL; 

import org.codehaus.xfire.client.Client; 

public class DynamicXfireWebService { 

/** 
* @param args 
*/ 
public static void main(String[] args) { 
Client client = null; 
try { 
client = new Client(new URL("http://localhost:8080/xfire/services/MathService?wsdl")); 
Object[] result1 = client.invoke("add", new Object[] {1, 2}); 
Object[] result2 = client.invoke("minus", new Object[] {1, 2}); 

System.out.println(result1[0]); 
System.out.println(result2[1]); 
} catch (MalformedURLException e) { 
e.printStackTrace(); 
} catch (Exception e) { 
e.printStackTrace(); 
} 

} 

} 
 

    这个方法应该是最适合SOA精神的,但经测试不是一方面不稳定,另一方面数组之类的输出有误


5. 根据WSDL生成Client Stub 
创建client文件夹里含build.xml文件,内容如下


<?xml version="1.0"?> 
<project default="genfiles" basedir="."> 
    <property name="lib" value="WebRoot/WEB-INF/lib" /> 
    <path id="myclasspath"> 
        <fileset dir="${lib}"> 
            <include name="*.jar" /> 
        </fileset> 
        <pathelement location="${genfiles}" /> 
    </path> 
    <property name="code_path" value="src" /> 
    <property name="wsdl_path" value="http://localhost:8080/xfire/MathService?wsdl" /> 
    <property name="code_package" value="com.playphone.ws.client" /> 
    <target name="genfiles" description="Generate the files"> 
        <taskdef name="wsgen" classname="org.codehaus.xfire.gen.WsGenTask" classpathref="myclasspath" /> 
        <wsgen outputDirectory="${code_path}" wsdl="${wsdl_path}" package="${code_package}" binding="xmlbeans" overwrite="true" /> 
    </target> 
</project>
 

    用ms dos打开至client文件夹,执行ant. 
在所生成的client工程里写一个含math方法的ClientTest


四、启动Tomcat, 测试 
运行客户端程序即可看到结果。






 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics