In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the knowledge of "how to write a rmi with java". Many people will encounter such a dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
RMI refers to a remote method call (Remote Method Invocation). It is a mechanism that allows objects on one Java virtual machine to call methods on objects in another Java virtual machine. Any object that can be called with this method must implement the remote interface.
When such an object is called, its parameter is "marshalled" and it is sent from the local virtual machine to the remote virtual machine whose parameter is "unmarshalled". When this method terminates, the results from the remote machine are marshalled and sent to the caller's virtual machine. If the method call causes an exception to be thrown, the exception is indicated to the caller.
When providing remote access, we first need to define what the remote end can access. In Java, defining such interfaces requires the implementation of the Remote interface
Public interface Business extends Remote {public String echo (String msg) throws RemoteException;}
After defining the interface, these functions need to be implemented by ourselves on the server side, so declare a class implementation that we provide the interface.
Public class BusinessImpl implements Business {@ Overridepublic String echo (String msg) throws RemoteException {if ("quit" .equals IgnoreCase (msg)) {System.out.println ("Server will be shutdown"); System.exit (0);} System.out.println ("Message from client:" + msg); return "Server response:" + msg;}}
After implementing this method, there is a question of how to run it. Since it is a remote access, there must be a port number and an instance, so we also need to register our code.
Public class Server {public static final String SERVER_REGISTER_NAME = "BusineeDemo"; public static void main (String [] args) throws RemoteException {int port = 2016; UnicastRemoteObject.exportObject (business,port); Registry registry = LocateRegistry.createRegistry (1099); registry.rebind (SERVER_REGISTER_NAME, business);}}
There are two Java classes: UnicastRemoteObject and LocateRegistry
One interface: Registry
Registry interface: provides a remote interface for storing and fetching references to simple remote objects, which are obtained through variable names of any String type, bind,unbind,rebind methods for changing registered names, and lookup and list methods for querying currently bound objects.
UnicastRemoteObject class: used to export a remote object
LocateRegistry class: a helper class program used to obtain a reference to a remote calling object, mainly to build a remote object on a specific IP to accept callbacks from a specific port.
Now that the simple server side is complete, let's look at the client side:
The client code is even simpler. We mentioned earlier that we can get the currently bound service through the lookup method of Registry, so naturally, we need to get the Registry first.
Public class Client {public static void main (String [] args) throws RemoteException, NotBoundException {/ / Registry registry = LocateRegistry.getRegistry ("localhost"); Registry registry = LocateRegistry.getRegistry ("localhost", 1099); Business business = (Business) registry.lookup (Server.SERVER_REGISTER_NAME); System.out.println (business.echo ("Hello Server"));}}
This is the end of the content of "how to write a rmi in java". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.