Brief Analysis of Java RMI and .NET Remoting (original) Tang Dynasty
Java and .Net provide remote processing functions, but not exactly the same. Java remote processing is implemented through a "shared interface", and .NET can be implemented through a "shared command set". Below will be specifically described in these two ways.
Java remote processing
Java Remote Method Call (RMI) provides a remote communication feature of the Java program language, which enables programs running on the client to call objects on the remote server, so that Java programmers can distribute operations in the network environment. Creating a simple Java Distributed Remote Method Call program can operate as follows,
First, define a remote interface: In Java, the remote object is an instance of a class that implements a remote interface, and the remote interface declares how each method to be called remotely. When you need to create a remote object, we hide the basic implementation details of the grassroots by passing an interface, and the client will send a message through the interface handle. The remote interface has the following features: 1) The remote interface must be the public attribute. If not, unless the client is in the same package in the same package, the call will result in an error result when trying to implement the remote object to implement the remote interface. 2) The remote interface must expand the interface java.rmi.remote. 3) In addition to the specific exception specific to the application itself, each method in the remote interface must declare java.rmi.RemoteException in its own Throws clause. (Or the parent class of RemoteException). 4) A remote object (whether it is direct or local objects), which is used as a parameter or return value must be declared as a remote interface without claiming to implement the class. Below is the definition of the interface RMisample of the remote interface
Import java.rmi. *; public interface rmisample extends Remote {public int sum (int A, int b) throws remoteException;}
Second, implement a remote interface: Remote object implementation classes must extend the remote object Java.rmi.UnicastRemoteObject class and implement the defined remote interface. The implementation class of the remote object contains code that implements the remote method specified by each remote interface. This class can also contain additional methods, but customers can only use methods in remote interfaces. Because the customer is a handle pointing to the interface, not which class it. The constructor must be defined for the remote object, even if only one default constructor is defined, use it to call the underlying class constructor. Because the basic class constructor may throw java.rmi.RemoteException, even if you don't use it, you must throw a java.rmi.RemoteException exception. The following is a statement of remote object implementation classes:
import java.rmi *;. import java.rmi.server *;. public class RmiSampleImpl extends UnicastRemoteObject implements RmiSample {RmiSampleImpl () throws RemoteException {super ();} public int sum (int a, int b) throws RemoteException {return a b;}}
Third, write server classes: The class containing the main method can be used as itself, or it can be completely another class. By following RMisampleServer, create an instance of a remote object, and launch the registration service from the specified port number through the CreateRegistry method of the Java.rmi.Registry.LocateRegistry class, or start the registration service program by executing the RMIREGISTRY command, the default for the registration service The running port is 1099. The remote object name must be bound to a reference to the remote object: Naming.Rebind ("// localhost: 8808 / sample-server", server); the following is a declaration of the server class: import java.rmi. *; Import Java .rmi.registry *;. public class RmiSampleServer {public static void main (String args []) {try {LocateRegistry.createRegistry (8808); SampleServerImpl Server = new SampleServerImpl (); // with the object instance name "SAMPLE- Server "bundled naming.rebind (" // localhost: 8808 / sample-server ", server);} catch (java.net.malformedurlexception me) {system.out.println (" Malformed URL: " me.toString () );} Catch (RemoteException Re) {system.out.println ("Remote Exception:" Re.toString ());}}}
4. Writing client classes using remote services: There are two main functions of the client class, one is to construct the Registration Server STUB program instance through the Naming.lookup method, and the other is the remote method on the server remote object. The following is a statement of the server class:
import java.rmi *; import java.rmi.server *; public class RmiSampleClient {public static void main (String [] args) {try {String url =.. "// localhost: 8808 / SAMPLE-SERVER"; RmiSample RmiObject = (RMisample) Naming.lookup (URL); System.out.Println ("1 2 =" RMIObject.Sum (1, 2));} catch (remoteException Exc) {system.out.println ("Error in LOOKUP: " EXC.TOSTRING ());} catch (java.net.malformedurlexception exc) {system.out.println (" Malformed URL: " EXC.TOSTOSTRING ());} catch (java.rmi.rmi.rmi.notboundexception EXC) ) {System.out.println ("notbound:" EXC.TOSTRING ());}}} 5, compile code: To compile Java source files, run javac command: javac rmisample.java rmisampleImpl.java RmisampleServer.java RmisampleTeerent .java
6. Creating roots and drying for remote objects: To create a settle program and skeleton file, RMIC compiler should be run in full name of the compiled class package that contains remote objects. STUB is a remote object at the client's agent, which passes the RMI call to the server-side skeleton (the latter is responsible for passing the call to the actual remote method) as follows: D: / RMI> RMIC -D D: / RMI RMISampleImp executes this command, if the RMIC is successful, there will be two new classes in the RMI directory: rmisampleImpl_stub.class rmisampleImpl_skel.class They correspond to the stub and skeleton. "Skeleton).
7. Run the code: Run the server: Under Windows, enter the following command, start the RMisampleServer program in the background: D: / RMI> Java RMisampleServer Run client program: D: / RMI> Java RMisampleClient client output: 1 2 = 3
.NET remote processing.
Microsoft's .NET Remoting provides a framework that allows objects to interact with another object. From the structure, .NET Remoting object is ideal for accessing resources through the network, without having to process SOAP-based WebServices The challenges brought. Here is a few steps that create a simple .NET Remoting Distributed Remote Method Call program.
Step 1: Create a shared remote object
Create a C # Library and name it RemoteObject. This will create a "shared command set" for our .NET Remote client and server. public class RemoteObject: System.MarshalByRefObject {public RemoteObject () {System.Console.WriteLine ( "New Referance Added!");} public int sum (int a, int b) {return a b;}} namespace object It is desirable. Keep in mind that if you get the information that does not exist in system.Runtime.Remoting.Channels.TCP namespace, check if you add a reference to System.Runtime.Remoting.dll as the code above. Using system; using system.Runtime; We use the name space for the object is Remotesample, the following object is MarshalByrefObject, where we created a reference and all the work required to complete all of the server-side operations. namespace RemoteSample {public class RemoteObject: System.MarshalByRefObject {public RemoteObject () {System.Console.WriteLine ( "New Referance Added!");} public int sum (int a, int b) {return a b;}}} // Remotesample Name Space End Save File, named RemoteObject.cs to compile files with command line CSC / T: Library RemoteObject.cs, you get a remoteObject.dll file, and you can use it in compiling other C # files. Step 2: Create server objects
Create a server object and name it RemoteServer. The following will be implemented when creating a server object: 1) Construct the server channel. TCPServerChannel is one of the two channel types supported by .NET Remoting, you can set it to the object responsed to the request from which port, ChannelServices.RegisterChannel will put the port number and the TCP / IP stack in the operating system set. Tcpserverchannel Channel = New TCPServerChannel (8808); ChannelServices.RegisterChannel (Channel);
You can also be set to another channel type HTTP, as long as you simply use the HttpserverChannel object in the System.Runtime.Remoting.Channels.http namespace. The difference between the HTTP and TCP channels is that if the application is running on the LAN, it is best to use the TCP channel because its performance is better than the HTTP channel; if the application is running on the Internet, sometimes according to the firewall The configuration, HTTP is the only choice. It is necessary to remember that if a firewall software is used, the firewall should configure the TCP data traffic through the port selected by you.
2) Register the object Type on the server as a known type. RemotingConfiguration.registerwellknownServiceType (TypeObject ", WellkNOWNOBJECTMODE.SINGLECALL); This line code sets some of the parameters in the service and binds the name of the object you want to use with the remote object, the first parameter is bound Objects, the second parameter is a string of the remote object name in the TCP or HTTP channel, and the third parameter allows the container to know when there is a request to the object, how to handle the object. Although WellknownObjectMode.singlecall uses an instance of an object to all caller, it generates an instance of this object for each customer. If you use WellkNownObjectMode.singlecall, each incoming message is served by the same object instance. Complete object code as follows: using System; using System.Runtime; using System.Runtime.Remoting; using System.Runtime.Remoting.Channels; using System.Runtime.Remoting.Channels.Tcp; using RemoteSample; namespace RemoteSampleServer {public class RemoteServer {public static void Main (String [] args) {TcpServerChannel channel = new TcpServerChannel (8808); ChannelServices.RegisterChannel (channel); RemotingConfiguration.RegisterWellKnownServiceType (typeof (RemoteObject), "RemoteObject", WellKnownObjectMode.SingleCall); System. Console.writeline ("press any key"); system.console.readline ();}}} saves files, named RemoteServer.cs with command line CSC /R :System.Runtime.Remoting.dll /R:RemoteObject.dll RemoteServer.cs compiles the REMOTSERVER.EXE file generated by this program.
Step 3: Create a Remote client program to create a server object and name it RemoteClient. When you create a server object, you first created a TCP client channel, which is not bound to a port; secondly get a reference to the remote RemoteObject object. The activator.getObject method returns a value of an object type, and we will then return it to RemoteObject. We pass the parameters that we passed to the RemotingConfiguration in the server object, the first parameter is the object type, and the second parameter is the URI of the remote object.
ChannelServices.RegisterChannel (new TcpClientChannel ()); RemoteObject remoteobj = (RemoteObject) Activator.GetObject (typeof (RemoteObject), "tcp: // localhost: 8808 / RemoteObject"); RemoteClient all the code as follows:
using System; using System.Runtime.Remoting; using System.Runtime.Remoting.Channels; using System.Runtime.Remoting.Channels.Tcp; using RemoteSample; namespace RemoteSampleClient {public class RemoteClient {public static void Main (string [] args) {ChannelServices.RegisterChannel (new TcpClientChannel ()); RemoteObject remoteobj = (RemoteObject) Activator.GetObject (typeof (RemoteObject), "tcp: // localhost: 8808 / RemoteObject"); Console.WriteLine ( "1 2 =" RemoteObj.sum (1, 2) .tostring ()); console.readline (); // Do not let window close before you can see the result}
}
Save the file, named RemoteClient.cs with command line CSC /R :System.Runtime.Remoting.dll /r :RemoteObject.dll RemoteClient.cs generated RemoteClient.exe file.
Step 4: Test run Server.exe in Windows, then open another form to run client.exe. If everything is normal, the client output: 1 2 = 3.
It can be seen that NET Remoting does not need to create a settt root program and a skeleton file, which uses a simple method than Java's RMI, and provides an excellent method for the resource within the Internet and even Internet, using a wide range of RMIs than Java.