Ok this post to say that I found the part of an answer:
Here is the code !
The Blocking call is this package test.lang;
import org.apache.log4j.Category;
public class Blocking
{
public static final String MESSAGE
= "This is the message to access in the Blocking call";
public static final int MAX_TIME = 2500;
Category log;
public Blocking()
{
log = Category.getInstance (Blocking.class);
}
public String call (int aDelay)
{
log.info ("Blocking call");
try
{
Thread.sleep (aDelay);
}catch (Exception e)
{
log.warn ("Thread was Interrupted");
}
return MESSAGE;
}
public String call ()
{
return this.call (MAX_TIME);
}
}
Now that this is defined, I made a threaded version of it (Wrapper class).package test.lang;
public class BlockingThread
extends Thread
{
public Blocking blk = null;
public Object returnedValue = null;
BlockingThread ()
{
blk = new Blocking ();
}
public void run ()
{
returnedValue = blk.call ();
}
public Object getValue()
{
return returnedValue;
}
}
Finally the hardest the call: /**
* Who ever wants to use this code could allegdly place the following
* line :
* Based upon an idea of Duffy JohnJ
*
* as posted on yahoogroups Advanced Java mailing list
* SomeType callBlockingMethod(Blocking obj, long timeout)
* {
* Thread t = new BlockingThread(obj);
* t.start();
* t.join(timeout);
* if (t.isAlive())
* {
* return null;
* } else
* {
* return t.someValue();
* }
* }
*
*/
package test.lang;
import org.apache.log4j.Category;
import org.apache.log4j.BasicConfigurator;
public class NonBlockingWrapper
{
public Object callBlockingMethod(long timeout)
{
BlockingThread t = new BlockingThread();
t.start();
Thread.currentThread ().yield ();
try
{
t.join(timeout);
}
catch (Exception ex)
{
System.out.print ("Exception thrown");
}
if (t.isAlive())
{
return null;
} else
{
return t.getValue ();
}
}
/**
*
*/
public static void main (String[] args)
{
BasicConfigurator.resetConfiguration ();
BasicConfigurator.configure ();
NonBlockingWrapper NBW = new NonBlockingWrapper();
System.out.println ("Response is " + NBW.callBlockingMethod (3500));
}
}
Tx a lot to John,
hope it helps !
Thomas,