Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Callout Handler - receiving delayed events on the same thread

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm new to Java, but not to software development. I have a requirement to be able to schedule events to occur in the future on the same thread - which I believe rules out using java.util.concurrent.Future and friends. What I have implemented allows any number of "Callouts" to be placed with a "CalloutHandler". The placer of the Callouts must register as an event listener with the CalloutHandler. Each Callout has an associated "action" and delay. The CalloutHandler periodically checks its collection of Callouts. When a Callout is found whose delay has passed, the CalloutHandler fires an event that is caught by the event listener. The listener then runs code associated with the Callout action. The CalloutHandler then removes the Callout from its collection. Example code follows:

Callout:




CalloutHandler:



CalloutEvent:



ICalloutEventListener:



The above code is all implemented in a library.

Test harness that calls the above code:



Produces output similar to this:

No callouts in list.
3 callouts in list.
3 callouts in list.
Callout interval has passed.
TestStuff.doTest(): event action: 1
2 callouts in list.
2 callouts in list.
Callout interval has passed.
TestStuff.doTest(): event action: 2
1 callouts in list.


I have another implementation that uses Futures to fire events back to a listener, but I will not bore you with the details of it.

The above implementation is bare bones - the final implementation will need to allow for the canceling of any/all existing Callouts, and the placement of "conditional" Callouts (only add to the collection if a Callout with the same action does not already exist). Which I can cope with.

This is all well and good, but is there already something in the Java library that provides this functionality? Either in full, or something I can extend to fit my needs? I have been trawling both the Java API docs and Google, but have not found anything to satisfy my requirements.

Or should I be looking at using Futures and running the delayed code in another thread? I fear the concurrency issues that may/will arise from this approach. Or am I missing the point entirely?
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I haven't looked fully at your code, but isn't it what java.util.Timer does ? If your class extends TimerTask, you should be able do the same. Unless I overlooked something.
 
Stuart James
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, a quick test shows that you have not overlooked anything. I have made the problem seem much bigger than it was - I'm still thinking like a COBOL/VB6 programmer. In hindsight, I should have posted this in the Beginning Java forum. I have some work ahead of me...

Thanks for your advice.
 
    Bookmark Topic Watch Topic
  • New Topic