Apache tuscany SCA实例

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

Apache tuscany SCA实例_第1张图片
其中,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是配置实现的一个组件配置文件



	
		
		
		      //接口是java类
			  //以web service方式发布服务
		
	


执行Launcher类,控制台会打印出以下信息
2012-2-18 20:09:10 org.apache.tuscany.sca.node.impl.NodeImpl
信息: 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

就会看到以下关于服务的描述文件:
  
- 
- 
- 
- 
   
  
- 
- 
- 
   
  
  
  
  
  
- 
   
  
- 
   
  
- 
- 
   
   
  
  
- 
   
- 
   
- 
   
  
- 
   
  
  
  
- 
- 
   
  
  
  


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

Apache tuscany SCA实例_第2张图片
主要不同之处在于添加了spring的配置文件,以及.composite文件的一些修改
beans.xml文件如下:还可能有sca:reference,sca:property标签,这里简化了


	

    
			

组件的配置文件如下:



	
		
		
	
	
		
	

其他的一样,执行Launcher启动服务的结果如下:
2012-2-18 20:19:02 org.apache.tuscany.sca.node.impl.NodeImpl
信息: 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文件中配置好了,添加如下依赖即可:

			org.springframework
			spring
			${spring.version}
		
		
			org.springframework
			spring-core
			${spring.version}
		
		
			org.springframework
			spring-beans
			${spring.version}
		
		
			org.springframework
			spring-context
			${spring.version}
		


项目路径如下:

Apache tuscany SCA实例_第3张图片

客户端测试代码,用于调用发布的服务
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.
 



      
       
  
          
          
        
        
        
       
    
  




	


    
    
    
    
	

	
	
	
	

		

先执行服务器端,发布服务的应用程序,即LaunchSpring
2012-2-21 17:13:51 org.apache.tuscany.sca.node.impl.NodeImpl
信息: 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
信息: 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

程序源代码见附件
  • SCA_插件.rar (72.5 KB)
  • 下载次数: 304
  • Apache tuscany SCA实例_第4张图片
  • 大小: 14.3 KB
  • Apache tuscany SCA实例_第5张图片
  • 大小: 15.1 KB
  • Apache tuscany SCA实例_第6张图片
  • 大小: 20.3 KB
  • example1.rar (18.8 KB)
  • 下载次数: 152
  • 查看图片附件

你可能感兴趣的:(spring,xml,java,bean)