module-info.java
file
I do not understand all the moving parts, but I did succeed somehow in accessing the new Project Loom features, virtual threads & structured concurrency, being previewed and incubated respectively in Java 19.
Here is my main
method.
record Event( UUID id , Instant when , Integer reading ) {}
try ( var scope = new StructuredTaskScope.ShutdownOnFailure() )
{
Future < UUID > futureId = scope.fork( ( ) -> UUID.randomUUID() );
Future < Instant > futureWhen = scope.fork( ( ) -> Instant.now() );
Future < Integer > futureReading = scope.fork( ( ) -> ThreadLocalRandom.current().nextInt( 1 , 10 ) );
scope.join(); // Join both forks
scope.throwIfFailed(); // ... and propagate errors
Event event = new Event( futureId.get() , futureWhen.get() , futureReading.get() );
System.out.println( event );
}
catch ( InterruptedException e )
{
throw new RuntimeException( e );
}
catch ( ExecutionException e )
{
throw new RuntimeException( e );
}
That code uses these imports:
import java.time.Instant;
import java.util.UUID;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadLocalRandom;
When run:
Event[id=de316aca-10b1-41ed-bfc6-732c4e184566, when=2022-08-07T03:04:48.207650Z, reading=9]
I added a module-info.java
file outside my package hierarchy, at src/main/java
.
module LoomEx {
requires jdk.incubator.concurrent;
}
And my POM
. I started with the Apache Maven QuickStart archetype at listed. Then I updated all the version numbers to the latest.
<?xml version="1.0" encoding="UTF-8"?>
<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>work.basil.example</groupId>
<artifactId>Loom</artifactId>
<version>1.0-SNAPSHOT</version>
<name>Loom</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.release>19</maven.compiler.release>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.9.0</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (maybe moved to parent pom) -->
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.2.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.3.0</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.10.1</version>
<configuration>
<!--<compilerVersion>19</compilerVersion>-->
<release>19</release>
<!--<compilerArgs>--source 19</compilerArgs>-->
<compilerArgs>--enable-preview</compilerArgs>
<!--<compilerArgs>--add-modules jdk.incubator.concurrent</compilerArgs>-->
</configuration>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M7</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>3.0.1</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>3.0.0</version>
</plugin>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>4.0.0-M3</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.4.0</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
I tried using various Maven POM elements taken from pages such as this and this and this, and hints from JEP 11: Incubator Modules and JEP 428: Structured Concurrency (Incubator), hoping to not need the module-info.java
file. But I could not make it work. Adding the module info file was the only route to success for me.
And of course I had to set the usual multiple oddball settings that IntelliJ buries deep within various disparate places to specify use of Java 19. See other Stack Overflow Questions for directions on these troublesome settings. (Drives me batty. Definitely the most annoying problem/flaw in IntelliJ. Why can't IntelliJ just read the Java version from the Maven POM or Gradle settings and be done with it?)