4

Is it legal to inject a @Stateful into an MDB?

@Stateful
public class InteruptBean implements Interrupt {
    ....
}

@MessageDriven(...)
public class EchoTrigger implements MessageListener {
    @EJB Interrupt interrupt;

    ....
}

Or better phrased: Can I use a stateful EJB to pass state around in an asynchronous Event Driven Architecture?

MaDa
  • 10,511
  • 9
  • 46
  • 84
Jonathan S. Fisher
  • 8,189
  • 6
  • 46
  • 84

2 Answers2

4

Yes it does not make sense. Because stateful session beans are meant for processing multiple requests from same client so that they have client-actions oriented processing. In this case MDB will be beans clients. MDB supports single request model. A request comes to MDB (in form of message) and it is processed. So both types of beans don't match in processing model.

ag112
  • 5,537
  • 2
  • 23
  • 42
2

Yes, it's "legal", but it's nonsensical. MDBs instances are pooled like SLSBs. The MDB will become non-functional after the SFSB times out.

It might work to explicitly create the SFSB at some point, and then pass a reference to the SFSB in the messages being sent to drive the MDB.

Brett Kail
  • 33,593
  • 2
  • 85
  • 90
  • To get around this, I found this syntax: @Resource private MessageDrivenContext context; Interrupt interrupt = (Interrupt) context.lookup("ejb/myStateful"); – Jonathan S. Fisher Jun 30 '11 at 14:59