EJB3 JNDI lookup error in Java EE application application
I am trying to access EJB3 from a Java EE client application but I am not getting anything other than search failures. The client application runs in a Java EE client application container.
My Java EE Application "CoreServer" provides multiple beans with remote interfaces. I have no problem accessing them from a web application deployed on the same Glassfish v3.0.1.
Now I am trying to access it from the client application:
public class Main {
public static void main(String[] args) {
CampaignControllerRemote bean = null;
try {
InitialContext ctx = new InitialContext();
bean = (CampaignControllerRemote) ctx.lookup("java:global/CoreServer/CampaignController");
} catch (Exception e) {
System.out.println(e.getMessage());
}
if (bean != null) {
Campaign campaign = bean.get(361);
if (campaign != null) {
System.out.println("Got "+ campaign);
}
}
}
}
When I run it in Glassfish and run it from appclient I get this error:
Lookup failed for 'java:global/CoreServer/CampaignController' in SerialContext targetHost=localhost,targetPort=3700,orb'sInitialHost=localhost,orb'sInitialPort=3700
However, what is the exact same JNDI name that I use when looking up the bean from WebApplication (via SessionContext, not InitialContext - is that important?). Additionally, when I deploy "CoreServer", Glassfish says:
Portable JNDI names for EJB CampaignController : [java:global/CoreServer/CampaignController!mvs.api.CampaignControllerRemote, java:global/CoreServer/CampaignController]
Glassfish-specific (Non-portable) JNDI names for EJB CampaignController : [mvs.api.CampaignControllerRemote, mvs.api.CampaignControllerRemote#mvs.api.CampaignControllerRemote]
I tried all four names, none worked. Is the appclient unable to access beans using (only) remote interfaces?
a source to share
If you are talking about a standalone client use this answer:
Here's the method I use to find the JNDI for Glassfish v2 might be very similar to v3:
private void lookupJndi() {
final Properties props = new Properties();
props.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.enterprise.naming.SerialInitContextFactory");
String host = "hostname.domain";
logger.log(Level.INFO, "Connecting to CORBA Host: " + host);
props.setProperty("org.omg.CORBA.ORBInitialHost", host);
try {
InitialContext ic = new InitialContext(props);
scheduleManager = (ScheduleManagerRemote) ic.lookup("ScheduleManagerRemote");
experimentManager = (ExperimentManagerRemote) ic.lookup("ExperimentManager");
facilityManager = (FacilityManagerRemote) ic.lookup("FacilityManager");
} catch (NamingException e) {
...
}
The key part is getting com.sun INITIAL_CONTEXT_FACTORY. Also, make sure you have all Glassfish dependencies bundled with your application. For glass fish v2 there are many. The v2 banks are: javaee, appserv-rt, appserv-ext, appserv-admin, appserv-deployment-client.
It can be much easier with v3, but it definitely works for v2.x
a source to share