All accesses to the Computer Implemented in Tuscany SCA Java (2) are local, and all generated components and call components are carried out in the local Tuscany container. Next I will perform remote calls, this is the true strength of Tuscany. We still use Tuscany
In SCA Java (2), you only need to modify the. Composite file and add a server and client class to test the code.
Here we bind the calculator component to the RMI service and only modify the previous. Composite file, as shown below:
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>
To bind to RMI service, you only need to add
<Service name = "Calculator">
<Interface. Java interface = "com. ajun. Tuscany. server. icalculator"/>
<Tuscany: binding. Rmi Port = "8099" host = "localhost" servicename = "calculatorrmiservice"/>
</Service>
The service name is: calculator, which must be the same as your implementation class.
<Interface. Java interface = "com. ajun. Tuscany. server. icalculator"/> interface exposed to external access
<Tuscany: binding. RMI Port = "8099" host = "localhost" servicename = "calculatorrmiservice"/> RMI server IP address and port number, and RMI service name.
In this way, the computer components are bound to the RMI service.
Write a service class to start the RMI service.
Calculatorrmiserver. Java
Package COM. ajun. tuscany. server; import Java. io. ioexception; import Org. apache. tuscany. SCA. host. embedded. scadomain;/*** RMI server startup * @ author ajun * @ E-mail zhaojun2066@gmail.com * @ blog http://blog.csdn.net/ajun_studio * @ version creation time: 9:44:32 */public class calculatorrmiserver {/*** @ Param ARGs * @ throws ioexception */public static void main (string [] ARGs) throws ioexception {system. out. println ("uses the SCA component as an Rmi interface for external access ....... "); scadomain domain = scadomain. newinstance ("com/ajun/Tuscany/Server/calculator. composite "); system. out. println ("Carriage Return key ....... "); system. in. read (); domain. close (); system. out. println ("exit ....... "); system. exit (0 );}}
The following information is displayed when running the above class:
Use the SCA component as the RMI interface for external access ....... 2012-7-23 22:32:10 Org. apache. tuscany. SCA. node. impl. nodeimpl <init> information: Creating node: COM/ajun/Tuscany/Server/calculator. composite2012-7-23 22:32:12 Org. apache. tuscany. SCA. node. impl. nodeimpl upload Enode information: loading contribution: file:/E:/springdm/calculator_rmi/bin/22:32:14 Org. apache. tuscany. SCA. node. impl. nodeimpl start information: Starting node: COM/ajun/Tuscany/Server/calculator. composite enter key .......
Client code: calculatorrmiclient. Java
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 client * @ author ajun * @ E-mail zhaojun2066@gmail.com * @ blog http://blog.csdn.net/ajun_studio * @ version creation time: 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 ));}}
Run the above Code:
3 + 2 = 5.03 - 2 = 1.03 * 2 = 6.03 / 2 = 1.5
The code structure is as follows: for the remaining code, see Tuscany SCA Java (2)