Multiple @Singleton Instances
b.eckenfels Sep 22, 2011 7:28 AMIf I deploy my Test MessageDrivenBean on 7.0.1 standalone-preview profile with a @EJB dependency to a @Singleton, I can see, that the Singleton class (constructor) is called on each instanciation of a MDB, therefore I have more than one java class, not a real singleton.
However the postContruct() Method is only called for the first Singleton.
{code}
13:15:37,864 INFO [stdout] (Thread-6049 (group:HornetQ-client-global-threads-3178554)) EJB3Singleton() Startup...13084479
13:15:37,864 INFO [stdout] (Thread-6049 (group:HornetQ-client-global-threads-3178554)) EJB3MDB.initialize()
13:15:37,864 INFO [stdout] (Thread-6049 (group:HornetQ-client-global-threads-3178554)) onMessage() START
13:15:37,895 INFO [stdout] (Thread-6049 (group:HornetQ-client-global-threads-3178554)) EJB3Singleton() Startup...23965508
13:15:37,895 INFO [stdout] (Thread-6049 (group:HornetQ-client-global-threads-3178554)) EJB3Singleton() Post Construct...23965508
13:15:37,958 INFO [stdout] (Thread-6049 (group:HornetQ-client-global-threads-3178554)) EJB3Singleton() returning hash....23965508
13:15:37,958 INFO [stdout] (Thread-6049 (group:HornetQ-client-global-threads-3178554)) my singleton: 23965508
13:15:39,958 INFO [stdout] (Thread-6049 (group:HornetQ-client-global-threads-3178554)) onMessage() DONE
13:16:05,381 INFO [stdout] (Thread-6031 (group:HornetQ-client-global-threads-3178554)) onMessage() START
13:16:05,396 INFO [stdout] (Thread-6031 (group:HornetQ-client-global-threads-3178554)) EJB3Singleton() returning hash....23965508
13:16:05,396 INFO [stdout] (Thread-6031 (group:HornetQ-client-global-threads-3178554)) my singleton: 23965508
13:16:07,396 INFO [stdout] (Thread-6031 (group:HornetQ-client-global-threads-3178554)) onMessage() DONE
13:16:11,162 INFO [stdout] (Thread-6050 (group:HornetQ-client-global-threads-3178554)) onMessage() START
13:16:11,162 INFO [stdout] (Thread-6050 (group:HornetQ-client-global-threads-3178554)) EJB3Singleton() returning hash....23965508
13:16:11,162 INFO [stdout] (Thread-6050 (group:HornetQ-client-global-threads-3178554)) my singleton: 23965508
13:16:11,303 INFO [stdout] (Thread-6051 (group:HornetQ-client-global-threads-3178554)) EJB3Singleton() Startup...26566365
13:16:11,303 INFO [stdout] (Thread-6051 (group:HornetQ-client-global-threads-3178554)) EJB3MDB.initialize()
13:16:11,303 INFO [stdout] (Thread-6051 (group:HornetQ-client-global-threads-3178554)) onMessage() START
13:16:11,303 INFO [stdout] (Thread-6051 (group:HornetQ-client-global-threads-3178554)) EJB3Singleton() returning hash....23965508
13:16:11,303 INFO [stdout] (Thread-6051 (group:HornetQ-client-global-threads-3178554)) my singleton: 23965508
13:16:13,162 INFO [stdout] (Thread-6050 (group:HornetQ-client-global-threads-3178554)) onMessage() DONE
13:16:13,303 INFO [stdout] (Thread-6051 (group:HornetQ-client-global-threads-3178554)) onMessage() DONE
{code}
As you can see in 13:15:37,864; 13:15:37,895 and 13:16:11,303 a new Java Class (constructor) was created, but only the second instance have been "postConstructed" and injected. Is this a problem with the dependency injector? The EJB 3.1 spec is not clear if the container is actualy allowed to instanciate and default-construct more than one singleton instance.
Singleton looks like:
{code}
@Singleton
@LocalBean
@ConcurrencyManagement(ConcurrencyManagementType.CONTAINER)
//@Startup
@Lock(LockType.READ)
public class EJB3Singleton
{
public EJB3Singleton() {
System.out.println("EJB3Singleton() Startup..." + this.hashCode());
}
@PostConstruct
public void postConstruct() {
System.out.println("EJB3Singleton() Post Construct..." + this.hashCode());
}
@PreDestroy
public void preDestroy() {
System.out.println("EJB3Singleton() Pre Destroy..." + this.hashCode());
}
public int getHash() {
System.out.println("EJB3Singleton() returning hash...." + this.hashCode());
return this.hashCode();
}
}
{code}
Markup fix: Bernd Eckenfels