How to remove a task from ScheduledExecutorService?

Simply cancel the future returned by scheduledAtFixedRate(): // Create the scheduler ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(1); // Create the task to execute Runnable r = new Runnable() { @Override public void run() { System.out.println(“Hello”); } }; // Schedule the task such that it will be executed every second ScheduledFuture<?> scheduledFuture = scheduledExecutorService.scheduleAtFixedRate(r, 1L, 1L, TimeUnit.SECONDS); // … Read more

Where do I create and use ScheduledThreadPoolExecutor, TimerTask, or Handler?

I prefer to use ScheduledThreadPoolExecutor. Generally, if I understand your requirements correctly, all these can be implemented in your activity, TimerTask and Handler are not needed, see sample code below: public class MyActivity extends Activity { private ScheduledExecutorService scheduleTaskExecutor; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); scheduleTaskExecutor= Executors.newScheduledThreadPool(5); // This schedule a task to run every … Read more

How to run certain task every day at a particular time using ScheduledExecutorService?

As with the present java SE 8 release with it’s excellent date time API with java.time these kind of calculation can be done more easily instead of using java.util.Calendar and java.util.Date. Use date time class’s i.e. LocalDateTime of this new API Use ZonedDateTime class to handle Time Zone specific calculation including Daylight Saving issues. You … Read more

How to run a background task in a servlet based web application?

Your problem is that you misunderstand the purpose of the servlet. It’s intented to act on HTTP requests, nothing more. You want just a background task which runs once on daily basis. EJB available? Use @Schedule If your environment happen to support EJB (i.e. a real Java EE server such as WildFly, JBoss, TomEE, Payara, … Read more