Solution to task termination after Timer exception

Source: Internet
Author: User

In the system provided to the customer, several timers are executed at regular intervals.

However, the customer reported that some data was not successful, so the log showed that the task was terminated because Timer reported an error.

Then you need to execute the task again when Timer reports an error, so add the code in the catch, but it is not very good.

Find

Java. util. concurrent. ScheduledExecutorService;

So let's take a look at how to modify the original Timer code

Public static ScheduledExecutorService executorService = Executors. newScheduledThreadPool (1);/*** execute once every 1 minute */public static void runTimer () {executorService. scheduleAtFixedRate (new Runnable () {@ Overridepublic void run () {try {.....} catch (Exception e) {e. printStackTrace () ;}}, 0, 60, TimeUnit. SECONDS );}

In this way, the desired result is achieved.

 

Note: you must add try catch to catch an exception before you can continue to execute the next task after the exception. Otherwise, the task will suspend and do nothing.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.