2

I have used otto event bus as explain here. It works great.

http://square.github.io/otto/

https://code.google.com/p/guava-libraries/wiki/EventBusExplained

Today I realize that if I create event and subscribed it in 2 place, both get called.

To more clear on this:

I have jar which uses event bus for internal communication. When I use this jar in my application, I can register on event bus and catch the event as well.

How to avoid this?

morya
  • 835
  • 2
  • 10
  • 19

2 Answers2

3

Looks like it's working as intended. You might want to create two distinct events to solve your problem.

mbmc
  • 5,024
  • 5
  • 25
  • 53
1

If you're registering multiple destinations and they are all subscribing for a particular event on an event bus, all of those destination will notified when that event is published. This is the intended way the Otto bus works. It follows the publish / subscribe pattern.

It's important to note that it's not because you're registered to an event bus that you will get all of the event being posted to that bus. You need to also subscribe for that event.

Alright for you use case, do you really need to be registered to that specific bus? Seems like you don't want to be notified of specific events being used within that library. Why don't you just create a second Bus instance? You could then use it to communicate within your app only without conflicting with your library communication. This would allow you to be subscribed to the same events that the library uses but only your app would be registered to that app. That would create the separation that I think you're looking fore. So in conclusion you'd have original bus for the library communication and a second bus for your app communication.

Maybe I'm missing what you're trying to do here.

Miguel
  • 19,793
  • 8
  • 56
  • 46
  • 1
    **Behavior of Event Bus:** When you register on event bus and subscribe for same event at multiple location then all associated @subscribe will get called. **In my case** I just want to check if there is any way to call only one subscribe event, even though that event is subscribed at multiple location. I guess this is not possible. – morya Jul 28 '14 at 19:16
  • @user2713030 I have updated the question. ANy thought on this – morya Jul 29 '14 at 19:15
  • Lavign : Whats your thought on this – – morya Jul 29 '14 at 19:16
  • @morya I've updated my answer, I'm still not exactly sure what you're trying to achieve, you tell me if that makes sense or not. – Miguel Jul 29 '14 at 19:36