低调星空

程序是字母与数字的魔术
posts - 17, comments - 1, trackbacks - 0, articles - 1
  IT博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

一个XFire服务简单实例

Posted on 2007-05-17 14:12 citystar 阅读(2203) 评论(0)  编辑 收藏 引用
 

下面是一个XFire服务实例,这个例子已经在tomcat 5.5.28+jdk1.4.2中测试通过。

1、 创建服务接口ReadArticle,代码如下

package cn.starfield.service;

 

public interface ReadArticle {

       String getContent(String article) ;

}

本类实现了一个服务方法getContent(String article)

2、 创建服务类ReadArticleImpl,代码如下:

package cn.starfield.service.impl;

 

import java.io.BufferedReader;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.io.InputStreamReader;

 

import org.apache.commons.logging.Log;

 

import cn.starfield.log.Logger;

import cn.starfield.service.ReadArticle;

 

public class ReadArticleImpl implements ReadArticle {

       private static Log log = Logger.getLog();

 

       public String getContent(String article) {

              StringBuffer datas = new StringBuffer(1024);

              File articleFile = new File("../webapps/xfiredemo/art/" + article

                            + ".art");

              try {

                     BufferedReader br = new BufferedReader(new InputStreamReader(

                                   new FileInputStream(articleFile)));

 

                     if (br != null) {

                            String line = br.readLine();

                            while (line != null) {

                                   datas.append(line);

                                   datas.append('\n');

                                   line = br.readLine();

                            }

                     }

              } catch (FileNotFoundException e) {

                     log.error("Article conn't found!", e);

              } catch (IOException e) {

                     log.error("Article conn't read!", e);

              }

              return datas.toString();

       }

}

本类实现了服务接口,完成服务的最终操作。

3、 创建services.xml,代码如下:

<beans xmlns="http://xfire.codehaus.org/config/1.0">

         <service>

                   <name>ReadArticle</name>

                   <namespace>http://starfield.cn/ReadArticle</namespace>

                   <serviceClass>cn.starfield.service.ReadArticle</serviceClass>

                   <implementationClass>

                            cn.starfield.service.impl.ReadArticleImpl

                   </implementationClass>

         </service>

</beans>

将此文件置于class/META-INF目录下

4、 修改web.xml。修改方法在前而的部署手册中已经说明。

 

 

下载工程代码后,在lib中导入必要的包就可发布服务了。(需要的包在前面的手册中有说明)

 

客户端调用代码如下:

package cn.starfield.service.client;

 

import java.lang.reflect.Proxy;

import java.net.MalformedURLException;

 

import org.apache.commons.logging.Log;

import org.codehaus.xfire.client.Client;

import org.codehaus.xfire.client.XFireProxy;

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 cn.starfield.log.Logger;

import cn.starfield.service.ReadArticle;

 

public class ReadArticleClient {

       Log log = Logger.getLog();

       public String getContent(String atrticle) {

              String content = null;

              String serviceURL = "http://127.0.0.1:8080/xfiredemo/services/ReadArticle";

 

              // 创建service对象

              Service serviceModel = new ObjectServiceFactory().create(

                            ReadArticle.class, null, "http://test.yicha.cn/adreport", null);

 

              XFireProxyFactory serviceFactory = new XFireProxyFactory();

              try {

                     // 获取服务对象

                     ReadArticle service = (ReadArticle) serviceFactory.create(

                                   serviceModel, serviceURL);

 

                     // 获取客户端代理

                     Client client = ((XFireProxy) Proxy.getInvocationHandler(service))

                                   .getClient();

                     // 启动response压缩

                     client.setProperty(CommonsHttpMessageSender.GZIP_RESPONSE_ENABLED,

                                   Boolean.TRUE);

                     // 启动request压缩

                     client.setProperty(CommonsHttpMessageSender.GZIP_RESPONSE_ENABLED,

                                   Boolean.TRUE);

                     // 同时启动responserequest压缩

                     client.setProperty(CommonsHttpMessageSender.GZIP_ENABLED,

                                   Boolean.TRUE);

                     // 忽略超时

                     client.setProperty(CommonsHttpMessageSender.HTTP_TIMEOUT, "0");

 

                     // 调用服务

                     content = service.getContent(atrticle);

              } catch (MalformedURLException e) {

                     log.error("Service conn't be use.", e);

              }

              return content;

 

       }

 

       /**

        * 测试用

        */

       public static void main(String[] args) {

              ReadArticleClient client = new ReadArticleClient();

              String content = client.getContent("article1");

              System.out.print(content);

       }

}

只有注册用户登录后才能发表评论。