0

In a weld bean (not EJB), I'm trying to handle an event Asynchronously. To do so I'm trying to fire an asynchronous event using Event.fireAsync and then catch the event using a method that takes the event object as parameter and is annotated with @ObservesAsync annotation. The fireAsync does run, but the @ObservesAsync method is never called.

  @Inject
  Event<CustomEvent> customEvent;

  public void sendEvent(ObjectRequest request) {
    customEvent.fireAsync(new CustomEvent(request))
                           .thenAccept((CustomEvent) -> {
                             logger.info(">>>>  Event thenAccept");
                           });
  }

  public void handeEvent(@ObservesAsync CustomEvent customEvent) {
    logger.info("||||||| Received CustomEvent");
    this.attribute= logProcessing(customEvent.getRequest());
  }

PS: Using Event.fire and @Observes does work , but is not asynchronous which I need this code to be.

Morty
  • 3,463
  • 4
  • 17
  • 21
  • There is apparently some information missing here, I could counter your example with much the same in [CDI TCK](https://github.com/cdi-spec/cdi-tck/tree/master/impl/src/main/java/org/jboss/cdi/tck/tests/event/observer/context/async) which apparently passes. Some some possibly silly questions - Is the observer located on a CDI bean? Are you, when testing it, allowing some time for completion (e.g. not exit right away)? – Siliarus Oct 03 '18 at 06:38
  • It's indeed located in a CDI bean ( a SessionScoped bean to be precise), As for alowing time to elapse, It's inside a REST call and I don't see what I can do to ensure that. (I think the time issue you're mentioning may explain why it's not called but how can I ensure it has time). – Morty Oct 03 '18 at 12:01
  • A REST call? Aren't you by chance observing it in a different thread from the one in which you are firing the event? E.g. using async calls or something like that? You may want to expand the code you attached to the question to account for that. – Siliarus Oct 03 '18 at 14:40
  • By that timing issue I meant whether you aren't just doing a dummy test where you fire event and immediately expect observer answer and exit the program - in such case, it wouldn't show up. A little bit of `Thread.sleep()` on main thread (the one where you fire the event) would do the trick. – Siliarus Oct 03 '18 at 14:42
  • Actually I'm trying to do an async call, I'm not starting a new thread explicitly for the Observes but I'm hoping that it's called asynchronously. Basically from the begining I wanted to do the call asynchronously; I tried starting a thread but didn't work because I use SessionScoped beans , and I'm not using EJB so the @Asynchronous annotation didn't work, which left with the asynchronous Event which didn't work either. – Morty Oct 03 '18 at 14:52

0 Answers0