Perform lookup in a Java stand alone client
Applications that do not run in a container cannot use java: lookup names because it is the container that configures the java: name space for the application. Instead, an application of this type must look up the object directly from the name server. Also, the stand alone client cannot rely on a container to resolve the location of the name server as it is running outside of one. When this is the case the name server location needs to be specified in the application code.
As the location of the name server and the JNDI name of the EJB are environment specific, the stand alone client should obtain the necessary values to pass to the InitialContext from an external resource, like a properties file.
In the following sections, we show three examples of JNDI lookups to appservers running the target EJB. These examples do not use a properties file. There is one example to connect to a:
Single server
Server cluster
Server cluster with fault-tolerant initial context
Single server
Example 6-2 shows the lookup of an EJB home that is running in the single server, Server1, configured in the node app1. In this example there is only one server so there is no possibility of specifying multiple name servers. In this example the name server is running on the same appserver as the target EJB, so the JNDI name does not need to be fully qualified.
Example 6-2 Single server - lookup EJB home
// Get the initial contextHashtable env = new Hashtable();env.put(Context.INITIAL_CONTEXT_FACTORY, "com.ibm.websphere.naming.WsnInitialContextFactory");env.put(Context.PROVIDER_URL, "corbaloc::app1:2809");Context initialContext = new InitialContext(env);// Look up the home interface using the JNDI nametry {java.lang.Object ejbHome = initialContext.lookup("ejb/BeenThere");beenThereHome = (BeenThereHome)javax.rmi.PortableRemoteObject.narrow((org.omg.CORBA.Object) ejbHome, BeenThereHome.class);}catch (NamingException e) { // Error getting the home interface...}
Server cluster
Example 6-3 shows the lookup of an EJB home that is running in the cluster, EJBcluster. The name can be resolved if any of the cluster members are running. As this is a stand alone client, the location of the name service still needs to be specified.
Example 6-3 Server cluster - lookup EJB home
// Get the initial contextHashtable env = new Hashtable();env.put(Context.INITIAL_CONTEXT_FACTORY, "com.ibm.websphere.naming.WsnInitialContextFactory");env.put(Context.PROVIDER_URL, "corbaloc::app1:2809");Context initialContext = new InitialContext(env);// Look up the home interface using the JNDI nametry {java.lang.Object ejbHome = initialContext.lookup("cell/clusters/EJBcluster/ejb/BeenThere");beenThereHome = (BeenThereHome)javax.rmi.PortableRemoteObject.narrow((org.omg.CORBA.Object) ejbHome, BeenThereHome.class);}catch (NamingException e) { // Error getting the home interface...}
Server cluster with fault-tolerant initial context
In the previous example, the EJB is running on clustered servers but still relies on one server for name service lookups. Example 6-4 shows how to obtain the initial context via a fault-tolerant provider URL.
Example 6-4 Cluster - lookup EJB home via fault-tolerant provider URL & specify path
// Get the initial contextHashtable env = new Hashtable();env.put(Context.INITIAL_CONTEXT_FACTORY, "com.ibm.websphere.naming.WsnInitialContextFactory");env.put(Context.PROVIDER_URL, "corbaloc::app1:2809,:app2:2809");Context initialContext = new InitialContext(env);// Look up the home interface using the JNDI nametry {java.lang.Object ejbHome = initialContext.lookup("cell/clusters/EJBcluster/BeenThere");
This is fault tolerant, so if one process running a name server goes down, another can be used, and the cluster can be accessed via the appropriate cell-based path in the lookup.
Prev | Home | Next WebSphere is a trademark of the IBM Corporation in the United States, other countries, or both.
IBM is a trademark of the IBM Corporation in the United States, other countries, or both.