`

Apache tuscany SCA实例

    博客分类:
  • 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:sayHelloResponse" /> 
  </wsdl:operation>
  </wsdl:portType>
- <wsdl:binding name="HelloWorldBinding" type="tns:HelloWorld">
  <SOAP:binding style="document" transport="http://schemas.xmlsoap.org/soap/http" /> 
- <wsdl:operation name="sayHello">
  <SOAP:operation /> 
- <wsdl:input>
  <SOAP:body use="literal" /> 
  </wsdl:input>
- <wsdl:output>
  <SOAP:body use="literal" /> 
  </wsdl:output>
  </wsdl:operation>
  </wsdl:binding>
- <wsdl:service name="HelloWorldService">
- <wsdl:port name="HelloWorldPort" binding="tns:HelloWorldBinding">
  <SOAP:address location="http://localhost:8080/HelloWorld" /> 
  </wsdl:port>
  </wsdl:service>
  </wsdl:definitions>


第二种实现方式,是使用spring方式,整个项目结构如下


主要不同之处在于添加了spring的配置文件,以及.composite文件的一些修改
beans.xml文件如下:还可能有sca:reference,sca:property标签,这里简化了
<?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://www.springframework.org/schema/sca"
       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" type="helloworld.HelloWorld" target="HelloWorldServiceBean" />
    <bean id="HelloWorldServiceBean" class="helloworld.HelloWorldImpl" >
	</bean>		
</beans>

组件的配置文件如下:
<?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">

	<service name="HelloWorld" promote="HelloWorldComponent">
		<interface.java interface="helloworld.HelloWorld" />
		<binding.ws uri="http://localhost:8080/services/HelloWorldService" />
	</service>
	<component name="HelloWorldComponent">
		<implementation.spring location="resources/spring/beans.xml" />
	</component>
</composite>

其他的一样,执行Launcher启动服务的结果如下:
2012-2-18 20:19:02 org.apache.tuscany.sca.node.impl.NodeImpl <init>
信息: Creating node: helloworld.composite
2012-2-18 20:19:03 org.apache.tuscany.sca.node.impl.NodeImpl configureNode
信息: Loading contribution: file:/D:/Eclipse%20J2EE%20workspace/scaTest/bin/
2012-2-18 20:19:05 org.apache.tuscany.sca.node.impl.NodeImpl start
信息: Starting node: helloworld.composite
- Loading XML bean definitions from URL [file:/D:/Eclipse%20J2EE%20workspace/scaTest/bin/resources/spring/beans.xml]
2012-2-18 20:19:07 org.apache.tuscany.sca.http.jetty.JettyServer addServletMapping
信息: Added Servlet mapping: http://PC-20090611SAHL:8080/services/HelloWorldService
- Refreshing org.apache.tuscany.sca.implementation.spring.runtime.context.SCAGenericApplicationContext@10b23cf: display name [org.apache.tuscany.sca.implementation.spring.runtime.context.SCAGenericApplicationContext@10b23cf]; startup date [Sat Feb 18 20:19:07 CST 2012]; parent: org.apache.tuscany.sca.implementation.spring.runtime.context.SCAParentApplicationContext@12b9f14
- Bean factory for application context [org.apache.tuscany.sca.implementation.spring.runtime.context.SCAGenericApplicationContext@10b23cf]: org.springframework.beans.factory.xml.XmlBeanFactory@d8e902
- Pre-instantiating singletons in org.springframework.beans.factory.xml.XmlBeanFactory@d8e902: defining beans [HelloWorldServiceBean]; parent: org.apache.tuscany.sca.implementation.spring.runtime.context.SCAParentApplicationContext@12b9f14
Server started...


以上只是发布服务,并没有调用发布的服务,以下是一个使用spring实现的服务发布和调用的例子。


使用了maven管理项目,但由于对SCA的一些依赖包还不够熟悉,因此建了一个用户库,将下载下来的tuscany、 sca所需要的jar包加到用户库中,该项目的构建环境下添入新建的sca用户库,即可以再程序中使用sca相关的类。对于spring的依赖在maven的pom文件中配置好了,添加如下依赖即可:
<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-beans</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>${spring.version}</version>
		</dependency>


项目路径如下:



客户端测试代码,用于调用发布的服务
package useService;


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

import com.sohu.mywork.HelloSpringConsumer;

import spring.SpringApplicationContextHolder;

public class UseServiceClient {
	public static void main(String[] args) {

		
		SCADomain.newInstance("comsumer.composite");
		System.out.println("client start...");
		
		HelloSpringConsumer consumer = (HelloSpringConsumer) SpringApplicationContextHolder.context.getBean("helloSpringConsumer");
		
		System.out.println(consumer.execute("sohu"));
	}

}


服务器端发布服务:
package launch;

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

public class LauncherSpring {

	public static void main(String[] args) {
		SCADomain.newInstance("hellospring.composite");
		System.out.println("server started...");
		try {
			Thread.sleep(1000000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}

}
}


其中服务器端的组件配置文件和spring配置文件与上一个例子相同,而客户端的sping配置文件盒组件配置文件为consumerbeans.xml,组件配置文件为consumer.composite.
<?xml version="1.0" encoding="UTF-8"?> 
<composite name="consumerComposite"
targetNamespace="http://tuscany.apache.org/xmlns/sca/1.0"
xmlns="http://www.osoa.org/xmlns/sca/1.0"
xmlns:nsl="http://www.osoa.org/xmlns/sca/1.0" >


      
       
<component name="HelloSpring">  
        <implementation.spring   location="consumerbeans.xml"/>  
        <reference name = "HelloSpringService">  <!-- mapping beans.xml   sca:reference -->
        <binding.ws uri="http://localhost:8080/HelloSpringService" />
        
        </reference>
       
  </component>  
</composite>  


<?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://www.springframework.org/schema/sca"
       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:reference name="HelloSpringService" type="com.sohu.mywork.Hello"/>

    <bean id="helloSpringConsumer" class="com.sohu.mywork.HelloSpringConsumer" >
    <property name="service">
    <ref bean="HelloSpringService"/>
    </property>
	</bean>

	<bean id="springBeanFactoryHolder" class="spring.SpringApplicationContextHolder" >
	
	
	</bean>

		
</beans>

先执行服务器端,发布服务的应用程序,即LaunchSpring
2012-2-21 17:13:51 org.apache.tuscany.sca.node.impl.NodeImpl <init>
信息: Creating node: hellospring.composite
2012-2-21 17:13:52 org.apache.tuscany.sca.node.impl.NodeImpl configureNode
信息: Loading contribution: file:/F:/Eclipse%20Workspace/example1/target/classes/
2012-2-21 17:13:53 org.apache.tuscany.sca.node.impl.NodeImpl start
信息: Starting node: hellospring.composite
- Loading XML bean definitions from URL [file:/F:/Eclipse%20Workspace/example1/target/classes/beans.xml]
2012-2-21 17:13:53 org.apache.tuscany.sca.http.jetty.JettyServer addServletMapping
信息: Added Servlet mapping: http://zjm-HP:8080/HelloSpringService
- Refreshing org.apache.tuscany.sca.implementation.spring.runtime.context.SCAGenericApplicationContext@1c958af: display name [org.apache.tuscany.sca.implementation.spring.runtime.context.SCAGenericApplicationContext@1c958af]; startup date [Tue Feb 21 17:13:53 CST 2012]; parent: org.apache.tuscany.sca.implementation.spring.runtime.context.SCAParentApplicationContext@992fa5
- Bean factory for application context [org.apache.tuscany.sca.implementation.spring.runtime.context.SCAGenericApplicationContext@1c958af]: org.springframework.beans.factory.xml.XmlBeanFactory@10718b7
Spring parent context - containsBean called for name: loadTimeWeaver
- Pre-instantiating singletons in org.springframework.beans.factory.xml.XmlBeanFactory@10718b7: defining beans [helloSpringBean]; parent: org.apache.tuscany.sca.implementation.spring.runtime.context.SCAParentApplicationContext@992fa5
server started...

再执行客户端的代码,即UseServiceClient,调用已经发布的服务
2012-2-21 17:15:25 org.apache.tuscany.sca.node.impl.NodeImpl <init>
信息: Creating node: comsumer.composite
2012-2-21 17:15:26 org.apache.tuscany.sca.node.impl.NodeImpl configureNode
信息: Loading contribution: file:/F:/Eclipse%20Workspace/example1/target/classes/
2012-2-21 17:15:27 org.apache.tuscany.sca.node.impl.NodeImpl start
信息: Starting node: comsumer.composite
- Loading XML bean definitions from URL [file:/F:/Eclipse%20Workspace/example1/target/classes/consumerbeans.xml]
- Refreshing org.apache.tuscany.sca.implementation.spring.runtime.context.SCAGenericApplicationContext@bd93cd: display name [org.apache.tuscany.sca.implementation.spring.runtime.context.SCAGenericApplicationContext@bd93cd]; startup date [Tue Feb 21 17:15:27 CST 2012]; parent: org.apache.tuscany.sca.implementation.spring.runtime.context.SCAParentApplicationContext@24bfaa
- Bean factory for application context [org.apache.tuscany.sca.implementation.spring.runtime.context.SCAGenericApplicationContext@bd93cd]: org.springframework.beans.factory.xml.XmlBeanFactory@a34783
Spring parent context - containsBean called for name: loadTimeWeaver
- Pre-instantiating singletons in org.springframework.beans.factory.xml.XmlBeanFactory@a34783: defining beans [helloSpringConsumer,springBeanFactoryHolder]; parent: org.apache.tuscany.sca.implementation.spring.runtime.context.SCAParentApplicationContext@24bfaa
Spring parent context - getBean called for name: HelloSpringService
client start...
Hello sohu

程序源代码见附件
  • 大小: 14.3 KB
  • 大小: 15.1 KB
  • 大小: 20.3 KB
分享到:
评论
3 楼 wang3065 2012-10-30  
运行 LauncherSpring 的结果:
 
信息: Starting node: hellospring.composite  
server started...


运行 UseServiceClient 的时候报错了:
严重: Reference not found for component reference: Component = HelloSpring Reference = HelloSpringService
2012-10-30 15:06:23 org.apache.tuscany.sca.assembly.builder.impl.CompositeBindingURIBuilderImpl
警告: No implementation for component: Component = HelloSpring
2012-10-30 15:06:23 org.apache.tuscany.sca.assembly.builder.impl.CompositeBindingURIBuilderImpl
严重: Reference not found for component reference: Component = HelloSpring Reference = HelloSpringService
Exception in thread "main" org.osoa.sca.ServiceRuntimeException: Reference not found for component reference: Component = HelloSpring Reference = HelloSpringService
	at org.apache.tuscany.sca.node.impl.NodeImpl.analyzeProblems(NodeImpl.java:653)
	at org.apache.tuscany.sca.node.impl.NodeImpl.configureNode(NodeImpl.java:574)
	at org.apache.tuscany.sca.node.impl.NodeImpl.<init>(NodeImpl.java:167)
	at org.apache.tuscany.sca.node.impl.NodeFactoryImpl.createSCANodeFromClassLoader(NodeFactoryImpl.java:37)
	at org.apache.tuscany.sca.host.embedded.impl.DefaultSCADomain.init(DefaultSCADomain.java:175)
	at org.apache.tuscany.sca.host.embedded.impl.DefaultSCADomain.<init>(DefaultSCADomain.java:97)
	at org.apache.tuscany.sca.host.embedded.SCADomain.createNewInstance(SCADomain.java:182)
	at org.apache.tuscany.sca.host.embedded.SCADomain.newInstance(SCADomain.java:63)
	at useService.UseServiceClient.main(UseServiceClient.java:14)

2 楼 wang3065 2012-10-30  
运行 LauncherSpring 的结果:
1 楼 wang3065 2012-10-30  
你好,我运行example1的时候报一个错,求解

相关推荐

Global site tag (gtag.js) - Google Analytics