Java remote method call practices

Source: Internet
Author: User

Server business interface
Public interface Biz extends Remote {
Public String aloha () throws RemoteException;
}

Server business implementation
Public class BizImpl extends UnicastRemoteObject implements Biz {
Private static final long serialVersionUID =-8559392326593037120L;
Public BizImpl () throws RemoteException {
Super ();
}
@ Override
Public String aloha () throws RemoteException {
Return "Aloha Baby .";
}
}
Remote implementation name for Server Registration
Public class Server {
Public Server (){
Try {
Biz bis = new BizImpl ();
// Style-1
Registry registry = LocateRegistry. createRegistry (RMIParameter. PORT );
Registry. rebind (RMIParameter. NAME, bis); // replace existing binding name
// Style-2
// Java. rmi. Naming. rebind (RMIParameter. NAME, bis); // replace existing binding name
} Catch (Exception e ){
E. printStackTrace ();
}
}
Public static void main (String args []) {
New Server ();
}
}

Style-2 needs to specify a Policy file or run rmiregistry
Java-Djava. security. policy = folder_x/rmi/policy_file creative. fire. rmi. Server
Policy file content:
Grant {
Permission java.net. SocketPermission "localhost: 9527", "connect, resolve ";
};
Run rmiregistry to modify jre/lib/security/java. policy under the JDK directory.
Rmiregistry (linux)
Start rmiregistry (windows)
Allow anyone (targets) to do anything (actions ):
Grant {
Permission java. security. AllPermission "","";
};
OR
Grant {
Permission java. security. AllPermission;
};
Client piling call
Public class Client {
Public static void main (String args []) {
Try {
Biz bis = (Biz) Naming. lookup (RMIPREFIX + PORT + NAME );
System. out. println (bis. aloha ());
} Catch (Exception e ){
E. printStackTrace ();
}
}
}

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.