日期:2014-05-16  浏览次数:20388 次

基于SCA(tuscany)的SOA实践之三(发布jsonrpc服务是如此的简单)
本文讲述基于SCA(tuscany)的SOA实践,本篇讲如何把一个java类通过tuscany发布成jsonrpc服务
开发环境jdk6+eclipse3.4+wst+stp+tuscany1.3.1
以下步骤和发布web服务大体相同,不同的地方我会标准一下
1   建立一个动态web工程tuscanytest,源文件目录是src/main/java,资源文件目录是src/main/resources,web 目录是src/main/webapp,把tuscany的jar复制到src/main/webapp/WEB-INF/lib/目录下

2   接口,sca中接口可以有java和wsdl2种方法,本例使用java
    定义接口 IUserservice,代码如下
package com.tuscanytest.service;

import java.util.List;

import org.osoa.sca.annotations.Remotable;

@Remotable
public interface IUserservice {
//如果服务是分布式的,就需要使用@Remotable声明
    List<User> getAll();
}


3 定义User和Address类,都是简单的pojo对象,代码如下
User类
package com.tuscanytest.service;

public class User {

    private String id;
    private String name;
    private String password;
    private Address address;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

}

Address类
package com.tuscanytest.service;

public class Address {

    private String street;

    public String getStreet() {
        return street;
    }

    public void setStreet(String street) {
        this.street = street;
    }

}  

4 定义IUserservice的UserServiceImpl实现类,这里只是简单的从内存中获得模拟数据,代码如下
package com.tuscanytest.service;

import java.util.ArrayList;
import java.util.List;

public class UserServiceImpl implements IUserservice {

    private static List<User> users = null;
    static {
        users = new ArrayList<User>();
        for (int i = 0; i < 10; i++) {
            User user = new User();
            user.setId("id" + i);
            user.setName("name" + i);
            user.setPassword("password" + i);
            Address address = new Address();
            address.setStreet("street" + i);
            user.setAddress(address);
            users.add(user);
        }
    }

    public List<User> getAll() {
        return users;
    }

}


5 在src/main/resources/下定义default.composite文件,代码如下
<?xml version="1.0" encoding="UTF-8"?>
<composite xmlns="http://www.osoa.org/xmlns/sca/1.0"
    xmlns:tuscany="http://tuscany.apache.org/xmlns/sca/1.0" name="tuscanytest"
    targetNamespace="http://serivce.tuscanytest.com">
    <component name="UserServiceImpl">
        <implementation.java class="com.tuscanytest.service.UserServiceImpl"></implementation.java>
    </component>
    <service name="UserService" promote="UserServiceImpl">
        <interface.java interface="com.tuscanytest.service.IUserservice" />
        <binding.ws></binding.ws>
        <!--定义了发布服务绑定的协议,使用webservice,还有其他多种协议绑定,可参考tuscany文档-->
        <tuscany:binding.jsonrpc></tuscany:binding.jsonrpc>
        <!--和web服务相比增加上面一行配置,就实现了jsonrpc支持-->
    </service>
</composite>

<tuscany:binding.jsonrpc></tuscany:binding.jsonrpc>
和发布webservice相比多了这一行,是的,只需要这一行你就可以增加jsonrpc支持

文件最终部署到webapp/WEB-INF/classes/目录下即可

6 定义sca-contribution.xml,代码如下
<?xml version="1.0" encoding="UTF-8"?>
<contribution xmlns="http://www.osoa.org/xmlns/sca/1.0"
              targetNamespace="http://serivce.tuscanytest.com"
              xmlns:tus