Listing 4. UseCalculator.java, Which Connects to and Uses Our
Calculator EJB
package il.co.lerner.calculator;
import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;
import il.co.lerner.calculator.Calculator;
import il.co.lerner.calculator.CalculatorHome;
class UseCalculator
{
public static void main(String[] args)
{
try
{
// Get a naming context
InitialContext jndiContext =
System.out.println("Got context");
// Get a reference to the Calculator Bean
Object ref = jndiContext.lookup
System.out.println("Got reference");
// Get a reference from this to the
// Bean's Home interface
CalculatorHome home = (CalculatorHome)
PortableRemoteObject.narrow
// Create a Calculator object from the
// Home interface
Calculator calculator = home.create();
// call multiply()
System.out.println("Multiplying 2 x 3:");
System.out.println
}
catch(Exception e)
{
System.out.println(e.toString());
}
}
}
Copyright © 1994 - 2019 Linux Journal. All rights reserved.