users@glassfish.java.net

Re: Is there a way to automatically instantiate a bean at startup?

From: <glassfish_at_javadesktop.org>
Date: Tue, 10 Mar 2009 11:17:05 PDT

I have spent a while digging into the LifecycleListener and while I've got it running under Glassfish I don't seem to be able to instantiate my mailer bean. I found a limited number of examples and threads on the net, but so far they aren't helping.

I tried injection first but from what I've read that won't work because the injection is attempted before the server is at a ready state. So next I tried using a context lookup (below). My understanding is that I need to be looking up the bean's remote interface because a local interface won't be available to the container. To do this I had to include the Mailer bean in the MailerLifecycleListener bean so that I could import the remote interface. I also found somewhere that I need to include the actual EJBs jar file in the domain's lib directory. I've tried all of this and I'm catching a javax.naming.NameNotFoundException exception.

Can anyone point me in the right direction here?

  public void handleEvent(LifecycleEvent event) throws ServerLifecycleException {
        LifecycleEventContext context = event.getLifecycleEventContext();

        if (LifecycleEvent.READY_EVENT == event.getEventType()) {
            try {
                InitialContext initialContext =
                        context.getInitialContext();
                mailer = (MailerRemote) initialContext.lookup("mailer/MailerBean");
            } catch (Exception exception) {
                context.log("Error while handling life cycle event", exception);
            }

            if (mailer != null) {
                mailer.stopTimers();
                mailer.startTimer();
            } else {
                context.log("can't find mailer");
            }
            return;
        }
[Message sent by forum member 'eric_marsh' (eric_marsh)]

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