日期:2014-05-17  浏览次数:20729 次

Apache tuscany SCA实例
  SCA即服务组件架构。比较著名的实现是apache 下的开源项目tuscany。本文从搭建tuscany sca环境到实现两个简单示例说起。
  附件中提供了tuscany插件的下载,将其解压后的plugins和features文件夹下的内容分别复制到eclipse下对应的文件夹下,即完成了插件的安装。
  除了插件的安装,项目中还要导入相应的tuscany sca jar包(在附件中也有,将zip文件解压,找到目录下的lib子目录即可),当然如果使用maven就可以把项目对jar包的依赖通过pom文件进行配置,这里为了简单起见,先建立一个普通的java项目。
整个项目的目录结构如下图所示:


其中,HelloWorld是一个远程接口,而HelloWorldImpl是实现该接口的一个实现类
package helloworld;

import org.osoa.sca.annotations.Remotable;

@Remotable
public interface HelloWorld {

	public void sayHello(String name);
}


Launcher 是用于启动服务的一个测试类。
package launch;

import org.apache.tuscany.sca.host.embedded.SCADomain;

public class Launcher {

	public static void main(String[] args) throws InterruptedException {

		
		SCADomain.newInstance("helloworld.composite");
		System.out.println("Server started...");
		
		while(true){
			Thread.sleep(1000000);
		}
	}

}

helloworld.composite是配置实现的一个组件配置文件
<?xml version="1.0" encoding="UTF-8"?>
<composite xmlns="http://www.osoa.org/xmlns/sca/1.0"
    xmlns:t="http://tuscany.apache.org/xmlns/sca/1.0"
    xmlns:c="http://helloworld"
    targetNamespace="http://helloworld"
    name="helloworld">

	<component name="HelloWorldComponent">
		<implementation.java   //以java方式实现 class="helloworld.HelloWorldImpl"/>
		<service name="HelloWorld">
		    <interface.java interface="helloworld.HelloWorld" />  //接口是java类
			<binding.ws uri="http://localhost:8080/HelloWorld"/>  //以web service方式发布服务
		</service>
	</component>
</composite>


执行Launcher类,控制台会打印出以下信息
2012-2-18 20:09:10 org.apache.tuscany.sca.node.impl.NodeImpl <init>
信息: Creating node: helloworld.composite
2012-2-18 20:09:13 org.apache.tuscany.sca.node.impl.NodeImpl configureNode
信息: Loading contribution: file:/D:/Eclipse%20J2EE%20workspace/ws/bin/
2012-2-18 20:09:16 org.apache.tuscany.sca.node.impl.NodeImpl start
信息: Starting node: helloworld.composite
2012-2-18 20:09:19 org.apache.tuscany.sca.http.jetty.JettyServer addServletMapping
信息: Added Servlet mapping: http://PC-20090611SAHL:8080/HelloWorld
Server started...


代表服务已经发布,如果此时在浏览器中输出服务的访问地址http://localhost:8080/HelloWorld?wsdl

就会看到以下关于服务的描述文件:
 <?xml version="1.0" encoding="UTF-8" ?> 
- <wsdl:definitions name="HelloWorldService" targetNamespace="http://helloworld/" xmlns:tns="http://helloworld/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:SOAP="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:SOAP11="http://schemas.xmlsoap.org/wsdl/soap/">
- <wsdl:types>
- <xs:schema attributeFormDefault="qualified" elementFormDefault="unqualified" targetNamespace="http://helloworld/" xmlns:tns="http://helloworld/" xmlns:xs="http://www.w3.org/2001/XMLSchema">
- <xs:element name="sayHelloResponse">
  <xs:complexType /> 
  </xs:element>
- <xs:element name="sayHello">
- <xs:complexType>
- <xs:sequence>
  <xs:element minOccurs="0" name="arg0" nillable="true" type="xs:string" /> 
  </xs:sequence>
  </xs:complexType>
  </xs:element>
  </xs:schema>
  </wsdl:types>
- <wsdl:message name="sayHelloResponse">
  <wsdl:part name="sayHelloResponse" element="tns:sayHelloResponse" /> 
  </wsdl:message>
- <wsdl:message name="sayHello">
  <wsdl:part name="sayHello" element="tns:sayHello" /> 
  </wsdl:message>
- <wsdl:portType name="HelloWorld">
- <wsdl:operation name="sayHello">
  <wsdl:input message="tns:sayHello" /> 
  <wsdl:output message="tns