Chinaunix首页 | 论坛 | 博客
  • 博客访问: 703974
  • 博文数量: 147
  • 博客积分: 6010
  • 博客等级: 准将
  • 技术积分: 1725
  • 用 户 组: 普通用户
  • 注册时间: 2008-08-22 10:36
文章分类

全部博文(147)

文章存档

2011年(1)

2010年(1)

2009年(35)

2008年(110)

我的朋友

分类: Java

2008-09-14 22:23:07

web service是一种在一台计算机上部署,其他计算机都可以通过网络访问的分布式组件技术
web service组件的开发者无法确定将来使用该组件的技术平台,可能是java,也可能是c#。
所以ws使用的是一种独立于任何具体技术方案的标准语言wsdl来描述接口,在具体客户使用ws组件的时候,在通过特定的工具将wsdl接口转化为具体语言,而且是通过soap协议实现客户端和远程ws组件的访问通信。
举个简单的例子:
@Stateless()
/*07*/@WebService()
/*08*/public class MyFirstWS {
/*09*/    public String HelloBits()
/*10*/    {
/*11*/        return "hello bits";
/*12*/    }   
/*13*/}
可以在部署到jboss服务器上:不过要对应:jboss4-jdk1.5    jboss5-1.6
上面组件部署成功时,可以通过如下路径实现对ws组件的远程访问。
http://服务器地址:服务端口/web服务名称Service/web服务名称
()可以查看wsdl接口!
ws客户端代理:
  wsinport -s ./
这里的类是对应jdk1.6的要是使用jdk1.5要重新编译!
将生成的而客户端代理打包。
客户端程序:
public class test
/*03*/{      
/*04*/   public static void main(String args[]) throws Exception
/*05*/   {
/*06*/        MyFirstWSService service = new MyFirstWSService();
/*07*/        MyFirstWS port = service.getMyFirstWSPort();
/*08*/        java.lang.String result = port.helloBits();
/*09*/        System.out.println("Result = "+result);
/*10*/   }
/*11*/}
在上面的例子中ws中接口就对应了类中的所有的公共方法,如果希望将类中的某些方法实现成为ws方法,可以通过@WebService标注将特定的方法声明为ws方法,这种情况下没有标记的既不能被远程客户端通过soap协议访问。例子:
@Stateless()
/*07*/@WebService()
/*08*/public class MyFirstWS {
/*09*/    public String HelloBits()
/*10*/    {
/*11*/        return "hello bits";
/*12*/    }   
         pubic int add(){
             ..............
         }
/*13*/}
基于接口的ws
@WebService()
/*07*/public interface ICalc
/*08*/{
/*09*/    int add(int a, int b);
/*10*/}
@Stateless() 
/*08*/@WebService(endpointInterface="bitsservice.com.ICalc")
/*09*/public class MyCalculator implements ICalc
/*10*/{
/*11*/     public String getMsg(String name)
/*12*/     {
/*13*/        return "Hello " + name + "!";
/*14*/     }
/*15*/     public int add(int a,int b)
/*16*/     {
/*17*/            return a+b;
/*18*/     }
/*19*/     public int sub(int a,int b)
/*20*/     {
/*21*/            return a-b;
/*22*/     }
/*23*/}
通过ws远程调用组件时,有两种方法:同步和异步,上面的例子是同步调用,下面介绍异步:
直接通过wsinport -s ./ 生成的本地代理只能同步调用ws组件方法。
异步调用首先要先有配置文件
       wsdlLocation="http://127.0.0.1:9080/MyCalculatorService/MyCalculator?wsdl"   
       xmlns:jaxws="">
       true

生成本地代理:
wsinport -b config.xml -s ./
客户端:
/public class test
/*07*/{
/*08*/    public static void main(String[] args) throws Exception
/*09*/    {
/*10*/        MyCalculatorService service = new MyCalculatorService();
/*11*/        ICalc port = service.getMyCalculatorPort();
/*12*/        int arg0 = 10;
/*13*/        int arg1 = 20;           
/*14*/        myHandler obj=new myHandler();
/*15*/        Future result = port.addAsync(arg0, arg1, obj);
/*16*/        while(!result.isDone())
/*17*/        {
/*18*/            Thread.sleep(100);
/*19*/        }
/*20*/    }       
/*21*/}
/*22*/
/*23*/class myHandler implements AsyncHandler
/*24*/{
/*25*/     public void handleResponse(Response response)
/*26*/     {
/*27*/         try {
/*28*/              System.out.println("Result = "+ response.get().getReturn());
/*29*/          } catch(Exception ex) {}
/*30*/     }
/*31*/};
可移植的ws组件调用:
web.properties
wsdlocation=http://127.0.0.1:9080/MyCalculatorService/MyCalculator?wsdl
servicename=MyCalculatorService
namespace=
portname=MyCalculatorPort

public class test
/*09*/{
/*10*/    public static void main(String[] args) throws Exception
/*11*/    {
/*12*/          Properties p = new Properties();                        
/*13*/                FileInputStream f=new FileInputStream("web.properties");
/*14*/                p.load(f);                                              
/*15*/          String nspace=p.getProperty("namespace");
/*16*/          String sername=p.getProperty("servicename");
/*17*/          String wsl=p.getProperty("wsdlocation");
/*18*/          String portname=p.getProperty("portname");
/*19*/          URL myurl = new URL(wsl);
/*20*/        QName sn=new QName(nspace, sername);
/*21*/        QName pn=new QName(nspace, portname);
/*22*/        Service service = Service.create(myurl,sn);
/*23*/        ICalc port =(ICalc)service.getPort(pn, ICalc.class);
/*24*/        int iResult=port.add(12,13);
/*25*/        System.out.println(iResult);               
/*26*/    }  
/*27*/}
ws组件主要用于服务性商务逻辑,客户程序在使用服务时,通常进行频繁的复杂的数据交换,
前面的例子多数是基于基本类型,在实际开发中实用范围比较狭窄,下面讲解使用自定义类型和集合类型!
ws服务请求过程中主要是网络连接和关闭,如果是基本类型频繁的请求,就会影响效率。尽量使用实体类型而非基本数据类型是设计ws组件的一个很好的原则。
实体类型:
例子:
@Stateless()
/*06*/@WebService()
/*07*/public class myService {
/*08*/    public Person getPerson()
/*09*/    {         
/*10*/        Person p=new Person();
/*11*/        p.setAge(32);
/*12*/        p.setName("zhangsan");
/*13*/        p.setSex("male");
/*14*/        return p;
/*15*/    }
/*16*/    public void setPerson(Person p)
/*17*/    {         
/*18*/        p.show();
/*19*/    }
/*20*/}
public class Person
/*04*/{
/*05*/    private String name ;
/*06*/    private String sex;
/*07*/    private int  age;
/*08*/    public void show()
/*09*/    {
/*10*/        System.out.println(this.getName());
/*11*/        System.out.println(this.getSex());
/*12*/        System.out.println(this.getAge());
/*13*/   
/*14*/    }
/*15*/
/*16*/    public String getName() {
/*17*/        return name;
/*18*/    }
/*19*/
/*20*/    public void setName(String name) {
/*21*/        this.name = name;
/*22*/    }
/*23*/
/*24*/    public String getSex() {
/*25*/        return sex;
/*26*/    }
/*27*/
/*28*/    public void setSex(String sex) {
/*29*/        this.sex = sex;
/*30*/    }
/*31*/
/*32*/    public int getAge() {
/*33*/        return age;
/*34*/    }
/*35*/
/*36*/    public void setAge(int age) {
/*37*/        this.age = age;
/*38*/    }
/*39*/}
生成客户端代理

客户端:
public class test
/*07*/{
/*08*/    public static void main(String[] args) throws Exception
/*09*/    {
/*10*/                    MyServiceService service = new MyServiceService();
/*11*/            MyService port = service.getMyServicePort();                       
/*12*/            Person result = port.getPerson();
/*13*/            System.out.println(result.getName());
/*14*/            System.out.println(result.getSex());
/*15*/            System.out.println(result.getAge());
/*16*/            Person myp=new Person();
/*17*/            myp.setName("wangwu");
/*18*/            myp.setAge(32);
/*19*/            myp.setSex("male");
/*20*/            port.setPerson(myp);           
/*21*/       
/*22*/    }       
/*23*/}
ws集合类型
数组:(适用所有平台技术)
@Stateless()
/*06*/@WebService()
/*07*/public class myService {
/*08*/    public Person[] getAllPerson()
/*09*/    {
/*10*/         Person[]  lst=new Person[2];
/*11*/          Person p=new Person();
/*12*/        p.setAge(32);
/*13*/        p.setName("zhangsan");
/*14*/        p.setSex("male");
/*15*/        lst[0]=p;
/*16*/        Person p1=new Person();
/*17*/        p1.setAge(32);
/*18*/        p1.setName("zhangsan");
/*19*/        p1.setSex("male");
/*20*/        lst[1]=p1;
/*21*/        return lst;
/*22*/    }
/*23*/    public void setPerson(Person[] p)
/*24*/    {         
/*25*/        for(int i=0;i/*26*/        {
/*27*/        p[i].show();
/*28*/      }
/*29*/    }
/*30*/}
public class Person
/*04*/{
/*05*/    private String name ;
/*06*/    private String sex;
/*07*/    private int  age;
/*08*/    public void show()
/*09*/    {
/*10*/        System.out.println(this.getName());
/*11*/        System.out.println(this.getSex());
/*12*/        System.out.println(this.getAge());
/*13*/   
/*14*/    }
/*15*/
/*16*/    public String getName() {
/*17*/        return name;
/*18*/    }
/*19*/
/*20*/    public void setName(String name) {
/*21*/        this.name = name;
/*22*/    }
/*23*/
/*24*/    public String getSex() {
/*25*/        return sex;
/*26*/    }
/*27*/
/*28*/    public void setSex(String sex) {
/*29*/        this.sex = sex;
/*30*/    }
/*31*/
/*32*/    public int getAge() {
/*33*/        return age;
/*34*/    }
/*35*/
/*36*/    public void setAge(int age) {
/*37*/        this.age = age;
/*38*/    }
/*39*/}
public class test
/*07*/{
/*08*/    public static void main(String[] args) throws Exception
/*09*/    {
/*10*/                    MyServiceService service = new MyServiceService();
/*11*/            MyService port = service.getMyServicePort();                       
/*12*/            java.util.List result = port.getAllPerson();
/*13*/            for(int i=0;i/*14*/            {
/*15*/                System.out.println(result.get(i).getAge());
/*16*/                System.out.println(result.get(i).getName());
/*17*/                System.out.println(result.get(i).getSex());
/*18*/            }
/*19*/            java.util.List indata=new java.util.ArrayList();
/*20*/            Person p=new Person();
/*21*/            p.setName("zhangsan");
/*22*/            p.setSex("male");
/*23*/            p.setAge(32);
/*24*/            indata.add(p);                       
/*25*/            Person p1=new Person();
/*26*/            p1.setName("wangwu");
/*27*/            p1.setSex("male");
/*28*/            p1.setAge(32);
/*29*/            indata.add(p1);
/*30*/            port.setPerson(indata);       
/*31*/    }       
/*32*/}
链表(只适用java)
@Stateless()
/*07*/@WebService()
/*08*/public class myService {
/*09*/    public List getAllPerson()
/*10*/    {
/*11*/          List lst=new ArrayList();
/*12*/          Person p=new Person();
/*13*/        p.setAge(32);
/*14*/        p.setName("zhangsan");
/*15*/        p.setSex("male");
/*16*/        lst.add(p);
/*17*/        Person p1=new Person();
/*18*/        p1.setAge(32);
/*19*/        p1.setName("zhangsan");
/*20*/        p1.setSex("male");
/*21*/        lst.add(p1);
/*22*/        return lst;
/*23*/    }
/*24*/    public void setPerson(List  p)
/*25*/    {         
/*26*/        for(int i=0;i/*27*/        {
/*28*/        p.get(i).show();
/*29*/      }
/*30*/    }
/*31*/}
public class Person
/*04*/{
/*05*/    private String name ;
/*06*/    private String sex;
/*07*/    private int  age;
/*08*/    public void show()
/*09*/    {
/*10*/        System.out.println(this.getName());
/*11*/        System.out.println(this.getSex());
/*12*/        System.out.println(this.getAge());
/*13*/   
/*14*/    }
/*15*/
/*16*/    public String getName() {
/*17*/        return name;
/*18*/    }
/*19*/
/*20*/    public void setName(String name) {
/*21*/        this.name = name;
/*22*/    }
/*23*/
/*24*/    public String getSex() {
/*25*/        return sex;
/*26*/    }
/*27*/
/*28*/    public void setSex(String sex) {
/*29*/        this.sex = sex;
/*30*/    }
/*31*/
/*32*/    public int getAge() {
/*33*/        return age;
/*34*/    }
/*35*/
/*36*/    public void setAge(int age) {
/*37*/        this.age = age;
/*38*/    }
/*39*/}
public class test
/*07*/{
/*08*/    public static void main(String[] args) throws Exception
/*09*/    {
/*10*/                    MyServiceService service = new MyServiceService();
/*11*/            MyService port = service.getMyServicePort();                       
/*12*/            java.util.List result = port.getAllPerson();
/*13*/            for(int i=0;i/*14*/            {
/*15*/                System.out.println(result.get(i).getAge());
/*16*/                System.out.println(result.get(i).getName());
/*17*/                System.out.println(result.get(i).getSex());
/*18*/            }
/*19*/            java.util.List indata=new java.util.ArrayList();
/*20*/            Person p=new Person();
/*21*/            p.setName("zhangsan");
/*22*/            p.setSex("male");
/*23*/            p.setAge(32);
/*24*/            indata.add(p);                       
/*25*/            Person p1=new Person();
/*26*/            p1.setName("wangwu");
/*27*/            p1.setSex("male");
/*28*/            p1.setAge(32);
/*29*/            indata.add(p1);
/*30*/            port.setPerson(indata);       
/*31*/    }       
/*32*/}


阅读(1633) | 评论(1) | 转发(0) |
0

上一篇:EJB事务

下一篇:线程的基础知识

给主人留下些什么吧!~~

chinaunix网友2009-03-17 17:47:28

什么东东看不懂