I have a servlet to process the login request. The user name and password are passed to one MessageDrivenBean from the servlet. And from onMessage() method in the MessageDrivenBean I call Login BMP bean to verify the login information. If Login information exists in the database, return true, otherwise return false. Now this return value is in onMessage() method in the MessageDrivenBean. However, onMessage() has return type void, how can I return a value from here to the original servlet or I can directly call sentRedirect inside onMessage() and go to the main page? Please do not question this design. This is assignment from the instructor. Thanks in advance for your help.
This is really a misuse of the technology. You are being asked to use an asynchronous process to mimic a synchronous call. Personally, I think the assignment is contrived but I understand that you have no control of this. What you are going to want to do is actually fairly simple. Have your servlet create a temporary Queue and set this as the replyTo Destination on the JMS Message. After sending the JMS message your servlet will need to block for a message on this temporary Queue. Your MDB will then process the message and dump its reponse, in the form of another JMS Message, in the replyTo queue specified in the original message. The servlet will then pick up the response.
Just back tracking for one moment, are you sure that you have to use a message-driven bean and not a session bean? Simon
posted 17 years ago
Hi Chris, Now I have temporary queue in the servlet like the following: // 1: Lookup ConnectionFactory via JNDI QueueConnectionFactory queueConnectionFactory = (QueueConnectionFactory) ctx.lookup("javax.jms.QueueConnectionFactory"); // 2: Use ConnectionFactory to create JMS connection QueueConnection queueConnection = queueConnectionFactory.createQueueConnection(); // 3: Use Connection to create session QueueSession qSession = queueConnection.createQueueSession( false, Session.AUTO_ACKNOWLEDGE); Queue replyQueue = qSession.createTemporaryQueue(); QueueReceiver qReceiver = qSession.createReceiver(replyQueue); qReceiver.setMessageListener(new LogBean()); queueConnection.start(); MapMessage m = null; m = tsession.createMapMessage(); m.setJMSReplyTo(replyQueue); Here LogBean is the messageDrivenBean. I have to return one interger value from onMessage() to the servlet. There are two questions: How can I send back the interger value from onMessage() method? How can I pick it up in the servlet? Many thanks.