Overview

Java 9 is adding a new way of getting information about current and any spawned processes.

The primary goal of the developers is, to make it easier for you to manage processes on your operating system. They start with giving you the ability to enumerate the system processes via the JEP 102: Process API Updates. That means, you can get different properties for each process, e.g. the PID, the process name, the user who started it, the path of the process, resource usage, etc.

Why should we use the Process API?

Modern software development has become increasingly concerned about its runtime and deployment concerns.

Historically, developers might just raise a ticket with an “ops” team to tell them to deploy the latest version of their software. Now many teams automate their build and deploy environments, deliver automatically updating software to their customers and can go from development machine to production with just a git push. Devops, SREs, doesn’t matter what you call it, it’s a good thing.

Current Process ID by ProcessHandle

ProcessHandle identifies and provides control of native processes. Each individual process can be monitored for liveness, list its children, get information about the process or destroy it.

In Java 9 we just write the following to get current process:

We use ProcessHandle.current() to get the ProcessHandle reference for our own process. ProcessHandle is a new interface that exposes a lot of functionality for inspecting and understanding processes.

The method pid() on ProcessHandle returns the process id of the current or asked process.

ProcessHandle.Info | Current Java Process Information

ProcessHandle class provides info() method which returns an instance of ProcessHandle.Info, this instance can be used to get the following details:

  • Start time of the process.
  • User of the process.
  • Total CPU time accumulated of the process
  • Executable path name of the process
  • Arguments if any of the process.

Output:

Optional[2017-08-18T09:16:42.826Z] Optional[codenuclear-red] Optional[PT0.265629S] Optional[C:\Program Files\Java\jdk-9\bin\javaw.exe] Optional.empty

It is important to note that java.lang.ProcessHandle.Info is a public interface defined within another interface java.lang.ProcessHandle.

Spawned Process Information

It is also possible to get the process information of a newly spawned process. In this case, after we spawn the process and get an instance of the java.lang.Process, we invoke the toHandle() method on it to get an instance of java.lang.ProcessHandle.

Enumerate Process List

We can list all the processes currently in the system, which are visible to the current process. The returned list is a snapshot at the time when the API was invoked, so it’s possible that some processes terminated after taking the snapshot or some new processes were added.

In order to do that, we can use the static method allProcesses() available in the java.lang.ProcessHandle interface which returns us a Stream of ProcessHandle. As there are many processes returned.

Here we are filter out those methods which do not having any command associated:

Triggering Actions on Process Termination

onExit() method of java.lang.ProcessHandle makes it possible for a thread to use this method’s returned java.util.concurrent.CompletableFuture object to trigger actions to run synchronously or asynchronously when the process on which they depend terminates.

First it creates and starts a notepad.exe process. Next, it obtains this process’s handle and uses this handle to obtain a completable future for the process. The onExit.get() call causes main() to wait for process termination, at which point thenAccept() executes its lambda, producing output similar to that shown here:

Started PID:10468 Stopped PID:10468

Conclusion

The improvements in the Process API offer Java programmers a much better story when it comes to writing automated tooling for deploying, managing and upgrading their runtime.

It's good to share...Share on FacebookTweet about this on TwitterShare on LinkedInPin on PinterestShare on Google+Email this to someone

Leave a Reply

Your email address will not be published. Required fields are marked *