Java: (Threads/Timer) Run a function everyday at say 6AM - java

I have my application on Windows platform and want a java function to be executed everyday at some particular period of time. Need some guidance how to go about it. Have already looked for some previous posts but need some understanding of which method to use and how?
Thanks.

If you're on Windows, use scheduled task
If you're on Linux/Unix, use cron

You may find Quartz of use. It's a Java framework which provides the ability to invoke tasks at particular intervals, at particular times of day etc.
So you can invoke Tomcat and the Quartz framework - built into your app and suitably configured - can invoke methods at particular intervals/times of day.

I made the same process in C#.
-First Create table that includes time table that when you want to run your function
- Get time from table and calculate interval like after checking adding +1 day or 24 hours to your time which you get from table.
I made it for schedule in university.I used dayname,date,time.Like this you can control also which day ,which time will run your function

Since you are using JSP and having your own server, you might set it as a scheduled task (Windows) to run at 6AM Everyday
First this is called Cron Jobs
Go to the Task Scheduler
Win 7:
Start -> All Programs -> Accessories -> System Tools -> Task Scheduler
From the right panel choose "Create Basic Task ..."
Give the task a name, hit next.
In the trigger tab, choose daily, then click next.
The next tab will let you set the specified date to start launching the trigger, the time, set the time and hit next.
In the Action tab, choose "Start A Program", then Next/.
In the next tab, click browse next to the program/script field, then navigate to the location of the Apache tomcat Server and choose the application.
(for the appache from WAMP Package: wamp\bin\apache\Apache2.2.11\bin\httpd.exe) tom cat will difer a bit I guess.
hit Next then Check the "Open the Properties Dialog for this task when I click Finish" then click Finish.
Now go to the action tab, select the first and only action available, click edit.
in the program/script field and modify type a space, then URL
change the URL to the URL you use to access your page.
This will let you be able to launch the script everyday at 6AM.

Related

One-time Java Configuration file on a desktop app

I am doing now a desktop app like a organizer. When the program starts, it has 0 tasks, obviously. I go adding more tasks, but when I close the program and then restart it, all the tasks I have done will be on null.
I think I can use a file that the program will consult. This file will have all the relevant information for load all the tasks.
But, and problem comes here, I want to make a question like "Where do you want to save the configuration file?" It is like Eclipse`s message at start with the workspace. The idea is that the message will only be show until the user specify a valid route.
Can we do this in Java?
Use java.util.prefs.Preferences: https://docs.oracle.com/javase/8/docs/api/java/util/prefs/package-summary.html

File location listener in Java - Spring application

I have a Java - Spring application. I want to set up a feature to listen a file location(Folder) and has to process a file as a file get created in this folder.
My plan is to set up a spring quartz cron job which will run in every five minutes and process the files available.
Please suggest me a better approach.
A choice of solution depends on how quickly an appearance of a waited file must be detected.
If it is not urgently and at least 1 minutes would be OK, then your solution using any kind of cron job would be OK too.
If a file should be detected asap, then you must provide a permanent running program, that detects a file in very short intervals. This can be reached by for this task dedicated daemon. If your program has nothing to do during wait time, then it can contain part for the file detection.
If you have moved on to/are on java8, it provides Watch Service API that allows us to monitor file and folder locations.
Have a look at Watch Service API

executing code every 24h, but only if app is opened

I want to execute specific code in my app every 24h.
I know how to use AlarmManager but problem is I don't want that code to be executed if app isn't running.
I want it to be executed when app starts but only if 24h has passed after last execution.
It doesn't matter if more 24h has passed
For example if user opens my app for the first time code will execute. If it opens it again after 20min nothing will happen. if it opens it after 24h or more code will be executed.
I would save to a file, the time the program was started. You can check this before writing to see how long it has been.
Note: if there is a task you want to run at least 24h apart, you really want the last time the task was performed. e.g. if the application is opened every 8h you still want it to run.
Why don't you use Shared Preferences storage option provided by Android platform to store last access information and then act accordingly.
For more details refer this link: Android developer reference for Storage Options

schedule java program

I want to run my java program in regular interval , lets say, in every 3 hours. I am thinking to write a .bat file and put command to call java class. But what is the best way to run .bat regularly in windows xp. Thanks in advance. I dont want to use third party tool.
Windows scheduled tasks are built for exactly that purpose.
You can run things on multiple schedules (so you can get your every-three-hour behaviour) and you can get your code to run whether or not logged in.
The multiple scheduling is a bit tricky. You basically set it up as a daily task to start with but, near the end, it will ask you if you want to do advanced features.
Select yes then you can set up multiple schedules at that point.
If you want a pure Java Based solution you can try QUARTZ SCHEDULER. But as paxdiablo already mentioned, Windows Task Scheduler will do that as well.

Run an application as background process

I have developed a Java application. I want to run it as a background process.
It should not be displayed as Running Application in task Manager, but it should be displayed in Processes List.
Help me out.
Regards,
Jigar
I've edited your question since it's a pure windows issue.
I would suggest you to take a look at ways to run your java application as a windows service.
In addition to Command Prompt option given by Rafael Roman, we can also schedule task through Control Panel -> Scheduled Tasks -> Add Schedued Task. For more info:
http://technet.microsoft.com/en-us/library/cc738106(v=ws.10).aspx
There's no need for complex solutions.
use the at commando and create a bat that runs your program.
type in any cmd windows:
at 10:00 c:\run_my_script.bat
remember to replace 10:00 to a time 1 minute after the current time of your machine

Categories