Using Remote objects

Source: Internet
Author: User

The whole purpose of RMI is to simplify the use of remote objects as much as possible. The only additional thing we need to do in the client program is to find and retrieve the remote interface from the server. Since then, the rest of the thing is normal Java programming: sending messages to objects. Here are the programs that use Perfecttime:

: Displayperfecttime.java
//Uses remote Object Perfecttime
package c15.ptime;
Import java.rmi.*;
Import java.rmi.registry.*;

public class Displayperfecttime {public
  static void Main (string[] args) {
    System.setsecuritymanager (
      new Rmisecuritymanager ());
    try {
      Perfecttimei t = 
        (Perfecttimei) naming.lookup (
          "//colossus:2005/perfecttime");
      for (int i = 0; i < i++)
        System.out.println ("Perfect time =" +
          t.getperfecttime ());
    } catch (Exception e) {
      e.printstacktrace ();}}}
///:~


The ID string is the same as the string that registers the object with the naming, and the first part indicates the URL and the port number. Because we are ready to use a URL, we can also specify a machine on the Internet.
The returned from Naming.lookup () must be styled to the remote interface, not to the class. If you swap a class, you get an offending prompt.
In the following method calls:
T.getperfecttime ()
We can see that once the handle of the remote object is obtained, programming with it is very similar to programming with local objects (there is only one difference: The remote method "throws" a remoteexception violation).

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.