I want to implement a project where I check system timings whenever I am logged in.
If I enter the office and log in my system the I should get the time and also when I go for a break I just lock my PC and go so at that time time should stop and again when I login it should start.
Basically it should show me the total time I was logged in my computer/PC.
In java you can obtain the time by several ways. Two of them are :
instanciate a new Date object :
Date myDate = new Date() ;
using :
System.currentTimeMillis()
I think you should think of using a software dedicated to that instead of implementing this in Java.
What you could do is this:
Create a Java application that can
log the time somewhere, using the
System.currentTimeMillis() or new
Date() approach.
Create a scheduled task in windows
that runs each time the user locks,
unlocks, logs on and logs off the
system. This scheduled task should
run your application. It should be
as simple as calling a batch file
which in turn invokes your Java
application.
The application should use all the
times captured to calculate the
effective time.
You can use System.currentTimeMillis() (or System.nanoTime()) for the start and the end, and then calculate the difference.
This will give you the time when you start / close Java. Linking this to system startup / system shutdown can be done by launching the application on startup. But that depends on the OS.
Related
I have multiple executable JARs, let's call them task1.jar, task2.jar, task3.jar, etc...
They mainly access the database and do housekeeping tasks on the server.
They are run by cron jobs, one after another, early in the morning everyday. The crob jobs are set about 15 minutes apart.
The crob jobs simply just call:
java -jar taskX.jar
Depending on the data on the database, their durations to finish will vary.
The task executions must not overlap. Otherwise when they access the database at the same time, the server will crash. It has happened before when one task takes too long to run and hasn't finished before another task has started.
So my question is... is there anyway to check a JAR has started or finished so that the next JAR can be delayed?
I would preferably resolve this with Java methods over changing any configs on server which I have limited access on.
Edit: #mksmanjit Sorry, I didn't mention before. There is also one complication - not all of them are run everyday. For example, task1.jar and task3.jar may be run everyday, task2.jar may be run every two days and task4.jar may be run every Mon, Wed, Fri, etc... So the situation can get quite complicated.
Couple of ways as I see it !
Let all JAR (or tasks) write Logs in a common or a separate log file. (Usually feasible if you only want to capture minor details such as start time , stop time, result etc) Assuming format you keep is manually readable.
As per above suggestion, create a Table in DB to keep a track of task activity along with task name, it's details such as trigger by, start time, end time, result etc
Alternatively, create database Triggers to monitor event (such as write or read) on DB table and whenever your task executes, trigger will also execute depending on what DB actions you want your trigger to perform such as sending e-mail, writing to table, printing simple log
Let JARs (or tasks) also print logs on console just in case assuming they are designed to view from any console or UI at runtime !
I am just curious on if it would be possible to write a java application and then have it run automatically during a certain time of day??
For example: If I had a program that would like open a text file and write the current date in it and then close. Would it be possible for me to get it to run at say 8:00 A.M. everyday without me having to run it?
The simplest solution would be to use a batch job scheduler, eg. cron on *nix or Windows scheduler. It can easily trigger your program to run at the desired time.
Otherwise, you could write a long running Java program that detects when 8am has rolled around and perform your desired action.
Also you can check janos♦'s answer below to Executing a task at a particular time in the morning using ScheduledExecutorService, hope it will help good luck...
https://codereview.stackexchange.com/questions/63520/executing-a-task-at-a-particular-time-in-the-morning-using-scheduledexecutorserv
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.
I'm working on a project that will record data on real time events using Java on a linux system.
I have all of the HTML scraping stuff down, that's fine, what I need to figure out is the scheduling and management of the tasks.
There are potentially up to forty events occurring each week, at varying times and events can last up to three hours.
I can create and update the calendar of these events at will, my problem is how to:
Schedule a process to scrape each event at the right time, and update the schedule if there's a change.
Ensure once the scrape process has begun that it stays running for the entire (indeterminate) duration of the event.
Can anyone advise how best to approach this? I'm not sure where I need to start.
Thanks!
a) Schedule a process to scrape each event at the right time, and
update the schedule if there's a change.
If you do not want to use a library, a good starting point for scheduling your tasks can be ScheduledExecutorService. Though you may find other scheduling frameworks useful for your problem out of which Quartz can specifically give you a flexibility in how to schedule the next task based on the current schedule execution results; it also provides a cron capability so that if your schedule is fixed, you can take advantage of a fixed scheduled calendar.
b) Ensure once the scrape process has begun that it stays running for
the entire (indeterminate) duration of the event.
Assuming that you're using a library for HTML scraping, you don't need to ensure it's running since it will be Java task object initiated from your application.
I've written an application in java and I want to add a feature to report the uptime of the application. Is there a Class/method in the JVM that can do this?
Should I save the timestamp when application starts, then calculate the difference with the current uptime in the moment of the request?
What's a better way to retrieve application uptime in Java?
You can use RuntimeMXBean.getUptime()
RuntimeMXBean rb = ManagementFactory.getRuntimeMXBean();
long uptime = rb.getUptime();
The result is in milliseconds:
RuntimeMXBean mxBean = ManagementFactory.getRuntimeMXBean();
System.out.println(mxBean.getUptime());
See: ManagementFactory.getRuntimeMXBean() javadoc.
It is just like Heartbeat application. IN which You have write small application which communicate to your java application. Whenever You application start you have to store the time with date in file/database. Monitor application will check the application running or not. The monitor application also take difference of end time and start time sand display the application time.
The solution I know is to use System.currentTimeMillis() as it's described here and here. There also was similar question