Skip to content

Commit

Permalink
#30 - Add example-jdk-jsonb with graalvm native image build
Browse files Browse the repository at this point in the history
  • Loading branch information
rbygrave committed Dec 8, 2021
1 parent 0f0afcb commit f5dac5c
Show file tree
Hide file tree
Showing 10 changed files with 270 additions and 0 deletions.
141 changes: 141 additions & 0 deletions examples/example-jdk-jsonb/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
<?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>
<parent>
<groupId>org.avaje</groupId>
<artifactId>java11-oss</artifactId>
<version>3.3</version>
<relativePath/>
</parent>

<groupId>org.example</groupId>
<artifactId>example-jdk-jsonb</artifactId>
<version>1</version>

<properties>
<!-- <maven.compiler.source>18</maven.compiler.source>-->
<!-- <maven.compiler.target>18</maven.compiler.target>-->
<!-- <javadoc.source>11</javadoc.source>-->
<java.release>17</java.release>
<native.maven.plugin.version>0.9.8</native.maven.plugin.version>
</properties>

<dependencies>

<dependency>
<groupId>io.avaje</groupId>
<artifactId>avaje-jex-jdk</artifactId>
<version>2.1-SNAPSHOT</version>
</dependency>

<dependency>
<groupId>io.avaje</groupId>
<artifactId>avaje-jsonb</artifactId>
<version>0.9</version>
</dependency>

<!-- <dependency>-->
<!-- <groupId>io.avaje</groupId>-->
<!-- <artifactId>avaje-jsonb-generator</artifactId>-->
<!-- <version>0.9</version>-->
<!-- <scope>provided</scope>-->
<!-- </dependency>-->

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.30</version>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.30</version>
</dependency>

<!-- <dependency>-->
<!-- <groupId>ch.qos.logback</groupId>-->
<!-- <artifactId>logback-classic</artifactId>-->
<!-- <version>1.2.3</version>-->
<!-- </dependency>-->

</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<release>17</release>
<!-- <compilerArgs>-->
<!-- &#45;&#45;enable-preview-->
<!-- </compilerArgs>-->
<annotationProcessorPaths>
<path>
<groupId>io.avaje</groupId>
<artifactId>avaje-jsonb-generator</artifactId>
<version>0.9</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
<!-- <plugin>-->
<!-- <groupId>io.repaint.maven</groupId>-->
<!-- <artifactId>tiles-maven-plugin</artifactId>-->
<!-- <version>2.22</version>-->
<!-- <extensions>true</extensions>-->
<!-- <configuration>-->
<!-- <tiles>-->
<!-- <tile>org.avaje.tile:lib-classpath:1.1</tile>-->
<!-- </tiles>-->
<!-- </configuration>-->
<!-- </plugin>-->
</plugins>
</build>

<profiles>
<profile>
<id>native</id>
<build>
<plugins>
<plugin>
<groupId>org.graalvm.buildtools</groupId>
<artifactId>native-maven-plugin</artifactId>
<version>${native.maven.plugin.version}</version>
<extensions>true</extensions>
<executions>
<execution>
<id>build-native</id>
<goals>
<goal>build</goal>
</goals>
<phase>package</phase>
</execution>
<execution>
<id>test-native</id>
<goals>
<goal>test</goal>
</goals>
<phase>test</phase>
</execution>
</executions>
<configuration>
<imageName>mytest</imageName>
<mainClass>org.example.Main</mainClass>
<buildArgs>
<buildArg>--no-fallback</buildArg>
<buildArg>--allow-incomplete-classpath</buildArg>
</buildArgs>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>


</project>
11 changes: 11 additions & 0 deletions examples/example-jdk-jsonb/src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module example.jdkTwo {

requires io.avaje.jex.jdk;
requires org.slf4j;
requires io.avaje.jsonb;

//requires static io.avaje.jsonb.generator;

provides io.avaje.jsonb.Jsonb.Component with org.example.jsonb.GeneratedJsonComponent;
//exports org.example to com.fasterxml.jackson.databind;
}
10 changes: 10 additions & 0 deletions examples/example-jdk-jsonb/src/main/java/org/example/HelloDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.example;

import io.avaje.jsonb.Json;

@Json
public class HelloDto {

public long id;
public String name;
}
66 changes: 66 additions & 0 deletions examples/example-jdk-jsonb/src/main/java/org/example/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package org.example;

import io.avaje.jex.Context;
import io.avaje.jex.Jex;
import io.avaje.jex.core.JsonbJsonService;
import io.avaje.jsonb.Jsonb;
//import org.example.jsonb.GeneratedJsonComponent;
//import org.example.jsonb.GeneratedJsonComponent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;
import java.util.Set;
//import java.util.concurrent.Executor;
//import java.util.concurrent.Executors;

public class Main {

private static final Logger log = LoggerFactory.getLogger(Main.class);

public static void main(String[] args) {

Jsonb jsonb = Jsonb.newBuilder().build();//.add(new GeneratedJsonComponent()).build();

Jex.create()
.configure(config -> config.jsonService(new JsonbJsonService(jsonb)))
//.attribute(Executor.class, Executors.newVirtualThreadPerTaskExecutor())
.routing(routing -> routing
.get("/", ctx -> ctx.text("hello world"))
.get("/foo/{id}", Main::fooBean)
.get("/delay", Main::delay)
.get("/dump", ctx -> {
dumpThreadCount();
ctx.text("done");
})
)
.port(7003)
.start();
}

private static void fooBean(Context ctx) {
HelloDto bean = new HelloDto();
bean.id = Integer.parseInt(ctx.pathParam("id"));
bean.name = "Rob";
ctx.json(bean);
}

private static void delay(Context ctx) {
log.info("delay start");
try {
Thread.sleep(5_000);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
e.printStackTrace();
}
ctx.text("delay done");
log.info("delay done");
}

private static void dumpThreadCount() {
Map<Thread, StackTraceElement[]> allStackTraces = Thread.getAllStackTraces();
System.out.println("Thread count: " + allStackTraces.size());
Set<Thread> threads = allStackTraces.keySet();
System.out.println("Threads: " + threads);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<html><body>basic</body><html>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<html>index</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
plain-file
19 changes: 19 additions & 0 deletions examples/example-jdk-jsonb/src/main/resources/logback.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<configuration scan="true" scanPeriod="10 seconds">
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>TRACE</level>
</filter>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>

<root level="INFO">
<appender-ref ref="STDOUT"/>
</root>

<logger name="io.avaje.jex" level="TRACE"/>
<logger name="io.avaje.http.client" level="TRACE"/>
<logger name="org.eclipse.jetty" level="WARN"/>

</configuration>
19 changes: 19 additions & 0 deletions examples/example-jdk-jsonb/src/test/resources/logback-test.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<configuration scan="true" scanPeriod="10 seconds">
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>TRACE</level>
</filter>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>

<root level="INFO">
<appender-ref ref="STDOUT"/>
</root>

<logger name="io.avaje.jex" level="TRACE"/>
<logger name="io.avaje.http.client" level="TRACE"/>
<logger name="org.eclipse.jetty" level="WARN"/>

</configuration>
1 change: 1 addition & 0 deletions examples/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

<modules>
<module>example-jdk</module>
<module>example-jdk-jsonb</module>
<module>example-jetty</module>
<module>example-grizzly</module>

Expand Down

0 comments on commit f5dac5c

Please sign in to comment.