简体   繁体   中英

In EJB 3.0 , PostConstruct not called

I want to have a scheduled job run once every hour, the scheduled job is defined in EJB 3.0 stateless session bean (the bean class is annotated with @Stateless). I am restricted to EJB 3.0 so I cannot use @Singleton/@Schedule. The issue is that @PostConstruct annotated method to initialize the timer defined in that EJB 3.0 session bean is never called.

My timer method that is supposed to run once every hour is annotated with @Timeout in the stateless session bean.

Reading other questions it says in EJB 3.0, session beans are lazily created, ie. unless the bean is called from code outside the bean class, PostConstruct annotated method is never called. How do I call the bean from outside the bean and make sure I call only one time when the Tomee EJB container is deployed?

The bean just runs a method once every hour. There is no need to call any method in the bean either locally or remotely.

It's possible to create a stateless container that has minSize and maxSize set to 1. The result should be that TomEE will eagerly fill your pool to the minimum size and not exceed the desired maximum size.

You probably don't want that setup for all your stateless beans, however, so you'll also need to explicitly declare a stateless container that uses default settings.

<Container id="Default Stateless Container" type="STATELESS">
</Container>

<Container id="pizza" type="STATELESS">
    maxSize = 1
    minSize = 1
</Container>

I've called the second container pizza rather than something descriptive like singletons just so it's clear the name is not any kind of magic -- use any name you like. Note the order of the container declarations is significant. When there are two stateless containers, TomEE will by default put all the stateless beans in the first container unless you specify otherwise.

To ensure your bean goes into the second container you'll want to an openejb-jar.xml file in the META-INF/ folder of that bean's jar. For example, if the bean class is com.example.Peperoni then you could put it into the pizza container like so:

<?xml version="1.0"?>
<openejb-jar>
    <ejb-deployment ejb-name="Peperoni"
         deployment-id="Peperoni"
         container-id="pizza"/>
</openejb-jar>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM