title: How to use Java EE's @Startup correctly?
body: <p>I want to move a long-running process in an Java EE application from synchronous to asynchronous execution. The process should start automatically and then run every hour.
  The project runs in Glassfish 3.1 with Java EE 6.</p>
  
  <p>After some reading, I believe that I can start such services in Java EE 6 via <code>@Singleton</code> and <code>@Startup</code>.
  So, I create a new class</p>
  
  <pre><code>@Startup
  @Singleton
  public class MyService {
  }
  </code></pre>
  
  <p>and run the existing integration tests via maven.
  To my surprise, the tests fail, down to the the most basic "page is there"-test. They don't find the webpages any longer, instead getting error pages.</p>
  
  <p>Experimenting, I remove the <code>@Startup</code> annotation, and run the tests once more. This time, they succeed.</p>
  
  <p>Manually starting a server and navigating to the page yields the expected result: The page is there.
  What's more, in eclipse, the tests they succeed with both annotations in place. The only difference I can see is that it is eclipse that starts the server, not the Cargo Maven plugin.</p>
  
  <p>What could possibly go wrong here? 
  What do I have to do to use <code>@Startup</code> correctly?</p>
  
  <p>Update:
  After switching back to IntelliJ IDEA from eclipse, I still have this problem.
  However, I see some exceptions - maybe you can make something out of them:</p>
  
  <blockquote>
    <p>[#|2011-07-25T10:22:51.529+0200|SEVERE|glassfish3.1|javax.enterprise.system.tools.deployment.org.glassfish.deployment.common|_ThreadID=65;_ThreadName=Thread-1;|Exception while invoking class org.glassfish.ejb.startup.EjbApplication start method</p>
    
    <p>javax.ejb.EJBException: javax.ejb.CreateException: Initialization failed for Singleton MailService
        at com.sun.ejb.containers.AbstractSingletonContainer$SingletonContextFactory.create(AbstractSingletonContainer.java:719)
        at com.sun.ejb.containers.AbstractSingletonContainer.instantiateSingletonInstance(AbstractSingletonContainer.java:449)
        at org.glassfish.ejb.startup.SingletonLifeCycleManager.initializeSingleton(SingletonLifeCycleManager.java:216)
        at org.glassfish.ejb.startup.SingletonLifeCycleManager.initializeSingleton(SingletonLifeCycleManager.java:177)
        at org.glassfish.ejb.startup.SingletonLifeCycleManager.doStartup(SingletonLifeCycleManager.java:155)
        at org.glassfish.ejb.startup.EjbApplication.start(EjbApplication.java:177)
    ...</p>
    
    <p>Caused by: javax.ejb.CreateException: Initialization failed for Singleton MailService
        at com.sun.ejb.containers.AbstractSingletonContainer.createSingletonEJB(AbstractSingletonContainer.java:545)
        at com.sun.ejb.containers.AbstractSingletonContainer.access$100(AbstractSingletonContainer.java:79)
        at com.sun.ejb.containers.AbstractSingletonContainer$SingletonContextFactory.create(AbstractSingletonContainer.java:717)
        ... 36 more</p>
    
    <p>Caused by: java.lang.NullPointerException
        at java.util.concurrent.ConcurrentHashMap.get(ConcurrentHashMap.java:768)
        at org.jboss.weld.manager.BeanManagerImpl.getBean(BeanManagerImpl.java:1209)
        at org.jboss.weld.manager.BeanManagerImpl.getBean(BeanManagerImpl.java:144)
        at org.glassfish.weld.services.JCDIServiceImpl._createJCDIInjectionContext(JCDIServiceImpl.java:144)</p>
  </blockquote>