Not sure I get all of it, but in essence the container maintains a pool of
Stateless instances.
Whenever you do an invocation on a Stateless bean, the container borrows an
instance out of the pool. If no instance is available, the container can
lazily create a one-time usage bean, or wait until an instance becomes
available.

Whatever happens, the instance offered by the container to perform the
invocation can't be simultaneously used.
Similarly for the lifecycle methods such as @PostConstruct or @PreDestroy

Of course, this is for stateless session beans. For singleton beans, a
method can be simultaneously called by multiple threads.

Hope it helps
--
Jean-Louis Monteiro
http://twitter.com/jlouismonteiro
http://www.tomitribe.com


On Sun, Jan 30, 2022 at 5:42 PM Ed Slavich <[email protected]>
wrote:

> Hi Jean-Louis, thanks for your reply.  Here's a concrete example:
>
> @Stateless
> public class QuartzService {
>     @Inject
>     private ServletContext servletContext;
>
>     private volatile Scheduler scheduler;
>
>     @PostConstruct
>     public void initialize() {
>         scheduler = getScheduler();
>     }
>
>     private Scheduler getScheduler() {
>         try {
>             return ((StdSchedulerFactory) servletContext.getAttribute(
>                     QuartzInitializerListener.QUARTZ_FACTORY_KEY)
>             ).getScheduler();
>         } catch (final Exception e) {
>             throw new RuntimeException("Failed to obtain instance of
> Quartz Scheduler", e);
>         }
>     }
> }
>
> We need to wait until the ServletContext is available before retrieving
> the scheduler, but since I'm assigning a variable in a multithreaded
> environment I'm not sure if there will be memory visibility issues, or if
> TomEE has some way of making sure that the bean's variables are
> synchronized before putting it into service.
>
> -- Ed
>
> > On Jan 24, 2022, at 3:42 AM, Jean-Louis Monteiro <
> [email protected]> wrote:
> >
> > Hi,
> >
> > What's the goal?
> > What are you trying to solve with it?
> > --
> > Jean-Louis Monteiro
> > http://twitter.com/jlouismonteiro
> > http://www.tomitribe.com
> >
> >
> > On Mon, Jan 17, 2022 at 12:33 AM Ed Slavich <
> [email protected]>
> > wrote:
> >
> >> Hello,
> >>
> >> When a bean instance variable is set in a @PostConstruct method, does
> the
> >> variable need to be declared volatile?  For example:
> >>
> >> @Stateless
> >> public class SomeBean {
> >>    private volatile Foo foo;
> >>
> >>    @PostConstruct
> >>    private void initialize() {
> >>        foo = new Foo();
> >>    }
> >> }
> >>
> >> Or does TomEE provide some sort of guarantee that makes volatile
> >> unnecessary?
> >>
> >> Thanks,
> >> Ed
>
>

Reply via email to