2

Is it possible for a class that is subscribed to a certain event class to fire the same event type without listening to it?

Example:

Class A {

  EventBus bus = new EventBus();
  public A() {
     bus.register(this);
     bus.post ( new String("event!"));
  }

  @Subscribe public void consume(String event) {
     System.out.println("Got event: "+event);
  }

}
Jose
  • 90
  • 6

2 Answers2

4

You could have your event class include the source of the event (the object that posted the event) and then just ignore any events where the source is this. I'd recommend trying to make your class handle events consistently regardless of the source, though.

ColinD
  • 108,630
  • 30
  • 201
  • 202
0

No, there isn't. How could EventBus determine where an event originated in the first place?

If you want to ignore certain events, you must include enough information in the event object itself to determine if the event should be ignored.

Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413
  • Well it could use the StackTrace of the calling thread... but I guess this would be an heavy operation to be called frequently. See here: http://stackoverflow.com/questions/1069066/get-current-stack-trace-in-java – Jose Nov 01 '12 at 15:23
  • 1
    Yep -- and that wouldn't help you if you had multiple instances of the class and wanted them to listen to each other's events. – Louis Wasserman Nov 01 '12 at 16:04