本文讲述基于SCA(tuscany)的SOA实践,本篇讲如何通过tuncany访问web服务
先说一下开发环境jdk6+eclipse3.4+wst+stp+tuscany1.3.1
1 建立一个动态java工程tuscanyclient,源文件目录是src/main/java,资源文件目录是src/main/resources,引用上一篇的tuscany所需要的jar包,并引用上一篇的IUserservice接口和User对象
2 建立Client类,代码如下
package com.tuscanytest.client; import java.util.List; import org.apache.tuscany.sca.host.embedded.SCADomain; import org.osoa.sca.annotations.Reference; import com.tuscanytest.service.IUserservice; import com.tuscanytest.service.User; public class Client { private IUserservice userservice; public List<User> getAll(){ return userservice.getAll(); } public IUserservice getUserservice() { return userservice; } @Reference public void setUserservice(IUserservice userservice) { this.userservice = userservice; } public static void main(String[] args) { SCADomain domain = SCADomain.newInstance("client.composite"); Client client = domain.getService(Client.class, "ClientImpl"); List<User> all = client.getAll(); for (User user : all) { System.out.println(user.getId()); System.out.println(user.getName()); System.out.println(user.getAddress().getStreet()); } } }
3 把上篇服务的描述文件UserService.wsdl放在src/main/resources/目录下
可以直接通过浏览器访问服务的wsdl,然后保存下来
4 在src/main/resources/下定义client.composite文件,代码如下
<?xml version="1.0" encoding="ISO-8859-15"?> <composite xmlns="http://www.osoa.org/xmlns/sca/1.0" xmlns:tuscany="http://tuscany.apache.org/xmlns/sca/1.0" name="tuscanyclient" targetNamespace="http://client.tuscanytest.com"> <component name="ClientImpl"> <implementation.java class="com.tuscanytest.client.Client" /> </component> <reference name="userservice" promote="ClientImpl/userservice" target="UserService"> <interface.java interface="com.tuscanytest.service.IUserservice" /> <binding.ws url="http://localhost:9080/tuscanytest/IUserService" wsdlElement="http://service.tuscanytest.com/#wsdl.port(IUserserviceService/IUserservicePort)"/> </reference> </composite>
5 启动上篇的服务,然后执行前面的client类,可以看到控制台的输出,这些就是调用ws服务后获得的数据
id0 name0 street0 id1 name1 street1 id2 name2 street2 id3 name3 street3 id4 name4 street4 id5 name5 street5 id6 name6 street6 id7 name7 street7 id8 name8 street8 id9 name9 street9
通过tuscany作为客户端访问web服务真的很简单,当然并不是所有的服务都会提供给你java的接口和类,不过好在tuscany有另外的方法来完成,以后会陆续介绍
1 楼
jackyrong
2009-02-05
我运行时,出现
Caused by: org.apache.axis2.AxisFault: Connection refused: connect
why?
Caused by: org.apache.axis2.AxisFault: Connection refused: connect
why?
2 楼
紧急下潜
2009-02-07
jackyrong 写道
我运行时,出现
Caused by: org.apache.axis2.AxisFault: Connection refused: connect
why?
请问你的web服务是启动状态的吗?
3 楼
jackyrong
2009-02-08
是呀,web服务启动的?你是用什么版本的tusacany呢?
能否把代码发给我一下,谢谢,jackyrong@tom.com
能否把代码发给我一下,谢谢,jackyrong@tom.com
4 楼
jackyrong
2009-02-08
Exception in thread "main" org.osoa.sca.ServiceRuntimeException: java.lang.NoSuchMethodError: org.w3c.dom.Node.lookupPrefix(Ljava/lang/String;)Ljava/lang/String;
运行你的程序,还出现如下错误
运行你的程序,还出现如下错误
5 楼
jackyrong
2009-02-08
另外我的版本是JDK 5,应该没关系吧
6 楼
紧急下潜
2009-03-03
jackyrong 写道
另外我的版本是JDK 5,应该没关系吧
从上面的描述来看,可能有影响,你用jdk6试试