Skip to content

Commit f7ab04f

Browse files
authored
Update task sample for java 11 (GoogleCloudPlatform#1382)
* update task sample for java 11 * remove region tags * Remove CLI and update readme * respond to PR comments * Update pom.xml * Update scopes * Remove systempath dependency * Update tasks sample to use spring boot handler * Don't fail on missing web.xml * Add configuration for missing web.xml * Update licenses and styling * checkstyle * Format pom * Update style
1 parent 4c37c62 commit f7ab04f

File tree

10 files changed

+478
-0
lines changed

10 files changed

+478
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
# Use the official maven/Java 8 image to create a build artifact.
3+
# https://hub.docker.com/_/maven
4+
FROM maven:3.6-jdk-11 as builder
5+
6+
# Copy local code to the container image.
7+
WORKDIR /app
8+
COPY pom.xml .
9+
COPY src ./src
10+
11+
# Build a release artifact.
12+
RUN mvn package -DskipTests
13+
14+
# Use the Official OpenJDK image for a lean production stage of our multi-stage build.
15+
# https://hub.docker.com/_/openjdk
16+
# https://docs.docker.com/develop/develop-images/multistage-build/#use-multi-stage-builds
17+
FROM openjdk:11-jre-slim
18+
19+
# Copy the jar to the production image from the builder stage.
20+
COPY --from=builder /app/target/task-handler-j11-*.jar /task-handler-j11.jar
21+
22+
# Run the web service on container startup.
23+
CMD ["java","-Djava.security.egd=file:/dev/./urandom","-Dserver.port=${PORT}","-jar","/task-handler-j11.jar"]
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Task Handler App for Google Cloud Tasks App Engine Queue Samples
2+
3+
This is a companion application using Spring Boot to process Cloud Tasks requests.
4+
See directory [`appengine-java11/tasks`](../tasks/README.md) for instructions.
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
Copyright 2019 Google LLC
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
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, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
-->
17+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
18+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
19+
<modelVersion>4.0.0</modelVersion>
20+
<groupId>com.example.appengine</groupId>
21+
<artifactId>task-handler-j11</artifactId>
22+
<version>0.0.1-SNAPSHOT</version>
23+
24+
<!--
25+
The parent pom defines common style checks and testing strategies for our samples.
26+
Removing or replacing it should not affect the execution of the samples in anyway.
27+
-->
28+
<parent>
29+
<groupId>com.google.cloud.samples</groupId>
30+
<artifactId>shared-configuration</artifactId>
31+
<version>1.0.11</version>
32+
</parent>
33+
34+
<properties>
35+
<maven.compiler.target>11</maven.compiler.target>
36+
<maven.compiler.source>11</maven.compiler.source>
37+
</properties>
38+
39+
<dependencyManagement>
40+
<dependencies>
41+
<dependency>
42+
<!-- Import dependency management from Spring Boot -->
43+
<groupId>org.springframework.boot</groupId>
44+
<artifactId>spring-boot-dependencies</artifactId>
45+
<version>2.1.4.RELEASE</version>
46+
<type>pom</type>
47+
<scope>import</scope>
48+
</dependency>
49+
50+
<dependency>
51+
<groupId>org.springframework.cloud</groupId>
52+
<artifactId>spring-cloud-dependencies</artifactId>
53+
<version>Greenwich.SR1</version>
54+
<type>pom</type>
55+
<scope>import</scope>
56+
</dependency>
57+
</dependencies>
58+
</dependencyManagement>
59+
60+
<dependencies>
61+
62+
<dependency>
63+
<groupId>org.springframework.boot</groupId>
64+
<artifactId>spring-boot-starter-web</artifactId>
65+
<version>2.1.4.RELEASE</version>
66+
<exclusions>
67+
<!-- Exclude the Tomcat dependency -->
68+
<exclusion>
69+
<groupId>org.springframework.boot</groupId>
70+
<artifactId>spring-boot-starter-tomcat</artifactId>
71+
</exclusion>
72+
</exclusions>
73+
</dependency>
74+
<dependency>
75+
<groupId>org.springframework.boot</groupId>
76+
<artifactId>spring-boot-starter-jetty</artifactId>
77+
<version>2.1.4.RELEASE</version>
78+
</dependency>
79+
80+
</dependencies>
81+
82+
<build>
83+
<plugins>
84+
<plugin>
85+
<groupId>org.springframework.boot</groupId>
86+
<artifactId>spring-boot-maven-plugin</artifactId>
87+
<version>2.1.4.RELEASE</version>
88+
<executions>
89+
<execution>
90+
<goals>
91+
<goal>repackage</goal>
92+
</goals>
93+
</execution>
94+
</executions>
95+
</plugin>
96+
<plugin>
97+
<groupId>com.google.cloud.tools</groupId>
98+
<artifactId>appengine-maven-plugin</artifactId>
99+
<version>2.0.0-rc5</version>
100+
<configuration>
101+
<version>task-handler</version>
102+
</configuration>
103+
</plugin>
104+
</plugins>
105+
106+
</build>
107+
108+
</project>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Copyright 2019 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
runtime: java11
16+
instance_class: F4
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright 2019 Google LLC
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.taskhandler;
18+
19+
import org.springframework.boot.SpringApplication;
20+
import org.springframework.boot.autoconfigure.SpringBootApplication;
21+
import org.springframework.context.annotation.Bean;
22+
import org.springframework.web.client.RestTemplate;
23+
24+
@SpringBootApplication
25+
public class Application {
26+
27+
public static void main(String[] args) {
28+
SpringApplication.run(Application.class, args);
29+
}
30+
31+
@Bean
32+
public RestTemplate restTemplate() {
33+
return new RestTemplate();
34+
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright 2019 Google LLC
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.taskhandler;
18+
19+
import org.springframework.http.HttpStatus;
20+
import org.springframework.web.bind.annotation.RequestBody;
21+
import org.springframework.web.bind.annotation.RequestMapping;
22+
import org.springframework.web.bind.annotation.RequestMethod;
23+
import org.springframework.web.bind.annotation.ResponseStatus;
24+
import org.springframework.web.bind.annotation.RestController;
25+
26+
@RestController
27+
public class TaskHandlerController {
28+
29+
@RequestMapping(
30+
value = "/tasks/create",
31+
method = RequestMethod.POST,
32+
consumes = "application/octet-stream")
33+
@ResponseStatus(HttpStatus.OK)
34+
public String taskHandler(@RequestBody String body) {
35+
String output;
36+
output = String.format("Received task with payload %s", body);
37+
System.out.println(output);
38+
39+
return output;
40+
}
41+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Copyright 2019 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.

appengine-java11/tasks/README.md

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
# Google Cloud Tasks App Engine Queue Samples
2+
3+
Sample command-line program for interacting with the Cloud Tasks API
4+
using App Engine queues.
5+
6+
App Engine queues push tasks to an App Engine HTTP target. This directory
7+
contains both the App Engine app to deploy, as well as the snippets to run
8+
locally to push tasks to it, which could also be called on App Engine.
9+
10+
`CreateTask.java` is a simple command-line program to create
11+
tasks to be pushed to the App Engine app.
12+
13+
Directory `task-handler/` is the main App Engine app. This app serves as an endpoint to receive
14+
App Engine task attempts.
15+
16+
17+
## Initial Setup
18+
19+
* Set up a Google Cloud Project and enable billing.
20+
* Enable the
21+
[Cloud Tasks API](https://console.cloud.google.com/launcher/details/google/cloudtasks.googleapis.com).
22+
* Download and install the [Cloud SDK](https://cloud.google.com/sdk).
23+
* Download and install [Maven](http://maven.apache.org/install.html).
24+
* Set up [Google Application Credentials](https://cloud.google.com/docs/authentication/getting-started).
25+
26+
## Creating a queue
27+
28+
To create a queue using the Cloud SDK, use the following gcloud command:
29+
30+
```
31+
gcloud beta tasks queues create-app-engine-queue my-appengine-queue
32+
```
33+
34+
Note: A newly created queue will route to the default App Engine service and
35+
version unless configured to do otherwise.
36+
37+
## Deploying the App Engine app
38+
39+
[Using Maven and the App Engine Plugin](https://cloud.google.com/appengine/docs/flexible/java/using-maven)
40+
& [Maven Plugin Goals and Parameters](https://cloud.google.com/appengine/docs/flexible/java/maven-reference)
41+
42+
- Copy the sample apps to your local machine:
43+
```
44+
git clone https://github.com/GoogleCloudPlatform/java-docs-samples
45+
```
46+
47+
- Move into the `task-handler` directory:
48+
```
49+
cd java-docs-samples/appengine-java11/task-handler
50+
```
51+
52+
- Deploy the app
53+
```
54+
mvn clean package appengine:deploy -Dapp.deploy.projectId=<your-project-id>
55+
```
56+
57+
## Run the Sample Using the Command Line
58+
59+
- Move into the `appengine-java11/tasks` directory and compile the app:
60+
```
61+
cd ../tasks
62+
mvn package
63+
```
64+
65+
Set environment variables:
66+
67+
First, your project ID:
68+
69+
```
70+
export GOOGLE_CLOUD_PROJECT="<YOUR_GOOGLE_CLOUD_PROJECT>"
71+
```
72+
73+
Then the queue ID, as specified at queue creation time. Queue IDs already
74+
created can be listed with `gcloud beta tasks queues list`.
75+
76+
```
77+
export QUEUE_ID="my-appengine-queue"
78+
```
79+
80+
And finally the location ID, which can be discovered with
81+
`gcloud beta tasks queues describe $QUEUE_ID`, with the location embedded in
82+
the "name" value (for instance, if the name is
83+
"projects/my-project/locations/us-central1/queues/my-appengine-queue", then the
84+
location is "us-central1").
85+
86+
```
87+
export LOCATION_ID="us-central1"
88+
```
89+
90+
Create a task, targeted at the `/tasks/create` endpoint, with a payload specified:
91+
92+
```
93+
mvn exec:java -Dexec.mainClass="com.example.task.CreateTask"
94+
```
95+
96+
The App Engine app serves as a target for the push requests. It has an
97+
endpoint `/tasks/create` that reads the payload (i.e., the request body) of the
98+
HTTP POST request and logs it. The log output can be viewed with [Stackdriver Logging](https://console.cloud.google.com/logs/viewer?minLogLevel=0).

0 commit comments

Comments
 (0)