Eruanno
Eruanno

Reputation: 35

EJB Singleton service fails at deploy

I'm rather newbie to JBoss and annotations. I have following code example. Irrelevant details are cutted out.

@Singleton
@Startup
public class SomeBean {

    @Resource
    TimerService timerService;

    @Inject
    AnotherSingleton anotherOne;

    Timer timer;

    @PostConstruct
    private void ejbCreate() {
        timer = timerService.createIntervalTimer(0, interval, tc);
    }

    @Timeout
    public void run() throws Exception {
    }
}

@Singleton
public class AnotherSingleton {

    @Inject
    Repository rep;
}

There is case that when war is deploying on JBoss it fails with exception from Repository producer (service in another Jboss is not available).

Caused by: java.lang.IllegalStateException: WFLYEE0042: Failed to construct component instance

So process ends with

WFLYCTL0186:   Services which failed to start:      service jboss.deployment.unit."someservices-view.war".component.SomeBean.START

What options do i have? Can i tell JBoss to don't @Inject beans on startup but when code is executed by timer? Can i somehow catch exception? @Schedule is out of question becaouse i need to configure Timer.

Upvotes: 1

Views: 2127

Answers (1)

Gimby
Gimby

Reputation: 5284

Injections are handled by the CDI specification which provides a feature to "wrap" injections as it were, like so.

@Inject
Instance<AnotherSingleton> anotherOneInstance;

This basically creates a proxy around the AnotherSingleton and you can delay obtaining an actual reference to it at the time that you need it.

AnotherSingleton anotherOne = anotherOneInstance.get();

This should allow deployment to succeed and your timer to initialize, but of course if at the moment you attempt to use anotherOne and the repository is not available, the code will still break with an exception.

Alternatively, you can always do a manual lookup through the BeanManager to not have to rely on any form of dependency injection, but that should always be a last resort as it just leads to cumbersome code.

Upvotes: 1

Related Questions