TUSCANY SCA JAVA (四) 引用RMI服务

 TUSCANY SCA JAVA (三) 中是将计算器作为一个RMI服务进行绑定,对外提供进行访问的,还有一种情况是如果所需要的功能在外界已经做好了,并以RMI开放供外界访问,那么tuscany如何引用呢?有这么一个例子,如果计算器的功能在外界已经实现好了,并以RMI开放供外界访问,来看看怎么进行引用。

来看下图:


从图中可以看出计算器组件 引用是指向了RMI服务端,并且提升为计算器组合构件的引用。

下面来说主要实现代码:

供外加访问的RMI服务端代码:

关于java怎么注册RMI服务,google一下就知道了

ICalculatorRMI.java 添加@Remotable说明可以进行远程访问

package com.ajun.tuscany.rmiserver;

import java.rmi.Remote;
import java.rmi.RemoteException;

import org.osoa.sca.annotations.Remotable;

/**
 * 计算器远程RMI接口
 * @author ajun
 * @e-mail zhaojun2066@gmail.com
 * @blog http://blog.csdn.net/ajun_studio
 * @version 创建时间:2012-7-25  下午9:25:52
 */
@Remotable
public interface ICalculatorRMI extends Remote{

	/**
	 * 加法
	 * @param n1
	 * @param n2
	 * @return
	 */
	double add(double n1,double n2) throws RemoteException;
	
	/**
	 * 减法
	 * @param n1
	 * @param n2
	 * @return
	 */
	double subtract(double n1,double n2) throws RemoteException;
	
	/**
	 * 除法
	 * @param n1
	 * @param n2
	 * @return
	 */
	double divide(double n1,double n2) throws RemoteException;
	/**
	 * 乘法
	 * @param n1
	 * @param n2
	 * @return
	 */
	double multiply(double n1,double n2) throws RemoteException;
	
	
}

CalculatorRMI.java实现类

package com.ajun.tuscany.rmiserver;

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

/**
 * 计算器RMI服务类
 * @author ajun
 * @e-mail zhaojun2066@gmail.com
 * @blog http://blog.csdn.net/ajun_studio
 * @version 创建时间:2012-7-25  下午9:26:19
 */
public class CalculatorRMI extends UnicastRemoteObject implements
		ICalculatorRMI {

	private static final long serialVersionUID = -6698638302125311021L;

	protected CalculatorRMI() throws RemoteException {
		super();
	}

	@Override
	public double add(double n1, double n2) throws RemoteException {
		return n1+n2;
	}

	@Override
	public double subtract(double n1, double n2) throws RemoteException {
		return n1-n2;
	}

	@Override
	public double divide(double n1, double n2) throws RemoteException {
		return n1*n2;
	}

	@Override
	public double multiply(double n1, double n2) throws RemoteException {
		return n1/n2;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}

注册RMI服务

CalculatorRMIServer.java

package com.ajun.tuscany.rmiserver;

import java.rmi.AlreadyBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

/**
 * 注册RMI服务类
 * @author ajun
 * @e-mail zhaojun2066@gmail.com
 * @blog http://blog.csdn.net/ajun_studio
 * @version 创建时间:2012-7-25  下午9:29:47
 */
public class CalculatorRMIServer {

	/**
	 * @param args
	 * @throws RemoteException 
	 * @throws AlreadyBoundException 
	 */
	public static void main(String[] args) throws RemoteException, AlreadyBoundException {
		CalculatorRMI rmi = new CalculatorRMI();
		//端口号8099
		Registry rmiRegistry = LocateRegistry.createRegistry(8099);
		//为rmi绑定的服务的名字为CalculatorRMIService
		rmiRegistry.bind("CalculatorRMIService", rmi);
	}

}

tuscany这边共有6个java类,其中四个接口分别代表加减乘除,其余两个为计算器类和计算器接口

加法: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);
	}

}


客户端调用代码: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));
	}
}



测试:

运行CalculatorRMIServer.java

最后运行CalculatorServerClient.java

输出:

2012-7-25 21:39:49 org.apache.tuscany.sca.node.impl.NodeImpl <init>
信息: Creating node: com/ajun/tuscany/server/Calculator.composite
2012-7-25 21:39:51 org.apache.tuscany.sca.node.impl.NodeImpl configureNode
信息: Loading contribution: file:/E:/springdm/Calculator_rmi_ref/bin/
2012-7-25 21:39:52 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 = 1.5
Server divide
3 / 2 = 6.0

目录 ................................................................................................................................................................................... 5  1.  通用注解、API、客户程序和实现模型 ................................................................................................................. 7  1.1. 简介 ........................................................................................................................................................................... 7  1.2. 实现的元数据 ........................................................................................................................................................... 7  1.2.1. 服务元数据 ............................................................................................................................................................ 8  1.2.2.@Reference ........................................................................................................................................................... 8  1.2.3. @Property ............................................................................................................................................................. 9  1.2.4. 实现作用域:@Scope、@Init、@Destroy ....................................................................................................... 9  1.3 接口元数据 .............................................................................................................................................................. 10  1.3.1. @Remotable ....................................................................................................................................................... 10  1.3.2. @Conversational ................................................................................................................................................ 11  1.4. 客户 API .................................................................................................................................................................. 11  1.4.1. SCA构件访问服务 .............................................................................................................................................. 11  1.4.2. 非 SCA构件的实现访问服务 ............................................................................................................................ 11  1.5. 错误处理 ................................................................................................................................................................. 12  1.6. 异步与会话编程 ..................................................................................................................................................... 12  1.6.1. @OneWay ........................................................................................................................................................... 12 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

飓风zj

感谢打赏,thanks

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值