• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

use temporary queue to get return value from onMessage() and how to pick it up

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 CMP 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.
Chris from Javaranch suggested me to use temporary queue to get the return value from the servlet. Then I created a 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 Boolean or interger value from onMessage() to the servlet. There are two questions:
How can I send back the value from onMessage() method?
How can I pick it up in the servlet?
Please do not question the design. This is an assignment I have no control. Thanks in advance for your help.
 
author
Posts: 3892
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, first of all the LogBean that you use in the servlet should NOT be a MessageDriven bean. That's your problem -- you don't want an asynchronous notification, you want a synchronous notification. So don't do a setMessageListener(). Instead, just let the QueueReceiver do a receive(timeoutvalue) and it will receive the message sent back from the MDB that wraps the CMP.
BTW, the MDB that wraps the CMP will have to take the queue reference out of the reply-to-queue and then do a send() to it to send you your reply. So, things will work like this:
(1) Send request message to Queue MDB is listening on.
(2) do a synchronous receive() on the reply-to-queue (in other words, wait...)
(3) MDB receives the message from its Q
(4) MDB calls the CMP
(5) MDB writes the response to the reply-to-Q
(6) Servlet receives the message when the receive() message on the QueueReceiver comes back.
(7) Servlet processes the message.
Does this help?
Kyle
 
Joe qiang
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot for your reply. It is very clear to me what I need to do.
Here is partial code of the servlet. I compiled it and there is no error:
// Initialize JNDI
Context ctx = new InitialContext(System.getProperties());
// 1: Lookup ConnectionFactory via JNDI
TopicConnectionFactory factory =
(TopicConnectionFactory)
ctx.lookup("javax.jms.TopicConnectionFactory");
// 2: Use ConnectionFactory to create JMS connection
TopicConnection connection =
factory.createTopicConnection();
// 3: Use Connection to create session
TopicSession session = connection.createTopicSession(
false, Session.AUTO_ACKNOWLEDGE);
// 4: Lookup Desintation (topic) via JNDI
Topic topic = (Topic) ctx.lookup("logtopic");
// 5: Create a Message Producer
TopicPublisher publisher = session.createPublisher(topic);
// 6: Create a text message, and publish it
TextMessage msg = session.createTextMessage();
// 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);
queueConnection.start();
userName = request.getParameter("userName");
userPw = request.getParameter("Password");
if (userName.equals("")||userPw.equals("")){
response.sendRedirect("/CourseWebApp/login.jsp?message=novalue");
}
else{
msg.setText(userName+"$"+userPw);
msg.setJMSReplyTo(replyQueue);
publisher.publish(msg);
session.commit();
Message inMessage = (Message)qReceiver.receive();
System.out.println("message in: "+ inMessage.getStringProperty("match"));

In the onMessage() method, here is code:
protected MessageDrivenContext ctx;
protected int match = 0;
public void setMessageDrivenContext(MessageDrivenContext ctx) {
this.ctx = ctx;
}
public void onMessage(Message msg) {
try {
TextMessage tm = (TextMessage) msg;
String text = tm.getText();
System.out.println("message is: " + text);
String user1Name ="";
String user1Pw = "";
//String patternStr = "$";
StringTokenizer user1 = new StringTokenizer(text,"$");
int count = 1;
while (user1.hasMoreTokens()){
if (count == 1){
user1Name = user1.nextToken();
count++;
}
else {
user1Pw = user1.nextToken();
}
}
System.out.println("reach context: ");
ctx2 = new InitialContext(System.getProperties());
LoginHome home = (LoginHome)
PortableRemoteObject.narrow(
ctx2.lookup("LoginHome"), LoginHome.class);
System.out.println("find loginHome: ");
Login login = home.findByPrimaryKey(user1Name);
match = login.verify(user1Name, user1Pw);
System.out.println("match is: " + match);

// 1: Lookup ConnectionFactory via JNDI
ConnectionFactory cF = (ConnectionFactory) ctx.lookup("javax.jms.ConnectionFactory");
// 2: Use ConnectionFactory to create JMS connection
Connection connection = cF.createConnection();
// 3: Use Connection to create session
Session session2 = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Message msg2 = session2.createMessage();
Queue replyQueue = (Queue)msg2.getJMSReplyTo();
MessageProducer producer = session2.createProducer(replyQueue);

if (match==1){
msg2.setStringProperty("match","1");
}
else{
msg2.setStringProperty("match","0");
}
}
catch(JMSException e) {
e.printStackTrace();
}
catch(Exception e) {
e.printStackTrace();
}
finally {
producer.send(msg2);
session2.commit();
}
}

When I compiled it, there are 7 error message. I checked JMS API, they are fine to me. Why does the compiler complain?
src\LogBean.java:106: cannot resolve symbol
symbol : method lookup (java.lang.String)
location: interface javax.ejb.MessageDrivenContext
ConnectionFactory cF = (ConnectionFactory) ctx.lookup("javax.jms.ConnectionF
actory");
^
src\LogBean.java:109: cannot resolve symbol
symbol : method createConnection ()
location: interface javax.jms.ConnectionFactory
Connection connection = cF.createConnection();
^
src\LogBean.java:111: cannot resolve symbol
symbol : method createSession (boolean,int)
location: interface javax.jms.Connection
Session session2 = connection.createSession(false, Session.AUTO_ACKNOWLEDGE)
;
^
src\LogBean.java:115: cannot resolve symbol
symbol : method createProducer (javax.jms.Queue)
location: interface javax.jms.Session
MessageProducer producer = session2.createProducer(replyQueue);
^
src\LogBean.java:143: cannot resolve symbol
symbol : variable msg2
location: class examples.LogBean
producer.send(msg2);
^
src\LogBean.java:143: cannot resolve symbol
symbol : variable producer
location: class examples.LogBean
producer.send(msg2);
^
src\LogBean.java:144: cannot resolve symbol
symbol : variable session2
location: class examples.LogBean
session2.commit();
^
7 errors
Could you please help me take a look at the code and let me know whether I am in the right track? Many thanks.
 
reply
    Bookmark Topic Watch Topic
  • New Topic