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
Related
I am developing an app which has multiple features which need to perform work in the background.
Each feature needs to run at the same time and perform some work parallelly.
Currently, I am using JobIntentService to perform work but I have noticed that when I try to run multiple JobIntentServices then only one run while others are made to wait and executed one by one.
I am planning to use the Service. I want to know can 5 services run at the same time and perform work parallelly?
If no then is there any alternative?
Any help would be grateful.
Yes. It is possible to run services parallelly in Android.
Yes they should be able to run parallel (as pointed out by #Omkar), depending on the work load that the device is already having. The reason your JobIntentServices weren’t running parallel is because the OS will choose when to call those services when it thinks is best, thus sometimes they won’t run “right away”, using a Service would be the best solution for parallel running work that needs to be ran ASAP
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.
I am working on an application in which I want multiple tasks to be executed simultaneously.
I also want to be able to keep track of the number of such tasks being run in parallel, and sometimes add yet another task to be processed in parallel, in addition to the current set of tasks already being processed.
One more thing- I want to do the above, not only in a desktop app, but also in a cloud app, in which I initialise another virtual machine running Tomcat, and then repeat all of the above in that instance.
What is the best way to do this? If you can point me to the correct theory/guides on this subject, that would be great, although code samples are also welcome.
Concurrency is a huge topic in Java, please take your time for it
Lesson: Concurrency
Concurrency in a Java program is accomplished by starting your own Threads. Multiple processes can only be realized with multiple JVMs. When you are done with the basics, you want to take a look at Executors. They will help to implement your application in a structured way since they abstract from Threads to Tasks.
I don't know how much time you have planned for this, but if you are really at the start, get Java Concurrency in Practice, read it and write a kick-ass concurrent Java application.
Raising the whole thing to a distributed level is a whole other story. You cannot tackle that all at once.
Wow... What a series of steps. Start by extending Runnable, then using Thread to run and manage your Jobs. After that, you can get into Tomcat.
I am new to Java development, and looking for general design patterns for a data collection application written in Java.
I have already written the prototype, which is a basic Java console application that uses SAX to retrieve data and store it in a database.
Obviously, this is not a Web app, so it doesn't need to run in a container like Tomcat, but what would people recommend? The application currently uses a basic Java timer to run every 5 minutes.
So the basic requirements that I can think of are
It needs to run all the time, so if it crashes, it needs to be restarted.
It needs to do its work every 5 minutes, so it needs a timer.
It could use Hibernate, but not if it creates any overhead, as this is a highly
date intensive application.
So what I am looking for are suggestions like:
You could run a timer widget thingumbob under Tomcat anyway and get requirement #1.... or Spring 99 has all of the features you need.
etc.
For this type of application you could have a main process that spawns a thread that does the actual work. This thread would be in a loop that basically checks to see if it supposed to be running or not. If it is running it continues. Once it does its work you can use Thread.sleep(msToSleep) to put the thread to sleep for 5 minutes. So it would go in a continuous cycle of working and sleeping. Not timer required. The main process can "ping" the thread to see if it is still functional and if it is not spawn a new thread. Depending on the OS there are similar techniques to make sure the main process is running. Using an ORM like Hibernate will add overhead so you will have to way the trade-offs between transaction performance and ease of development. If you are converting your data to objects yourself you will have to use a profiler to see if you are actually implementing it more efficiently than an ORM can.
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.