Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Java EE's @Startup correctly?

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.

After some reading, I believe that I can start such services in Java EE 6 via @Singleton and @Startup. So, I create a new class

@Startup
@Singleton
public class MyService {
}

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.

Experimenting, I remove the @Startup annotation, and run the tests once more. This time, they succeed.

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.

What could possibly go wrong here? What do I have to do to use @Startup correctly?

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:

[#|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

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) ...

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

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)

like image 756
Urs Reupke Avatar asked Jul 22 '11 14:07

Urs Reupke


1 Answers

The problem wasn't with the annotations after all, it was a misplaced beans.xml. Putting it in META-INF, as required, helped me to get past this one.

Switching to IntelliJ IDEA helped solve the problem: In it, the log file contained highly visible errors for creating the bean as a @Singleton as well as a @ApplicationScoped.

Another thing that proved very helpful was to try the exact same application in JBoss. Here, it worked, so I was confident I had not misunderstood the basic concepts.

like image 98
Urs Reupke Avatar answered Nov 15 '22 09:11

Urs Reupke