views:

464

answers:

4

I'm trying to access an EJB3 from a JEE client-application, but keep getting nothing but lookup failures. The client application is running within the JEE Application Client Container.

My JEE Application 'CoreServer' is exposing a number of beans with remote interfaces. I have no problem accessing them from a Web Application deployed on the same Glassfish v3.0.1.

Now I'm trying to access it from a 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 deploy it to Glassfish and run it from the 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, that's exactly the same JNDI-name I use when I lookup the bean from the WebApplication (via SessionContext, not InitialContext - does that matter?). Also, when I deploy 'CoreServer', Glassfish reports:

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 with (only) Remote interfaces?

A: 

If you are talking about a stand-alone client, use this answer:

Here's the method I use for JNDI lookup for glassfish v2, it may be quite similar for 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 the com.sun INITIAL_CONTEXT_FACTORY. Also make sure you have all the glassfish dependencies bundled with your app. For glassfish v2, there are a lot. The v2 jars are: javaee, appserv-rt, appserv-ext, appserv-admin, appserv-deployment-client.

Things may be a lot simpler with v3, but this definitely works for v2.x

Brian Leathem
A: 

If you are talking about an application client use this answer:

Don't bother with jndi lookup; you can use @EJB injection into static fields of the application class.

Brian Leathem
A: 

Have you tried java:global/CoreServer/CampaignController!mvs.api.CampaignControllerRemote instead of java:global/CoreServer/CampaignController ?

RaviG