add new core-java-os module

This commit is contained in:
Loredana 2019-03-11 23:02:10 +02:00
parent 7f24cbad71
commit 63b652270a
17 changed files with 161 additions and 19 deletions

View File

@ -9,7 +9,6 @@
- [Java 9 Convenience Factory Methods for Collections](http://www.baeldung.com/java-9-collections-factory-methods)
- [New Stream Collectors in Java 9](http://www.baeldung.com/java9-stream-collectors)
- [Java 9 CompletableFuture API Improvements](http://www.baeldung.com/java-9-completablefuture)
- [Java 9 Process API Improvements](http://www.baeldung.com/java-9-process-api)
- [Introduction to Java 9 StackWalking API](http://www.baeldung.com/java-9-stackwalking-api)
- [Introduction to Project Jigsaw](http://www.baeldung.com/project-jigsaw-java-modularity)
- [Java 9 Optional API Additions](http://www.baeldung.com/java-9-optional)
@ -25,7 +24,6 @@
- [Iterate Through a Range of Dates in Java](https://www.baeldung.com/java-iterate-date-range)
- [Initialize a HashMap in Java](https://www.baeldung.com/java-initialize-hashmap)
- [Java 9 Platform Logging API](https://www.baeldung.com/java-9-logging-api)
- [Guide to java.lang.Process API](https://www.baeldung.com/java-process-api)
- [Immutable Set in Java](https://www.baeldung.com/java-immutable-set)
- [Multi-Release Jar Files](https://www.baeldung.com/java-multi-release-jar)
- [Ahead of Time Compilation (AoT)](https://www.baeldung.com/ahead-of-time-compilation)

26
core-java-os/.gitignore vendored Normal file
View File

@ -0,0 +1,26 @@
*.class
0.*
#folders#
/target
/neoDb*
/data
/src/main/webapp/WEB-INF/classes
*/META-INF/*
.resourceCache
# Packaged files #
*.jar
*.war
*.ear
# Files generated by integration tests
*.txt
backup-pom.xml
/bin/
/temp
#IntelliJ specific
.idea/
*.iml

8
core-java-os/README.md Normal file
View File

@ -0,0 +1,8 @@
=========
This module uses Java 9, so make sure to have the JDK 9 installed to run it.
##
### Relevant Articles:
- [Java 9 Process API Improvements](http://www.baeldung.com/java-9-process-api)
- [Guide to java.lang.Process API](https://www.baeldung.com/java-process-api)

82
core-java-os/pom.xml Normal file
View File

@ -0,0 +1,82 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.baeldung</groupId>
<artifactId>core-java-os</artifactId>
<version>0.1.0-SNAPSHOT</version>
<name>core-java-os</name>
<packaging>jar</packaging>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-java</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../parent-java</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>${commons-collections4.version}</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>${commons-io.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${commons-lang3.version}</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<!-- test scoped -->
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${assertj.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>core-java-os</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<!-- util -->
<commons-lang3.version>3.5</commons-lang3.version>
<commons-collections4.version>4.1</commons-collections4.version>
<collections-generic.version>4.01</collections-generic.version>
<!-- testing -->
<assertj.version>3.6.1</assertj.version>
<asspectj.version>1.8.9</asspectj.version>
<maven.compiler.source>1.9</maven.compiler.source>
<maven.compiler.target>1.9</maven.compiler.target>
<guava.version>25.1-jre</guava.version>
</properties>
</project>

View File

@ -3,5 +3,5 @@ package com.baeldung.java9.process;
public class ProcessCompilationError {
//This method has been written to generate error to display
//how process errorStream() can consume error
public static void();
//public static void();
}

View File

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="STDOUT" />
</root>
</configuration>

View File

@ -18,13 +18,13 @@ import org.slf4j.LoggerFactory;
public class ProcessAPIEnhancementsUnitTest {
Logger log = LoggerFactory.getLogger(ProcessAPIEnhancementsTest.class);
Logger log = LoggerFactory.getLogger(ProcessAPIEnhancementsUnitTest.class);
@Test
public void givenCurrentProcess_whenInvokeGetInfo_thenSuccess() throws IOException {
ProcessHandle processHandle = ProcessHandle.current();
ProcessHandle.Info processInfo = processHandle.info();
assertNotNull(processHandle.getPid());
assertNotNull(processHandle.pid());
assertEquals(false, processInfo.arguments()
.isPresent());
assertEquals(true, processInfo.command()
@ -51,7 +51,7 @@ public class ProcessAPIEnhancementsUnitTest {
.start();
ProcessHandle processHandle = process.toHandle();
ProcessHandle.Info processInfo = processHandle.info();
assertNotNull(processHandle.getPid());
assertNotNull(processHandle.pid());
assertEquals(false, processInfo.arguments()
.isPresent());
assertEquals(true, processInfo.command()
@ -72,7 +72,7 @@ public class ProcessAPIEnhancementsUnitTest {
Stream<ProcessHandle> liveProcesses = ProcessHandle.allProcesses();
liveProcesses.filter(ProcessHandle::isAlive)
.forEach(ph -> {
assertNotNull(ph.getPid());
assertNotNull(ph.pid());
assertEquals(true, ph.info()
.command()
.isPresent());
@ -102,12 +102,12 @@ public class ProcessAPIEnhancementsUnitTest {
Stream<ProcessHandle> children = ProcessHandle.current()
.children();
children.filter(ProcessHandle::isAlive)
.forEach(ph -> log.info("PID: {}, Cmd: {}", ph.getPid(), ph.info()
.forEach(ph -> log.info("PID: {}, Cmd: {}", ph.pid(), ph.info()
.command()));
Stream<ProcessHandle> descendants = ProcessHandle.current()
.descendants();
descendants.filter(ProcessHandle::isAlive)
.forEach(ph -> log.info("PID: {}, Cmd: {}", ph.getPid(), ph.info()
.forEach(ph -> log.info("PID: {}, Cmd: {}", ph.pid(), ph.info()
.command()));
}
@ -121,12 +121,12 @@ public class ProcessAPIEnhancementsUnitTest {
.start();
ProcessHandle processHandle = process.toHandle();
log.info("PID: {} has started", processHandle.getPid());
log.info("PID: {} has started", processHandle.pid());
CompletableFuture<ProcessHandle> onProcessExit = processHandle.onExit();
onProcessExit.get();
assertEquals(false, processHandle.isAlive());
onProcessExit.thenAccept(ph -> {
log.info("PID: {} has stopped", ph.getPid());
log.info("PID: {} has stopped", ph.pid());
});
}

View File

@ -30,7 +30,7 @@ public class ProcessApiUnitTest {
@Test
public void processInfoExample() throws NoSuchAlgorithmException {
ProcessHandle self = ProcessHandle.current();
long PID = self.getPid();
long PID = self.pid();
ProcessHandle.Info procInfo = self.info();
Optional<String[]> args = procInfo.arguments();
Optional<String> cmd = procInfo.commandLine();
@ -45,7 +45,7 @@ public class ProcessApiUnitTest {
Stream<ProcessHandle> allProc = ProcessHandle.current().children();
allProc.forEach(p -> {
System.out.println("Proc " + p.getPid());
System.out.println("Proc " + p.pid());
});
}
@ -54,7 +54,7 @@ public class ProcessApiUnitTest {
public void createAndDestroyProcess() throws IOException, InterruptedException {
int numberOfChildProcesses = 5;
for (int i = 0; i < numberOfChildProcesses; i++) {
createNewJVM(ServiceMain.class, i).getPid();
createNewJVM(ServiceMain.class, i).pid();
}
Stream<ProcessHandle> childProc = ProcessHandle.current().children();
@ -62,10 +62,10 @@ public class ProcessApiUnitTest {
childProc = ProcessHandle.current().children();
childProc.forEach(processHandle -> {
assertTrue("Process " + processHandle.getPid() + " should be alive!", processHandle.isAlive());
assertTrue("Process " + processHandle.pid() + " should be alive!", processHandle.isAlive());
CompletableFuture<ProcessHandle> onProcExit = processHandle.onExit();
onProcExit.thenAccept(procHandle -> {
System.out.println("Process with PID " + procHandle.getPid() + " has stopped");
System.out.println("Process with PID " + procHandle.pid() + " has stopped");
});
});
@ -73,14 +73,14 @@ public class ProcessApiUnitTest {
childProc = ProcessHandle.current().children();
childProc.forEach(procHandle -> {
assertTrue("Could not kill process " + procHandle.getPid(), procHandle.destroy());
assertTrue("Could not kill process " + procHandle.pid(), procHandle.destroy());
});
Thread.sleep(5000);
childProc = ProcessHandle.current().children();
childProc.forEach(procHandle -> {
assertFalse("Process " + procHandle.getPid() + " should not be alive!", procHandle.isAlive());
assertFalse("Process " + procHandle.pid() + " should not be alive!", procHandle.isAlive());
});
}

View File

@ -14,7 +14,7 @@ import java.lang.Integer;
import org.junit.jupiter.api.Test;
class ProcessUnderstandingTest {
class ProcessUnderstandingUnitTest {
@Test
public void givenSourceProgram_whenExecutedFromAnotherProgram_thenSourceProgramOutput3() throws IOException {

View File

@ -0,0 +1,13 @@
*.class
#folders#
/target
/neoDb*
/data
/src/main/webapp/WEB-INF/classes
*/META-INF/*
# Packaged files #
*.jar
*.war
*.ear

View File

@ -379,6 +379,7 @@
<!-- <module>core-java-11</module> --> <!-- We haven't upgraded to java 11. Fixing in BAEL-10841 -->
<module>core-java-8</module>
<!--<module>core-java-9</module> --> <!-- We haven't upgraded to java 9. Fixing in BAEL-10841 -->
<!--<module>core-java-os</module> --> <!-- We haven't upgraded to java 9.-->
<module>core-java-arrays</module>
<module>core-java-collections</module>
<module>core-java-collections-list</module>
@ -1015,6 +1016,7 @@
<!-- <module>core-java-11</module> --> <!-- We haven't upgraded to java 11. Fixing in BAEL-10841 -->
<module>core-java-8</module>
<!--<module>core-java-9</module> --> <!-- We haven't upgraded to java 9. Fixing in BAEL-10841 -->
<!--<module>core-java-os</module> --> <!-- We haven't upgraded to java 9.-->
<module>core-java-arrays</module>
<module>core-java-collections</module>
<module>core-java-collections-list</module>