1. Current tasks:
The CEO of the last chapter of the Universal candy machine, I hope we can send it a report on inventory and machine status. (It's just that little Cese,ceo will be impressed by my ability to encode hiahiahia~)
(1) Add the location variable (2) to the code in the previous chapter, and added the candy machine Monitor class. There is a problem: I click to run after the console what the reaction is not, I can not enter information. What's the situation?
1 Public classGumballmachine {2 String location;3 intCount = 0;4 PublicGumballmachine (String location,intcount) {5 Super();6 //other Code7 This. location =Location ;8 }9 Ten PublicString getLocation () { One returnLocation ; A } - - //Other methods the}
1 ImportUnit10.state.GumballMachine;2 3 Public classGumballmonitor {4 Gumballmachine Machine;5 6 PublicGumballmonitor (Gumballmachine machine) {7 Super();8 This. Machine =Machine ;9 }Ten One Public voidReport () { ASYSTEM.OUT.PRINTLN ("Gumball machine:" +machine.getlocation ()); -System.out.println ("Current Inventory:" +Machine.getcount ()); -System.out.println ("Current state:" +machine.getstate ()); the } -}
1 ImportUnit10.state.GumballMachine;2 3 Public classgumballmachinetestdrive {4 Public voidMain (string[] args) {5 intCount = 0;6 //use the command line to pass in locations and numbers7 if(args.length<2){8System.out.println ("Gumballmachine <name><inventory>");9System.exit (1);Ten } One ACount = Integer.parseint (args[1]); -Gumballmachine machine =NewGumballmachine (args[0],count); -Gumballmonitor monitor =Newgumballmonitor (machine); the - Monitor.report (); - //Other test Code - } +}
But here's the thing: the CEO wants to monitor the candy machine remotely, and now the monitor and the candy machine are running on the same JVM!
Workaround: Remote agent. remote proxies are like "local representatives of remote objects." Remote objects: Live in a different Java Virtual machine (JVM) heap. Local representative: An object that can be called by a local method, whose behavior is forwarded to the remote object. Here the "local heap" is the CEO's desktop, it is actually and agent in the chat, it thought he was chatting with the real candy machine. "Remote Heap" is a candy machine, it is really doing things.
(1) Understanding RMI
(2) Turn gumballmachine into a remote service and provide some methods that can be called remotely.
(3) Create an agent proxy that can communicate with the remote Gumballmachine (RMI is needed here), and then gumballmonitor with the monitoring system.
2.RMI
Java has built-in remote call functionality, we only need to modify the code, so that it meets the requirements of RMI.
Customer Object---Customer helper---Service helper object (skeleton)--service object
(1) turning a normal object into a service that can be invoked by a remote client:
A. Making a remote interface: The client helper object (stub) and the service object implement this interface.
B. Making a remote implementation: real-action implementation.
C. Using stubs and skeleton generated by rmic: This is the helper class for customers and services, which is handled automatically when we run the Rmic tool.
D. Start RMI registry: Just like a phone book, all registered services are here.
E. Turn on remote services: Register the service and start running.
1 ImportJava.rmi.Remote;2 Importjava.rmi.RemoteException;3 4 Public InterfaceMyRemoteextendsRemote {5 //because the remote method call uses network and I/O, it must be considered as a risky6 //The return type of the method must be either the primitive type or the serializable type because it needs to be transported over the network7 PublicString SayHello ()throwsremoteexception;8}
1 Importjava.rmi.Naming;2 Importjava.rmi.RemoteException;3 ImportJava.rmi.server.UnicastRemoteObject;4 5 Public classMyremoteimplextendsUnicastRemoteObjectImplementsMyRemote {6 7 protectedMyremoteimpl ()throwsRemoteException {8 Super();9 }Ten One PublicString SayHello () { A return"Server Says:hey"; - } - the Public Static voidMain (string[] args) { - Try { - //Create a new remote object -MyRemote Service =NewMyremoteimpl (); + //bind the remote object to RMI Regstry (something like a phone book), register the name Remotehello, - //The User Client uses this name to find the corresponding agent: "I'm looking for a stub named Remotehello." +Naming.rebind ("Remotehello", service); A}Catch(Exception e) { at e.printstacktrace (); - } - } -}
My environment variables are right, so I still don't know what the reason is.
Resolved: The book command is%rmic, but I want to hit rmic is right,% does not need, it is just its input prompt, not the command part of the thing.
(2) Gets the sub object: When the service is bound to the RMI registry, it is actually tied to the stub, so it can be found through lookup.
1 Importjava.rmi.*;2 3 Public classmyremoteclient {4 Public Static voidMain (string[] args) {5 Newmyremoteclient (). Go (); 6 }7 8 Public voidGo () {9 Try {TenMyRemote service = (MyRemote) naming.lookup ("Rmi://10.15.199.179/remotehello"); OneString s =Service.sayhello (); A System.out.println (s); -}Catch(Exception e) { - e.printstacktrace (); the } - } -}
3. Make Gumballmachine a remote service:
(1) Create a remote interface for Gumballmachine.
(2) Confirm that all return types of the interface method are serializable: Here the state interface is not serializable, so extends Serializable. But it's not finished! In the previous chapter of the program, we
(3) Implement this interface in Gumballmachine.
1 Importjava.rmi.*;2 Importunit10.state.State;3 4 Public InterfaceGumballmachineremoteextendsRemote {5 Public intGetCount ()throwsremoteexception;6 PublicString getLocation ()throwsremoteexception;7 PublicState GetState ()throwsremoteexception;8}
1 Import java.io.Serializable; 2 3 Public Interface extends serializable{4 ... 5 }
Headfirst Learning Notes: Proxy mode (5.2)