Tuscany_SCA进行SOA开发学习笔记(第一节)

    SOA面向服务的架构,是一个组建模型,它将应用程序的不同的功能单元(常常称之为服务)通过定义良好的接口联系起来。

    SOA的解决方案主要有两个:JBI(Java Business Integration)业务集成架构;SCA(Service Component Architecture)服务组建架构。相比较JBI,SCA不受语言的限制,而JBI只支持java;SCA吸收了Spring的IoC思想,并实现了多级分布式应用的IoC模型。

    下面开始第一个例子:

一、搭建环境

    下载Eclipse,安装JDK,并在Eclipse下进行配置,具体不详述。这里我用的是:eclipse-jee-indigo-SR1-win32、tuscany-distribution-all-2.0

二、新建工程

     打开Eclipse,新建一个普通的java工程,点击“Next”,注意有个"Default ouput folder",这里编译生成的文件放在了“bin”目录。注意:在Apatch官网上下载的例子都是在"target/classes"目录,这里新建的java project的名字为SCAJAVA


     然后在工程下面新建目录"lib",将下载的tuscany-distribution-all-2.0\tuscany-sca-2.0\lib下的所有的jar包拷贝到工程下的lib文件夹中,然后构建路径。

三、编写服务端代码

     模拟一个加减乘除的计算器。

     新建接口AddService.java

     package calculator;
     public interface AddService {
double add(double n1,double n2);
     }

     编写它AddService的实现类。新建AddServiceImpl.java,实现AddService接口,如下:

    package calculator;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import org.oasisopen.sca.annotation.Service;
    public class AddServiceImpl implements AddService{
@Override
public double add(double n1, double n2) {
Logger logger = Logger.getLogger("calculator");
logger.log(Level.FINEST, "Adding "+n1+"and" + n2);
System.out.println("AddServiceImpl-->add");
return (n1 + n2);
}
}

      同理写上接口SubtractService、MultiplyService、DivideService,并实现们的java类为:SubtractServiceImpl、MultiplyServiceImp、DivideServiceImple

      编写大组件CalculatorService,实现对加减乘除的控制,该接口的定义如下:

     package calculator;
     public interface CalculatorService {
double add(double n1,double n2);
double subtract(double n1,double n2);
double multiply(double n1,double n2);
double divide(double n1,double n2);
     }

      CalculatorService的实现类的编写,名字为CalculatorServiceImpl,内容如下:

package calculator;
import org.oasisopen.sca.annotation.Reference;
import org.oasisopen.sca.annotation.Service;
public class CalculatorServiceImpl implements CalculatorService {
private AddService addService;
private SubtractService subtractService;
private MultiplyService multiplyService;
private DivideService divideService;

public CalculatorServiceImpl(){
System.out.println("CalculatorServiceImpl构造函数");
}

@Reference
public void setAddService(AddService addService) {
System.out.println("CalculatorServiceImpl-->setAddService");
this.addService = addService;
}

@Reference
public void setSubtractService(SubtractService subtractService) {
System.out.println("CalculatorServiceImpl-->setSubtractService");
this.subtractService = subtractService;
}

@Reference
public void setMultiplyService(MultiplyService multiplyService) {
System.out.println("CalculatorServiceImpl-->setMultiplyService");
this.multiplyService = multiplyService;
}


@Reference
public void setDivideService(DivideService divideService) {
System.out.println("CalculatorServiceImpl-->setDivideService");
this.divideService = divideService;
}


@Override
public double add(double n1, double n2) {
System.out.println("CalculatorServiceImpl-->add("+n1+","+n2+")");
return addService.add(n1, n2);
}


@Override
public double subtract(double n1, double n2) {
System.out.println("CalculatorServiceImpl-->subtract");
return subtractService.subtract(n1, n2);
}


@Override
public double multiply(double n1, double n2) {
System.out.println("CalculatorServiceImpl-->multiply");
return multiplyService.multiply(n1, n2);
}

@Override
public double divide(double n1, double n2) {
System.out.println("CalculatorServiceImpl-->divide");
return divideService.divide(n1, n2);
}
}

     注意:在要注入的set方法前面要加上“@reference”

四、编写.component文件

     这里取名为Calculator.component,内容如下:


           xmlns:tuscany="http://tuscany.apache.org/xmlns/sca/1.1"
           targetNamespace="http://sample"
           name="Calculator">


   
       
   

   
       
   

   
       
   

   
       
   

   
       
       
       
       
       
   



      注意:在CalculatorServiceComponent中的“addService”必须与CalculatorServiceImpl.java中的成员变量同名,其余三个也是一样。

五、编写客户端测试

      起名为:CalculatorTest.java,内容如下:

    package calculatortest;




import org.apache.tuscany.sca.Node;
import org.apache.tuscany.sca.TuscanyRuntime;
import org.oasisopen.sca.NoSuchServiceException;


import calculator.CalculatorService;


public class CalculatorTest {


/**
* @param args
*/
public static void main(String[] args) {
Node node = TuscanyRuntime.runComposite("calculator/Calculator.composite","bin");
CalculatorService calculatorService = null;
try {
calculatorService = node.getService(CalculatorService.class, "CalculatorServiceComponent");
System.out.println(calculatorService.add(10, 21));
System.out.println(calculatorService.subtract(21, 10));
System.out.println(calculatorService.multiply(10, 21));
System.out.println(calculatorService.divide(21, 10));
} catch (NoSuchServiceException e) {
e.printStackTrace();
}
}
}

六、运行测试


从中我们可以看到具体的执行过程,得到了我们想要的结果

你可能感兴趣的:(Tuxscany_SCA)