GithubHelp home page GithubHelp logo

janpollmann / watchdog Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 0.0 204 KB

Java8 function calls with timeout (if necessary interruption of the thread)

Home Page: https://janpollmann.github.io/Watchdog/

License: The Unlicense

Java 99.96% HTML 0.04%
executorservice function-call future java8 timeout watchdog interrupt thread

watchdog's Introduction

Java 8 (repeated) function call support

Each function call has a timeout and terminates the execution. After each function call (even after a timeout or an exception) a TaskResult is created with one of the following ResultCode:

  • ResultCode.OK The execution is completed without exception and the result can be obtained with TaskResult#getResult

  • ResultCode.TIMEOUT The function call was NOT completed, the task was canceled

  • ResultCode.ERROR An unexpected exception has occurred. The exception can be determined with TaskResult#getErrorReason.

Quick start

This library supports Runnable, Callable, Consumer and Function. When a timeout occurs, the worker thread is interrupted. Be careful to handle the interruption properly, otherwise a thread of an ExecutorService (⇒ your function) could end up in an infinite loop (⇒ examples). One way to handle an interruption is as follows:

if (Thread.interrupted()) {
  throw new InterruptedException();
}

Setup

  1. Import the Library in your project (see instructions below)

  2. Create & store a WatchdogFactory. Each factory stores two ExecutorServices

  3. Use Watchable.builder(…​) to create a watchable and WatchableOptions.builder(…​) to create the options
    optional: add a ResultProcessor to the watchable builder (this callback not monitored)

  4. Create an asynchronous function call with WatchdogFactory#submitFunctionCall, a synchronized function call using WatchdogFactory#waitForCompletion or create a RepeatableTask with WatchdogFactory#createRepeated

  5. In case of a RepeatableTask call the task with RepeatableTask#submitFunctionCall and RepeatableTask#waitForCompletion

Import the Library

Gradle

repositories {
    maven {
        url = uri("https://maven.pkg.github.com/JanPollmann/Watchdog")
    }
}

dependencies {
    implementation 'de.pollmann.watchdog:watchdog:<version>'
}

Maven

Follow the GitHub documentation of the desired package: https://github.com/JanPollmann/Watchdog/packages

Important

Caution
  • Please remind: ResultProcessor (if specified) will be called after the monitored function call without any timeout. If there is heavy computational work, the call will take longer as specified (or will not terminate if there is an infinite loop)

    • Obviously, you could execute a RepeatableTask as ResultProcessor :)

  • The timeout is specified in milliseconds

    • A timeout of 0 ms will be handled as no timeout

  • For not repeated function calls, the input of a function/consumer is passed to the builder!

  • As soon as the internal worker of the WatchdogFactory gets garbage collected, ExecutorServices#shutdown is called for every ExecutorServices (finalize)

    • A RepeatableTask has a reference to the internal worker

    • To terminate the RepeatableTask call RepeatableTask#terminate

    • A terminated RepeatableTask will throw a RepeatableTaskTerminatedException!

Changelog

Example

Just implement loop. Remarks:

  • A timeout of 0 ms will be handled as no timeout

  • both tasks have a ResultConsumer registered, but that’s an optional feature

watchdog's People

Contributors

janpollmann avatar

Stargazers

 avatar

Watchers

 avatar

watchdog's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.