+

Search Tips   |   Advanced Search

Develop singleton session beans

Create a bean implementation class for a singleton session bean, introduced by the EJB 3.1 specification. The EJB container initializes only one instance of a singleton session bean, and that instance is shared by all clients. Because a single instance is shared by all clients, singleton session beans have special life cycle and concurrency semantics.

Make sure that you understand the inheritance rules for each annotation you implement. For example, the @ConcurrencyManagement annotation is coded on the singleton session bean class only. We cannot use the @ConcurrencyManagement annotation in the class that it extends, or any class higher in the class inheritance tree.

Singleton session beans can have business local, business remote, and web service client views; they cannot have EJB 2.1 local or remote client views. This singleton session bean support replaces the proprietary startup bean functionality, which has been deprecated.

The following example shows a basic singleton session bean:

public interface Configuration {
 Object get(String name);
 void set(String name, Object value);
}

@Singleton
public class ConfigurationBean implements Configuration {
 private Map<String, Object> settings = new HashMap<String, Object>();

 public Object get(String name) {
  return settings.get(name);
 }

 public void set(String name, Object value) {
  settings.put(name,value);
 }
}

As with other enterprise bean types, we can also declare metadata for singleton session beans in the deployment descriptor rather than using annotations; for example:

<?xml version="1.0"?>
<ejb-jar
  xmlns="http://java.sun.com/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_1.xsd"
  version="3.1"
>
  <enterprise-beans>
    <ejb-name>ConfigurationBean</ejb-name>
    <business-local>com.ibm.example.Configuration</business-local>
    <ejb-class>com.ibm.example.ConfigurationBean</ejb-class>
    <session-type>Singleton</session-type>
  </enterprise-beans>
</ejb-jar>


Subtopics


Related tasks

  • Use asynchronous beans
  • Enable an application to wait for a messaging engine to start

  • Startup beans service settings