在 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 [email protected] * @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; }
package com.ajun.tuscany.rmiserver; import java.rmi.RemoteException; import java.rmi.server.UnicastRemoteObject; /** * 计算器RMI服务类 * @author ajun * @e-mail [email protected] * @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 [email protected] * @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); } }
加法:IAdd.java
package com.ajun.tuscany.server; /** * 加法接口 * @author ajun * @e-mail [email protected] * @blog http://blog.csdn.net/ajun_studio * @version 创建时间:2012-7-19 下午9:47:25 */ public interface IAdd { double add(double n1,double n2); }
package com.ajun.tuscany.server; /** * 减法接口 * @author ajun * @e-mail [email protected] * @blog http://blog.csdn.net/ajun_studio * @version 创建时间:2012-7-19 下午9:52:00 */ public interface ISubtract { double subtract(double n1,double n2); }
package com.ajun.tuscany.server; /** * 乘法接口 * @author ajun * @e-mail [email protected] * @blog http://blog.csdn.net/ajun_studio * @version 创建时间:2012-7-19 下午9:50:47 */ public interface IMultiply { double multiply(double n1,double n2); }
package com.ajun.tuscany.server; /** * 除法接口 * @author ajun * @e-mail [email protected] * @blog http://blog.csdn.net/ajun_studio * @version 创建时间:2012-7-19 下午9:48:53 */ public interface IDivide { double divide(double n1,double n2); }
package com.ajun.tuscany.server; /** * 计算机接口 * @author ajun * @e-mail [email protected] * @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); }
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 [email protected] * @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