Recommended method for loading a URL via a scheduled task on Windows

As pointed out by Remus Rusanu, PowerShell would be the way to go. Here’s a simple one-liner that you can use to create a scheduled task, without needing to write a separate .ps1 file: powershell -ExecutionPolicy Bypass -Command Invoke-WebRequest ‘http://localhost/cron.aspx’ -UseBasicParsing Note that line breaks are added only for clarity in all of these command … Read more

Provide time zone to Spring @Scheduled?

It turned out that I could not use the @Scheduled annotation, but I implemented a work-around. In the JavaDoc of the SchedulingConfigurer it is stated that: [SchedulingConfigurer is] Typically used for setting a specific TaskScheduler bean to be used when executing scheduled tasks or for registering scheduled tasks in a programmatic fashion as opposed to … Read more

Java thread affinity

You can’t do this in pure java. But if you really need it — you can use JNI to call native code which do the job. This is the place to start with: http://ovatman.blogspot.com/2010/02/using-java-jni-to-set-thread-affinity.html http://blog.toadhead.net/index.php/2011/01/22/cputhread-affinity-in-java/ UPD: After some thinking, I’ve decided to create my own class for this: ThreadAffinity.java It’s JNA-based, and very simple — … Read more

How to stop a Runnable scheduled for repeated execution after a certain number of executions

You can use the cancel() method on Future. From the javadocs of scheduleAtFixedRate Otherwise, the task will only terminate via cancellation or termination of the executor Here is some example code that wraps a Runnable in another that tracks the number of times the original was run, and cancels after running N times. public void … Read more

How does the OS scheduler regain control of CPU?

The OS sets up a hardware timer (Programmable interval timer or PIT) that generates an interrupt every N milliseconds. That interrupt is delivered to the kernel and user-code is interrupted. It works like any other hardware interrupt. For example your disk will force a switch to the kernel when it has completed an IO.

ScheduledExecutorService Exception handling

tl;dr Any exception escaping your run method halts all further work, without notice. Always use a try-catch within your run method. Try to recover if you want scheduled activity to continue. @Override public void run () { try { doChore(); } catch ( Exception e ) { logger.error( “Caught exception in ScheduledExecutorService. StackTrace:\n” + t.getStackTrace() … Read more