users@glassfish.java.net

Asynchronous Ejb, Futur.isDone() isn't working

From: <glassfish_at_javadesktop.org>
Date: Sun, 31 Jan 2010 08:01:54 PST

I’m trying to use Asynchronous Ejd with Glassfish V3 and the Future.isDone() isn’t working.
This code is just for reproduce the case

My Ejb
[i]package fr.paddy.ejb31;

import java.util.concurrent.Future;
import javax.ejb.AsyncResult;
import javax.ejb.Asynchronous;
import javax.ejb.Remote;
import javax.ejb.Stateless;

@Stateless
@Remote(HelloEjbAsynchronousRemote.class)
public class HelloEjbAsynchronous implements HelloEjbAsynchronousRemote {

    @Asynchronous
    @Override
    public Future<String> ejbAsynchronousSayHello(String name){
        System.out.println("Begin - HelloEjbAsynchronos->ejbAsynchronousSayHello "+name);

        try{
           Thread.sleep(5*1000);
        }catch (Exception e){
            e.printStackTrace();
        }

        System.out.println("End - HelloEjbAsynchronos->ejbAsynchronousSayHello "+name);

        return new AsyncResult<String>("Hello "+name);
    }
}[/i]

My client

[i]package clientejb31;

import java.util.Properties;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import fr.paddy.ejb31.HelloEjbAsynchronousRemote;

public class MainNotWorking {

    public static void main(String[] args) {
        Properties props = new Properties();
        props.setProperty("java.naming.factory.initial",
                             "com.sun.enterprise.naming.SerialInitContextFactory");
        props.setProperty("java.naming.factory.url.pkgs",
                             "com.sun.enterprise.naming");
        props.setProperty("java.naming.factory.state",
                             "com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl");
        props.setProperty("org.omg.CORBA.ORBInitialHost", "localhost");
        props.setProperty("org.omg.CORBA.ORBInitialPort", "3700");

        try{
            InitialContext ic = new InitialContext(props);
            String ret="";

            HelloEjbAsynchronousRemote ha = (HelloEjbAsynchronousRemote)ic.lookup("java:global/EjbAsynchronous/HelloEjbAsynchronous");
            Future future = ha.ejbAsynchronousSayHello("Patrick");

            while (!future.isDone()){
                Thread.sleep(1000);
                System.out.println("I do other things ...");

                System.out.println("futur.isDone"+future.isDone());
            }

            ret = (String)future.get();
            System.out.println("ret : "+ret);

        }catch (NamingException ne){
            ne.printStackTrace();
        }catch (InterruptedException ie){
            ie.printStackTrace();
        }catch (ExecutionException ee){
            ee.printStackTrace();
        }
    }
}[/i]

The logs on server
[i]INFO: Begin - HelloEjbAsynchronos->ejbAsynchronousSayHello Patrick
INFO: End - HelloEjbAsynchronos->ejbAsynchronousSayHello Patrick
[/i]
The logs on client
[i]I do other things ...
futur.isDonefalse
I do other things ...
futur.isDonefalse
I do other things ...
futur.isDonefalse
I do other things ...
futur.isDonefalse
I do other things ...
futur.isDonefalse
I do other things ...
futur.isDonefalse
I do other things ...
futur.isDonefalse
I do other things ...
futur.isDonefalse
I do other things ...
futur.isDonefalse
I do other things ...
futur.isDonefalse
I do other things ...
futur.isDonefalse
I do other things ...
futur.isDonefalse
I do other things ...
futur.isDonefalse
I do other things ...
futur.isDonefalse[/i]

isDone never return true so the loop is infinite
I found a workaround, I read the value inside the loop and that's working!
But how works the isDone method?
And what it is its interest?

My client, with the workaround
[i] public static void main(String[] args) {
        Properties props = new Properties();
        props.setProperty("java.naming.factory.initial",
                             "com.sun.enterprise.naming.SerialInitContextFactory");
        props.setProperty("java.naming.factory.url.pkgs",
                             "com.sun.enterprise.naming");
        props.setProperty("java.naming.factory.state",
                             "com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl");
        props.setProperty("org.omg.CORBA.ORBInitialHost", "localhost");
        props.setProperty("org.omg.CORBA.ORBInitialPort", "3700");

        try{
            InitialContext ic = new InitialContext(props);
            String ret="";

            HelloEjbAsynchronousRemote ha = (HelloEjbAsynchronousRemote)ic.lookup("java:global/EjbAsynchronous/HelloEjbAsynchronous");
            Future future = ha.ejbAsynchronousSayHello("Patrick");

            while (!future.isDone()){
                Thread.sleep(1000);
                System.out.println("I do other things ...");
                System.out.println("futur.isDone"+future.isDone());
                // just a hack, because at the moment future.isDone doesn't work !
                try{
                    Object o = future.get(10,TimeUnit.MILLISECONDS);
                }catch (TimeoutException te){
                   // te.printStackTrace();
                }
                // end of the hack
                System.out.println("futur.isDone"+future.isDone());
            }

            ret = (String)future.get();
            System.out.println("ret : "+ret);

        }catch (NamingException ne){
            ne.printStackTrace();
        }catch (InterruptedException ie){
            ie.printStackTrace();
        }catch (ExecutionException ee){
            ee.printStackTrace();
        }
    }[/i]

The logs on server
[i]INFO: Begin - HelloEjbAsynchronos->ejbAsynchronousSayHello Patrick
INFO: End - HelloEjbAsynchronos->ejbAsynchronousSayHello Patrick[/i]

the logs on client
I[i] do other things ...
futur.isDonefalse
futur.isDonefalse
I do other things ...
futur.isDonefalse
futur.isDonefalse
I do other things ...
futur.isDonefalse
futur.isDonefalse
I do other things ...
futur.isDonefalse
futur.isDonefalse
I do other things ...
futur.isDonefalse
futur.isDonetrue
ret : Hello Patrick[/i]

Patrick Champion
http://paddyweblog.blogspot.com/
[Message sent by forum member 'patrick_champion' (patrick.champion_at_gmail.com)]

http://forums.java.net/jive/thread.jspa?messageID=383941