📄 lightbulbclient.java
字号:
import org.omg.CORBA.*;
import org.omg.CosNaming.*;
// Chapter 12, Listing 2
public class LightBulbClient
{
public static void main(String args[])
{
try
{
// First step : initialize our client ORB
// Create the ORB
ORB orb = ORB.init(args, null);
// Second step: lookup the light bulb using nameservice
// We use the same code from our servant to create the
// nameservice reference and namecomponent.
System.out.println ("Looking for lightbulb");
// Get a name service reference
org.omg.CORBA.Object object =
orb.resolve_initial_references("NameService");
// Narrow to a NamingContext object
NamingContext namingContext =
NamingContextHelper.narrow(object);
// Creating a naming component for our light bulb servant
NameComponent component =
new NameComponent ("LightBulb", "");
// NamingContext requires an array, not a single
// NameComponent
NameComponent componentList[] = { component };
// Now, here the client differs. We want to resolve (lookup)
// the name component representing the light bulb.
org.omg.CORBA.Object remoteRef =
namingContext.resolve(componentList);
// Next, we narrow it to get an address book
LightBulb lb = LightBulbHelper.narrow (remoteRef);
// Finally, we have a remote reference to our light bulb.
System.out.println ("Found light bulb");
// Turn bulb on and off till it fails, demonstrating exceptions
for (;;)
{
try
{
lb.on();
System.out.print ("Light on.... ");
lb.off();
System.out.print ("Light off.... ");
}
catch (BulbBrokenException bbe)
{
System.err.println ();
System.err.println ("Bulb broken exception encountered");
break;
}
}
}
catch (Exception e)
{
System.err.println ("Error - " + e);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -