4

I'm working on a large Java EE 6 application that is deployed on JBoss 6 Final. My current tasks involve using @Inject consistently instead of @EJB, but I'm running into some problems on some types of beans, specifically @MessageDriven beans and beans with @Scheduled methods.

What happens is that if I'm unlucky with the timing (for @Schedule) or if there are messages in the MDBs' queues at startup, instantiation of the beans will fail because the injected resources (which are EJBs themselves) are not bound yet.

Because I use @Inject, I'm guessing that the EJB container considers my beans to be ready, since the container itself does not care about @Inject; it probably simply assumes that since there are no @EJB injections, the beans are ready for use. The injected CDI proxies will then fail because the resources to inject aren't actually bound yet.

Tiny example:

@Stateless
@LocalBean
public class MySupportingBean {

    public void doSomething() {
        ...
    }
}

@Singleton
public class MyScheduledBean {

    @Inject
    private MySupportingBean supportingBean;

    @Schedule(second = "*/1", hour = "*", minute = "*", persistent = false)
    public void onTimeout() {
        supportingBean.doSomething();
    }
}

The above example will probably not fail often because there are only two beans, but the project I'm working on binds lots of EJBs, which will amplify the problem. But it might fail because there is no guarantee that MySupportingBean is bound first, and if onTimeout is invoked before MySupportingBean is bound, then instantiation of MyScheduledBean will fail. If I used @EJB instead, MyScheduledBean wouldn't be bound until the dependency to MySupportingBean was satisfied.

Note that the example will not fail in onTimeout itself, but when CDI attempts to inject MySupportingBean.

I've read a lot of posts on different forums where many people argue that @Inject is always better. Generally, I agree, but how do they handle @Schedule or @MessageDriven combined with @Inject? In my experience, it comes down to dumb luck whether the beans will work or not in those cases, and the beans will fail arbitrarily, depending on which order the EJBs are deployed in, and when @Schedule or onMessage are invoked.

MaDa
  • 10,511
  • 9
  • 46
  • 84

1 Answers1

0

It might help to explicitly define the dependencies using the JBoss proprietary annotation @Depends or using the jboss.xml file.

See this for a somewhat similar question: How to order deployment of EJBs and JMS queue config in JBoss 5?

Community
  • 1
  • 1
Arjan Tijms
  • 37,782
  • 12
  • 108
  • 140
  • I have considered that possibility, but being application server agnostic is kind of important; for political reasons far above me, we're likely to switch to another app server in the near future. Besides, should we really have to depend on proprietary annotations to make a generic framework work as expected? – Nils-Petter Nilsen Jan 10 '11 at 07:26
  • 1
    You're right, we shouldn't. But when push comes to shove a practical solution is sometimes necessary. When using the xml file, you might be able to include similar files for all other major servers (do these other servers even have this problem btw?). I do agree it's an ugly solution and hope someone else is able to provide a standard compliant answer. – Arjan Tijms Jan 10 '11 at 22:00
  • I guess you're right. It's not the answer I was hoping for, but of course we have to be pragmatic. Of the two evils, I think the XML file is the lesser one - at least that way I won't introduce JBoss-specific annotations in the source code. And we have some deployment descriptors for JBoss already, which must of course be replaced with a new app server anyway. We're likely to move to WebSphere at some point in the near future, but we haven't started testing it yet. It'll be interesting to see if WebSphere shows similar behaviour. – Nils-Petter Nilsen Jan 13 '11 at 07:16
  • Indeed. You can check today though whether Glassfish also has this behavior. About the near future, I hope that this future will indeed be near. Although there is a beta of Websphere 8, no release date has been disclosed. A conservative guess would place a final release somewhere late 2012. – Arjan Tijms Jan 14 '11 at 11:38