How to program Java app to execute everyday at a certain time? - java

My computer is turned on 7/24. However, it does gets locked. I have an app that includes Selenium and other APIs. I need it to execute everyday at say 6 AM. Is there any way I could do this. I heard about the Windows Scheduler. Is there any way to make it execute on its own? Or waht is the best way to do this. (I have Windows 7)

What you need is a Quartz scheduler. Please find the link for the same : http://quartz-scheduler.org/
Using quartz scheduler you can schedule a job to run everyday at 6 AM using cron expressions. You can create cron expressions using www.cronmaker.com

Related

Is it possible to run a code at certain time period?

So, I am writing a Java code where I want the program to take content from the web every monday 12 am.
I found SO many answers telling me how to run a program for certain amount of time. I want to run the program and execute every monday morning.
I want to know if any one has ANY idea where to start?
I found of AlarmManager for android applications (which is sort what I want) but I want it for Java program not application.
Or is this even possible?
Sure! If you are on linux or mac, just set up a cronjob to run at 0 0 * * 1
It'd be something like:
java MyScript.java
as the task.
Try this:
Scheduled Tasks With Cron for Java
http://quartz-scheduler.org/ is the standard tool to setup cron-like jobs
Yes it is using Windows Task Manager!

Job that runs java class and updates web server via AWS

I'm new to web servers. I have a java class that does a set of computations. I want to have this java class run every hour and update my domain on AWS, with the data.
My question is how/where do I set this job to run?
Is there a standard for this? Or does AWS have something I can use? I know how to read/write my data to AWS.
Should a cron job be used? Should the cron job run on AWS?
You have 2 options for this.
Set a cron job and let the operating system execute the script that starts your java program every hour or so.
Use something like Quartz Scheduler. In this case your Java program would be running continuously and the scheduler would be within your Java program.
There are various advantages and disadvantages to both approaches. In the first case the advantage is that if something wrong happens to the program, you know that in the next hour a new process with a fresh new instance of your program will launch, while in the second case if your Java program hangs for some reason you won't know unless you have some kind of monitoring. However, in case 2 you can maintain some kind of state information you might want to keep between runs. Quartz has also lots of advanced features, like maintaining info about executions in a database.
You can also have the Quartz Scheduler run within your webserver itself (so no need for another process). Its just an extra few .jar files to include. So it depends what you actually want to do. You can refer to what features it supports here.

Execute code when system time reaches a specific time [duplicate]

This question already has answers here:
run a Java program in specific time
(2 answers)
Closed 9 years ago.
How can i implement code to do something when a specific system time is reached?
The only solution i have thought of is using a timer to "tick" every few minutes or hours to check if the specific time has been reached.
Are there any other better solutions ?
Thanks.
sorry if i had not been clear, i would be implementing the code inside my Java program, it is to clear records of a log before a new day is coming and save the records.
Example: Clear the current records and save these records at 23:59.
In pure Java, there is a Timer class. This is useful if you have a program running already. Or you are running a web app that is always up.
Another alternative is to use operating system (UNIX cron) and have it start the Java program at that time. This is useful if you don't meet the conditions for Timer.
You can just make a timer with a long duration. If the trigger time will be 350 minutes from now, there's no point having a timer poll every minute to see if the time is reached. Just set your timer to 350 minutes. Once it fires, remove the timer. This is called a one-shot timer. I can't answer how to specifically do this in Java, unfortunately.
If you are using Unix-like systems have a look at cron
If you are on Windows have a look at What is the Windows version of cron?
if i understand your question correctly, for unix, you can put your code in crontab and schedule it to run at specific system time. while for windows, you can use task scheduler. this is how we do it to run specific test scripts for nightly builds.
Quartz Scheduler Framework is an enterprise class framework that can be used as a Timer.
did you try Quartz Scheduler? , it is a powerful and advance scheduler framework, to help Java developer to scheduler a job to run at a specified date and time.click here for more
I think java.util.concurrent.ScheduledExecutorService is enough.

Java/Database project automation

I have a Java/Database project in Netbeans that I would like to run once a day at a set time. I am using Derby for the database driver. I am trying to automate a process.
How can I 'schedule' this program to run at specified times?
How can I customize this to keep running until a certain criteria is met?
Say my criteria is that It has to populate 500 rows in the database. (So say at the scheduled time it runs it can only populate 400 rows, then maybe 2 hours later it tries running again to fill the last 100 rows)
Lastly, what are the best practices of automation and scheduled tasks?
How can I 'schedule' this program to run at specified times?
This can be done one of two ways, depending on your operating system - write a job that kicks off the java program at the intervals you need. You may then hook up the job to be started off on start up.
In Linux you can accomplish this with a cron job or so. On windows you may refer to this http://support.microsoft.com/kb/308569.
You may also program the scheduler into your java program using http://quartz-scheduler.org or http://www.sauronsoftware.it/projects/cron4j/ .
How can I customize this to keep running until a certain criteria is met?
This is perhaps best established from within your program, although it is hard to give you directions without much info.
Lastly, what are the best practices of automation and scheduled tasks?
Depending on your application architecture, scheduling and automation can be handled either from within the app or get support from the operating system. The criteria depends on how much control the application needs, which platform makes scheduling easy etc.
Hope this helps.
Quartz is a scheduling project for Java. I have used it in many projects and find it to be very intuitive.
It may be a little over the top for what your after but worth a look anyway.
You can make use of Timer for scheduling the events & the events/task must be implemented using TimerTask

What framework to use for advanced job scheduling in Java?

In my application I need to have periodically run background tasks (which I can easily do with Quartz - i.e. schedule a given job to be run at a specific time periodically).
But I would like to have a little bit more control. In particular I need to:
have the system rerun a task that wasn't run at its scheduled time (i.e. the server was down and because of this the task was not run. In such a situation I want the 'late' task to be run ASAP)
it would be nice to easily control tasks - i.e. run a task on demand or see when a given task was last run or reschedule a given task to be run at a different time
It seems to me that the above points can be achieved with Spring Batch Admin, but I don't have much experience in this area yet. Also, I've seen numerous posts on how Spring Batch is not a scheduling tool so I'm becoming to have doubts what the right tool for the job is here.
So my question is: can the above be achieved with Spring Batch Admin? Or perhaps Quartz is enough but needs configuring to do the above? Or maybe I need both? Or something else?
Thanks a lot :)
Peter
have the system rerun a task that wasn't run at its scheduled time
This feature in Quartz is called Misfire Instructions and does exactly what you need - but is a lot more flexible. All you need is to define JDBCJobStore.
it would be nice to easily control tasks - i.e. run a task on demand or see when a given task was last run or reschedule a given task to be run at a different time
You can use Quartz JMX to access various information (like previous and next run time) or query the Quartz database tables directly. There are also free and commercial management tools basex on the above input. I believe you can also manually run jobs there.
Spring Batch can be integrated with Quartz, but not replace it.

Categories

Resources