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

Scheduledexecutorservice within a ScheduledExecutorService

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Initially I made my code to have 1 Scheduledexecutorservice.
A linked list of dates organized by time were used in this scheduledexecutorservice.
And this worked perfectly, at a specific time and date the action would be enacted.

The issue was that if I made any changes to the spreadsheet I was drawing data from, the changes wouldn't update in my code because it would have already reached the last line of my code when the edits happened.
So instead I planned on implementing polling.
At a certain time of the day reread from the spreadsheet.
To do this I had another Scheduledexecutorservice run the code that reads the spreadsheet at specific intervals/periods, updates the linked list, and uses the original scheduledexecutorservice to schedule.
So now I have a Scheduledexecutorservice within a scheduledexecutor service.

Now my issue is with stopping time of the original (inner) scheduledexecutorservice.
For example if I deleted a row, i'd expect that whatever date and time the row had wouldn't be executed.
However if I didn't have a stop time for the original scheduledexecutorservice the row's action at the specific would still be executed because it had been scheduled before and I never stopped it!
What's worse if it repeats many times before a certain date/time before a deletion of a row then it performs the action many times instead of just once or none (if deleted).

I added a stop to the inner/original scheduledexecutor service and the best I could get was it perform 2 actions if the row is not deleted (want 1 action) or do no action (if the row is deleted which is what I want!).
My setup right now is that the outer scheduledexecutor service polls every minute (actually not going to do this frequently but for testing purposes) reading off the spreadsheet.
The inner/original scheduledexecutor stops after (61 seconds).
I did this on purpose because the dates i have are on the minute, so at 11:30:00 instead of 11:30:10 or something.

Initially what I had was the inner/original stops after (59 seconds)
But what I found out was that if there is an action supposed to happen between 59 seconds and the minute mark it won't get executed because it's after the inner/original scheduled executor service stops (what actually does the execution) and before the outer scheduledexecutorservice runs (that reads the spreadsheet and calls the inner scheduledexecutorservice).

Therefore having the inner scheduledexecutor stop a second longer than the outer ensures that I don't get the above problem at the cost of having the action performed twice.
My code is for a simple announcements program that runs on Discord which is a voip/text chat service.
So having double announcements posted at a certain time is not that big of a deal.
However I'd prefer to get this resolved if possible.
 
Saloon Keeper
Posts: 12254
259
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow, all of that sounds needlessly complicated and brittle. If I'm getting this right, you have a spreadsheet containing announcements that you want to make at given times. Why don't you just use a WatchService to get notified when your spreadsheet changes, then shutdown the currently running executor service, cancelling all announcements that haven't been made yet, and reschedule all the announcements that are in the updated spreadsheet?
 
Glenn Jayasuriya
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:Why don't you just use a WatchService to get notified when your spreadsheet changes


I'm not familiar with watchservice.
Forgot to mention that this is a google sheets document and not a excel spreadsheet. Would watchservice work with that?
Thanks!
 
Stephan van Hulst
Saloon Keeper
Posts: 12254
259
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well depending on what Google supports, you can implement a Watchable, but polling is probably easier. Anyway, I think you can just do something like this:

A different repeating task can then simply poll the spreadsheet and call the resetAndAnnounceLater() method of the Announcer.
    Bookmark Topic Watch Topic
  • New Topic