Implementation of the remote interface

Source: Internet
Author: User
Tags bind port number

The

server must contain a class that extends the UnicastRemoteObject and implements a remote interface. This class can also contain additional methods, but the customer can only use the methods in the remote interface. This is obvious because the client gets only one handle to the interface, not the class that implements it. The
must explicitly define the builder for the remote object, even if you are only ready to define a default builder to invoke the underlying class builder. It must be written clearly, as it must "throw" the RemoteException violation. The
below lists the implementation procedures for the remote interface perfecttime:

 

//: Perfecttime.java//The implementation of the Perfecttime/remote object package c15.ptime; IM
Port java.rmi.*;
Import java.rmi.server.*;
Import java.rmi.registry.*;

Import java.net.*; public class Perfecttime extends UnicastRemoteObject implements Perfecttimei {//implementation of the INTERFA
  Ce:public long Getperfecttime () throws RemoteException {return System.currenttimemillis (); }//must implement constructor to throw//Remoteexception:public Perfecttime () throws RemoteException {//Su Per (); called automatically}//registration for RMI serving:public static void Main (string[) args) {System.setse
    Curitymanager (New Rmisecuritymanager ());
      try {perfecttime pt = new Perfecttime ();
      Naming.bind ("//colossus:2005/perfecttime", PT);
    System.out.println ("Ready to Does Time");
    catch (Exception e) {e.printstacktrace (); }
  }
} ///:~


Here, main () controls all the details of setting up the server. When you save an RMI object, you must take the following actions somewhere in your program:
(1) Create and install a security manager to support RMI. As part of the Java release package, the only one that applies to RMI is Rmisecuritymanager.
(2) Create one or more instances of the remote object. Here, you can see that the Perfecttime object is created.
(3) Register at least one remote object to the RMI remote object registry. A method owned by a remote object can generate a handle to another remote object. As a result, customers only need to visit the registry once, get the first remote object.

1. Setting up the Registry
Here, you can see a call to the static method Naming.bind (). However, this call requires that the registry be run as a stand-alone process on the computer. The name of the registry server is Rmiregistry. In a 32-bit Windows environment, you can use:
Start Rmiregistry
Make it run in the background. In Unix, use:
Rmiregistry &
Like many network programs, Rmiregistry is located at an IP address where the machine starts, but it must also monitor a port. If you call Rmiregistry as above, and you do not use parameters, the registry port defaults to 1099. If you want it to be in one of the other ports, simply add a parameter to the command line and specify that port number. For this example, the port will be at 2005, so Rmiregistry should start as follows (for 32-bit Windows):
Start Rmiregistry 2005
For UNIX, use the following command:
Rmiregistry &
Port-related information must be transmitted to the bind () command, along with the IP address of the machine on which the registry resides. But if we want to test the RMI program locally, as the network program in this chapter has been testing, doing so will cause problems. In the JDK version 1.1.1, there are two issues (note ⑦):
(1) localhost cannot work with RMI. So in order to complete the test of RMI on a single machine, the name of the machine must be provided. To investigate the name of your own machine in a 32-bit Windows environment, go to the Control Panel, select Network, and select the "Identity" card, which lists the computer's name. In my own case, my machine is called "Colossus" (because I save a variety of development systems with several large-capacity hard drives--clossus is the meaning of "giants"). It seems that uppercase will be ignored.
(2) RMI cannot work unless the computer has an active TCP/IP connection, even if all components only need to communicate with each other in the local machine. This means that you must connect to your ISP (ISPs) before attempting to run the program, or you will get some ambiguous offending message.

⑦: To find this information, I don't know how many brain cells have been damaged.

With these considerations in mind, the bind () command becomes the following:
Naming.bind ("//colossus:2005/perfecttime", PT);
If you use the default port 1099, there is no need to specify a port, so you can use:
Naming.bind ("//colossus/perfecttime", PT);
In a future version of JDK (after 1.1), once the localhost problem is corrected, the local test can be performed normally, with the IP address removed, using only identifiers:
Naming.bind ("Perfecttime", PT);
The service name is arbitrary, and it is here just as Perfecttime, as the class name, but you can modify it as appropriate. The most important thing is to make sure that it is a unique name in the registry so that the client can get the remote object normally. If the name is already in the registration list, a alreadyboundexception violation will be obtained. To prevent this problem, consider insisting on using rebind () and giving up bind (). This is because rebind () either adds a new entry or replaces an entry with the same name.
Although main () exits, our objects have been created and registered, so the registry remains active until the customer arrives and makes a request to it. As long as the rmiregistry is running and we don't call the Naming.unbind () method for the name, the object is definitely in that place. For this reason, when we design our own code, we need to first close rmiregistry and restart it when compiling a new version of the remote object.
It is not always important to start the rmiregistry as an external process. If you know beforehand that you are the only application required for the registry, you can start it inside the program using the following code:
Locateregistry.createregistry (2005);
As before, 2005 represents the port number we chose in this example. This is equivalent to executing rmiregistry 2005 at the command line. However, this approach is often more convenient when designing RMI code, because it cancels the extra steps required to start and abort the registry. Once this code has been executed, you can use naming to "bind"--bind () as before.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.