天天看点

Tuscany SCA案例分析(二)(连载中...)

今天给大家讲的是calculator的分布式版本,在这个案例中,我们先来看看其设计图,如下:

Tuscany SCA案例分析(二)(连载中...)

从图中可以明显地看出有3个节点nodeA,nodeB,nodeC,而nodeA上有三个组件,nodeB和nodeC上分别有一个组件。这3个节点在同一个domain中。服务引用的关系在这里就不多说了。

那么我们下面进入开发过程了。

先建立一个普通的java project,项目名叫"MyDistributedCalculator",默认设置

Tuscany SCA案例分析(二)(连载中...)

 在书写主要的业务逻辑单元代码之前,先象案例一一样,加入MyTuscany用户库,好,现在集中精力书写逻辑代码,因为这里对案例一有一个承接关系,所以建议看案例二之前先看明白案例一。我们这里将案例一项目中的calculator包下的除CalculatorClient.java文件外的java文件都copy到myDistributedCalculator项目的calculator包下。如下图:

Tuscany SCA案例分析(二)(连载中...)

这里绝大部分代码和案例一都一样,只是因为分布式的原因,所以就要修改几个接口部分的代码,因为AddServiceComponent和SubtractServiceComponent都在另外的机子上,所以在他们相应的服务接口上要声明为@Remotable,以便让nodeA上的CalculatorServiceComponent远程引用,这和案例一是不一样的地方。这里给出他们修改后的代码:

Tuscany SCA案例分析(二)(连载中...)
Tuscany SCA案例分析(二)(连载中...)
Tuscany SCA案例分析(二)(连载中...)
Tuscany SCA案例分析(二)(连载中...)

package  calculator;

Tuscany SCA案例分析(二)(连载中...)
Tuscany SCA案例分析(二)(连载中...)

import  org.osoa.sca.annotations.Remotable;

Tuscany SCA案例分析(二)(连载中...)
Tuscany SCA案例分析(二)(连载中...)
Tuscany SCA案例分析(二)(连载中...)
Tuscany SCA案例分析(二)(连载中...)

@Remotable

Tuscany SCA案例分析(二)(连载中...)
Tuscany SCA案例分析(二)(连载中...)

public   interface  AddService  ... {

Tuscany SCA案例分析(二)(连载中...)
Tuscany SCA案例分析(二)(连载中...)

    double add(double n1, double n2);

Tuscany SCA案例分析(二)(连载中...)
Tuscany SCA案例分析(二)(连载中...)

}

Tuscany SCA案例分析(二)(连载中...)
Tuscany SCA案例分析(二)(连载中...)
Tuscany SCA案例分析(二)(连载中...)
Tuscany SCA案例分析(二)(连载中...)

package  calculator;

Tuscany SCA案例分析(二)(连载中...)
Tuscany SCA案例分析(二)(连载中...)

import  org.osoa.sca.annotations.Remotable;

Tuscany SCA案例分析(二)(连载中...)
Tuscany SCA案例分析(二)(连载中...)
Tuscany SCA案例分析(二)(连载中...)
Tuscany SCA案例分析(二)(连载中...)

@Remotable

Tuscany SCA案例分析(二)(连载中...)
Tuscany SCA案例分析(二)(连载中...)

public   interface  SubtractService  ... {

Tuscany SCA案例分析(二)(连载中...)
Tuscany SCA案例分析(二)(连载中...)

    double subtract(double n1, double n2);

Tuscany SCA案例分析(二)(连载中...)
Tuscany SCA案例分析(二)(连载中...)

}

接下来要做的就是要写一个domain manager类和node启动类,我们先建立node包,然后建立相应的文件,其中用于启动domain manager类的文件名为DomainNode.java,用于启动node的类文件名为CalculatorNode.java

他们分别的内容为:

Tuscany SCA案例分析(二)(连载中...)
Tuscany SCA案例分析(二)(连载中...)
Tuscany SCA案例分析(二)(连载中...)
Tuscany SCA案例分析(二)(连载中...)
Tuscany SCA案例分析(二)(连载中...)

package  node;

Tuscany SCA案例分析(二)(连载中...)
Tuscany SCA案例分析(二)(连载中...)

import  java.io.IOException;

Tuscany SCA案例分析(二)(连载中...)
Tuscany SCA案例分析(二)(连载中...)

import  javax.xml.namespace.QName;

Tuscany SCA案例分析(二)(连载中...)
Tuscany SCA案例分析(二)(连载中...)

import  org.apache.tuscany.sca.domain.SCADomain;

Tuscany SCA案例分析(二)(连载中...)

import  org.apache.tuscany.sca.node.SCANode;

Tuscany SCA案例分析(二)(连载中...)

import  org.apache.tuscany.sca.node.SCANodeFactory;

Tuscany SCA案例分析(二)(连载中...)
Tuscany SCA案例分析(二)(连载中...)

import  calculator.CalculatorService;

Tuscany SCA案例分析(二)(连载中...)
Tuscany SCA案例分析(二)(连载中...)
Tuscany SCA案例分析(二)(连载中...)
Tuscany SCA案例分析(二)(连载中...)
Tuscany SCA案例分析(二)(连载中...)

public   class  CalculatorNode  ... {

Tuscany SCA案例分析(二)(连载中...)
Tuscany SCA案例分析(二)(连载中...)
Tuscany SCA案例分析(二)(连载中...)
Tuscany SCA案例分析(二)(连载中...)

    public static void main(String[] args) throws Exception ...{

Tuscany SCA案例分析(二)(连载中...)
Tuscany SCA案例分析(二)(连载中...)

        // Check that the correct arguments have been provided

Tuscany SCA案例分析(二)(连载中...)
Tuscany SCA案例分析(二)(连载中...)

        if (null == args || args.length < 2) ...{

Tuscany SCA案例分析(二)(连载中...)

             System.err.println("Useage: java CalculatorNode domainname nodename");   

Tuscany SCA案例分析(二)(连载中...)

             System.exit(1);

Tuscany SCA案例分析(二)(连载中...)

        }    

Tuscany SCA案例分析(二)(连载中...)
Tuscany SCA案例分析(二)(连载中...)
Tuscany SCA案例分析(二)(连载中...)

        try ...{

Tuscany SCA案例分析(二)(连载中...)

            String domainName = args[0];

Tuscany SCA案例分析(二)(连载中...)

            String nodeName   = args[1];

Tuscany SCA案例分析(二)(连载中...)
Tuscany SCA案例分析(二)(连载中...)

            ClassLoader cl = CalculatorNode.class.getClassLoader();

Tuscany SCA案例分析(二)(连载中...)
Tuscany SCA案例分析(二)(连载中...)

            SCANodeFactory nodeFactory = SCANodeFactory.newInstance();

Tuscany SCA案例分析(二)(连载中...)

            SCANode node = nodeFactory.createSCANode(nodeName, domainName);

Tuscany SCA案例分析(二)(连载中...)

            node.addContribution(nodeName, cl.getResource(nodeName + "/"));

Tuscany SCA案例分析(二)(连载中...)

            //System.out.println(cl.getResource(nodeName + "/").toString());

Tuscany SCA案例分析(二)(连载中...)

            node.deployComposite(new QName("http://sample", "Calculator"));

Tuscany SCA案例分析(二)(连载中...)

            node.start();             

Tuscany SCA案例分析(二)(连载中...)
Tuscany SCA案例分析(二)(连载中...)

            // nodeA is the head node and runs some tests while all other nodes

Tuscany SCA案例分析(二)(连载中...)

            // simply listen for incoming messages

Tuscany SCA案例分析(二)(连载中...)
Tuscany SCA案例分析(二)(连载中...)

            if ( nodeName.equals("nodeA") ) ...{            

Tuscany SCA案例分析(二)(连载中...)

                // do some application stuff

Tuscany SCA案例分析(二)(连载中...)

                CalculatorService calculatorService = 

Tuscany SCA案例分析(二)(连载中...)

                    node.getDomain().getService(CalculatorService.class, "CalculatorServiceComponentA");

Tuscany SCA案例分析(二)(连载中...)
Tuscany SCA案例分析(二)(连载中...)

                // Calculate

Tuscany SCA案例分析(二)(连载中...)

                System.out.println("3 + 2=" + calculatorService.add(3, 2));

Tuscany SCA案例分析(二)(连载中...)

                System.out.println("3 - 2=" + calculatorService.subtract(3, 2));

Tuscany SCA案例分析(二)(连载中...)

                System.out.println("3 * 2=" + calculatorService.multiply(3, 2));

Tuscany SCA案例分析(二)(连载中...)

                System.out.println("3 / 2=" + calculatorService.divide(3, 2));

Tuscany SCA案例分析(二)(连载中...)
Tuscany SCA案例分析(二)(连载中...)

                // a little hidden loop test to put some load on the nodes

Tuscany SCA案例分析(二)(连载中...)
Tuscany SCA案例分析(二)(连载中...)

                if (args.length > 2)...{

Tuscany SCA案例分析(二)(连载中...)
Tuscany SCA案例分析(二)(连载中...)

                    for (int i=0; i < 1000; i++)...{

Tuscany SCA案例分析(二)(连载中...)

                        // Calculate

Tuscany SCA案例分析(二)(连载中...)

                        System.out.println("3 + 2=" + calculatorService.add(3, 2));

Tuscany SCA案例分析(二)(连载中...)

                        System.out.println("3 - 2=" + calculatorService.subtract(3, 2));

Tuscany SCA案例分析(二)(连载中...)

                        System.out.println("3 * 2=" + calculatorService.multiply(3, 2));

Tuscany SCA案例分析(二)(连载中...)

                        System.out.println("3 / 2=" + calculatorService.divide(3, 2));

Tuscany SCA案例分析(二)(连载中...)

                    }

Tuscany SCA案例分析(二)(连载中...)

                }

Tuscany SCA案例分析(二)(连载中...)
Tuscany SCA案例分析(二)(连载中...)

            } else ...{

Tuscany SCA案例分析(二)(连载中...)

                // start up and wait for messages

Tuscany SCA案例分析(二)(连载中...)
Tuscany SCA案例分析(二)(连载中...)

                try ...{

Tuscany SCA案例分析(二)(连载中...)

                    System.out.println("Node started (press enter to shutdown)");

Tuscany SCA案例分析(二)(连载中...)

                    System.in.read();

Tuscany SCA案例分析(二)(连载中...)
Tuscany SCA案例分析(二)(连载中...)

                } catch (IOException e) ...{

Tuscany SCA案例分析(二)(连载中...)

                    e.printStackTrace();

Tuscany SCA案例分析(二)(连载中...)

                }  

Tuscany SCA案例分析(二)(连载中...)

            }

Tuscany SCA案例分析(二)(连载中...)
Tuscany SCA案例分析(二)(连载中...)

            // stop the node and all the domains in it 

Tuscany SCA案例分析(二)(连载中...)

            node.stop(); 

Tuscany SCA案例分析(二)(连载中...)
Tuscany SCA案例分析(二)(连载中...)
Tuscany SCA案例分析(二)(连载中...)

        } catch(Exception ex) ...{

Tuscany SCA案例分析(二)(连载中...)

            System.err.println("Exception in node - " + ex.getMessage());

Tuscany SCA案例分析(二)(连载中...)

            ex.printStackTrace(System.err);

Tuscany SCA案例分析(二)(连载中...)

        }

Tuscany SCA案例分析(二)(连载中...)

    }

Tuscany SCA案例分析(二)(连载中...)

}

Tuscany SCA案例分析(二)(连载中...)
Tuscany SCA案例分析(二)(连载中...)
Tuscany SCA案例分析(二)(连载中...)
Tuscany SCA案例分析(二)(连载中...)
Tuscany SCA案例分析(二)(连载中...)

package  node;

Tuscany SCA案例分析(二)(连载中...)
Tuscany SCA案例分析(二)(连载中...)

import  org.apache.tuscany.sca.domain.SCADomain;

Tuscany SCA案例分析(二)(连载中...)

import  org.apache.tuscany.sca.domain.SCADomainFactory;

Tuscany SCA案例分析(二)(连载中...)
Tuscany SCA案例分析(二)(连载中...)
Tuscany SCA案例分析(二)(连载中...)
Tuscany SCA案例分析(二)(连载中...)
Tuscany SCA案例分析(二)(连载中...)
Tuscany SCA案例分析(二)(连载中...)

public   class  DomainNode  ... {

Tuscany SCA案例分析(二)(连载中...)
Tuscany SCA案例分析(二)(连载中...)

    private static String DEFAULT_DOMAIN_URI = "http://localhost:8877";

Tuscany SCA案例分析(二)(连载中...)
Tuscany SCA案例分析(二)(连载中...)
Tuscany SCA案例分析(二)(连载中...)

    public static void main(String[] args) ...{

Tuscany SCA案例分析(二)(连载中...)
Tuscany SCA案例分析(二)(连载中...)
Tuscany SCA案例分析(二)(连载中...)

        try ...{

Tuscany SCA案例分析(二)(连载中...)

            SCADomainFactory domainFactory = SCADomainFactory.newInstance();

Tuscany SCA案例分析(二)(连载中...)

            SCADomain domain = domainFactory.createSCADomain(DEFAULT_DOMAIN_URI); 

Tuscany SCA案例分析(二)(连载中...)

            domain.start();

Tuscany SCA案例分析(二)(连载中...)
Tuscany SCA案例分析(二)(连载中...)

            System.out.println("Domain started (press enter to shutdown)");

Tuscany SCA案例分析(二)(连载中...)

            System.in.read();

Tuscany SCA案例分析(二)(连载中...)
Tuscany SCA案例分析(二)(连载中...)

            domain.stop();

Tuscany SCA案例分析(二)(连载中...)
Tuscany SCA案例分析(二)(连载中...)

        } catch (Exception e) ...{

Tuscany SCA案例分析(二)(连载中...)

            e.printStackTrace();

Tuscany SCA案例分析(二)(连载中...)

        }

Tuscany SCA案例分析(二)(连载中...)
Tuscany SCA案例分析(二)(连载中...)

        System.out.println("Domain stopped");

Tuscany SCA案例分析(二)(连载中...)

    }

Tuscany SCA案例分析(二)(连载中...)
Tuscany SCA案例分析(二)(连载中...)

}

Tuscany SCA案例分析(二)(连载中...)

这里将我自己的理解和对代码的分析写下来:

先从DomainNode.java开始分析,里面用到了两个类, SCADomainFactory 和 SCADomain。代码比较简单,就是用 SCADomainFactory 本身的newInstance()生成自己的实例,然后通过 createSCADomain()函数生成SCADomain的实例,然后用 domain.start(); 启动domain。这里要特别注意的是,虽然代码很简单,但其实它完成的工作可不少,首先 createSCADomain()会去实例化一个实现了SCADomain接口的类,这里是SCADomainImpl类,通过查看分析tuscany的源代码,发现该类初始化函数调用了init()函数

Tuscany SCA案例分析(二)(连载中...)

而在init()函数中完成了一些关键的工作,其中里面有一个就是初始化一些domain manager的服务组件,那么根据什么来初始化这些组件呢,就是根据domain.composite文件,该文件的位置是在tuscany-sca-all-1.0.1-incubating.jar文件内部,如图:

Tuscany SCA案例分析(二)(连载中...)

该文件的内容如下:

Tuscany SCA案例分析(二)(连载中...)

< composite  xmlns ="http://www.osoa.org/xmlns/sca/1.0"

Tuscany SCA案例分析(二)(连载中...)

           targetNamespace ="http://sample"

Tuscany SCA案例分析(二)(连载中...)

           xmlns:sample ="http://sample"

Tuscany SCA案例分析(二)(连载中...)

           xmlns:tuscany ="http://tuscany.apache.org/xmlns/sca/1.0"

Tuscany SCA案例分析(二)(连载中...)

           name ="Domain" >

Tuscany SCA案例分析(二)(连载中...)
Tuscany SCA案例分析(二)(连载中...)

     < component  name ="domain" >

Tuscany SCA案例分析(二)(连载中...)

        < tuscany:implementation .resource location ="webroot" />

Tuscany SCA案例分析(二)(连载中...)

         < service  name ="Resource" >

Tuscany SCA案例分析(二)(连载中...)

             < tuscany:binding .http uri ="http://localhost:8877/domain" />

Tuscany SCA案例分析(二)(连载中...)

         </ service >

Tuscany SCA案例分析(二)(连载中...)

     </ component >             

Tuscany SCA案例分析(二)(连载中...)
Tuscany SCA案例分析(二)(连载中...)

     < component  name ="DomainManagerComponent" >

Tuscany SCA案例分析(二)(连载中...)

         < implementation .java class ="org.apache.tuscany.sca.domain.impl.DomainManagerServiceImpl" />

Tuscany SCA案例分析(二)(连载中...)

         < service  name ="DomainManagerInitService" >

Tuscany SCA案例分析(二)(连载中...)

             < interface .java interface ="org.apache.tuscany.sca.domain.DomainManagerInitService" />

Tuscany SCA案例分析(二)(连载中...)

             < binding .sca />

Tuscany SCA案例分析(二)(连载中...)

         </ service >

Tuscany SCA案例分析(二)(连载中...)

         < service  name ="DomainManagerNodeEventService" >

Tuscany SCA案例分析(二)(连载中...)

             < interface .java interface ="org.apache.tuscany.sca.domain.DomainManagerNodeEventService" />

Tuscany SCA案例分析(二)(连载中...)

             < binding .ws uri ="http://localhost:8877/DomainManagerComponent/DomainManagerNodeEventService" />

Tuscany SCA案例分析(二)(连载中...)

         </ service >

Tuscany SCA案例分析(二)(连载中...)

         < service  name ="DomainManagementService" >

Tuscany SCA案例分析(二)(连载中...)

             < interface .java interface ="org.apache.tuscany.sca.domain.management.DomainManagementService" />

Tuscany SCA案例分析(二)(连载中...)

             < tuscany:binding .jsonrpc uri ="http://localhost:8877/DomainManagerComponent/DomainManagementService" />

Tuscany SCA案例分析(二)(连载中...)

         </ service >         

Tuscany SCA案例分析(二)(连载中...)

     </ component >   

Tuscany SCA案例分析(二)(连载中...)
Tuscany SCA案例分析(二)(连载中...)

</ composite > 注意到上面的 < tuscany:binding .http uri ="http://localhost:8877/domain" />,这里是这些组件注册为相应的servlet时的uri地址,这也就要求DomainNode.java中的 private static String DEFAULT_DOMAIN_URI = "http://localhost:8877";必须是domain manager的根地址,必须和 tuscany:binding的uri保持一致。到目前为止,我们就解释完DomainNode的内容了,下面我们看看复杂些的CalculatorNode.java的内容。其实他的内容仔细看也不复杂,主干部分的代码功能主要是从命令行中接受两个参数,一个是domainname,一个是nodename,然后根据这些信息来启动node,这里跟前面的DomainNode有点类似,也是先用 SCANodeFactory的newInstance()实例化自身,再用 createSCANode实例化出一个实现了SCANode接口的 类,这里是SCANodeImpl类,该类的初始化函数中也执行了其相应的init()函数,而init()函数则完成了对node管理组件的初始化,是根据tuscany-sca-all-1.0.1-incubating.jar文件内部的node.composite文件,下面将其文件内容列出如下:

Tuscany SCA案例分析(二)(连载中...)

< composite  xmlns ="http://www.osoa.org/xmlns/sca/1.0"

Tuscany SCA案例分析(二)(连载中...)

           targetNamespace ="http://management"

Tuscany SCA案例分析(二)(连载中...)

           xmlns:sample ="http://management"

Tuscany SCA案例分析(二)(连载中...)

           xmlns:tuscany ="http://tuscany.apache.org/xmlns/sca/1.0"

Tuscany SCA案例分析(二)(连载中...)

           name ="Management" >

Tuscany SCA案例分析(二)(连载中...)
Tuscany SCA案例分析(二)(连载中...)

     < component  name ="DomainManagerComponent" >

Tuscany SCA案例分析(二)(连载中...)

         < implementation .java class ="org.apache.tuscany.sca.node.impl.DomainManagerServiceImpl" />

Tuscany SCA案例分析(二)(连载中...)

         < reference  name ="domainManager" >

Tuscany SCA案例分析(二)(连载中...)

             < interface .java interface ="org.apache.tuscany.sca.domain.DomainManagerNodeEventService" />

Tuscany SCA案例分析(二)(连载中...)

             < binding .ws uri ="http://localhost:8878/DomainManagerComponent/DomainManagerNodeEventService" />

Tuscany SCA案例分析(二)(连载中...)

         </ reference >

Tuscany SCA案例分析(二)(连载中...)

     </ component >            

Tuscany SCA案例分析(二)(连载中...)
Tuscany SCA案例分析(二)(连载中...)

     < component  name ="NodeManagerComponent" >

Tuscany SCA案例分析(二)(连载中...)

         < implementation .java class ="org.apache.tuscany.sca.node.impl.NodeManagerServiceImpl" />

Tuscany SCA案例分析(二)(连载中...)

         < service  name ="NodeManagerInitService" >

Tuscany SCA案例分析(二)(连载中...)

             < interface .java interface ="org.apache.tuscany.sca.node.NodeManagerInitService" />

Tuscany SCA案例分析(二)(连载中...)

             < binding .sca />

Tuscany SCA案例分析(二)(连载中...)

         </ service >

Tuscany SCA案例分析(二)(连载中...)

         < service  name ="NodeManagerService" >

Tuscany SCA案例分析(二)(连载中...)

             < interface .java interface ="org.apache.tuscany.sca.node.NodeManagerService" />

Tuscany SCA案例分析(二)(连载中...)

             < binding .ws uri ="http://localhost:8878/NodeManagerComponent/NodeManagerService" />

Tuscany SCA案例分析(二)(连载中...)

         </ service >

Tuscany SCA案例分析(二)(连载中...)

         < service  name ="ComponentManagerService" >

Tuscany SCA案例分析(二)(连载中...)

             < interface .java interface ="org.apache.tuscany.sca.node.ComponentManagerService" />

Tuscany SCA案例分析(二)(连载中...)

             < tuscany:binding .jsonrpc uri ="http://localhost:8878/NodeManagerComponent/ComponentManagerJson" />

Tuscany SCA案例分析(二)(连载中...)

         </ service >

Tuscany SCA案例分析(二)(连载中...)

     </ component >            

Tuscany SCA案例分析(二)(连载中...)
Tuscany SCA案例分析(二)(连载中...)

     < component  name ="node" >

Tuscany SCA案例分析(二)(连载中...)

        < tuscany:implementation .resource location ="webroot" />

Tuscany SCA案例分析(二)(连载中...)

         < service  name ="Resource" >

Tuscany SCA案例分析(二)(连载中...)

             < tuscany:binding .http uri ="http://localhost:8878/node" />

Tuscany SCA案例分析(二)(连载中...)

         </ service >

Tuscany SCA案例分析(二)(连载中...)

     </ component >     

Tuscany SCA案例分析(二)(连载中...)
Tuscany SCA案例分析(二)(连载中...)

</ composite >

Tuscany SCA案例分析(二)(连载中...)

通过这些信息再与下图相比来理解吧

Tuscany SCA案例分析(二)(连载中...)

好了,这里有点初学者会有点糊涂,看看DomainNode.java就可以知道,先要启动nodeB和nodeC,因为他们是被引用的服务组件,再是启动nodeA,因为它上的CalculatorServiceComponent才能引用到相应的服务。这里有点急了,都忘记要写装配文件.composite文件了,否则怎么装配这些服务呀,执行结果要等到写完正确的装配文件再说,开动吧。先按下图建立目录和文件,写nodeA的composite文件和sca-contribution.xml文件,列出内容如下:

Tuscany SCA案例分析(二)(连载中...)

我在分布式SCADomain说明中说了如何加载contribution的,sca-contribution.xml里面就说明了哪些composite可部署,Calculator.composite则告诉了该构件中有哪里组件。

下面给出nodeA下的sca-contribution.xml和Calculator.composite内容:

Tuscany SCA案例分析(二)(连载中...)

<!-- sca-contribution.xml内容  -->

Tuscany SCA案例分析(二)(连载中...)

<? xml version="1.0" encoding="UTF-8" ?>

Tuscany SCA案例分析(二)(连载中...)
Tuscany SCA案例分析(二)(连载中...)

< contribution  xmlns ="http://www.osoa.org/xmlns/sca/1.0"

Tuscany SCA案例分析(二)(连载中...)

          targetNamespace ="http://sample"

Tuscany SCA案例分析(二)(连载中...)

              xmlns:sample ="http://sample" >

Tuscany SCA案例分析(二)(连载中...)

    < deployable  composite ="sample:Calculator" />

Tuscany SCA案例分析(二)(连载中...)

</ contribution >

Tuscany SCA案例分析(二)(连载中...)

<!--

Tuscany SCA案例分析(二)(连载中...)

 Calculator.composite文件内容

Tuscany SCA案例分析(二)(连载中...)

-->

Tuscany SCA案例分析(二)(连载中...)

<? xml version="1.0" encoding="UTF-8" ?>

Tuscany SCA案例分析(二)(连载中...)
Tuscany SCA案例分析(二)(连载中...)

< composite  xmlns ="http://www.osoa.org/xmlns/sca/1.0"

Tuscany SCA案例分析(二)(连载中...)

           targetNamespace ="http://sample"

Tuscany SCA案例分析(二)(连载中...)

           xmlns:sample ="http://sample"

Tuscany SCA案例分析(二)(连载中...)

           name ="Calculator" >

Tuscany SCA案例分析(二)(连载中...)
Tuscany SCA案例分析(二)(连载中...)

     < component  name ="CalculatorServiceComponentA" >

Tuscany SCA案例分析(二)(连载中...)

         < implementation .java class ="calculator.CalculatorServiceImpl" />

Tuscany SCA案例分析(二)(连载中...)

         < reference  name ="addService"  target ="AddServiceComponentB"   />      

Tuscany SCA案例分析(二)(连载中...)

         < reference  name ="subtractService"  target ="SubtractServiceComponentC"   />

Tuscany SCA案例分析(二)(连载中...)

         < reference  name ="multiplyService"  target ="MultiplyServiceComponentA" />      

Tuscany SCA案例分析(二)(连载中...)

         < reference  name ="divideService"  target ="DivideServiceComponentA"   />

Tuscany SCA案例分析(二)(连载中...)

     </ component >     

Tuscany SCA案例分析(二)(连载中...)
Tuscany SCA案例分析(二)(连载中...)

     < component  name ="MultiplyServiceComponentA" >

Tuscany SCA案例分析(二)(连载中...)

         < implementation .java class ="calculator.MultiplyServiceImpl"   />

Tuscany SCA案例分析(二)(连载中...)

     </ component >    

Tuscany SCA案例分析(二)(连载中...)
Tuscany SCA案例分析(二)(连载中...)

     < component  name ="DivideServiceComponentA" >

Tuscany SCA案例分析(二)(连载中...)

         < implementation .java class ="calculator.DivideServiceImpl"   />

Tuscany SCA案例分析(二)(连载中...)

     </ component >

Tuscany SCA案例分析(二)(连载中...)
Tuscany SCA案例分析(二)(连载中...)

</ composite >

每个相应的node目录下的sca-contribution.xml内容都一样的,不同的只是Calculator.composite里的内容,因为在nodeA中只有两个组件MultiplyServiceComponentA、DivideServiceComponentA还有些引用。其他node的Calculator.composite内容如下:

Tuscany SCA案例分析(二)(连载中...)

<? xml version="1.0" encoding="UTF-8" ?>

Tuscany SCA案例分析(二)(连载中...)

<!--

Tuscany SCA案例分析(二)(连载中...)

nodeB下的Calculator.composite内容

Tuscany SCA案例分析(二)(连载中...)

-->

Tuscany SCA案例分析(二)(连载中...)

< composite  xmlns ="http://www.osoa.org/xmlns/sca/1.0"

Tuscany SCA案例分析(二)(连载中...)

           targetNamespace ="http://sample"

Tuscany SCA案例分析(二)(连载中...)

           xmlns:sample ="http://sample"

Tuscany SCA案例分析(二)(连载中...)

           name ="Calculator" >

Tuscany SCA案例分析(二)(连载中...)
Tuscany SCA案例分析(二)(连载中...)

     < component  name ="AddServiceComponentB" >

Tuscany SCA案例分析(二)(连载中...)

         < implementation .java class ="calculator.AddServiceImpl"   />

Tuscany SCA案例分析(二)(连载中...)

     </ component >

Tuscany SCA案例分析(二)(连载中...)
Tuscany SCA案例分析(二)(连载中...)

</ composite >

Tuscany SCA案例分析(二)(连载中...)

<? xml version="1.0" encoding="UTF-8" ?>

Tuscany SCA案例分析(二)(连载中...)

<!--

Tuscany SCA案例分析(二)(连载中...)

nodeC下的Calculator.composite内容

Tuscany SCA案例分析(二)(连载中...)

-->

Tuscany SCA案例分析(二)(连载中...)

< composite  xmlns ="http://www.osoa.org/xmlns/sca/1.0"

Tuscany SCA案例分析(二)(连载中...)

           targetNamespace ="http://sample"

Tuscany SCA案例分析(二)(连载中...)

           xmlns:sample ="http://sample"

Tuscany SCA案例分析(二)(连载中...)

           name ="Calculator" >

Tuscany SCA案例分析(二)(连载中...)
Tuscany SCA案例分析(二)(连载中...)

     < component  name ="SubtractServiceComponentC" >

Tuscany SCA案例分析(二)(连载中...)

         < implementation .java class ="calculator.SubtractServiceImpl"   />

Tuscany SCA案例分析(二)(连载中...)

         < service  name ="SubtractService" >

Tuscany SCA案例分析(二)(连载中...)

             < binding .sca uri ="http://localhost:8086/SubtractServiceComponentC" />

Tuscany SCA案例分析(二)(连载中...)

         </ service >

Tuscany SCA案例分析(二)(连载中...)

     </ component >

Tuscany SCA案例分析(二)(连载中...)
Tuscany SCA案例分析(二)(连载中...)

</ composite > 好了,装配的配置文件都搞好了,下面我们来运行下,看看结果如何。

先从eclipse里象启动一般java程序一样启动DomainNode.java

Tuscany SCA案例分析(二)(连载中...)

然后再要启动CalculatorNode.java,这里启动要传入不同的参数,这里给出启动nodeB时打开启动对话框配置的传入参数图:

Tuscany SCA案例分析(二)(连载中...)

启动nodeC和nodeA的参数只要用nodeC和nodeA替换nodeB就可以了,给出个最终结果图吧,如下:

Tuscany SCA案例分析(二)(连载中...)

记住启动的顺序,先要启动nodeB和nodeC,最后启动nodeA呀,成功了记得告诉我,谢谢

继续阅读