yf0994 / guava-libraries

Automatically exported from code.google.com/p/guava-libraries
Apache License 2.0
0 stars 0 forks source link

Create a ListenableScheduledFuture #1140

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
The ScheduledFutures returned by the ScheduledExecutorService#schedule* methods 
never return a value, but they _may_ thrown an exception. Once this happens the 
associated Runnable will no longer be scheduled, so it is important to handle 
(or at least be notified of) such exceptions. Now, as best as I can tell there 
is currently no elegant way to do so; the best solution I've seen so far is 
described by [1] and [2]. It'd hardly call that approach "simple", "convenient" 
or "straightforward".

In my specific use case I'd like to simply log the exception when a scheduled 
runnable dies unexpectedly. So ideally I create a straightforward 
FutureCallback implementation which does just that, and then register it with 
the ListenableFuture returned by one of the 
ListeningScheduledExecutorService#schedule* methods... except that these 
methods do not actually return a ListenableFuture. Chris added the following 
comment to the interface:

    TODO(cpovirk): make at least the one-time schedule() methods return a ListenableFuture, too? But then we'll need ListenableScheduledFuture...

I understand the hesitation to introduce yet another interface, but I'd like to 
voice my +1 for this anyway. This would really simplify working with 
ScheduledFutures.

(I am aware that I could also create a "LoggingRunnable" which delegates to 
some other Runnable by invoking its #run() method within a try/catch block. 
Indeed, that would work in my specific case, but only because I control all 
code that actually schedules tasks.)

[1] 
http://stackoverflow.com/questions/6894595/scheduledexecutorservice-exception-ha
ndling
[2] 
http://www.cosmocode.de/en/blog/schoenborn/2009-12/17-uncaught-exceptions-in-sch
eduled-tasks

Original issue reported on code.google.com by stephan...@gmail.com on 7 Sep 2012 at 10:03

GoogleCodeExporter commented 9 years ago
Someone tried to do this internally.  He had some kind of problem, breaking 
someone's mock executor or something?  I forget.  It sounded surmountable, but 
he hasn't gotten back to it.  I'll optimistically mark this as Release14 in the 
hope that one of us will take over the CL.

Original comment by cpov...@google.com on 7 Sep 2012 at 2:03

GoogleCodeExporter commented 9 years ago

Original comment by cpov...@google.com on 7 Nov 2012 at 7:38

GoogleCodeExporter commented 9 years ago
Coming in r15.

http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/util/co
ncurrent/ListenableScheduledFuture.html

Original comment by cpov...@google.com on 22 Apr 2013 at 11:38

GoogleCodeExporter commented 9 years ago
Great! Thanks :)

Original comment by stephan...@gmail.com on 23 Apr 2013 at 5:56

GoogleCodeExporter commented 9 years ago
Can there be a version of Futures.dereference() that returns a 
ListenableScheduledFuture? 

Original comment by min...@gmail.com on 12 Sep 2013 at 8:39

GoogleCodeExporter commented 9 years ago
We'd have to fudge the getDelay implementation, but maybe that's OK. Hmm, but 
if it's OK for getDelay to return a fudged value, is there any advantage to 
having a ListenableScheduledFuture over a plain ListenableFuture?

Original comment by cpov...@google.com on 13 Sep 2013 at 4:51

GoogleCodeExporter commented 9 years ago
This issue has been migrated to GitHub.

It can be found at https://github.com/google/guava/issues/<id>

Original comment by cgdecker@google.com on 1 Nov 2014 at 4:13

GoogleCodeExporter commented 9 years ago

Original comment by cgdecker@google.com on 3 Nov 2014 at 9:08