Java Reference
In-Depth Information
Developing singleton EJBs
As the name implies, javax.ejb.Singleton is a session bean that guarantees
that there is one instance in the application, at most.
Note
Besides this, singleton EJBs fill up a well-known gap in EJB applications, that is,
the ability to have an EJB that is notified when the application starts and also when
the application stops. So, you can do all sorts of things with an EJB that you previ-
ously could only do with a load-on-startup servlet. It also gives you a place to hold
data that pertains to the entire application and all the users using it, without the
need for static class fields.
In order to turn your EJB into a singleton, all that is needed is to apply the @
javax.ejb.Singleton annotation on top of it.
Note
A singleton bean is similar to a stateful bean in that state information is maintained
across method invocations. However, there is just one singleton bean for each
server JVM, and it is shared by all of the EJBs and clients of an application. This
type of bean provides a convenient means of maintaining the overall state of an
application.
Another annotation that is worth learning is @javax.ejb.Startup that causes the
bean to be instantiated by the container when the application starts. This invokes the
@javax.annotation.PostConstruct annotation if you have defined one in your
EJB.
We now have enough information to understand our first EJB example. There is more
than one alternative for creating a Java Enterprise project. In the earlier chapter, we
have illustrated how to start from a project based on Eclipse JEE (dynamic web pro-
ject), binding it later to a JBoss runtime installation. That's obviously the simplest
Search WWH ::




Custom Search