在TUSCANY SCA JAVA (二)中所实现的计算机中所有的访问都是本地,生成的所有构件,调用构件都是在本地tuscany容器中进行的,下面我来进行远程调用,这才是tuscany的真正长处所在。我们还是利用TUSCANY SCA JAVA (二)中的代码,只需要修改和.composite文件和增加一个服务端和客户端的类就可以进行测试了。
在这里我们计算器构件绑定为RMI服务,只续修改之前的.composite文件而已,如下:
Culcalator.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" /> <service name="Calculator"> <interface.java interface="com.ajun.tuscany.server.ICalculator"/> <tuscany:binding.rmi port="8099" host="localhost" serviceName="CalculatorRMIService" /> </service> <reference name="add" target="AddComponent"/> <reference name="subtract" target="SubtractComponent"/> <reference name="multiply" target="MultiplyComponent"/> <reference name="divide" target="DivideComponent"/> </component> <component name="AddComponent"> <implementation.java class="com.ajun.tuscany.server.Add" /> </component> <component name="SubtractComponent"> <implementation.java class="com.ajun.tuscany.server.Subtract" /> </component> <component name="MultiplyComponent"> <implementation.java class="com.ajun.tuscany.server.Multiply" /> </component> <component name="DivideComponent"> <implementation.java class="com.ajun.tuscany.server.Divide" /> </component> </composite>绑定为RMI服务只需添加
<service name="Calculator">
<interface.java interface="com.ajun.tuscany.server.ICalculator"/>
<tuscany:binding.rmi port="8099" host="localhost" serviceName="CalculatorRMIService" />
</service>
服务的名称为:Calculator 这里必须和你的实现类一个名字,需要注意一下
<interface.java interface="com.ajun.tuscany.server.ICalculator"/> 透漏给外部访问的接口
<tuscany:binding.rmi port="8099" host="localhost" serviceName="CalculatorRMIService" /> RMI服务端ip和端口号、RMI服务的服务名称,以后供外界访问的名字
这样就将计算机构件绑定为RMI服务了。
编写一个服务类来启动RMI服务。
CalculatorRMIServer.java
package com.ajun.tuscany.server; import java.io.IOException; import org.apache.tuscany.sca.host.embedded.SCADomain; /** * RMI服务端启动 * @author ajun * @e-mail [email protected] * @blog http://blog.csdn.net/ajun_studio * @version 创建时间:2012-7-23 下午9:44:32 */ public class CalculatorRMIServer { /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { System.out.println("将SCA组件作为RMI接口供外界访问......."); SCADomain domain = SCADomain.newInstance("com/ajun/tuscany/server/Calculator.composite"); System.out.println("回车键推车......."); System.in.read(); domain.close(); System.out.println("退出......."); System.exit(0); } }运行上面的类出现一下信息:
将SCA组件作为RMI接口供外界访问....... 2012-7-23 22:32:10 org.apache.tuscany.sca.node.impl.NodeImpl <init> 信息: Creating node: com/ajun/tuscany/server/Calculator.composite 2012-7-23 22:32:12 org.apache.tuscany.sca.node.impl.NodeImpl configureNode 信息: Loading contribution: file:/E:/springdm/Calculator_rmi/bin/ 2012-7-23 22:32:14 org.apache.tuscany.sca.node.impl.NodeImpl start 信息: Starting node: com/ajun/tuscany/server/Calculator.composite 回车键推出.......
package com.ajun.tuscany.client; import java.net.MalformedURLException; import java.rmi.Naming; import java.rmi.NotBoundException; import java.rmi.RemoteException; import com.ajun.tuscany.server.ICalculator; /** * RMI客户端 * @author ajun * @e-mail [email protected] * @blog http://blog.csdn.net/ajun_studio * @version 创建时间:2012-7-23 下午9:44:44 */ public class CalculatorRMIClient { /** * @param args * @throws NotBoundException * @throws RemoteException * @throws MalformedURLException */ public static void main(String[] args) throws MalformedURLException, RemoteException, NotBoundException { ICalculator c= (ICalculator) Naming.lookup("//localhost:8099/CalculatorRMIService"); 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)); } }
3 + 2 = 5.0 3 - 2 = 1.0 3 * 2 = 6.0 3 / 2 = 1.5