TUSCANY SCA JAVA (五) 用其他脚本语言实现组件服务

先来看看整个项目的代码架构



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;
	}

	@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) {
		System.out.println("Server add");
		return this.add.add(n1, n2);
	}

	@Override
	public double subtract(double n1, double n2) {
		System.out.println("Server subtract");
		return this.subtract.subtract(n1, n2);
	}

	@Override
	public double divide(double n1, double n2) {
		System.out.println("Server divide");
		return this.divide.divide(n1, n2);
	}

	@Override
	public 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-n2
end

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.composite
2012-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.composite
Server add
3 + 2 = 5.0
Server subtract
3 - 2 = 1.0
Server multiply
3 * 2 = 6.0
Server divide
3 / 2 = 1.5



发布了67 篇原创文章 · 获赞 2 · 访问量 41万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览