In Tuscany SCA Java (3)
The calculator is bound as an Rmi service to provide external access. Another situation is that if the required functions are ready in the outside world, and open it for external access with RMI, so how does Tuscany reference it? In this example, if the calculator function has been implemented in the outside world and RMI is open for external access, let's see how to reference it.
Let's see:
It can be seen that the reference of the calculator component points to the RMI server and is promoted to the reference of the calculator component combination.
The main implementation code is as follows:
The code of the RMI server for external access:
Google will know how to register the RMI service in Java
Icalculatorrmi. Java add @ remotable instructions for remote access
Package COM. ajun. tuscany. rmiserver; import Java. RMI. remote; import Java. RMI. remoteException; import Org. osoa. SCA. annotations. remotable;/*** calculator remote RMI interface * @ author ajun * @ E-mail zhaojun2066@gmail.com * @ blog http://blog.csdn.net/ajun_studio * @ version creation time: 9:25:52 */@ remotablepublic interface icalculatorrmi extends remote {/*** addition * @ Param N1 * @ Param N2 * @ return */Double add (double N1, double N2) throws RemoteException;/*** subtraction * @ Param N1 * @ Param N2 * @ return */Double subtract (double N1, double N2) throws RemoteException; /*** Division * @ Param N1 * @ Param N2 * @ return */Double divide (double N1, double N2) throws RemoteException; /*** multiplication * @ Param N1 * @ Param N2 * @ return */Double multiply (double N1, double N2) throws RemoteException ;}
Calculatorrmi. Java implementation class
Package COM. ajun. tuscany. rmiserver; import Java. RMI. remoteException; import Java. RMI. server. unicastremoteobject;/*** calculator RMI service class * @ author ajun * @ e-mail zhaojun2066@gmail.com * @ blog http://blog.csdn.net/ajun_studio * @ version creation time: 9:26:19 */public class calculatorrmi extends unicastremoteobject implementsicalculatorrmi {Private Static final long serialversionuid =-writable; protected calculatorrmi () throws RemoteException {super ();} @ overridepublic double add (double N1, double N2) throws RemoteException {return N1 + N2;} @ overridepublic double subtract (double N1, double N2) throws RemoteException {return n1-n2 ;} @ overridepublic double divide (double N1, double N2) throws RemoteException {return N1 * N2;} @ overridepublic double multiply (double N1, double N2) throws RemoteException {return N1/N2 ;} /*** @ Param ARGs */public static void main (string [] ARGs) {// todo auto-generated method stub }}
Register RMI Service
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;/*** register the RMI service class * @ author ajun * @ E-mail zhaojun2066@gmail.com * @ blog http://blog.csdn.net/ajun_studio * @ version creation time: 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 (); // port number 8099 registry rmiregistry = locateregistry. createregistry (8099); // The Name Of The service bound to RMI is calculatorrmiservicermiregistry. BIND ("calculatorrmiservice", RMI );}}
There are 6 Java classes in Tuscany, four of which represent addition, subtraction, multiplication, division, and the other two are calculator and calculator interfaces.
Addition: iadd. Java
Package COM. ajun. tuscany. server;/*** addition interface * @ author ajun * @ E-mail zhaojun2066@gmail.com * @ blog http://blog.csdn.net/ajun_studio * @ version creation time: 9:47:25 */public interface iadd {double add (double N1, double N2 );}
Subtraction: isubtract. Java
Package COM. ajun. tuscany. server;/*** subtraction interface * @ author ajun * @ E-mail zhaojun2066@gmail.com * @ blog http://blog.csdn.net/ajun_studio * @ version creation time: 9:52:00 */public interface isubtract {double subtract (double N1, double N2 );}
Multiplication: imultiply. Java
Package COM. ajun. tuscany. server;/*** multiplication interface * @ author ajun * @ e-mail zhaojun2066@gmail.com * @ blog http://blog.csdn.net/ajun_studio * @ version creation time: 9:50:47 */public interface imultiply {double multiply (double N1, double N2 );}
Division: idivide. Java
Package COM. ajun. tuscany. server;/*** division interface * @ author ajun * @ E-mail zhaojun2066@gmail.com * @ blog http://blog.csdn.net/ajun_studio * @ version creation time: 9:48:53 */public interface idivide {double divide (double N1, double N2 );}
Calculator interface: icalculator. Java
Package COM. ajun. tuscany. server;/*** computer interface * @ author ajun * @ E-mail zhaojun2066@gmail.com * @ blog http://blog.csdn.net/ajun_studio * @ version creation time: 10:02:20 */public interface icalculator {/*** addition * @ Param N1 * @ Param N2 * @ return */Double add (double N1, double N2 ); /*** subtraction * @ Param N1 * @ Param N2 * @ return */Double subtract (double N1, double N2 ); /*** Division * @ Param N1 * @ Param N2 * @ return */Double divide (double N1, double N2 ); /*** multiplication * @ Param N1 * @ Param N2 * @ return */Double multiply (double N1, double N2 );}
Calculator implementation class: calculator. Java
Package COM. ajun. tuscany. server; import Org. osoa. SCA. annotations. reference; public class calculator implements icalculator {/*** references other components such as iadd, isubtract, imultiply, and idivide * @ reference. configure the composite file */private iadd add; // Add component private isubtract subtract; // subtract component private imultiply multiply; // multiply component private idivide divide; // division component public iadd getadd () {return add;} @ referencepublic void setadd (iadd Add add) {This. add = add;} public isubtract getsubtract () {return subtract;} @ referencepublic void setsubtract (isubtract subtract) {This. subtract = subtract;} public imultiply getmultiply () {return multiply;} @ referencepublic void setmultiply (imultiply multiply) {This. multiply = multiply;} public idivide getdivide () {return divide;} @ referencepublic void setdivide (idivide divide) {This. divide = divide;} @ overridepublic double add (double N1, double N2) {system. out. println ("server Add"); return this. add. add (N1, N2) ;}@ overridepublic double subtract (double N1, double N2) {system. out. println ("server subtract"); return this. subtract. subtract (N1, N2) ;}@ overridepublic double divide (double N1, double N2) {system. out. println ("server divide"); return this. divide. divide (N1, N2) ;}@ overridepublic double multiply (double N1, double N2) {system. out. println ("server multiply"); return this. multiply. multiply (N1, N2 );}}
Client call code: 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;/*** server * @ author ajun * @ e-mail zhaojun2066@gmail.com * @ blog http://blog.csdn.net/ajun_studio * @ version Creation Time: 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 ));}}
Test:
Run calculatorrmiserver. Java
Finally, run calculatorserverclient. java.
Output:
2012-7-25 21:39:49 Org. apache. tuscany. SCA. node. impl. nodeimpl <init> information: Creating node: COM/ajun/Tuscany/Server/calculator. composite2012-7-25 21:39:51 Org. apache. tuscany. SCA. node. impl. nodeimpl implements Enode information: loading contribution: file:/E:/springdm/calculator_rmi_ref/bin/21:39:52 Org. apache. tuscany. SCA. node. impl. nodeimpl start information: Starting node: COM/ajun/Tuscany/Server/calculator. compositeserver add3 + 2 = 5.0 server subtract3-2 = 1.0 server multiply3 * 2 = 1.5 server divide3/2 = 6.0