Jimmy Chan
Jimmy Chan

Reputation: 197

How to config and lookup JNDI in websphere?

I'm studying EJB now, and I create a simple EJB example in JBOSS and run successfully, here are my steps:

  1. Create an EJB project in myeclipse
  2. Create an interface named FirstEjb
  3. Create FirstEjbBean implemented the FirstEjb interface, and mark the EJB annotations

    
        @Remote
        @Stateless
        public class FirstEjbBean implements FirstEjb {    
            @Override
            public String saySomething(String name) {
                return "Hello, " + name;
            }    
        } 
    

  4. Create a Java project name "EjbClient" in MyEclipse, export the FirstEjb interface as a *.jar and the new Java project reference to it

  5. Add all the jars in directory "client" of JBOSS to EjbClient project
  6. Create a jndi.properties in the Ejb:

    
        java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
        java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
        java.naming.provider.url=localhost
    

7 .Create class FirstEjbClient.java

public class FirstEjbClient {
    public static void main(String[] args) throws NamingException {
        InitialContext context = new InitialContext();
        FirstEjb ejb = (FirstEjb) context.lookup("FirstEjbBean/remote");
        String something = ejb.saySomething("Jimmy.Chen");
        System.out.println(something);
    }
}

And then I can access the EJB successfully.

The question is, I don't know how to do this same in websphere.

There are some questions:

  1. Do I need to config anything in websphere after deploy the EJB project? Like JNDI??
  2. What jars should I import into the Client project? And those jars are in what directory of websphere?
  3. Do I still need the jndi.properties? And how to write it if needed?

I have search on the internet a lot, but all I found is config the data source in websphere.

Sorry for my poor English, hope there is someone can understand it and provide some help.

Thanks in advance!

Jimmy.Chen

Upvotes: 0

Views: 10504

Answers (2)

Jats
Jats

Reputation: 1

When you create an EJB project in Eclipse, an EJB deployment descriptor is created. You have to add all your JNDI resources in it, in the references tab. And you have to add websphere runtime JARs same as you have added any other JARs.

Upvotes: 0

Michal Fleischhans
Michal Fleischhans

Reputation: 293

Hello,
try this:

Properties props= new Properties();
props.setProperty(Context.INITIAL_CONTEXT_FACTORY,"com.ibm.websphere.naming.WsnInitialContextFactory");
props.setProperty(Context.PROVIDER_URL,"corbaloc:iiop:localhost:2809");
Context ctx = new InitialContext(props);
Object homeObject = ctx.lookup("some.package.MyEJBRemote"); 
MyEJBRemote myEJB = (MyEJBRemote) javax.rmi.PortableRemoteObject.narrow(homeObject,     some.package.MyEJBRemote);

However I'm not sure about the jars necessary to import, since you can add WebSphere Application Server X runtime library to the buildpath in Eclipse
WAS runtimes for Eclipse are on http://www.ibmdw.net/wasdev/

Upvotes: 0

Related Questions