Coder Social home page Coder Social logo

taskun's Introduction

taskun - A simple cron daemon thread on the JVM

Maven Central

What's this

"taskun" is an implementation of UNIX "crond" on the JVM, and it also has some extended notations.

How to install

Maven

<dependencies>
  <dependency>
    <groupId>com.github.seratch.taskun</groupId>
    <artifactId>taskun-scheduler</artifactId>
    <version>[1.5,)</version>
  </dependency>
  <dependency>
    <groupId>com.github.seratch.taskun</groupId>
    <artifactId>taskun-servlet-extension</artifactId>
    <version>[1.5,)</version>
  </dependency>
  
  <!-- OPTIONAL EXTENSIONS -->
  <dependency>
    <groupId>com.github.seratch.taskun</groupId>
    <artifactId>taskun-log4j-extension</artifactId>
    <version>[1.5,)</version>
  </dependency>
  <dependency>
    <groupId>com.github.seratch.taskun</groupId>
    <artifactId>taskun-guice-extension</artifactId>
    <version>[1.5,)</version>
  </dependency>
  <dependency>
    <groupId>com.github.seratch.taskun</groupId>
    <artifactId>taskun-spring-extension</artifactId>
    <version>[1.5,)</version>
  </dependency>
  <dependency>
    <groupId>com.github.seratch.taskun</groupId>
    <artifactId>taskun-s2-extension</artifactId>
    <version>[1.5,)</version>
  </dependency>
</dependencies>

Snippet1: Running taskun

src/main/java/snippet/EchoWorker.java

package snippet;
public class EchoWorker implements Runnable {
  public void run() {
    System.out.println("Hello, World!");
  }
}

src/main/resources/crontab.txt

*/1 * * * * snippet.EchoWorker

src/main/java/snippet/SampleMain.java

package snippet;
public static void main(String[] args) throws Exception {
  Taskun taskun = TaskunFactory.getInstance();
  taskun.initialize(new TaskunConfig());
  taskun.start();
  Thread.sleep(20000L);
}

Snippet2: Running snippet servlet

git clone [email protected]:seratch/taskun.git
cd taskun/taskun-servlet-extension
mvn jetty:run

# And you will see "Hello, World!" endlessly in the console.

[INFO] Started Jetty Server
[INFO] Starting scanner at interval of 3 seconds.
2011/04/02 0:00:53 com.github.seratch.taskun.taskun.crond.CronDaemon initialize
INFO: ----- Taskun scheduler initialized -----
2011/04/02 0:00:53 com.github.seratch.taskun.taskun.crond.CronDaemon initialize
INFO: Working at seratch-imac(seratch-imac)
2011/04/02 0:00:53 com.github.seratch.taskun.taskun.crond.CronDaemon initialize
INFO: Interval invocation : 3sec,com.github.seratch.taskun.servlet.snippet.EchoWorker,1
2011/04/02 0:00:53 com.github.seratch.taskun.taskun.crond.CronDaemon initialize
INFO: Crontab invocation : */1 * * * * com.github.seratch.taskun.servlet.snippet.EchoWorker*3
2011/04/02 0:00:53 com.github.seratch.taskun.taskun.crond.CronDaemon initialize
INFO: ----------------------------------------
2011/04/02 0:00:53 com.github.seratch.taskun.servlet.snippet.SnippetTaskunServlet init
INFO: Taskun scheduler has started!
[NamedServers:]
Hello, World! (Thread:20,Sat Apr 02 00:00:58 JST 2011)
Hello, World! (Thread:21,Sat Apr 02 00:01:00 JST 2011)
Hello, World! (Thread:20,Sat Apr 02 00:01:00 JST 2011)
...

Snippet3: Running taskun in your webapp

src/main/webapp/WEB-INF/web.xml

<servlet>
  <servlet-name>taskunServlet</servlet-name>
  <servlet-class>com.github.seratch.taskun.servlet.impl.SimpleTaskunServlet</servlet-class>
  <load-on-startup>3</load-on-startup>
</servlet>

src/main/resources/crontab.txt

*/1 * * * * com.github.seratch.taskun.servlet.snippet.EchoWorker*3
interval:3sec initial:5sec com.github.seratch.taskun.servlet.snippet.EchoWorker

src/main/resources/taskun.properties

enableInvokingTaskun=true
enableLoggingForEachCrondInvocation=true
namedServer1=.*server1
namedServer2=server2*.example.com
namedServer3=

Deploy and run

Snippet4: Injecting taskun, config and worker instances

NOTICE: taskun-guice-extension, taskun-spring-extension or taskun-s2-extension is required.

Snippet5: Using extended notations

Invoking seconds intervals

"initial" is waiting time for the first invoking since taskun taskun started.

interval:3sec initial:5sec snippet.EchoWorker

Invoking several threads at once.

Following will invoke 3 threads to do same command(=EchoWorker) at once:

*/1 * * * * snippet.EchoWorker*3

Snippet6: Using log4j for taskun logging

NOTICE: taskun-log4j-extension is required.

It is reuirqed to set LogImplClass to config.

package snippet;
 
import com.github.seratch.taskun.logging.TaskunLogLog4jImpl;
 
public static void main(String[] args) throws Exception {
  Taskun taskun = TaskunFactory.getInstance();
  TaskunConfig config = new TaskunConfig();
  config.setLogImplClass(TaskunLogLog4jImpl.class); 
  taskun.initialize(config);
  taskun.start();
  Thread.sleep(20000L);
}

taskun's People

Contributors

ma2gedev avatar sargue avatar seratch avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

ma2gedev sargue

taskun's Issues

Scheduler for interval executions stops when worker throwed Exception

src/
└── main
    ├── resources
    │   └── crontab.txt
    └── scala
        └── Hello.scala
  • src/main/scala/Hello.scala
class Hello extends Runnable {
  override def run() {
    println("Hello! (" + new java.util.Date + ")")
    throw new RuntimeException
  }
}
  • src/main/resources/crontab.txt
*/1 * * * * Hello
interval:3sec initial:5sec Hello
  • main
import com.github.seratch.taskun.scheduler._
val taskun = TaskunFactory.getInstance()
taskun.initialize(new config.TaskunConfig)
taskun.start()

Crontab parser bug

OK:

The following settings work as expected.

0,10,20,30,40,50 9,10,11,12 * * * example.Worker

NG:

In this case, "9,10,11,12" is ignored.

*/10 9,10,11,12 * * * example.Worker

It seems to be a crontab parser's bug and should be fixed.

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.