The correct way to force @Async work using ProxyFactory

53 Views Asked by At

Lets suppose we have an Actor interface:

  public interface Actor {
      ...
      void doAction(arg1, arg2, etc ...);
  }

And we have some implementations:

    @Async
    void doAction(...) {

Now we implement some lib, that has a map of actors, and it code smth like this:

    var actor = actorMap.get("actorName");
    actor.doAction(...);

And @Async doesn't work, cause particular actor is not a proxy, but a real instance. But we keep fighting. Lets proxy the call:

  ProxyFactory factory = new ProxyFactory(actor);
  factory.addInterface(Actor.class);
  factory.addAdvisor(new AsyncAnnotationAdvisor());

  var ifaceActor = (Actor) factory.getProxy();

  // this is a method call on the proxy!
  ifaceActor.doTheAction(update, null, this, session, sender);

And this starts to magically work, but I am not sure that the line with factory.addAdvice() is correct enough. Probably, there is a more correct way to get AsyncAdvice, but I failed to google it. (And yes, it is a sample code and I'll reuse adviser object. More over, I'll try to keep proxy objects, not recreate them always.)

0

There are 0 best solutions below