Skip to content

Commit e178e7f

Browse files
committed
update polyglot-chat-app to micronaut 4.2.2 and graalpy 23.1.1
1 parent 33ed7f7 commit e178e7f

File tree

174 files changed

+330
-74066
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

174 files changed

+330
-74066
lines changed

.github/workflows/polyglot-chat-app.yml

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,13 @@ jobs:
2020
timeout-minutes: 15
2121
strategy:
2222
matrix:
23-
version: [22.3.0] # temporarily locked to 22.3.0 due to R
23+
java-version: ['21']
2424
steps:
2525
- uses: actions/checkout@v3
2626
- uses: graalvm/setup-graalvm@v1
2727
with:
28-
version: ${{ matrix.version }}
29-
java-version: '17'
30-
components: 'python,R'
28+
java-version: ${{ matrix.java-version }}
29+
distribution: 'graalvm'
3130
github-token: ${{ secrets.GITHUB_TOKEN }}
3231
cache: 'gradle'
3332
- name: Run 'polyglot-chat-app'
@@ -36,4 +35,4 @@ jobs:
3635
./gradlew build # pre-build application
3736
./gradlew run &
3837
sleep 10
39-
curl http://localhost:8080/
38+
curl http://localhost:12345/

polyglot-chat-app/README.md

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,49 @@
11
# Polyglot Chat Application
22

3-
This example demonstrates how to integrate Python on GraalVM with a Java application.
3+
This example demonstrates how to integrate Python on GraalVM with a Micronaut application.
4+
The application uses the Gradle build tool.
45

56
### Prerequisites
67
- [Native Image](https://www.graalvm.org/latest/reference-manual/native-image/)
7-
- [R support](https://www.graalvm.org/latest/reference-manual/r/)
88
- [Python support](https://www.graalvm.org/latest/reference-manual/python/)
99

1010
## Preparation
1111

12-
1. Download and install the latest GraalVM JDK with Native Image, Python and R support using the [GraalVM JDK Downloader](https://github.com/graalvm/graalvm-jdk-downloader):
12+
1. Download the latest GraalPy as described on [https://www.graalvm.org/python/](https://www.graalvm.org/python/). For example on Linux:
1313
```bash
14-
bash <(curl -sL https://get.graalvm.org/jdk) -c 'python,R'
14+
wget https://github.com/oracle/graalpython/releases/download/graal-23.1.1/graalpy-23.1.1-linux-amd64.tar.gz
15+
tar xzf graalpy-23.1.1-linux-amd64.tar.gz
1516
```
1617

18+
2. Install the required packages for this demo into the _resources_ directory:
19+
```bash
20+
graalpy-23.1.1-linux-amd64/bin/graalpy -m venv src/main/resources/venv
21+
src/main/resources/venv/bin/graalpy -m pip install nltk
22+
```
23+
24+
3. Optional: Download and install GraalVM JDK for Java 21 or later to run Python with runtime compilation and to build a native image.
25+
The demo will work on any OpenJDK distribution, but will be much faster on GraalVM JDK.
26+
1727
## Building and Running the application:
1828

1929
1. Build application with Gradle:
2030
```bash
2131
./gradlew run
2232
```
2333

24-
2. Navigate to http://localhost:8080
25-
26-
You can connect from multiple browsers and chat via websockets. You can use "/"
27-
commands to trigger certain interesting functions. Available are:
28-
29-
/img [some string]
30-
31-
Searches for a random image from the internet matching [some string].
32-
33-
/gif [some string]
34+
2. Navigate to http://localhost:12345/#/chat/bob
3435

35-
Searches for a GIF on the internet matching [some string].
36+
You can connect from multiple browsers and chat via websockets.
37+
The Python code will load a language model in the background.
38+
Once it is ready, it will analyse the sentiments of all messages and add an emoji to the message to indicate the feelings conveyed.
39+
A chat may look like this (newest message at the top):
3640

37-
/= 1 2 3 4
38-
39-
Echos the arguments `1 2 3 4`.
40-
41-
/plot 1 1.4 2.6 6.4 25.6 102.4
42-
43-
Plots the points given.
41+
```
42+
[bob 😀] awesome, GraalVM and GraalPy rock!
43+
[bob 🫥] are we done yet?
44+
[bob 💬] still loading the sentiment model I believe
45+
```
4446

4547
### Learn More
4648

47-
Learn more about GraalVM polyglot capabilities [here](https://www.graalvm.org/latest/reference-manual/polyglot-programming/).
49+
Learn more about GraalVM polyglot capabilities [here](https://www.graalvm.org/latest/reference-manual/polyglot-programming/).

polyglot-chat-app/build.gradle

Lines changed: 49 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,70 @@
11
plugins {
2-
id("com.github.johnrengelman.shadow") version "7.0.0"
3-
id("io.micronaut.application") version "2.0.4"
2+
id("com.github.johnrengelman.shadow") version "8.1.1"
3+
id("io.micronaut.application") version "4.2.1"
4+
id("io.micronaut.aot") version "4.2.1"
45
}
56

67
version = "0.1"
7-
group = "poly.chat"
8+
group = "websocket.chat"
89

910
repositories {
1011
mavenCentral()
11-
maven { url "https://s01.oss.sonatype.org/content/repositories/snapshots/" }
12-
}
13-
14-
micronaut {
15-
runtime("netty")
16-
testRuntime("junit5")
17-
processing {
18-
incremental(true)
19-
annotations("poly.chat.*")
20-
}
2112
}
2213

2314
dependencies {
2415
annotationProcessor("io.micronaut:micronaut-http-validation")
25-
implementation("io.micronaut:micronaut-http-client")
26-
implementation("io.micronaut:micronaut-runtime")
27-
implementation("javax.annotation:javax.annotation-api")
16+
annotationProcessor("io.micronaut.serde:micronaut-serde-processor")
17+
annotationProcessor("io.micronaut.validation:micronaut-validation-processor")
18+
annotationProcessor("io.micronaut:micronaut-inject-java")
19+
compileOnly("io.micronaut:micronaut-inject-java")
20+
implementation("io.micrometer:context-propagation")
21+
implementation("io.micronaut:micronaut-websocket")
22+
implementation("io.micronaut.reactor:micronaut-reactor")
23+
implementation("io.micronaut.serde:micronaut-serde-jackson")
24+
implementation("io.micronaut.validation:micronaut-validation")
25+
implementation("jakarta.validation:jakarta.validation-api")
26+
compileOnly("io.micronaut:micronaut-http-client")
2827
runtimeOnly("ch.qos.logback:logback-classic")
29-
implementation("io.micronaut:micronaut-validation")
30-
implementation('javax.inject:javax.inject:1')
31-
implementation("org.graalvm.sdk:graal-sdk:21.2.0")
28+
runtimeOnly("org.yaml:snakeyaml")
29+
testImplementation("io.micronaut:micronaut-http-client")
30+
testImplementation("org.awaitility:awaitility:4.2.0")
31+
32+
implementation("jakarta.inject:jakarta.inject-api:2.0.1")
33+
34+
implementation("org.graalvm.sdk:graal-sdk:23.1.1")
35+
implementation("org.graalvm.polyglot:python:23.1.1")
3236
}
3337

3438

3539
application {
3640
mainClass.set("websocket.chat.Application")
3741
}
42+
java {
43+
sourceCompatibility = JavaVersion.toVersion("21")
44+
targetCompatibility = JavaVersion.toVersion("21")
45+
}
46+
47+
48+
graalvmNative.toolchainDetection = false
49+
micronaut {
50+
runtime("netty")
51+
testRuntime("junit5")
52+
processing {
53+
incremental(true)
54+
annotations("websocket.chat.*")
55+
}
56+
aot {
57+
// Please review carefully the optimizations enabled below
58+
// Check https://micronaut-projects.github.io/micronaut-aot/latest/guide/ for more details
59+
optimizeServiceLoading = false
60+
convertYamlToJava = false
61+
precomputeOperations = true
62+
cacheEnvironment = true
63+
optimizeClassLoading = true
64+
deduceEnvironment = true
65+
optimizeNetty = true
66+
}
67+
}
3868

3969

4070

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
micronautVersion=3.0.2-SNAPSHOT
1+
micronautVersion=4.2.2
4.09 KB
Binary file not shown.
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
distributionBase=GRADLE_USER_HOME
22
distributionPath=wrapper/dists
3-
distributionUrl=https\://services.gradle.org/distributions/gradle-7.2-bin.zip
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-8.4-bin.zip
4+
networkTimeout=10000
5+
validateDistributionUrl=true
46
zipStoreBase=GRADLE_USER_HOME
57
zipStorePath=wrapper/dists

polyglot-chat-app/gradlew

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
# Darwin, MinGW, and NonStop.
5656
#
5757
# (3) This script is generated from the Groovy template
58-
# https://github.com/gradle/gradle/blob/master/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
58+
# https://github.com/gradle/gradle/blob/HEAD/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
5959
# within the Gradle project.
6060
#
6161
# You can find Gradle at https://github.com/gradle/gradle/.
@@ -80,13 +80,11 @@ do
8080
esac
8181
done
8282

83-
APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit
84-
85-
APP_NAME="Gradle"
83+
# This is normally unused
84+
# shellcheck disable=SC2034
8685
APP_BASE_NAME=${0##*/}
87-
88-
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
89-
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
86+
# Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036)
87+
APP_HOME=$( cd "${APP_HOME:-./}" > /dev/null && pwd -P ) || exit
9088

9189
# Use the maximum available, or set MAX_FD != -1 to use that value.
9290
MAX_FD=maximum
@@ -133,22 +131,29 @@ location of your Java installation."
133131
fi
134132
else
135133
JAVACMD=java
136-
which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
134+
if ! command -v java >/dev/null 2>&1
135+
then
136+
die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
137137
138138
Please set the JAVA_HOME variable in your environment to match the
139139
location of your Java installation."
140+
fi
140141
fi
141142

142143
# Increase the maximum file descriptors if we can.
143144
if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then
144145
case $MAX_FD in #(
145146
max*)
147+
# In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked.
148+
# shellcheck disable=SC2039,SC3045
146149
MAX_FD=$( ulimit -H -n ) ||
147150
warn "Could not query maximum file descriptor limit"
148151
esac
149152
case $MAX_FD in #(
150153
'' | soft) :;; #(
151154
*)
155+
# In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked.
156+
# shellcheck disable=SC2039,SC3045
152157
ulimit -n "$MAX_FD" ||
153158
warn "Could not set maximum file descriptor limit to $MAX_FD"
154159
esac
@@ -193,18 +198,28 @@ if "$cygwin" || "$msys" ; then
193198
done
194199
fi
195200

196-
# Collect all arguments for the java command;
197-
# * $DEFAULT_JVM_OPTS, $JAVA_OPTS, and $GRADLE_OPTS can contain fragments of
198-
# shell script including quotes and variable substitutions, so put them in
199-
# double quotes to make sure that they get re-expanded; and
200-
# * put everything else in single quotes, so that it's not re-expanded.
201+
202+
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
203+
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
204+
205+
# Collect all arguments for the java command:
206+
# * DEFAULT_JVM_OPTS, JAVA_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments,
207+
# and any embedded shellness will be escaped.
208+
# * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be
209+
# treated as '${Hostname}' itself on the command line.
201210

202211
set -- \
203212
"-Dorg.gradle.appname=$APP_BASE_NAME" \
204213
-classpath "$CLASSPATH" \
205214
org.gradle.wrapper.GradleWrapperMain \
206215
"$@"
207216

217+
# Stop when "xargs" is not available.
218+
if ! command -v xargs >/dev/null 2>&1
219+
then
220+
die "xargs is not available"
221+
fi
222+
208223
# Use "xargs" to parse quoted args.
209224
#
210225
# With -n1 it outputs one arg per line, with the quotes and backslashes removed.

polyglot-chat-app/gradlew.bat

100755100644
Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
@rem limitations under the License.
1515
@rem
1616

17-
@if "%DEBUG%" == "" @echo off
17+
@if "%DEBUG%"=="" @echo off
1818
@rem ##########################################################################
1919
@rem
2020
@rem Gradle startup script for Windows
@@ -25,7 +25,8 @@
2525
if "%OS%"=="Windows_NT" setlocal
2626

2727
set DIRNAME=%~dp0
28-
if "%DIRNAME%" == "" set DIRNAME=.
28+
if "%DIRNAME%"=="" set DIRNAME=.
29+
@rem This is normally unused
2930
set APP_BASE_NAME=%~n0
3031
set APP_HOME=%DIRNAME%
3132

@@ -40,7 +41,7 @@ if defined JAVA_HOME goto findJavaFromJavaHome
4041

4142
set JAVA_EXE=java.exe
4243
%JAVA_EXE% -version >NUL 2>&1
43-
if "%ERRORLEVEL%" == "0" goto execute
44+
if %ERRORLEVEL% equ 0 goto execute
4445

4546
echo.
4647
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
@@ -75,13 +76,15 @@ set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
7576

7677
:end
7778
@rem End local scope for the variables with windows NT shell
78-
if "%ERRORLEVEL%"=="0" goto mainEnd
79+
if %ERRORLEVEL% equ 0 goto mainEnd
7980

8081
:fail
8182
rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
8283
rem the _cmd.exe /c_ return code!
83-
if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
84-
exit /b 1
84+
set EXIT_CODE=%ERRORLEVEL%
85+
if %EXIT_CODE% equ 0 set EXIT_CODE=1
86+
if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE%
87+
exit /b %EXIT_CODE%
8588

8689
:mainEnd
8790
if "%OS%"=="Windows_NT" endlocal
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
applicationType: default
2-
defaultPackage: poly.chat
2+
defaultPackage: websocket.chat
33
testFramework: junit
44
sourceLanguage: java
55
buildTool: gradle
6-
features: [annotation-api, app-name, gradle, http-client, java, java-application, junit, logback, netty-server, readme, shade, yaml]
6+
features: [annotation-api, app-name, awaitility, graalvm, gradle, http-client-test, java, java-application, junit, logback, micronaut-aot, micronaut-build, micronaut-http-validation, netty-server, reactor, readme, serialization-jackson, shade, validation, websocket, yaml, yaml-build]

polyglot-chat-app/settings.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11

2-
rootProject.name="poly-chat"
2+
rootProject.name="websocket.chat"
3+

0 commit comments

Comments
 (0)