• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

Accessing EJB in websphere from Standalone client program

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have deployed a EJB onto Websphere AppServer 5.0.
here is my code :

HOME :::::
package com.cognizant.sample.ejb.interfaces;
import javax.ejb.EJBHome;
import java.rmi.RemoteException;
import javax.ejb.CreateException;
public interface BoxHome extends EJBHome {

public Box create() throws CreateException, RemoteException;

}

Remote Interface ::::
package com.cognizant.sample.ejb.interfaces;
import javax.ejb.EJBObject;
import java.rmi.RemoteException;
public interface Box extends EJBObject{

public int CalcArea (int length,int width) throws RemoteException;
public void printArea(int length,int width) throws RemoteException;
public int CalcVolume(int length ,int width,int depth) throws RemoteException;

}
BEAN ::::
package com.cognizant.sample.ejb.impl;
import java.rmi.RemoteException;
import javax.ejb.EJBException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
public class BoxBean implements SessionBean {
public BoxBean() {
super();
}
public void setSessionContext(SessionContext arg0)
throws EJBException, RemoteException {

}
public void ejbRemove() throws EJBException, RemoteException {

}
public void ejbActivate() throws EJBException, RemoteException {

}
public void ejbPassivate() throws EJBException, RemoteException {

}

public int CalcArea (int length,int width) throws RemoteException{
return length * width;
}
public void printArea(int length,int width) throws RemoteException{
int area=length*width;
System.out.println("The area of a box of length " +length + " width" + width + " is "+ area);
}

public int CalcVolume(int length ,int width,int depth) throws RemoteException{
return length * width *depth;
}

}
I am trying to access it from a standalone java client program specifying the initial context factory class

STAND ALONE JAVA-CLIENT PROGRAM :
package com.cognizant.sample.ejb.clients;

import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;
import com.cognizant.sample.ejb.interfaces.*;
public class BoxClient {
public BoxClient() {
super();
}
public static void main(String[] args) {
try{

Properties props=new Properties();
props.put(Context.INITIAL_CONTEXT_FACTORY,"com.ibm.websphere.naming.WsnInitialContextFactory");
props.put(Context.PROVIDER_URL,"iiop://localhost:900");
Context ctx=new InitialContext(props);
Object obj=ctx.lookup("SAMPLEBOX");
BoxHome boxHome=(BoxHome)PortableRemoteObject.narrow(obj,BoxHome.class);
Box box=boxHome.create();

int area=box.CalcArea(1,2);
System.out.println("Area from client" +area);
box.printArea(2,3);
int volume=box.CalcVolume(2,3,4);
System.out.println("Volume from client" + volume);

}
catch(Exception ex){
ex.printStackTrace();
}


}
}

I am getting the following exception.
javax.naming.NoInitialContextException: Cannot instantiate class: com.ibm.websphere.naming.WsnInitialContextFactory. Root exception is java.lang.ClassNotFoundException: com.ibm.websphere.naming.WsnInitialContextFactory
I included the jar file naming.jar in the class path.Then another exception was thrown asking for jndiprovider.properties. i included namingclient.jar.again another exception was thrown.this process is cascading.
Can anyone tell me how to write a standalone client program to access the beans in Websphere ?Is my program right ???
What initialcontext factory and info should i provide ?
AND MORE IMPORTANTLY,WHAT JARS SHOULD BE IN MY CLASSPATH and what properties files containg which info SHOULD BE IN MY CLASSPATH???
If anybody could explain the concept of the contextfactory,it would be very helpful.Pls reply asap.
 
Ranch Hand
Posts: 2713
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moving to the IBM/Websphere forum...
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Naveen,
I forgot the exact name of the jar file to be present in the classpath. Right noe i dont have websphere installed in my m/c. I think it is websphere.jar. One important thing is for connecting to EJB's deployed in websphere u need to use IBM's JDK to compile/run the client program. It is shipped along with your websphere installation.
Karthik
 
Ranch Hand
Posts: 1551
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

should work with just,
Context ctx = new InitialContext();
The spec says IBM should populate the env with requirement at deploy time
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic