gihan-maduranga
gihan-maduranga

Reputation: 4811

How to handle multiple listeners with same argument in google guava eventbus

I have two listener that will accept same argument for when posting events into eventbus.But even though accept same argument those listeners are behave differently.

public class EventListener1 {



    @Subscribe
    public void executeJob(jobVO jobVO ) {
        //logic goes here
    }
}



public class EventListener2 {



    @Subscribe
    public void cancelJob(jobVO jobVO ) {
        //logic goes here but different
    }
}

EventBus eventBus = new EventBus();
eventBus.register(new EventListener1());
eventBus.register(new EventListener2());

user press execute button and wants to trigger EventListener1 like wise for cancel job.

for execute job

JobVO j=new JobVO();
j.setAction("executeJob");
etc... 
eventBus.post(j);

for cancel job

JobVO j=new JobVO();
j.setAction("cancelJob");
etc... 
eventBus.post(j);

My question is that how can i trigger specific listener when posting events into eventbus or it will call both listeners?

Upvotes: 1

Views: 959

Answers (1)

Louis Wasserman
Louis Wasserman

Reputation: 198591

Some simple solutions:

@Subscribe
public void executeJob(jobVO jobVO ) {
    if (!jobVO.getAction().equals("executeJob")) {
       return;
    }
    //logic goes here
}

...or, alternately, don't use EventBus:

JobVO j=new JobVO();
j.setAction("executeJob");
etc... 
eventListenerForExecuting.executeJob(j);

Upvotes: 2

Related Questions