Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

With Bean-Managed Concurrency, annotated as @ConcurrencyManagement(BEAN), the container sends all invocations into the bean and let's lets the Singleton bean instance decide how and when to synchronize access, if at all. Here the 'synchronization' keyword is allowed as well as the full javax.util.concurrent set of libraries.

...

Startup and Startup Ordering

Singletons have an @Startup annotation which can be applied to the bean class. When used, the Container will instantiate the Singleton instance eagerly when the application starts up, otherwise the Container will instantiate the Singleton instance lazily when the bean is first accessed.

If one Singleton refers to another Singleton in the @PostConstruct or @PreDestroy method, there must be some measure taken to ensure the other Singleton exists and is started. This sort of ordering is achieved with the @DependsOn annotation which can be used to list the names of Singleton beans that must be started before the Singleton bean using the annotation.

Code Block

@DependsOn({"SingletonB", "SingletonC"})
@Singleton
public class SingletonA {

}

Circular references are not supported. If BeanA uses @DependsOn to point to BeanB and BeanB also uses @DependsOn to point at BeanA, the result is a deployment exception. Be aware that circular references can happen in less trivial ways such as A referring to B which refers to C which refers to D which refers back to A. We will detect and print all circular dependencies (called circuits) at deploy time.

Note that @DependsOn is only required (and should only be used) if a Singleton uses another Singleton in its @PostConstruct method or @PreDestroy method. Simply having a reference to another Singleton and using it in other business methods does not require an @DependsOn declaration. The @DependsOn allows the Container to calculate the correct startup order and shutdown order so that it can guarantee the Singletons you need are available in your @PostConstruct or @PreDestroy methods. All Singletons will automatically be available to your business methods regardless if @DependsOn is used. Because of the greater chance of creating circular dependencies, it is better not to use the @DependsOn annotation "just in case" and should only be used when truly needed.

Example Code

Wiki Markup
{snippet:url=openejb3/examples/simple-singleton/src/main/java/org/superbiz/registry/ComponentRegistryBean.java|lang=java}

...