Premise: actually, I'm leveraging this misalignment for implementing a functionality; however, I don't believe is correct behavior accordingly to Java EE 7 specs, hence reporting it.
Executive summary
With reference to attached reproducer, and example scenario below; the @DependsOn annotation on wildfly 8.2.0.Final works also against Stateless EJB, but accordingly to Java EE 7 specs @DependsOn annotation:
Used to express an initialization dependency between singleton components.
Detailed description
Assume you have a @Singleton and while on @PreDestroy you need to persist some entities, which are normally done through a Stateless EJB serving as a "Controller"/"DAO", example:
@Singleton @Startup //@DependsOn(value={"AStatelessEJB"}) public class SingletonEJB { private static final Logger LOG = LoggerFactory.getLogger(SingletonEJB.class); @EJB private AStatelessEJB sl; @PostConstruct public void init() { LOG.info("SingletonEJB is here."); } @PreDestroy public void shutdown() { LOG.info("SingletonEJB is about to shutdown..."); sl.dummyPersist(); // line #30 } }
with the @DependsOn annotation currently commented, and where:
@Stateless(name="AStatelessEJB", mappedName="AStatelessEJB") public class AStatelessEJB { private static final Logger LOG = LoggerFactory.getLogger(AStatelessEJB.class); public void dummyPersist() { LOG.info("called dummyPersist()."); } }
just for simulating a "controller"/"DAO" example as a Stateless EJB.
Invoking wildfly shutdown will produce the following stacktrace:
19:26:34,570 INFO [org.jboss.as] (Controller Boot Thread) JBAS015874: WildFly 8.2.0.Final "Tweek" started in 98671ms - Started 347 of 399 services (107 services are lazy, passive or on-demand) 19:26:45,136 INFO [org.wildfly.extension.undertow] (MSC service thread 1-7) JBAS017535: Unregistered web context: /wildfly82-dependson ... 19:26:45,213 INFO [com.acme.wildfly82_dependson.SingletonEJB] (ServerService Thread Pool -- 24) SingletonEJB is about to shutdown... ... 19:26:45,213 ERROR [org.jboss.as.ejb3.invocation] (ServerService Thread Pool -- 24) JBAS014134: EJB Invocation failed on component AStatelessEJB for method public void com.acme.wildfly82_dependson.AStatelessEJB.dummyPersist(): org.jboss.as.ejb3.component.EJBComponentUnavailableException: JBAS014559: Invocation cannot proceed as component is shutting down ... at com.acme.wildfly82_dependson.AStatelessEJB$$$view2.dummyPersist(Unknown Source) [classes:] at com.acme.wildfly82_dependson.SingletonEJB.shutdown(SingletonEJB.java:30) [classes:] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [rt.jar:1.8.0_05]
However, with the annotation un-commented and active:
19:21:44,713 INFO [org.jboss.as] (Controller Boot Thread) JBAS015874: WildFly 8.2.0.Final "Tweek" started in 109462ms - Started 347 of 399 services (107 services are lazy, passive or on-demand) ... 19:22:17,908 INFO [org.wildfly.extension.undertow] (MSC service thread 1-3) JBAS017535: Unregistered web context: /wildfly82-dependson 19:22:17,971 INFO [org.wildfly.extension.undertow] (MSC service thread 1-8) JBAS017532: Host default-host stopping ... 19:22:18,017 INFO [com.acme.wildfly82_dependson.SingletonEJB] (ServerService Thread Pool -- 68) SingletonEJB is about to shutdown... 19:22:18,033 INFO [com.acme.wildfly82_dependson.AStatelessEJB] (ServerService Thread Pool -- 68) called dummyPersist(). ... 19:22:18,236 INFO [org.jboss.as.server.deployment] (MSC service thread 1-4) JBAS015877: Stopped deployment wildfly82-dependson.war (runtime-name: wildfly82-dependson.war) in 334ms
But I'm puzzled because as stated above the Java EE specs javadoc describe the @DependsOn annotation as Used to express an initialization dependency between singleton components. , therefore although I'm happy it actually works, I'm puzzled about the misalignments, hence reporting it.
Conclusions
I have the following questions, please?
- Can you clarify if this is the correct or intended behavior of the annotation on Wildfly?
- In the case the implementation of this annotation will be put in future release to adhere strictly to the Java EE 7 specs, what should be the correct way to implement the above scenario, in an alternative way?
(If this instead will not be modified, in other words the Wildfly implementation of the annotation will be more wide than the JavaEE 7 specs, then I got nothing to do as the application is already working and sorry for this report, but I thought worthy to mention).