users@glassfish.java.net

Re: startup bean ??

From: <glassfish_at_javadesktop.org>
Date: Mon, 29 Dec 2008 07:14:24 PST

Hi,

I have the same problem since I want to start an EJB timer when I deploy an EJB.

It is not easy to achieve that with EJB 3.0. You could try to create an EAR that contains a web application and the EJB. In this web application you can use the ContextListeners of the Servlet classes.

However I found a way to start the timer after GlassFish is started. Perhaps that is good enough for a real installation. The problem is that the Timer is lost when I redeploy from Eclipse since the Timer belongs to the application. So that is a little annoying but perhaps workable for you?

You can make your Life Cycle Module for GlassFish as described here:
http://blogs.sun.com/pblaha/entry/how_to_implement_cron_in
This code example is EJB 2.1. I succeeded in making one for EJB 3.0 which I post below.

Does anybody have a better suggestion for invoking a method on an EJB after it has been deployed?

I hope my code example can give you some idea what you can do with the Life Cycle Module.

BR
Jelte



import java.util.logging.Level;
import java.util.logging.Logger;

import javax.naming.InitialContext;

import com.xyz.playground.UsingResourceAdapter;
import com.sun.appserv.server.LifecycleEvent;
import com.sun.appserv.server.ServerLifecycleException;

public class LoggingLifeCycleListener implements com.sun.appserv.server.LifecycleListener
{
    private static final Logger LOGGER = Logger.getLogger(LoggingLifeCycleListener.class.getName());
    
    @Override
    public void handleEvent(LifecycleEvent lifecycleEvent) throws ServerLifecycleException
    {
        LOGGER.log(Level.INFO, "handleEvent " + getEventType(lifecycleEvent.getEventType()));
        if (lifecycleEvent.getEventType() == LifecycleEvent.READY_EVENT)
        {
            try
            {
                InitialContext initialContext =
                    lifecycleEvent.getLifecycleEventContext().getInitialContext();
                UsingResourceAdapter service = (UsingResourceAdapter) initialContext.lookup("com.xyz.playground.UsingResourceAdapter");
                service.startTimer();
            }
            catch (Exception exception)
            {
                LOGGER.log(Level.SEVERE, "Error while handling life cycle event", exception);
            }
        }
    }

    private String getEventType(int eventType)
    {
        String result;
        switch (eventType)
        {
            case 0:
                result = "INIT_EVENT";
                break;
            case 1:
                result = "STARTUP_EVENT";
                break;
            case 2:
                result = "READY_EVENT";
                break;
            case 3:
                result = "SHUTDOWN_EVENT";
                break;
            case 4:
                result = "TERMINATION_EVENT";
                break;
            default:
                result = "UNKNOWN_EVENT";
                break;
        }
        return result;
    }
}
[Message sent by forum member 'jeltejansons' (jeltejansons)]

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