XFire WebService使用实例

几年前搞过,当时只是匆匆的用。相隔几年之后再用时有些模糊了,参考了别人的例子实现了,特此总结下。

一、要使用先下载:http://xfire.codehaus.org/Download

二、先说服务端:

       1、新建JavaWeb项目,将jar包拷进webroot/lib下面;(xfire-all-1.2.6.jar和xfire-distribution-1.2.6\xfire-1.2.6\lib下面的所以jar都拷)

        2、在WebRoot/META-INF下面新建文件夹xfire;

       3、在xfire文件夹下面新建services.xml文件

    内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<!-- <beans>
    <service xmlns="http://xfire.codehaus.org/config/1.0">
      <name>Hello</name>
      <namespace>http://com.qh.helloWorld/Hello</namespace>
      <serviceClass>com.qh.helloWorld.Hello</serviceClass>
    </service>
</beans> -->
<beans xmlns="http://xfire.codehaus.org/config/1.0">
  <service>
    <name>KingTeamService</name>
    <namespace>http://com.yao.xfire.service/KingTeamService</namespace>
    <serviceClass>com.yao.xfire.service.MathService</serviceClass>
    <implementationClass>com.yao.xfire.service.MathServiceImpl</implementationClass>
  </service>
</beans>

4、在web.xml文件里面添加service拦截

内容如下:

<servlet>
		<servlet-name>xfireServlet</servlet-name>
		<servlet-class>org.codehaus.xfire.transport.http.XFireConfigurableServlet</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>xfireServlet</servlet-name>
		<url-pattern>/services/*</url-pattern>
	</servlet-mapping>

5、新建KingTeamService.java 接口

内容如下:

package com.yao.xfire.service;

public interface KingTeamService {
	public int add(int a,int b);
}

6、新建KingTeamService.java接口的实现类

内容如下:

package com.yao.xfire.service;

import com.sun.org.apache.commons.logging.Log;
import com.sun.org.apache.commons.logging.LogFactory;

public class MathServiceImpl implements KingTeamService{

	private final Log log = LogFactory.getLog(MathServiceImpl.class);
	@Override
    public int add(int a, int b) {
        log.info("invoke method add.");
        return a + b;
    }

}

三、调用服务的客户端:

1、MathServiceClient.java

package com.yao.xfire.client;

import java.net.MalformedURLException;

import org.codehaus.xfire.XFireFactory;
import org.codehaus.xfire.client.Client;
import org.codehaus.xfire.client.XFireProxyFactory;
import org.codehaus.xfire.service.Service;
import org.codehaus.xfire.service.binding.ObjectServiceFactory;
import org.codehaus.xfire.transport.http.CommonsHttpMessageSender;

import com.yao.xfire.service.KingTeamService;

public class MathServiceClient {
    private static KingTeamService mathService;
    
    private MathServiceClient(){}
     
    public static KingTeamService getMathService(String serviceUrl) throws MalformedURLException{
        if(mathService == null){
            //创建服务
            Service srvcModel = new ObjectServiceFactory().create(KingTeamService.class);
            //创建XFire对象
            XFireProxyFactory factory = new XFireProxyFactory(XFireFactory.newInstance().getXFire());
            //调用Web服务
            mathService = (KingTeamService) factory.create(srvcModel, serviceUrl);
             
            //设置客户端调用的属性
            Client client = Client.getInstance(mathService);
            client.setProperty(CommonsHttpMessageSender.HTTP_TIMEOUT, "300");
            client.setProperty(CommonsHttpMessageSender.DISABLE_KEEP_ALIVE, "true");
            client.setProperty(CommonsHttpMessageSender.DISABLE_EXPECT_CONTINUE, "true");
            //如果需要设置代理
            //client.setProperty(CommonsHttpMessageSender.HTTP_PROXY_HOST, "10.3.1.6" );
            //client.setProperty(CommonsHttpMessageSender.HTTP_PROXY_PORT, "8080");
        }
         
        return mathService;
    }
     
    public static void main(String[] args) throws MalformedURLException{
        KingTeamService service = MathServiceClient.getMathService("http://localhost:80/xfireD/services/MathService");
        int result = service.add(1, 14);
        System.out.println("get the result: " + result);
    }
}


然后将项目放进web服务器里面,运行。然后在浏览器里面输入访问地址进行测试:http://localhost/xfireD/services/KingTeamService?wsdl

运行客户端即可得到计算的结果。

例子虽然简单,但却将使用的过程说的清楚明白。以后可以参考着实现了,哈哈。


 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。