users@glassfish.java.net

Re: how to do JNDI lookups from LifeCycle modules

From: <glassfish_at_javadesktop.org>
Date: Tue, 11 Sep 2007 14:33:27 PDT

Funny you should ask as I just tested this today. In my EJB, I have:

[i]@Stateless(mappedName="com.canoga.nms.core.v1.schedule.quartz.QuartzSchedulerFacade")
public class QuartzSchedulerFacadeBean implements QuartzSchedulerFacadeRemote, QuartzSchedulerFacadeLocal {...

[/i]
And in my test LifecycleListener I have:
[i]
/*
 * Main.java
 *
 * Created on Sep 10, 2007, 7:29:26 PM
 *
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.canoga.nms.pkg.pcs.v1.lifecycle.service;

import com.canoga.nms.core.v1.schedule.quartz.QuartzSchedulerFacadeRemote;
import com.sun.appserv.server.LifecycleEvent;
import com.sun.appserv.server.LifecycleEventContext;
import com.sun.appserv.server.LifecycleListener;
import com.sun.appserv.server.ServerLifecycleException;
import javax.enterprise.deploy.spi.exceptions.ConfigurationException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

/**
 *
 * @author brett
 */
public class Main implements LifecycleListener {

    public void handleEvent(LifecycleEvent event) throws ServerLifecycleException {
        Object eventData = event.getData();
        LifecycleEventContext eventContext = event.getLifecycleEventContext();
        switch (event.getEventType()) {
        case LifecycleEvent.INIT_EVENT:
            onInitEvent(eventData, eventContext);
            break;
        case LifecycleEvent.STARTUP_EVENT:
            onStartupEvent(eventData, eventContext);
            break;
        case LifecycleEvent.READY_EVENT:
            onReadyEvent(eventData, eventContext);
            break;
        case LifecycleEvent.SHUTDOWN_EVENT:
            onShutdownEvent(eventData, eventContext);
            break;
        case LifecycleEvent.TERMINATION_EVENT:
            onTerminationEvent(eventData, eventContext);
            break;
        default:
            break;
        }
    }

    public void onInitEvent(Object eventData, LifecycleEventContext eventContext) {
        eventContext.log("onInitEvent called");
    }
    
    public void onStartupEvent(Object eventData, LifecycleEventContext eventContext) {
        eventContext.log("onStartupEvent called");
    }
    
    public void onReadyEvent(Object eventData, LifecycleEventContext eventContext) {
        eventContext.log("onReadyEvent called");
        testForBean(eventContext);
    }
    
    public void onShutdownEvent(Object eventData, LifecycleEventContext eventContext) {
        eventContext.log("onShutdownEvent called");
    }
    
    public void onTerminationEvent(Object eventData, LifecycleEventContext eventContext) {
        eventContext.log("onTerminationEvent called");
    }

    private void testForBean(LifecycleEventContext eventContext) {
        try {
            Context c = eventContext.getInitialContext();
            QuartzSchedulerFacadeRemote remote = (QuartzSchedulerFacadeRemote) c.lookup("com.canoga.nms.core.v1.schedule.quartz.QuartzSchedulerFacade");
            System.out.println("remote is " + remote);
        } catch (NamingException ne) {
            eventContext.log("naming exception getting quartz facade", ne);
        }
    }

}

[/i]

The key is that you need to get the JNDI name into global scope. See the EJB FAQ at [i]http://glassfish.dev.java.net/javaee5/ejb/EJB_FAQ.html[/i] on how to get
the name into global scope.

I don't think you can access EJB's through a local interface in this manner, however.
[Message sent by forum member 'bbergquist' (bbergquist)]

http://forums.java.net/jive/thread.jspa?messageID=234935