• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How can I have a thread listen for events?

 
James Elsey
Ranch Hand
Posts: 228
Android IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm not looking for someone to write my code, I'm looking for pointers on where I can begin finding the solution out for myself.

Making the asusmption that I have some 3rd party JAR in my application, that will send me events. Such as "you're in range of wireless network ABC"

What would I need to do in order to constantly listen out for such events? Would I need some thread that is constantly looping over a call to the API?

Please advise on where I can begin to figure this out

Thanks
 
Stephan van Hulst
Bartender
Pie
Posts: 6127
74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the third party code is 'sending you events', then all the necessary threading is already done by them. All you need to do is write handlers that react to the events.

It depends really. What do you mean by the JAR 'sending you events'? How do you know it's sending you these events?
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's going to depend on how the third-party library works.

If it's following a Publish/Subscribe model (like typical Java event handling) then you just register a listener with it, and process the events that get sent to you. As Stephan says, it will handle threading etc.

If it's not actually generating events, but is just changing state (and assuming you can't modify it), then you could create a thread that periodically polls it to see if a change has happened. How frequently would depend on how quickly you have to respond to changes.

And in this case, unless it's a simple situation, I'd be tempted to write this thread as a wrapper around the library that does generate events and notify listeners (so write a custom event class and listener interface). That way it would be easy for the rest of your application to respond to events without worrying about the threading details etc.
 
James Elsey
Ranch Hand
Posts: 228
Android IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the quick response guys.

To some extent, I can dictate what the third party API is going to do.

The API should be sending me data on how strong a wifi signal is (so I can display it). Obviously, if I walk closer to my router, the signal will increase, so how would this be pushed to my Java application?

Secondly, if I get close enough (ie I reach a particular signal strength threshold which I determine), the API would send me another notification to say "your close enough to pickup a good signal" or something similar

So really, I need to constantly be listening to signal strength, and secondly I need to wait for specific events that can be triggered when the API deems valid.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But as was said - this depends on how the third part API works.

Does it use Events - like Swing does? In this case you make handlers, you give the handlers to the API, and the API calls the appropriate methods at the appropriate times.

Does it use internal state? It has methods which provide 'how strong the signal is.' Then you have to ask about the strength all the time.

The key is - you need to read the documentation on how your third part API works. Figure out how it provides that information. Then program accordingly. We can't answer that question because we don't know the API you are working with.
 
James Elsey
Ranch Hand
Posts: 228
Android IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Steve,

I see where you're coming from, but the thing is the 3rd party API doesn't exist yet, hence I have "input" on how it will react.

I'm looking for suggestions on the best way of achieving such requirements. Is one way better than the other? Is one way easier to implement?
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, if you have input then the best way would be the Event scheme - in my opinion. You define Event objects which indicate the types of events which can occur. You create 'Handler' interfaces which clients implement when they want notification about those events. And you develop an API for registering Handlers for each Event type. Internally you detect when the event occurs, generate the correct type of Event Object, and push it to each Handler listening for that type of event. You probably want to push the Events out through a single Thread - probably the Event Dispatch Thread. If you want a guideline on how it works - look at Swing.
 
kri shan
Ranch Hand
Posts: 1473
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If it's following a Publish/Subscribe model (like typical Java event handling) then you just register a listener with it, and process the events that get sent to you. As Stephan says, it will handle threading etc.

If it's not actually generating events, but is just changing state (and assuming you can't modify it), then you could create a thread that periodically polls it to see if a change has happened. How frequently would depend on how quickly you have to respond to changes


It is a PUSH notification, not pooling. Event will send the PUSH notification, once the state changed and listener will listen the event notification on other side.
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kri shan wrote:It is a PUSH notification, not pooling. Event will send the PUSH notification, once the state changed and listener will listen the event notification on other side.

If you read what Matthew wrote again, you will see he only mentioned polling for the case where the 3rd party jar doesn't support the publish/subscribe model.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic