users@glassfish.java.net

Re: Remote Asynchronous Calls and Response

From: <forums_at_java.net>
Date: Wed, 27 Jul 2011 03:01:00 -0500 (CDT)

Thank you for the reply.
I don't want to use Future.get because my client would have to wait after
each request, which could be very long, but this is what I don't want.
I want the results to appear in a list progressively, as soon as the
calculation is finished.
I think I simplified my example too much...
My client sends a list of calculation requests to the server, the ejb
receiving these requests splits the list and dispatches the parts to many
special calculators. Here is another example:
//ICallback defines only public void addResult(Integer result);
public class CalculationClient implements ICallback {
  public void addResult(Integer result)
  {
    System.out.println(result);
  }
  
  public void doLengthyCalculation(List<ICalculation> calculations)
  {
    Context initalContext = new InitialContext();
    //calling the dispatcher here
    CalculationDispatcherRemote remoteEjb =
initalContext.lookup("java:global/CalculationServer/CalculationDispatcher!my.package.CalculationDispatcherRemote");
    //Send a list of calculations
    remoteEjb.calculate(this, calculations);
  }
}
//CalculationDispatcherRemote is annotated with @Remote
@Stateless
public class CalculationDispatcher implements CalculationDispatcherRemote {
  @EJB
  Calculator calculator1;
   
  @EJB
  Calculator calculator2;
  @Override
  public void calculate(ICallback callBack, List<ICalculation> calculations)
  {
    //splitting done differently, but here irrelevant
    ICalculation calculation = calculations.get(0);
    calculator1.calculateAsync(callBack, calculation);
    calculation = calculations.get(1);
    calculator2.calculateAsync(callBack, calculation);      
  }
}
//CalculatorLocal is annotated with @Local
@Stateless
public class Calculator implements CalculatorLocal {
  @Override
  @Asynchronous
  public Future<Integer> calculateAsync(ICallback callBack, ICalculation
calculation)
  {
    // Do calculation...
    callBack.addResult(newResult);
      
    Future<Integer> futureResult = new AsyncResult<Integer>(newResult);
    return futureResult;
  }
}
So if calculator2 returns result2 before calculator1 is finished then I want
result2 to appear in the client even if calculator1 is still calculating, I
dont want to wait for calculator1...
Thanks


--
[Message sent by forum member 'MohamzJava']
View Post: http://forums.java.net/node/826162