run periodic tasks on server in the background - java

What's the best/easiest way to run periodic tasks (like a daemon thread) on a tomcat/jetty server? How do I start the thread? Is there a simple mechanism or is this a bad idea at all?

If want to keep everything on java side, give a look to Quartz.
It handles failover and fine grained repartition of jobs, with the same flexibility of cron jobs.

It's okay and effective to stash a java.util.Timer (or better yet ScheduledExecutor) instance in your ServeletContext. Create it in a Servlet's init() call and all your servlets can add TimerTasks to it.

One general purpose way which works for many systems is simply to have a cron job which performs a periodic wget against your app.

I can't answer the tomcat/jetty stuff, but I've done similar things with Python based web apps.
I normally just run a separate app that does the periodic tasks needed. If interop is needed between the website and the app, that communication can happen through some sort of API (using something like XML-RPC/unix sockets/etc) or even just through the database layer, if that's adequate.
Hope that helps.

If you want to use a cron job but don't have administrative access to the development system, you can do a user crontab by executing the command:
crontab -e
It uses vi by default on most systems, but you can change it to the editor of your choice via:
export EDITOR=/usr/local/bin/my_editor
Then, executing the crontab -e command will launch your crontab file in your editor. Upon saving, the changes will be committed back into the system's cron.

Related

Distributed CRON in Kubernetes

distributed CRON in Kubernetes is still a work in progress (https://github.com/kubernetes/kubernetes/issues/2156).
What do you use for CRON jobs in Kubernetes today?
Do you recommend any solution that works well with Spring/JVM-based services? Spring/JVM startup time is quite high and if CRON scheduler started a new JVM for each job, startup time might be much higher than time of actual work - is there any solution that could run the job in existing JVM?
Thank you,
Jakub
I think Mesos Chronos is still ideal solution.
I wrote a small Go app that functions like cron but writes log info to stdout (no email!) and can be built into a static binary for easy containerization.
I built kubectl from source as a static binary and included it in the image (it may be a static binary in the most recent releases). Kubectl will automatically look for the service account token/certs in /var/run/secrets/kubernetes.io/serviceaccount/ so you should be good to go unless you're not using the default service account.
I then set up a crontab to run kubectl to create a job at the period that I wanted. The crontab and yaml files for the jobs can be mounted as a secret. You can either use conf2kube or some other way of generating the secrets. I wrote a simple python script.
It's totally a workaround until there is proper support but I hope that helps.
I'm using cron jobs in kubernetes with java, each job launches a new JVM, so no. No reuse here.
To reuse you must jave something like a webapp always running and schedule jobs to run inside this already running app.

Creating a background process using java

I want run a background process at a specific time. I want that process to be run on the server even without running the application from End-User side. The application is made using Spring. Please suggest how to approach for it.
Thanks and regards
Souvik
I depends highly on what platform you are working on, and what you want to achieve.
If it is a simple application, that you simply want to invoke that on specific time, then you can use scheduling tools available on your platform, for example, crontab for Unix, or scheduled task (at) for Windows.
If you want the application to be run as a daemon process, and the application itself will handle the scheduling, then you need to solve two problem: 1. create a daemon process (aka system service), and 2. doing scheduling in Java.
For problem 1, there are already answer for it. Just have a search on Google on "Java System Service" will give you some other useful tools too, like Java Service Wrapper
For problem 2, there are a lot of way to perform scheduling in Java. You can do it by built-in Timer etc, or using scheduling library like Quartz

Synchronize cronjobs on multiple servers

I am having multiple servers which run the same back-end process in JAVA through cron-tab scheduling.
Earlier I had just one server on which I used to schedule all jobs. This was easier to manage.
Now with increased servers and same process running on all servers with same code base, I want to schedule same cron-jobs on all machines.
I am facing problem, because I have to face lot of trouble and do manual work to change / update all servers if there is a single change.
Can anybody tell me some way to manage scheduling on multiple servers from a common point ?
Is there any tool / library / API etc to handle this kind of situation ?
As of now, I am updating / copying (using SCP) JAR on all servers and then restart all processes by individually logging into to each of the servers.
As of now, I will prefer If I do not have to change my JAVA code to manage this(In log run, I can think of that).
However, If I can create a small JAVA application (using Quartz) OR even a shell script to handle scheduling, I will go for it.
I want to be clear that updating JAR on all servers is not my problem, I am working on setup build script(ANT) for that.
My concern is common scheduling code for all servers.
Is there a way to schedule jobs on one server which starts jobs on remote server ? If this happens, I only have to manage scheduling on one server. I wont need to worry about clock sync issues as well ..
You can create a text file, containing the crontab entries in it, and when you deploy the jar file in each server, you can update the crontab at the same time as well, using ant, by running the command crontab mycrontab.txt.

Windows Scheduler Vs. Java TaskTimer

I have a .bat file in a Windows machine that starts our program by calling a main class of a Java executable(.Jar)
Now I need to run this every 30 mins.
I gone through several ways of doing it, but unable to decide which is better.
Scheduling through Windows scheduler or Using Java Timer. Which one to choose?
I want only one instance of the process running. If the previous process doesnt complete within 30min, i could wait.
Please let me know what to go for, based on my use case.
Thanks in advance.
You're better off using the Windows Scheduler. If there's a real risk of the process taking too long, you can create a file, or open a socket while the process is running and when another one tries to start up, it can detect that and simply quit. This would make it "miss" the 30m window (i.e. if the first job started at 12 and finished at 12:35, the next job would not start until 1).
But this way you don't have to worry at all about setting up long running processes, starting and stopping the java service, etc. The Windows scheduler just makes everything easier for something like this.
TimerTask is not a scheduling system, it is a library that provides tools for in-app scheduling. Seems that for your use-case you need a the system: you need it to run whether or not your app is running, you need reporting, etc. Windows Scheduler (or cron on unix/linux) is more appropriate for your needs.

Virtual Dedicated Server Question

I have a Virtual Dedicated Server, how can I run an application periodically (ie: every Monday - 19:00). I mean, the application starts automatically at a given time.
Is there any way to do it with Java, Ajax, PHP? (sorry, I'm noob)
You can create scheduled tasks in windows and cron jobs in linux/*nix like systems. If you can run programs on the server and want to use java you can use timers to schedule your task - here is a simple example.
I'm assuming your server is linux.
You may be able to do this with webmin.
You will need to use command line to install webmin and setting up a crontab from command line is really not that hard to do.
Here is a tutorial for crontab.
http://www.linuxweblog.com/crotab-tutorial
What OS does the server run?

Categories