Skip to content

Commit 7293836

Browse files
Merge pull request #494 from karesti/picocli-app
Convert Load to Quarkus App
2 parents 2bcaf37 + d22e679 commit 7293836

37 files changed

+1233
-745
lines changed
Binary file not shown.
Binary file not shown.
Binary file not shown.

quarkus-workshop-super-heroes/docs/src/docs/asciidoc/optional-cli/cli.adoc quarkus-workshop-super-heroes/docs/src/docs/asciidoc/optional-load/load.adoc

+9-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
[[cli]]
1+
[[load]]
22
= Loading the Microservices
33

44

@@ -8,17 +8,18 @@ You will download the load application, install it and run it.
88

99
== Give me some load!
1010

11-
In the `super-heroes/load-super-heroes` directory, there is an application that is **NOT** a Quarkus application.
12-
It's a simple Java app that simulates users interacting with the system so it generates some load.
11+
In the `super-heroes/load-super-heroes` directory, there is a command line Quarkus application .
12+
This application simulates users interacting with the system so it generates some load.
1313

1414
== Looking at Some Code
1515

16-
The `SuperHeroesLoad` class is just a `main` that executes the `FightScenario`, `HeroScenario` and `VillainScenario` in different threads.
16+
The `CLIMain` class is just a Picocli extension command line application that executes the `FightScenario`, `HeroScenario` and `VillainScenario`,
17+
on the same thread but randomly executing 3 different random calls to the 3 REST APIs.
1718
For example, if you look at the `HeroScenario`, you will see that it's just a suit of HTTP calls on the Hero API:
1819

1920
[source,indent=0]
2021
----
21-
include::{projectdir}/load-super-heroes/src/main/java/io/quarkus/workshop/superheroes/load/HeroScenario.java[tag=adocScenario]
22+
include::{projectdir}/load-super-heroes/src/main/java/io/quarkus/workshop/superheroes/load/scenarios/HeroScenario.java[tag=adocScenario]
2223
----
2324

2425
== Running the Load Application
@@ -30,12 +31,12 @@ Time to compile and start the load application using:
3031

3132
[source,shell]
3233
----
33-
./mvnw compile
34-
./mvnw exec:java
34+
./mvnw package
35+
java -jar ./target/quarkus-app/quarkus-run.jar -s
3536
----
3637
--
3738

38-
You will see the following logs:
39+
You will see the following logs. To stop the load, write something and press Enter key.
3940

4041
[source,shell]
4142
----

quarkus-workshop-super-heroes/docs/src/docs/asciidoc/spine.adoc

+1-1
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ endif::use-observability[]
137137

138138
// CLI
139139
ifdef::use-cli[]
140-
include::optional-cli/cli.adoc[leveloffset=+1]
140+
include::optional-load/load.adoc[leveloffset=+1]
141141
<<<
142142
endif::use-cli[]
143143

quarkus-workshop-super-heroes/pom.xml

+1
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@
141141
<module>super-heroes/rest-fights</module>
142142
<module>super-heroes/event-statistics</module>
143143
<module>super-heroes/rest-narration</module>
144+
<module>super-heroes/load-super-heroes</module>
144145
<module>docs</module>
145146
</modules>
146147
</profile>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
*
2+
!target/*-runner
3+
!target/*-runner.jar
4+
!target/lib/*
5+
!target/quarkus-app/*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#Maven
2+
target/
3+
pom.xml.tag
4+
pom.xml.releaseBackup
5+
pom.xml.versionsBackup
6+
release.properties
7+
.flattened-pom.xml
8+
9+
# Eclipse
10+
.project
11+
.classpath
12+
.settings/
13+
bin/
14+
15+
# IntelliJ
16+
.idea
17+
*.ipr
18+
*.iml
19+
*.iws
20+
21+
# NetBeans
22+
nb-configuration.xml
23+
24+
# Visual Studio Code
25+
.vscode
26+
.factorypath
27+
28+
# OSX
29+
.DS_Store
30+
31+
# Vim
32+
*.swp
33+
*.swo
34+
35+
# patch
36+
*.orig
37+
*.rej
38+
39+
# Local environment
40+
.env
41+
42+
# Plugin directory
43+
/.quarkus/cli/plugins/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
maven-wrapper.jar
Original file line numberDiff line numberDiff line change
@@ -1,118 +1,98 @@
11
/*
2-
* Copyright 2007-present the original author or authors.
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
39
*
4-
* Licensed under the Apache License, Version 2.0 (the "License");
5-
* you may not use this file except in compliance with the License.
6-
* You may obtain a copy of the License at
10+
* http://www.apache.org/licenses/LICENSE-2.0
711
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
9-
*
10-
* Unless required by applicable law or agreed to in writing, software
11-
* distributed under the License is distributed on an "AS IS" BASIS,
12-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
* See the License for the specific language governing permissions and
14-
* limitations under the License.
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
1518
*/
1619

17-
import java.net.*;
18-
import java.io.*;
19-
import java.nio.channels.*;
20-
import java.util.Properties;
21-
22-
public class MavenWrapperDownloader {
23-
24-
private static final String WRAPPER_VERSION = "0.5.5";
25-
/**
26-
* Default URL to download the maven-wrapper.jar from, if no 'downloadUrl' is provided.
27-
*/
28-
private static final String DEFAULT_DOWNLOAD_URL = "https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/"
29-
+ WRAPPER_VERSION + "/maven-wrapper-" + WRAPPER_VERSION + ".jar";
20+
import java.io.IOException;
21+
import java.io.InputStream;
22+
import java.net.Authenticator;
23+
import java.net.PasswordAuthentication;
24+
import java.net.URL;
25+
import java.nio.file.Files;
26+
import java.nio.file.Path;
27+
import java.nio.file.Paths;
28+
import java.nio.file.StandardCopyOption;
3029

31-
/**
32-
* Path to the maven-wrapper.properties file, which might contain a downloadUrl property to
33-
* use instead of the default one.
34-
*/
35-
private static final String MAVEN_WRAPPER_PROPERTIES_PATH =
36-
".mvn/wrapper/maven-wrapper.properties";
30+
public final class MavenWrapperDownloader
31+
{
32+
private static final String WRAPPER_VERSION = "3.2.0";
3733

38-
/**
39-
* Path where the maven-wrapper.jar will be saved to.
40-
*/
41-
private static final String MAVEN_WRAPPER_JAR_PATH =
42-
".mvn/wrapper/maven-wrapper.jar";
34+
private static final boolean VERBOSE = Boolean.parseBoolean( System.getenv( "MVNW_VERBOSE" ) );
4335

44-
/**
45-
* Name of the property which should be used to override the default download url for the wrapper.
46-
*/
47-
private static final String PROPERTY_NAME_WRAPPER_URL = "wrapperUrl";
36+
public static void main( String[] args )
37+
{
38+
log( "Apache Maven Wrapper Downloader " + WRAPPER_VERSION );
4839

49-
public static void main(String args[]) {
50-
System.out.println("- Downloader started");
51-
File baseDirectory = new File(args[0]);
52-
System.out.println("- Using base directory: " + baseDirectory.getAbsolutePath());
53-
54-
// If the maven-wrapper.properties exists, read it and check if it contains a custom
55-
// wrapperUrl parameter.
56-
File mavenWrapperPropertyFile = new File(baseDirectory, MAVEN_WRAPPER_PROPERTIES_PATH);
57-
String url = DEFAULT_DOWNLOAD_URL;
58-
if (mavenWrapperPropertyFile.exists()) {
59-
FileInputStream mavenWrapperPropertyFileInputStream = null;
60-
try {
61-
mavenWrapperPropertyFileInputStream = new FileInputStream(mavenWrapperPropertyFile);
62-
Properties mavenWrapperProperties = new Properties();
63-
mavenWrapperProperties.load(mavenWrapperPropertyFileInputStream);
64-
url = mavenWrapperProperties.getProperty(PROPERTY_NAME_WRAPPER_URL, url);
65-
} catch (IOException e) {
66-
System.out.println("- ERROR loading '" + MAVEN_WRAPPER_PROPERTIES_PATH + "'");
67-
} finally {
68-
try {
69-
if (mavenWrapperPropertyFileInputStream != null) {
70-
mavenWrapperPropertyFileInputStream.close();
71-
}
72-
} catch (IOException e) {
73-
// Ignore ...
74-
}
75-
}
40+
if ( args.length != 2 )
41+
{
42+
System.err.println( " - ERROR wrapperUrl or wrapperJarPath parameter missing" );
43+
System.exit( 1 );
7644
}
77-
System.out.println("- Downloading from: " + url);
7845

79-
File outputFile = new File(baseDirectory.getAbsolutePath(), MAVEN_WRAPPER_JAR_PATH);
80-
if (!outputFile.getParentFile().exists()) {
81-
if (!outputFile.getParentFile().mkdirs()) {
82-
System.out.println(
83-
"- ERROR creating output directory '" + outputFile.getParentFile().getAbsolutePath() + "'");
84-
}
46+
try
47+
{
48+
log( " - Downloader started" );
49+
final URL wrapperUrl = new URL( args[0] );
50+
final String jarPath = args[1].replace( "..", "" ); // Sanitize path
51+
final Path wrapperJarPath = Paths.get( jarPath ).toAbsolutePath().normalize();
52+
downloadFileFromURL( wrapperUrl, wrapperJarPath );
53+
log( "Done" );
8554
}
86-
System.out.println("- Downloading to: " + outputFile.getAbsolutePath());
87-
try {
88-
downloadFileFromURL(url, outputFile);
89-
System.out.println("Done");
90-
System.exit(0);
91-
} catch (Throwable e) {
92-
System.out.println("- Error downloading");
93-
e.printStackTrace();
94-
System.exit(1);
55+
catch ( IOException e )
56+
{
57+
System.err.println( "- Error downloading: " + e.getMessage() );
58+
if ( VERBOSE )
59+
{
60+
e.printStackTrace();
61+
}
62+
System.exit( 1 );
9563
}
9664
}
9765

98-
private static void downloadFileFromURL(String urlString, File destination) throws Exception {
99-
if (System.getenv("MVNW_USERNAME") != null && System.getenv("MVNW_PASSWORD") != null) {
100-
String username = System.getenv("MVNW_USERNAME");
101-
char[] password = System.getenv("MVNW_PASSWORD").toCharArray();
102-
Authenticator.setDefault(new Authenticator() {
66+
private static void downloadFileFromURL( URL wrapperUrl, Path wrapperJarPath )
67+
throws IOException
68+
{
69+
log( " - Downloading to: " + wrapperJarPath );
70+
if ( System.getenv( "MVNW_USERNAME" ) != null && System.getenv( "MVNW_PASSWORD" ) != null )
71+
{
72+
final String username = System.getenv( "MVNW_USERNAME" );
73+
final char[] password = System.getenv( "MVNW_PASSWORD" ).toCharArray();
74+
Authenticator.setDefault( new Authenticator()
75+
{
10376
@Override
104-
protected PasswordAuthentication getPasswordAuthentication() {
105-
return new PasswordAuthentication(username, password);
77+
protected PasswordAuthentication getPasswordAuthentication()
78+
{
79+
return new PasswordAuthentication( username, password );
10680
}
107-
});
81+
} );
82+
}
83+
try ( InputStream inStream = wrapperUrl.openStream() )
84+
{
85+
Files.copy( inStream, wrapperJarPath, StandardCopyOption.REPLACE_EXISTING );
86+
}
87+
log( " - Downloader complete" );
88+
}
89+
90+
private static void log( String msg )
91+
{
92+
if ( VERBOSE )
93+
{
94+
System.out.println( msg );
10895
}
109-
URL website = new URL(urlString);
110-
ReadableByteChannel rbc;
111-
rbc = Channels.newChannel(website.openStream());
112-
FileOutputStream fos = new FileOutputStream(destination);
113-
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
114-
fos.close();
115-
rbc.close();
11696
}
11797

11898
}
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,18 @@
1-
distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.6.1/apache-maven-3.6.1-bin.zip
2-
wrapperUrl=https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.5/maven-wrapper-0.5.5.jar
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.9.5/apache-maven-3.9.5-bin.zip
18+
wrapperUrl=https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.2.0/maven-wrapper-3.2.0.jar

0 commit comments

Comments
 (0)