/**//*CalculatorNode.java文件内容如下*/ package node; import java.io.IOException; import javax.xml.namespace.QName; import org.apache.tuscany.sca.domain.SCADomain; import org.apache.tuscany.sca.node.SCANode; import org.apache.tuscany.sca.node.SCANodeFactory; import calculator.CalculatorService; /** *//** * This client program shows how to run a distributed SCA node. In this case a * calculator node has been constructed specifically for running the calculator * composite. Internally it creates a representation of a node and associates a * distributed domain with the node. This separation is made different implementations * of the distributed domain can be provided. */ publicclass CalculatorNode ...{ publicstaticvoid main(String[] args) throws Exception ...{ // Check that the correct arguments have been provided if (null== args || args.length <2) ...{ System.err.println("Useage: java CalculatorNode domainname nodename"); System.exit(1); } try...{ String domainName = args[0]; String nodeName = args[1]; ClassLoader cl = CalculatorNode.class.getClassLoader(); SCANodeFactory nodeFactory =SCANodeFactory.newInstance(); SCANode node = nodeFactory.createSCANode(nodeName, domainName); node.addContribution(nodeName, cl.getResource(nodeName +"/")); //System.out.println(cl.getResource(nodeName + "/").toString()); node.deployComposite(new QName("http://sample", "Calculator")); node.start(); // nodeA is the head node and runs some tests while all other nodes // simply listen for incoming messages if ( nodeName.equals("nodeA") ) ...{ // do some application stuff CalculatorService calculatorService = node.getDomain().getService(CalculatorService.class, "CalculatorServiceComponentA"); // Calculate System.out.println("3 + 2="+ calculatorService.add(3, 2)); System.out.println("3 - 2="+ calculatorService.subtract(3, 2)); System.out.println("3 * 2="+ calculatorService.multiply(3, 2)); System.out.println("3 / 2="+ calculatorService.divide(3, 2)); // a little hidden loop test to put some load on the nodes if (args.length >2)...{ for (int i=0; i <1000; i++)...{ // Calculate System.out.println("3 + 2="+ calculatorService.add(3, 2)); System.out.println("3 - 2="+ calculatorService.subtract(3, 2)); System.out.println("3 * 2="+ calculatorService.multiply(3, 2)); System.out.println("3 / 2="+ calculatorService.divide(3, 2)); } } }else...{ // start up and wait for messages try...{ System.out.println("Node started (press enter to shutdown)"); System.in.read(); }catch (IOException e) ...{ e.printStackTrace(); } } // stop the node and all the domains in it node.stop(); }catch(Exception ex) ...{ System.err.println("Exception in node - "+ ex.getMessage()); ex.printStackTrace(System.err); } } } /**//*DomainNode.java 文件内容如下*/ package node; import org.apache.tuscany.sca.domain.SCADomain; import org.apache.tuscany.sca.domain.SCADomainFactory; /** *//** * This server program that loads a composite to provide simple registry function. * This server can be replaced with any registry that is appropriate but the components * in each node that talk to the registry should be replaced also. */ publicclass DomainNode ...{ privatestatic String DEFAULT_DOMAIN_URI ="http://localhost:8877"; publicstaticvoid main(String[] args) ...{ try...{ SCADomainFactory domainFactory =SCADomainFactory.newInstance(); SCADomain domain = domainFactory.createSCADomain(DEFAULT_DOMAIN_URI); domain.start(); System.out.println("Domain started (press enter to shutdown)"); System.in.read(); domain.stop(); }catch (Exception e) ...{ e.printStackTrace(); } System.out.println("Domain stopped"); } }