Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to use Seam in a JBoss timed service?

I've started to write some new JBoss timed service that was meant to use some existing seam components. But it seems that I can't access these components because of non-existing contexts. Is it possible to use them other than in the typical situation with JSF?

A little snippet to demonstrate what I want to do...

@Service
public class MyService extends DefaultTimedService implements TimedObject, DefaultServiceInterface {
    @Timeout
    public void ejbTimeout(Timer timer) {
        MyInterface loader = (MyInterface) Component.getInstance(MyInterface.SEAM_NAME, true);
        // throws no context!
    }
}

That throws the following exception for example:

java.lang.IllegalStateException: No application context active
    at org.jboss.seam.Component.forName(Component.java:1945)
    at org.jboss.seam.Component.getInstance(Component.java:2005)
like image 908
Daniel Bleisteiner Avatar asked Jan 22 '23 18:01

Daniel Bleisteiner


2 Answers

There is one way that is a little dirty and there are many developers that would not use such a hack ever but it will solve your problem:

import org.jboss.seam.contexts.Lifecycle;

@Service
public class MyService extends DefaultTimedService implements TimedObject, DefaultServiceInterface {
    @Timeout
    public void ejbTimeout(Timer timer) {
        Lifecycle.beginCall();

        MyInterface loader = (MyInterface) Component.getInstance(MyInterface.SEAM_NAME, true);
        // will not throw no context!
        // also the Component.getInstance(MyInterface.SEAM_NAME, true,true); call
        // is another way you could inject that component. 

        Lifecycle.endCall();
    }
}

I have used it in one project where I couldn't find anything else that worked. If anybody has another solution I am looking forward to seeing it here :).

like image 183
Bobby Avatar answered Jan 25 '23 07:01

Bobby


What scope have you defined for the component? Probably application context as it says so in the error.

...

So I poked around the source and found out that contexts are stored in a class named Contexts. All contexts seems to be thread specific, because they are encapsulated in ThreadLocal-instances. That means that has to specified for thread of the timed service...

The question remains though: how do create a context for a specific thread.

like image 25
Aleksi Yrttiaho Avatar answered Jan 25 '23 07:01

Aleksi Yrttiaho