TUSCANY SCA JAVA (五) 用其他指令碼語言實現元件服務

來源:互聯網
上載者:User

先來看看整個項目的代碼架構

com.ajun.tuscany.server包主要是加減乘除法的java介面和計算機介面和計算機實作類別。

com.ajun.tuscany.script主要是用指令碼語言來實現加減乘法服務功能。

com.ajun.tuscany.client用戶端調用。

server包主要代碼:

IAdd.java

package com.ajun.tuscany.server;/** * 加法介面 * @author ajun * @e-mail zhaojun2066@gmail.com * @blog http://blog.csdn.net/ajun_studio * @version 建立時間:2012-7-19  下午9:47:25 */public interface IAdd {double add(double n1,double n2);}

ISubtract.java

package com.ajun.tuscany.server;/** * 減法介面 * @author ajun * @e-mail zhaojun2066@gmail.com * @blog http://blog.csdn.net/ajun_studio * @version 建立時間:2012-7-19  下午9:52:00 */public interface ISubtract {double subtract(double n1,double n2);}

IMultiply.java

package com.ajun.tuscany.server;/** * 乘法介面 * @author ajun * @e-mail zhaojun2066@gmail.com * @blog http://blog.csdn.net/ajun_studio * @version 建立時間:2012-7-19  下午9:50:47 */public interface IMultiply {double multiply(double n1,double n2);}

IDivide.java

package com.ajun.tuscany.server;/** * 除法介面 * @author ajun * @e-mail zhaojun2066@gmail.com * @blog http://blog.csdn.net/ajun_studio * @version 建立時間:2012-7-19  下午9:48:53 */public interface IDivide {double divide(double n1,double n2);}

ICalculator.java

package com.ajun.tuscany.server;/** * 電腦介面 * @author ajun * @e-mail zhaojun2066@gmail.com * @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

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;}@Referencepublic void setAdd(IAdd add) {this.add = add;}public ISubtract getSubtract() {return subtract;}@Referencepublic void setSubtract(ISubtract subtract) {this.subtract = subtract;}public IMultiply getMultiply() {return multiply;}@Referencepublic void setMultiply(IMultiply multiply) {this.multiply = multiply;}public IDivide getDivide() {return divide;}@Referencepublic void setDivide(IDivide divide) {this.divide = divide;}@Overridepublic double add(double n1, double n2) {System.out.println("Server add");return this.add.add(n1, n2);}@Overridepublic double subtract(double n1, double n2) {System.out.println("Server subtract");return this.subtract.subtract(n1, n2);}@Overridepublic double divide(double n1, double n2) {System.out.println("Server divide");return this.divide.divide(n1, n2);}@Overridepublic double multiply(double n1, double n2) {System.out.println("Server multiply");return this.multiply.multiply(n1, n2);}}

script包用於實現java介面中的方法:

AddService.js

//加法,用js實現function add(n1,n2){return n1+n2;}

DivideService.groovy

//用Groovy實現除法def divide(n1,n2){return n1/n2}

MultiplyService.py

#用python實現def multiply(n1,n2):return n1*n2

SubtractService.rb

#減法用Ruby實現def subtract(n1,n2)return n1-n2end

Calculator.composite設定檔

<?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"><tuscany:implementation.script script="com/ajun/tuscany/script/AddService.js"/></component><component name="SubtractComponent"><tuscany:implementation.script script="com/ajun/tuscany/script/SubtractService.rb"/></component><component name="MultiplyComponent"><tuscany:implementation.script script="com/ajun/tuscany/script/MultiplyService.py"/></component><component name="DivideComponent"><tuscany:implementation.script script="com/ajun/tuscany/script/DivideService.groovy"/></component></composite>

用戶端調用:

CalculatorServerClient.java

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 zhaojun2066@gmail.com * @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));}}

運行:

2012-7-26 21:55:32 org.apache.tuscany.sca.node.impl.NodeImpl <init>資訊: Creating node: com/ajun/tuscany/server/Calculator.composite2012-7-26 21:55:33 org.apache.tuscany.sca.node.impl.NodeImpl configureNode資訊: Loading contribution: file:/E:/springdm/Calculator_script/bin/2012-7-26 21:55:36 org.apache.tuscany.sca.node.impl.NodeImpl start資訊: Starting node: com/ajun/tuscany/server/Calculator.compositeServer add3 + 2 = 5.0Server subtract3 - 2 = 1.0Server multiply3 * 2 = 6.0Server divide3 / 2 = 1.5

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.