aspose file tools*
The moose likes Java in General and the fly likes How can I have a thread listen for events? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "How can I have a thread listen for events?" Watch "How can I have a thread listen for events?" New topic
Author

How can I have a thread listen for events?

James Elsey
Ranch Hand

Joined: Dec 21, 2007
Posts: 228

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


Kind Regards, James. OCPJP 1.6 || My SCJP / OCJCP Study Notes
Interested in : SCJP, Google App Engine, Stripes, Android;|| My Bite-Size SCJP Study Blog
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3611
    
  14

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

Joined: Apr 06, 2010
Posts: 4363
    
    8

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

Joined: Dec 21, 2007
Posts: 228

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

Joined: Jan 28, 2003
Posts: 4174
    
  21

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.


Steve
James Elsey
Ranch Hand

Joined: Dec 21, 2007
Posts: 228

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

Joined: Jan 28, 2003
Posts: 4174
    
  21

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

Joined: Apr 08, 2004
Posts: 1372
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

Joined: May 30, 2012
Posts: 679
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How can I have a thread listen for events?