Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Note
EJB 3.1
EJB 3.1

The EJB 3.1 specification is not yet final and all API classes and documentation listed here are subject to change.
Since OpenEJB 3.1.

Singleton Overview

For the first time in years EJB has a new bean type, the @Singleton. In my opinion, the javax.ejb.Singleton will replace a lot of what people are using @Stateless for today.

The Singleton is essentially what you get if you take a Stateless bean and adjust the pool size to be exactly 1 resulting in there being exactly one instance of the Singleton bean in the application which can be invoked concurrently by multiple threads, like a servlet. It can do everything a Stateless can do such as support local and remote business interfaces, web services, security, transactions, and more. Additionally, the Singleton can get have its @PostConstruct method called with the application starts up and its @PreDestroy method called when the application shuts down. This allows it to serve as an application lifecycle listener which is something only Servlets could do before. It has an @Startup annotation which is similar in concept to the servlet <load-on-startup>, but unlike servlets it doesn't take a number as an argument. Instead, you can use an @DependsOn annotation to say which other Singletons you need and the container will ensure they start before you.

See the Singleton Example for sample bean code and client.

Concurrency

Singletons support two modes of concurrent access, Container-Managed Concurrency (the default) and Bean-Managed Concurrency.

...