tuscany构建webservice(入门)

1. 前言

目前项目用到tuscany,感觉思想还不错,使用配置也比较方便。为防自己日益退化的挨踢大脑,故作如下入门级备忘录,同时也希望能给其他初学者提供一定的帮助。

 

2. 下载

1) apache所有的开源框架可以到apache的资料库下载得到:http://archive.apache.org/dist/ 

 

2) 这里我们可以在如下网址下载到对应版本的Tuscany:
http://archive.apache.org/dist/tuscany/java/sca/2.0/tuscany-distribution-all-2.0.zip

 

3) 将下载到的zip包里面的lib目录下的所有jar包引入到工程

注:zip包里面有不少例子,可以多多参照学习

        

3. 在web.xml引入tuscany

	<filter>
		<filter-name>tuscanyFilter</filter-name>
		<filter-class>org.apache.tuscany.sca.host.webapp.TuscanyServletFilter</filter-class>
	</filter>
	
	<filter-mapping>
		<filter-name>tuscanyFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

 

4. 服务端代码及配置

1) 定义接口:

package samples.tuscany.helloworld;
import org.oasisopen.sca.annotation.Remotable;

@Remotable
public interface Helloworld {
    String sayHello(String name);
}

 

2) 定义实现类:

package samples.tuscany.helloworld;

public class HelloworldImpl implements Helloworld {
    public String sayHello(String name) {
        return "Helloworld " + name;
    }
}

 

3) 配置sca-contribution.xml

<?xml version="1.0" encoding="UTF-8"?>
<contribution xmlns="http://docs.oasis-open.org/ns/opencsa/sca/200912"
              xmlns:sample="http://sample">
   
   <deployable composite="sample:helloworld-contribution" />

</contribution>

  

 4) 配置 helloworld.composite

<?xml version="1.0" encoding="UTF-8"?>
<composite xmlns="http://docs.oasis-open.org/ns/opencsa/sca/200912"
           xmlns:tuscany="http://tuscany.apache.org/xmlns/sca/1.1"
           targetNamespace="http://sample"
           name="helloworld-contribution">

    <component name="HelloworldComponent">
        <implementation.java class="samples.tuscany.helloworld.HelloworldImpl"/> <!--以java方式实现-->
        <service name="Helloworld">
           	<binding.ws/> <!--以webservice方式发布服务-->
        </service>
    </component>

</composite>

  

注:sca-contribution.xml文件中的 sample 相当于域的一个别名, composite="sample:helloworld-contribution" ,就是指定该域下的 helloworld-contribution 组件。系统启动时会自动到固定目录下(参照下载的例子放根目录下 META-INF 目录里)查找以 composite 为扩展名的文件并分析文件中的 targetNamespace 是否与sca-contribution.xml文件中的域名一致,如果一致,那么再看下 name="helloworld-contribution" 是否与 sca-contribution.xml 文件中的组件名也一致,如果一致就说明该文件是服务的配置文件,系统会加载它并暴露服务。(经测试,不需要建sca-contribution.xml 文件也能部署成功,这里不做深究,建议都建上)

 

注: <service name="Helloworld">的名字必须跟@Remotable描述的接口名一致,实现类继承接口可以有多个,所以service节点可以为多个

 

5) 调试

 

启动应用服务器,在浏览器中输出服务的访问地址http://127.0.0.1:8080/gboss/HelloworldComponent/Helloworld?wsdl(具体地址启动应用服务器时控制台有打印),能看到我们熟悉的wsdl服务描述文件,说明服务部署成功。

 

可以将<binding.ws/>改为<binding.ws uri="/HelloworldService"/>,对应地址变为:http://127.0.0.1:8080/gboss/HelloworldService?wsdl

 

4. 客户端代码及配置

1) 新建一个工程作为客户端,更为直观。重复上面2、3步骤。

 

2) 定义接口

package samples.tuscany.helloworld.client;
import org.oasisopen.sca.annotation.Remotable;

@Remotable
public interface Helloworld {
    String sayHello(String name);
}

 

注:这里的接口目录跟服务端的接口目录可以不相同 

 

3) 定义接口映射存放类

package samples.tuscany.helloworld.client;

import org.oasisopen.sca.annotation.Reference;

public class ServiceRef {
	private Helloworld helloworld;

	public Helloworld getHelloworld() {
		return helloworld;
	}

	@Reference
	public void setHelloworld(Helloworld helloworld) {
		this.helloworld = helloworld;
	}
	
}

 

4) 配置sca-contribution.xml

<?xml version="1.0" encoding="UTF-8"?>
<contribution xmlns="http://docs.oasis-open.org/ns/opencsa/sca/200912"
              xmlns:sample="http://sample">
   
   <deployable composite="sample:serviceref" />

</contribution>

 

5) 配置serviceref.composite

<?xml version="1.0" encoding="UTF-8"?>
<composite xmlns="http://docs.oasis-open.org/ns/opencsa/sca/200912"
           xmlns:tuscany="http://tuscany.apache.org/xmlns/sca/1.1"
           targetNamespace="http://sample"
           name="serviceref">

    <component name="Serviceref">
		<implementation.java class="samples.tuscany.helloworld.client.ServiceRef"/> <!--可以看作存放接口映射的存放类-->
		<reference name="helloworld">
			<interface.java interface="samples.tuscany.helloworld.client.Helloworld"/>	<!--映射为java接口类型-->
			<binding.ws uri="http://127.0.0.1:8080/gboss/HelloworldComponent/Helloworld"/>
		</reference>
	</component>

</composite>

 

 

6) Java进程调用

package samples.tuscany.helloworld.client;

import org.apache.tuscany.sca.node.Contribution;
import org.apache.tuscany.sca.node.ContributionLocationHelper;
import org.apache.tuscany.sca.node.Node;
import org.apache.tuscany.sca.node.NodeFactory;

public class Test {
	public static void main(String[] args) {
		String servicerefLocation  = ContributionLocationHelper.getContributionLocation("META-INF/tuscany/serviceref.composite");
		Node node = NodeFactory.newInstance().createNode(new Contribution("xxx", servicerefLocation));
		node.start();
		ServiceRef serviceRef = node.getService(ServiceRef.class, "Serviceref");
		System.out.println(serviceRef.getHelloworld().sayHello("lufii"));
		node.stop();
	}
}

 

 

7) web容器调用:暂无

 

注:上面的是2.0版本通过NodeFactory调用服务的代码,1.6版本的API有些不同,由老的SCADomain调用,如下

 

8) 1.6版本:Java进程调用 

		SCADomain scaDomain = SCADomain.newInstance("META-INF/tuscany/el.composite");
		ServiceRef serviceRef = scaDomain.getService(ServiceRef.class, "HelloComponent");
		System.out.println(serviceRef .getHelloworld().sayHello("Alicy"));

 

9) 1.6版本:web容器调用

SCADomain scaDomain = (SCADomain) this.getRequest().getSession().getServletContext().getAttribute(WebAppServletHost.SCA_DOMAIN_ATTRIBUTE);

 

 SCADomain 的获取改为由容器获取,至于Request或者Session的获取这里不做详细介绍

5. 其他:与spring整合

现在比较多项目用到spring,所以需考虑与spring整合的情况。

 

与spring整合很简单,将上面helloworld.composite的配置改为

<?xml version="1.0" encoding="UTF-8"?>
<composite xmlns="http://docs.oasis-open.org/ns/opencsa/sca/200912"
           xmlns:tuscany="http://tuscany.apache.org/xmlns/sca/1.1"
           targetNamespace="http://sample"
           name="helloworld-contribution">

    <component name="HelloworldComponent">
       <implementation.spring location="helloworld-context.xml"/>
    </component>

</composite>

 

添加springbean 的配置:helloworld-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:sca="http://docs.oasis-open.org/ns/opencsa/sca-j/spring/200810"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/sca http://www.osoa.org/xmlns/sca/1.0/spring-sca.xsd">
	
	<sca:service name="Helloworld" target="HelloworldBean" />
	<bean id="HelloworldBean" class="samples.tuscany.helloworld.HelloworldImpl" />

</beans>

 

猜你喜欢

转载自mixo44.iteye.com/blog/1663011
今日推荐