Skip to content

Commit 4c37c62

Browse files
authored
Hello World samples for Java 11 (GoogleCloudPlatform#1380)
* simple hello world * Simple fat jar hello world * Reduce instance size * Add parent to pom.xml * Linting
1 parent 42db9a6 commit 4c37c62

File tree

7 files changed

+182
-0
lines changed

7 files changed

+182
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright 2019 Google Inc.
3+
*
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
7+
*
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.
15+
*/
16+
17+
import com.sun.net.httpserver.HttpServer;
18+
import java.io.IOException;
19+
import java.io.OutputStream;
20+
import java.net.InetSocketAddress;
21+
22+
public class Main {
23+
24+
public static void main(String[] args) throws IOException {
25+
// Create an instance of HttpServer bound to port 8080.
26+
HttpServer server = HttpServer.create(new InetSocketAddress(8080), 0);
27+
28+
// Set root URI path.
29+
server.createContext("/", (var t) -> {
30+
byte[] response = "Hello World from Google App Engine Java 11.".getBytes();
31+
t.sendResponseHeaders(200, response.length);
32+
try (OutputStream os = t.getResponseBody()) {
33+
os.write(response);
34+
}
35+
});
36+
37+
// Start the server.
38+
server.start();
39+
}
40+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Hello World App for Google App Engine Standard with Java 11
2+
3+
This sample shows how to deploy an application to Google App Engine, using the
4+
`entrypoint` element in the [app.yaml](app.yaml) to start your application. The
5+
sample uses the `java` command is used to compile and execute the Java source code.
6+
7+
## Setup
8+
See [Prerequisites](../README.md#Prerequisites).
9+
10+
## Deploy to App Engine Standard
11+
12+
```
13+
gcloud app deploy
14+
```
15+
16+
To view your app, use command:
17+
```
18+
gcloud app browse
19+
```
20+
Or navigate to http://<project-id>.appspot.com URL.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
runtime: java11
2+
entrypoint: java Main.java
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Single Fat Jar Application on Google App Engine Standard with Java 11
2+
3+
This sample shows how to deploy an application to Google App Engine using the
4+
a fat jar. There is no `entrypoint` field listed in the [`app.yaml`](src/main/appengine/app.yaml),
5+
as the application is a single fat jar with the correct MainClass field in the MANIFEST.
6+
7+
## Setup
8+
9+
See [Prerequisites](../README.md#Prerequisites).
10+
11+
## Deploy to App Engine Standard
12+
13+
```
14+
mvn clean package appengine:deploy -Dapp.deploy.projectId=<your-project-id>
15+
```
16+
17+
To view your app, use command:
18+
```
19+
gcloud app browse
20+
```
21+
Or navigate to http://<project-id>.appspot.com URL.
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?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">
2+
<modelVersion>4.0.0</modelVersion>
3+
<groupId>com.example.appengine</groupId>
4+
<artifactId>http-server</artifactId>
5+
<version>0.1</version>
6+
7+
<!--
8+
The parent pom defines common style checks and testing strategies for our samples.
9+
Removing or replacing it should not effect the execution of the samples in anyway.
10+
-->
11+
<parent>
12+
<groupId>com.google.cloud.samples</groupId>
13+
<artifactId>shared-configuration</artifactId>
14+
<version>1.0.11</version>
15+
</parent>
16+
17+
18+
<properties>
19+
<maven.compiler.source>11</maven.compiler.source>
20+
<maven.compiler.target>11</maven.compiler.target>
21+
</properties>
22+
23+
<build>
24+
<plugins>
25+
<plugin>
26+
<groupId>org.apache.maven.plugins</groupId>
27+
<artifactId>maven-jar-plugin</artifactId>
28+
<version>3.1.1</version>
29+
<configuration>
30+
<archive>
31+
<manifest>
32+
<mainClass>com.example.appengine.Main</mainClass>
33+
</manifest>
34+
</archive>
35+
</configuration>
36+
</plugin>
37+
<plugin>
38+
<groupId>com.google.cloud.tools</groupId>
39+
<artifactId>appengine-maven-plugin</artifactId>
40+
<version>2.0.0-rc5</version>
41+
<configuration>
42+
<version>http-server</version>
43+
</configuration>
44+
</plugin>
45+
</plugins>
46+
</build>
47+
</project>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
runtime: java11
2+
# No need for an entrypoint with single fatjar with correct manifest class-path entry.
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright 2019 Google Inc.
3+
*
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
7+
*
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.
15+
*/
16+
17+
package com.example.appengine;
18+
19+
import com.sun.net.httpserver.HttpServer;
20+
import java.io.IOException;
21+
import java.io.OutputStream;
22+
import java.net.InetSocketAddress;
23+
24+
public class Main {
25+
26+
public static void main(String[] args) throws IOException {
27+
// Create an instance of HttpServer with port 8080.
28+
HttpServer server = HttpServer.create(new InetSocketAddress(8080), 0);
29+
30+
// Set root URI path.
31+
server.createContext("/", (var t) -> {
32+
byte[] response = "Hello World!".getBytes();
33+
t.sendResponseHeaders(200, response.length);
34+
try (OutputStream os = t.getResponseBody()) {
35+
os.write(response);
36+
}
37+
});
38+
39+
// Create a second URI path.
40+
server.createContext("/foo", (var t) -> {
41+
byte[] response = "Foo!".getBytes();
42+
t.sendResponseHeaders(200, response.length);
43+
try (OutputStream os = t.getResponseBody()) {
44+
os.write(response);
45+
}
46+
});
47+
48+
server.start();
49+
}
50+
}

0 commit comments

Comments
 (0)