How to inject ApplicationScoped bean in Stateless bean?

ShirouWrath

I have Stateless bean that calls asynchronous operation. I would like to inject to this bean my another bean, which stores (or rather should store) running process status. Here is my code:

Processor:

@Stateless
public class Processor {

    @Asynchronous
    public void runLongOperation() {
        System.out.println("Operation started");
        try {
            for (int i = 1; i <= 10; i++) {
                //Status update goes here...
                Thread.sleep(1000);
            }
        } catch (InterruptedException e) {
        }
        System.out.println("Operation finished");
    }

}

ProcessorHandler:

@ManagedBean(eager = true, name="ph")
@ApplicationScoped
public class ProcessorHandler implements RemoteInterface {

    public String status;

    @EJB
    private Processor processor;

    @PostConstruct
    public void init(){
        status = "Initialized";
    }

    @Override
    public void process() {
        processor.runLongOperation();
    }

    public String getStatus() {
        return status;
    }


}

Process method of ProcessHandler is bound to a button. I would like to modify status of ProcessHandler bean from inside of Processor, so I can display updated status to user.

I tried to use @Inject, @ManagedProperty and @EJB annotations, but without success.

I'm testing my soulution on Websphere v8.5 developed using Eclipse EE.


When added inject to Processor class...

@Inject
public ProcessorHandler ph;

I got error:

The @Inject java.lang.reflect.Field.ph reference of type ProcessorHandler for the <null> component in the Processor.war module of the ProcessorEAR application cannot be resolved.
BalusC

You should never have any client-specific artifacts (JSF, JAX-RS, JSP/Servlet, etc) in your service layer (EJB). It makes the service layer unreusable across different clients/front-ends.

Simply move private String status field into the EJB as it's actually the one responsible for managing it.

@ManagedBean(eager = true, name="ph")
@ApplicationScoped
public class ProcessorHandler implements RemoteInterface {

    @EJB
    private Processor processor;

    @Override
    public void process() {
        processor.runLongOperation();
    }

    public String getStatus() {
        return processor.getStatus();
    }

}

Note that this won't work on a @Stateless, but on @Singleton or Stateful only for the obvious reasons.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Inject Singleton Session Bean into a Stateless Session Bean

From Dev

How can I inject a stateless session bean into a servlet?

From Dev

How can I inject a stateless session bean into a servlet?

From Dev

Arquillian can't @Inject Stateless bean

From Dev

@Inject'ed stateless bean returns null but @EJB not

From Dev

Inject a Stateless EJB in a SessionScoped CDI Bean

From Dev

Inject @SessionScoped CDI Bean to @Stateless EJB

From Dev

@Inject failing when injecting in a @Stateless bean

From Dev

Arquillian can't @Inject Stateless bean

From Dev

How to inject one bean to a set of bean in spring?

From Dev

How to inject raw Class to bean

From Dev

How to inject bean in a Tapestry service

From Dev

How to inject raw Class to bean

From Dev

How to inject bean in a Tapestry service

From Dev

@Named bean not injected into @Stateless bean

From Dev

How to get logged in user stateless bean

From Dev

How to get logged in user stateless bean

From Dev

Scope of Stateless Bean

From Dev

Stateless session bean transactions

From Dev

Scope of Stateless Bean

From Dev

Inject Spring bean in groovy bean

From Dev

How to inject a prototype bean in a Spring singleton Controller

From Dev

How to inject a bean in this MongoDB connection case?

From Dev

How to correct inject map of bean in spring context

From Dev

How to inject a @Named bean into a Junit test

From Dev

Spring: How to inject a String bean to the constructor?

From Dev

How to inject fields in bean-methods?

From Dev

How to inject bean validator automatically enabled by JPA?

From Dev

How to inject a @Named bean into a Junit test