Apache tuscany SCA实例
SCA即服务组件架构。比较著名的实现是apache 下的开源项目tuscany。本文从搭建tuscany sca环境到实现两个简单示例说起。
附件中提供了tuscany插件的下载,将其解压后的plugins和features文件夹下的内容分别复制到eclipse下对应的文件夹下,即完成了插件的安装。
除了插件的安装,项目中还要导入相应的tuscany sca jar包(在附件中也有,将zip文件解压,找到目录下的lib子目录即可),当然如果使用maven就可以把项目对jar包的依赖通过pom文件进行配置,这里为了简单起见,先建立一个普通的java项目。
整个项目的目录结构如下图所示:
其中,HelloWorld是一个远程接口,而HelloWorldImpl是实现该接口的一个实现类
执行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
就会看到以下关于服务的描述文件:
第二种实现方式,是使用spring方式,整个项目结构如下
主要不同之处在于添加了spring的配置文件,以及.composite文件的一些修改
beans.xml文件如下:还可能有sca:reference,sca:property标签,这里简化了
组件的配置文件如下:
其他的一样,执行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文件中配置好了,添加如下依赖即可:
客户端测试代码,用于调用发布的服务
先执行服务器端,发布服务的应用程序,即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
程序源代码见附件