Get application uptime - java

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

Related

AWS ECS on EC2 - Set system date to a specific value

Each night a job is invoked which checks whether it is first of a month. During our testing, we can't wait for days and hence want to change the system date to a first of the month so that job gets invoked and processes all transactions logged into database so far.
We are using AWS ECS on EC2. Containers largely run Java (Spring Boot) but other containers run NodeJS and Spring batch jobs (short lived tasks). Now I want to be able to set all containers to use a specific date (main requirement; and may be time too and that means that launch with some specific time and then run their clocks as normal clocks). The long running tasks are auto-scaled (min 3 and max 10).
How can this be achieved?
Additional information
The database is in UTC and all Java containers have time zone set to 'America/New_York'.
Other option thought was to override Java System.currentTimeMillis() through Aspect oriented programming (AOP) but the AOP code needs to invoked from some place. It was thought to do it through API but there are 2 issues, a) Unable not make AOP code to work yet & b) How to call API on each container when it starts?
EC2 instances run using Red Hat Enterprise Linux.

Fetching JVM timezone on Linux Box

Is it possible to get the TIMEZONE set in JVM using a command in Linux? I am trying to fetch this information from a production box where I wouldn't able to deploy any code to check.
JVM makes some statistics about the app available via JMX, and some are available by default, so it's rather likely you will be able to retrieve them from your app, too. You can use jconsole tool to connect to a Java app and read those values. I don't see the clock time being one of them, but you can check the application's startup time, and the uptime. Adding one to the other gives you the current time as the app sees it. You can find both values in jconsole in the "MBeans" tab: in the tree on the left select: java.lang / Runtime / Attributes - they will be called "Uptime" and "StartTime". Uptime is in milliseconds and StartTime is in milliseconds since January 1st, 1970.
PS: If the above doesn't work, you can try to retrieve the time indirectly. The time returned by System.currentTimeMillis() is based on the machine's clock, so it should be the same as returned by any other program that queries the clock, e.g. the command-line date command. One possible deviation would be the possibility of the java application using a different time zone, e.g. due to environment variables set differently than for your command-line program.
no. this is not possible. you cannot get the time set in JVM using a command in linux. for further information run "java" command in command prompt or console and you will see which commands JVM can receive directly

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.

How to measure time when being logged in

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.

Categories

Resources