📄 lightbulbclient.java
字号:
import java.rmi.*;
// Chapter 11, Listing 4
public class LightBulbClient
{
public static void main(String args[])
{
System.out.println ("Looking for light bulb service");
try
{
// Check to see if a registry was specified
String registry = "localhost";
if (args.length >=1)
{
registry = args[0];
}
// Registration format //registry_hostname (optional):port /service
String registration = "rmi://" + registry + "/RMILightBulb";
// Lookup the service in the registry, and obtain a remote service
Remote remoteService = Naming.lookup ( registration );
// Cast to a RMILightBulb interface
RMILightBulb bulbService = (RMILightBulb) remoteService;
// Turn it on
System.out.println ("Invoking bulbservice.on()");
bulbService.on();
// See if bulb has changed
System.out.println ("Bulb state : " + bulbService.isOn() );
// Conserve power
System.out.println ("Invoking bulbservice.off()");
bulbService.off();
// See if bulb has changed
System.out.println ("Bulb state : " + bulbService.isOn() );
}
catch (NotBoundException nbe)
{
System.out.println ("No light bulb service available in registry!");
}
catch (RemoteException re)
{
System.out.println ("RMI Error - " + re);
}
catch (Exception e)
{
System.out.println ("Error - " + e);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -