DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • How to Introduce a New API Quickly Using Micronaut
  • How to Merge HTML Documents in Java
  • Designing a Java Connector for Software Integrations
  • Jakarta NoSQL 1.0: A Way To Bring Java and NoSQL Together

Trending

  • Intro to RAG: Foundations of Retrieval Augmented Generation, Part 2
  • Next Evolution in Integration: Architecting With Intent Using Model Context Protocol
  • Building Reliable LLM-Powered Microservices With Kubernetes on AWS
  • AI-Driven Root Cause Analysis in SRE: Enhancing Incident Resolution
  1. DZone
  2. Data Engineering
  3. Databases
  4. Java 9: HTTP Client and Process API in JShell

Java 9: HTTP Client and Process API in JShell

Take a look at what you can do with Java 9's new HTTP/2 Client and the Process API all through JShell, the new REPL for Java!

By 
Martin Farrell user avatar
Martin Farrell
·
Updated Oct. 11, 17 · Tutorial
Likes (17)
Comment
Save
Tweet
Share
17.5K Views

Join the DZone community and get the full member experience.

Join For Free

This post continues my exploration of Java9 features from my My Top Java 9 Features blog post. Here we are experimenting with the Java 9 HTTP/2 Client and Process API in JShell.

HTTP/2 Client

The HTTP/2 Client is an incubator project in Java 9. This means the API isn’t finalized, so it has some scope for change in future versions. The most obvious changes from Java 9 to Java 10 will be moving it from the jdk.incubator.httpclient module to the “http.client” module, plus associated package name changes. It's worth keeping this in mind when using the API.

HTTP/2 doesn't work straight out the box in JShell, but it's ok, as it lets us see the Java Platform Modularity System (JPMS) in action. We simply pass the httpclient module into JShell using
–add-modules:

C:\jdk9TestGround>jshell -v --add-modules jdk.incubator.httpclient


We then import the HTTP libraries:

jshell> import jdk.incubator.http.*;


We can now call a website from JShell:

jshell> HttpClient httpClient = HttpClient.newHttpClient();
jshell> HttpRequest httpRequest = HttpRequest.newBuilder().uri(new URI("https://www.javabullets.com")).GET().build();
jshell> HttpResponse<String> httpResponse = httpClient.send(httpRequest, HttpResponse.BodyHandler.asString());
jshell> System.out.println(httpResponse.statusCode());
jshell> System.out.println(httpResponse.body());


The most interesting feature is the use of the Builder pattern to create the HTTP Request. This is defined in HttpRequest.Builder, which can be used to construct more complex HttpClient requests including authentication.

The syntax is similar to both the Jetty HttpClient and okhttp, which are Http/2-compliant. It is definitely much simpler than the old approach in Java.

Other useful features of this API are:

  • Asynchronous requests: This is more useful than the above example, as it is non-blocking. This is done through the HttpRequest.sendAsync method.
  • WebSockets: This is created through the WebSocket class, which has its own WebSocket.Builder. I’m going to cover this in another post, as it's clearer than on JShell.

Process API

The Process API simplifies accessing process information in Java.

Consider the details of my current JShell process:

jshell> System.out.println(ProcessHandle.current().pid());
8580

jshell> System.out.println(ProcessHandle.current().info());
[user: Optional[NEW-EJ0JTJ5I9B9\javabullets], cmd: C:\Program Files\Java\jdk-9\bin\java.exe, startTime: Optional[2017-10-09T19:41:21.743Z], totalTime: Optional[PT4.625S]]

jshell> System.out.println(ProcessHandle.current().parent());
Optional[6592]


We can also access system processes and Ids:

jshell> ProcessHandle.allProcesses().forEach(p -> System.out.println(p.pid()));
8276
9720
8012
480


Or info:

jshell> ProcessHandle.allProcesses().forEach(p -> System.out.println(p.info()));

[user: Optional[NEW-EJ0JTJ5I9B9\javabullets], cmd: C:\Program Files (x86)\PFU\ScanSnap\Update\ScanSnapUpdater.exe, startTime: Optional[2017-10-09T18:28:42.812Z], totalTime: Optional[PT0.78125S]]
[user: Optional[NEW-EJ0JTJ5I9B9\javabullets], cmd: C:\Windows\explorer.exe, startTime: Optional[2017-10-09T18:35:08.397Z], totalTime: Optional[PT25.234375S]]
[user: Optional[NEW-EJ0JTJ5I9B9\javabullets], cmd: C:\Windows\System32\cmd.exe, startTime: Optional[2017-10-09T18:36:11.522Z], totalTime: Optional[PT0.078125S]]


Now we have access to processes we can kill selective process – let's kill Notepad:

jshell> Process p = new ProcessBuilder("C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe").start();
p ==> Process[pid=9644, exitValue="not exited"]

jshell> p.destroy();


We also have the option to destroyForcibly if destroy doesn't kill the process.

The above examples show how useful and simple the Process API is for starting, killing, and monitoring processes. It frees us from relying on third-party libraries for providing process control.

API Java (programming language) JShell

Published at DZone with permission of Martin Farrell, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • How to Introduce a New API Quickly Using Micronaut
  • How to Merge HTML Documents in Java
  • Designing a Java Connector for Software Integrations
  • Jakarta NoSQL 1.0: A Way To Bring Java and NoSQL Together

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!