• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help??(Can we use wait method in Bean Class)

 
Venkateswara Rao
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can we use wait/notify methods in Stateless Session Bean.
 
Chris Mathews
Ranch Hand
Posts: 2712
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Venkateswara Rao:
Can we use wait/notify methods in Stateless Session Bean.

Not if you want to respect the EJB Specification.
Section 24.1.2 of the EJB 2.0 Specification

The enterprise bean must not attempt to manage threads. The enterprise bean must not attempt
to start, stop, suspend, or resume a thread; or to change a thread´┐Żs priority or name. The enterprise
bean must not attempt to manage thread groups.


Application Servers do not typically validate this type of thing, so it may even work. However, I highly recommend you address your problem in a different manner.
If you post back the problem you are trying to solve, then there is a good chance someone here could provide some insight.
[ February 19, 2003: Message edited by: Chris Mathews ]
 
Kyle Brown
author
Ranch Hand
Posts: 3892
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Considering that this would mean you would have to start a thread to make it useful, the answer is no.
Kyle
 
Venkateswara Rao
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your immediate reply.
My Problem is:
I donot want to wait till response comes in my Stateless Session Bean. Communication is Asynchronus.
For that reason i am waiting on Request Key for Predefined time , If Response comes within pre defined time just i am notifying on that RequestKey.
otherwise time occures and i will do appropriate action.
For these reasons i am using wait and notify method.
Is there any way to implement this without using wait and notify methods, Is so please tell me?
Thanks and Regards
Venkateswara Rao
 
Sunil Sahu
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If your EJB container supports EJB2.x Specification then you can implement this asynchronous communication using Message Driven Beans. This is the best way to handle such situation.
 
Venkateswara Rao
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sunil,
I think u missunderstand my problem:
//This is stateless bean
StateLessBean
{
public void sendRequestToMdb(String key,
String request)
{
// code for putting message into JMS QUEUE1
synchronized(key)
{
key.wait(1000);
}

if(flag == false ) {
/* waked up by time. Means time is over. No notify
Call has come. So withing 1000 milliseconds
response has not come. */
// Do Some Action
}

else {//Thread is wakedup by notify call. So Response has come within
1000 mill Seconds

flag= false;
}
}

public void handleResponse(String key, String response) {
synchronized(key) {
flag = true;
key.notify();
}
}
}

This is MDB listening on JMS QUEUE2 (other module is getting request from QUEUE1 and
putting response in this QUEUE2)
MessageDrivenBean {
public void onMessage(Message msg) {
// get Key and Response from msg.
remote.handleResponse(key,response); //invoking Bean method
}
}
I think u understand my problem. Is there any way to implement this logic without using
wait and notify methods
Thanks and Regards
Venkateswara Rao
 
Kyle Brown
author
Ranch Hand
Posts: 3892
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This makes no sense. If you need a response back within a second, then why not just use a synchronous protocol like RMI-IIOP?
Kyle
 
Venkateswara Rao
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Even if it is Sync. call how to handle this type of situation. Some times Sync. call also takes a lot of time to process. But i donot want to wait till call returns.
Thanks and Regards
Venkateswara Rao
 
Chris Mathews
Ranch Hand
Posts: 2712
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Venkateswara Rao:
Even if it is Sync. call how to handle this type of situation. Some times Sync. call also takes a lot of time to process. But i donot want to wait till call returns.

This doesn't make sense to me. If you don't want to block until the call returns, then the process is asynchronous. If you want to block then the process is synchronous. There is nothing in-between.
 
Tim Holloway
Bartender
Posts: 18415
58
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that what Venkat's trying for is to initiate processing but abort it it it takes too long (or at least do something that works along that model).
As I just mentioned in response to another recent post, I personally don't recommend EJBs for long-running processes. And in any event, there's no mechanism in EJB to start an EJB call and then forcibly terminate it from outside. If, on the other hand the idea is to either return the result of a process or a "please check back later" - as opposed to aborting the process, that comes under what I said here:
http://www.coderanch.com/t/311269/EJB-JEE/java/Doing-IO-operations-Session-Bean
 
Venkateswara Rao
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In swings. there is called Timer
In javax.swing.Timer
Each Timer has a list of ActionListeners and a delay (the time between actionPerformed() calls). When delay milliseconds have passed, a Timer sends the actionPerformed() message to its listeners. This cycle repeats until stop() is called, or halts immediately if the Timer is configured to send its message just once.
Is there any welogic API to do same.
In this case My Session Been has to implements ActionListener
Thanks and Regards
Venkateswa Rao
 
lmahesh kumar
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
EJB doesnot support threading in application , as such you cannot use wait method ..

Mahesh L.
 
Dana Hanna
Ranch Hand
Posts: 227
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The second MDB that you have listening to Queue 2 is un-neccesary. In your session bean create a QueueReciever with a message selector (I'm assuming that this key is in the message header - if not, then the first MDB needs to put it there). Then call recieve(long timeout) with the timeout in there. See the JMS API for more info...
YEEHAW!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!