EJB stateless bean injection
freakwave10 Mar 30, 2009 11:56 AMHi all,
I want to inject/utilize a stateless session bean in another stateful session bean.
Use Case: Small wizard that triggers a asynchronous process. The wizard takes some user input, submits a record to a db, and gets the result via a JMS message.
1. start conversation that is handled by SFSB (seam component meAccountWizardBean
)
2. at a certain point, use a SLSB (seam component tibcoJMSTopicSubscriber
) to subscribe to a JMS queue and wait for 30 seconds to get the result. The SLSB is only needed for this one method call, so I guess the ideal scope would be the EVENT scope.
Now, I tried a lot of solution, of which some are working. When I make the SLSB a pojo and inject it into the SFSB it works. But why not using the container to manage the queue receiver bean and just inject it. The only way I got this working is by using the @EJB annotation in the SFSB. Whenever I use @In, it does not work.
Now even @In if it would work, I think is not ideal. I only need this bean during one method call. Using @In to inject a Event scoped bean into a conversation scope bean would be a lot of overhead. During each step of the wizard, the EVENT scoped bean would be injected over and over again.
So my plan is to lookup the bean via the API.
Now here is the solution is would like to get working.
SFSB
@Stateless @Name("tibcoJMSTopicSubscriber") public class TibcoJMSTopicSubscriber implements TibcoJMSTopicSubscriberIf{ String serverUrl = "tcp://aixbomsdev:7222"; String userName = null; String password = null; String queueName = "queue.sample2"; String messageText; public javax.jms.Message subscribe() { // this method subscribes to a queue and waits for 30 seconds to receive a message
SFSB @Stateful @Name("meAccountWizardBean") public class MeAccountWizardBean implements MeAccountWizard { @Logger private Log log; @PersistenceContext EntityManager em; // below code works with a POJO seam component, not with SLSB //@In(create=true) //TibcoJMSTopicSubscriberIf tibcoJMSTopicSubscriber; public String subscribe() { // retreive the SLSB via the Stateless context. TibcoJMSTopicSubscriber tibcoJMSTopicSubscriber = (TibcoJMSTopicSubscriber) Component.getInstance("tibcoJMSTopicSubscriber", ScopeType.STATELESS, true); javax.jms.Message message = tibcoJMSTopicSubscriber.subscribe(); System.out.println("received message: " + message); if (message != null) { ...
In the above scenario I get a CAST exception. I tried all different things, even moving the SLSB to a event context, and getting it from there. I see the bean in the Application Context (debug page), and it is defined with Scope STATELESS
.
As I said, when I use the @EJB it workd, when I use a POJO it works, when I use @In with a SLSB it gives me NULL
.
I read all the books, and all say it is not a problem to use SLSB and inject them. Well not for me ;-). The project is a seam-gen, running on JBOSS 4.2.2. Latest seam version.
Thanks all for your help.