准备工作:
开发工具:Eclipse,这里用的是eclipse-jee-juno-win32,你可以更新eclipse soa相关插件
有利于以后的开发和可视化的操作
tuscany jar包,网址:http://tuscany.apache.org/sca-java-1x-releases.html
下载第一个,解压出来,找到lib,全部选中,在Eclipse里自定义一个tuscany的库,以后方便直接用。
例子:计算器加减乘除运算。
首先看看计算器组合构件的装配图:
其中包括了CalculatorServiceComponent,AddComponent,SubtractComponent,MultiplyComponent,DivideComponent这些组件。
CalculatorServiceComponent引用了其他的组件,CalculatorServiceComponent作为组合构件的服务。
整体代码结构如下图:
为了实现计算机功能,首先来设计相关接口和实现类
加法接口IAdd.java
package com.ajun.tuscany.server; /** * 加法接口 * @author ajun * @e-mail [email protected] * @blog http://blog.csdn.net/ajun_studio * @version 创建时间:2012-7-19 下午9:47:25 */ public interface IAdd { double add(double n1,double n2); }
加法实现类:Add.java
package com.ajun.tuscany.server; public class Add implements IAdd { @Override public double add(double n1, double n2) { return n1+n2; } }
package com.ajun.tuscany.server; /** * 减法接口 * @author ajun * @e-mail [email protected] * @blog http://blog.csdn.net/ajun_studio * @version 创建时间:2012-7-19 下午9:52:00 */ public interface ISubtract { double subtract(double n1,double n2); }
package com.ajun.tuscany.server; public class Subtract implements ISubtract { @Override public double subtract(double n1, double n2) { return n1-n2; } }
package com.ajun.tuscany.server; /** * 乘法接口 * @author ajun * @e-mail [email protected] * @blog http://blog.csdn.net/ajun_studio * @version 创建时间:2012-7-19 下午9:50:47 */ public interface IMultiply { double multiply(double n1,double n2); }
package com.ajun.tuscany.server; public class Multiply implements IMultiply { @Override public double multiply(double n1, double n2) { return n1*n2; } }
package com.ajun.tuscany.server; /** * 除法接口 * @author ajun * @e-mail [email protected] * @blog http://blog.csdn.net/ajun_studio * @version 创建时间:2012-7-19 下午9:48:53 */ public interface IDivide { double divide(double n1,double n2); }
package com.ajun.tuscany.server; public class Divide implements IDivide { @Override public double divide(double n1, double n2) { return n1/n2; } }
package com.ajun.tuscany.server; /** * 计算机接口 * @author ajun * @e-mail [email protected] * @blog http://blog.csdn.net/ajun_studio * @version 创建时间:2012-7-19 下午10:02:20 */ public interface ICalculator { /** * 加法 * @param n1 * @param n2 * @return */ double add(double n1,double n2); /** * 减法 * @param n1 * @param n2 * @return */ double subtract(double n1,double n2); /** * 除法 * @param n1 * @param n2 * @return */ double divide(double n1,double n2); /** * 乘法 * @param n1 * @param n2 * @return */ double multiply(double n1,double n2); }
计算机接口实现类:Calculator.java,引用其他四个组件IAdd,ISubtract,IMultiply,IDivide,并调用了对应的方法
package com.ajun.tuscany.server; import org.osoa.sca.annotations.Reference; public class Calculator implements ICalculator{ /** * 引用其他组件IAdd,ISubtract,IMultiply,IDivide * @Reference 说明引用其他组件 会和.composite文件进行配置 */ private IAdd add;//加法组件 private ISubtract subtract;//减法组件 private IMultiply multiply;//乘法组件 private IDivide divide;//除法组件 public IAdd getAdd() { return add; } @Reference public void setAdd(IAdd add) { this.add = add; } public ISubtract getSubtract() { return subtract; } @Reference public void setSubtract(ISubtract subtract) { this.subtract = subtract; } public IMultiply getMultiply() { return multiply; } @Reference public void setMultiply(IMultiply multiply) { this.multiply = multiply; } public IDivide getDivide() { return divide; } @Reference public void setDivide(IDivide divide) { this.divide = divide; } @Override public double add(double n1, double n2) { return this.add.add(n1, n2); } @Override public double subtract(double n1, double n2) { return this.subtract.subtract(n1, n2); } @Override public double divide(double n1, double n2) { return this.divide.divide(n1, n2); } @Override public double multiply(double n1, double n2) { return this.multiply.multiply(n1, n2); } }
至此以上为整个代码的服务端,下面我们用一般思路来实现客户端调用服务端。
package com.ajun.tuscany.client; import com.ajun.tuscany.server.Add; import com.ajun.tuscany.server.Calculator; import com.ajun.tuscany.server.Divide; import com.ajun.tuscany.server.IAdd; import com.ajun.tuscany.server.IDivide; import com.ajun.tuscany.server.IMultiply; import com.ajun.tuscany.server.ISubtract; import com.ajun.tuscany.server.Multiply; import com.ajun.tuscany.server.Subtract; /** * 普通java客户端 * @author ajun * @e-mail [email protected] * @blog http://blog.csdn.net/ajun_studio * @version 创建时间:2012-7-19 下午10:26:17 */ public class CalculatorClient { public static void main(String [] args){ Calculator c = new Calculator(); IAdd add = new Add(); c.setAdd(add); System.out.println("3 + 2 = "+c.add(3, 2)); ISubtract subtract = new Subtract(); c.setSubtract(subtract); System.out.println("3 - 2 = "+c.subtract(3, 2)); IMultiply multiply = new Multiply(); c.setMultiply(multiply); System.out.println("3 * 2 = "+c.multiply(3, 2)); IDivide divide= new Divide(); c.setDivide(divide); System.out.println("3 / 2 = "+c.divide(3, 2)); } }
3 + 2 = 5.0
3 - 2 = 1.0
3 * 2 = 6.0
3 / 2 = 1.5
以上程序并没有用tuscany的工厂来生成对象,完全利用java编程的特性。
要利用tuscany工厂,需要一个.composite结尾的xml文件,下面是以上计算机组件的xml文件
<?xml version="1.0" encoding="UTF-8"?> <composite xmlns="http://www.osoa.org/xmlns/sca/1.0" xmlns:tuscany="http://tuscany.apache.org/xmlns/sca/1.0" targetNamespace="http://calc" xmlns:c="http://calc" name="Calculator" > <component name="CalculatorServiceComponent"> <implementation.java class="com.ajun.tuscany.server.Calculator" /> <reference name="add" target="AddComponent"/> <reference name="subtract" target="SubtractComponent"/> <reference name="multiply" target="MultiplyComponent"/> <reference name="divide" target="DivideComponent"/> </component> <component name="AddComponent"> <implementation.java class="com.ajun.tuscany.server.Add" /> </component> <component name="SubtractComponent"> <implementation.java class="com.ajun.tuscany.server.Subtract" /> </component> <component name="MultiplyComponent"> <implementation.java class="com.ajun.tuscany.server.Multiply" /> </component> <component name="DivideComponent"> <implementation.java class="com.ajun.tuscany.server.Divide" /> </component> </composite>说明:组合构件的名字:Calculator
计算机构件的名字:CalculatorServiceComponent
<implementation.java class="com.ajun.tuscany.server.Calculator" />设置实现类,采用了java的实现方式,对应的实现类为:om.ajun.tuscany.server.Calculator
<reference name="add" target="AddComponent"/>引用其他组件 其中add必须和Calculator的set方法对应。
客户端程序代码:
package com.ajun.tuscany.client; import org.apache.tuscany.sca.host.embedded.SCADomain; import com.ajun.tuscany.server.Calculator; import com.ajun.tuscany.server.ICalculator; /** * 服务端 * @author ajun * @e-mail [email protected] * @blog http://blog.csdn.net/ajun_studio * @version 创建时间:2012-7-19 下午11:12:44 */ public class CalculatorServerClient { public static void main(String [] args){ SCADomain domain = SCADomain.newInstance("com/ajun/tuscany/server/Calculator.composite"); ICalculator c = domain.getService(Calculator.class, "CalculatorServiceComponent"); System.out.println("3 + 2 = "+c.add(3, 2)); System.out.println("3 - 2 = "+c.subtract(3, 2)); System.out.println("3 * 2 = "+c.multiply(3, 2)); System.out.println("3 / 2 = "+c.divide(3, 2)); } }
SCADomain domain = SCADomain.newInstance("com/ajun/tuscany/server/Calculator.composite"); 相当于spring 读取配置文件,进行实例化里的对象。
ICalculator c = domain.getService(Calculator.class, "CalculatorServiceComponent"); 获得实例对象 ,相当于spring的getBean();
运行输出结果:
2012-7-21 13:39:03 org.apache.tuscany.sca.node.impl.NodeImpl <init>
信息: Creating node: com/ajun/tuscany/server/Calculator.composite
2012-7-21 13:39:07 org.apache.tuscany.sca.node.impl.NodeImpl configureNode
信息: Loading contribution: file:/E:/springdm/Calculatoer_01/bin/
2012-7-21 13:39:11 org.apache.tuscany.sca.node.impl.NodeImpl start
信息: Starting node: com/ajun/tuscany/server/Calculator.composite
3 + 2 = 5.0
3 - 2 = 1.0
3 * 2 = 6.0
3 / 2 = 1.5
到此整个tuscany入门结束。