user4235401
user4235401

Reputation:

How to find InitialContext details in WebSphere?

I am working on setting up a servlet client which accesses Deployed EJBs. EJBs are deployed in 2 node setup on WebSphere 8.5 . On each server, I will be deploying Servlet which access EJBs. People will be connecting to Servlet which internal will connect to EJB and return the response.

To access the EJB I need to initialize the context. I believe code looks something around below lines.

 private void doSomething()  {      
      Hashtable env = new Hashtable(); 
      env.put(Context.INITIAL_CONTEXT_FACTORY,"com.ibm.websphere.naming.WsnInitialContextFactory"); 
      env.put(Context.PROVIDER_URL,"iiop//host:port"); 
      Object obj;      
      try{ 
            InitialContext ctx = new InitialContext(env); 
            obj =  ctx.lookup("EjbSample");       
      } catch(Exception ne){ ... } 
  }

My questions here are:

[Edit 1]: When I try to access EJB from same host on which WAS is running, I am getting following error

javax.naming.NameNotFoundException: Name "EjbSample" not found in context "serverlocal:CELLROOT/SERVERROOT

Upvotes: 2

Views: 3329

Answers (3)

Varun Jain
Varun Jain

Reputation: 1421

There are various ways of getting the InitialContext in Websphere and one of them is detailed out below.

You can do it using Cobra Object Link like given below.

Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY,
     "com.ibm.websphere.naming.WsnInitialContextFactory");
env.put(Context.PROVIDER_URL, "corbaloc:iiop:myhost.mycompany.com:2809");
Context initialContext = new InitialContext(env);

CORBA object URLs can contain more than one bootstrap address. You can use this feature when attempting to obtain an initial context from a server cluster. You can specify the bootstrap addresses for all servers in the cluster in the URL. The operation succeeds if at least one of the servers is running, eliminating a single point of failure. There is no guarantee of any particular order in which the address list will be processed. For example, the second bootstrap address may be used to obtain the initial context even though the server at the first bootstrap address in the list is available.

There are other options as well to do it and you can use this link for other options.

Upvotes: 0

cbarrett
cbarrett

Reputation: 36

The following KnowledgeCenter page discusses getting an InitialContext via a ProviderURL: https://www.ibm.com/support/knowledgecenter/en/SSAW57_8.5.5/com.ibm.websphere.nd.multiplatform.doc/ae/rnam_example_prop2.html

It states "A provider URL contains bootstrap server information that the initial context factory can use to obtain an initial context."

One can find the bootstrap port information for a particular server via the admin console or the serverindex.xml file. These port values can vary from node to node depending on settings used during the install or adding of Nodes and/or Servers into the Cell.

Admin Console:

serverindex.xml:

  • found on each server node at WAS_HOME/profiles/serverProfile/config/cells/cellName/nodes/nodeName

  • Contains a list of each server on the node and their server ports.

  • endPointName="BOOTSTRAP_ADDRESS" contains the desired port

Upvotes: 1

Gas
Gas

Reputation: 18050

If your client (servlet) is deployed on the same server/cluster, just use default InitialContext constructor, like below. Default, correct params will be provided. You only need to define these if your client is on different cell than EJBs.

InitialContext ctx = new InitialContext(); 
obj =  ctx.lookup("EjbSample");    

Moreover if you are using JavaEE 6,7,8 you can just inject your EJB like:

@EJB
EjbSample ejb;

and bind reference to the JNDI name during/or after the installation.

Upvotes: 1

Related Questions