问题
If I have a request scoped CDI bean injected into a @MessageDriven
EJB using JMS, as below, can I assume that any given Foo
instance will only be used by a single onMessage
invocation at a time?
In other words, in the below example, can I safely use member variables in the Foo
object to store state across subroutines, analogously to a JSF @RequestScoped
managed bean?
Note that it's ok if the same Foo
object gets recycled sequentially from one onMessage
call to the next, as long as each MessageDrivenBean
instance has its own Foo
instance such that two requests processing concurrently would be isolated.
@MessageDriven
public class MessageDrivenBean implements MessageListener {
@Inject
private Foo foo;
public void onMessage(Message m) {
foo.doSomething();
}
}
@Named
@RequestScoped
public class Foo {
private String property;
public void doSomething() {
property = ...;
}
}
回答1:
WRT the Request scope / context, the CDI spec in section 6.7.1 says that it will be active for a message driven bean implementing MessageListener. It is also destroyed after the delivery of the message, so you'll have a new instance for each message delivered. Further, section 6.7.3 states that the Application context is also active (as one would expect). Conversation and session scopes are not active.
回答2:
I wonder if this will work. What kind of protocol do you intend to use with the MDB?
MDBs are nearly always invoked asynchronously (e.g. via JMS), so there's no notion of any active request when onMessage()
is being called. Typically MDBs are also required to implement an interface matching the protocol they're listening to (e.g. for JMS the MDB needs to implement javax.jms.MessageListener
).
来源:https://stackoverflow.com/questions/8526447/requestscoped-cdi-injection-into-messagedriven-bean