Skip to content
This repository was archived by the owner on Sep 16, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,20 @@ If you are using Maven, add this to your pom.xml file:
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-tasks</artifactId>
<version>2.1.3</version>
<version>2.1.4</version>
</dependency>
```

If you are using Gradle without BOM, add this to your dependencies

```Groovy
implementation 'com.google.cloud:google-cloud-tasks:2.1.3'
implementation 'com.google.cloud:google-cloud-tasks:2.1.4'
```

If you are using SBT, add this to your dependencies

```Scala
libraryDependencies += "com.google.cloud" % "google-cloud-tasks" % "2.1.3"
libraryDependencies += "com.google.cloud" % "google-cloud-tasks" % "2.1.4"
```

## Authentication
Expand Down Expand Up @@ -76,6 +76,7 @@ Samples are in the [`samples/`](https://github.com/googleapis/java-tasks/tree/ma

| Sample | Source Code | Try it |
| --------------------------- | --------------------------------- | ------ |
| Tasks Sample Application | [source code](https://github.com/googleapis/java-tasks/blob/main/samples/native-image-sample/src/main/java/com/example/tasks/TasksSampleApplication.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-tasks&page=editor&open_in_editor=samples/native-image-sample/src/main/java/com/example/tasks/TasksSampleApplication.java) |
| Create Http Task | [source code](https://github.com/googleapis/java-tasks/blob/main/samples/snippets/src/main/java/com/example/task/CreateHttpTask.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-tasks&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/task/CreateHttpTask.java) |
| Create Http Task With Token | [source code](https://github.com/googleapis/java-tasks/blob/main/samples/snippets/src/main/java/com/example/task/CreateHttpTaskWithToken.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-tasks&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/task/CreateHttpTaskWithToken.java) |
| Create Queue | [source code](https://github.com/googleapis/java-tasks/blob/main/samples/snippets/src/main/java/com/example/task/CreateQueue.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-tasks&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/task/CreateQueue.java) |
Expand Down
119 changes: 119 additions & 0 deletions samples/native-image-sample/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
# Cloud Tasks Sample Application with Native Image

The Cloud Tasks sample application demonstrates some common operations with
[Google Cloud Tasks](https://cloud.google.com/tasks) and is compatible with
Native Image compilation.

This application will create a new queue called `graal-test-queue` if it does
not already exist.
It will then submit a new task to this queue.

## Setup Instructions

1. Follow the [GCP Project and Native Image Setup Instructions](../../README.md).

2. [Enable the Cloud Tasks APIs](https://console.cloud.google.com/apis/api/cloudtasks.googleapis.com).

### Run with Native Image Compilation

Navigate to this directory in a new terminal.

1. Compile the application using the Native Image Compiler. This step may take a few minutes.

```
$ mvn package -P native
```

The project uses an environment variable `LOCATION_ID` to run the test.

2. Run the application:

```
$ LOCATION_ID=us-east1 ./target/tasks-sample
```

The sample application uses an environment variable `LOCATION_ID`.

3. The application runs through some basic Cloud Tasks operations (create queue, create task) and then prints some results of the operations.

```
Test queue ready: name: "projects/xxxxxxxxxx/locations/us-central1/queues/graal-test-queue-4009"
rate_limits {
max_dispatches_per_second: 500.0
max_burst_size: 100
max_concurrent_dispatches: 1
}
retry_config {
max_attempts: 100
min_backoff {
nanos: 100000000
}
max_backoff {
seconds: 3600
}
max_doublings: 16
}
state: RUNNING

Created task: name: "projects/xxxxxxxxxx/locations/us-central1/queues/graal-test-queue-4009/tasks/5886258204485021611"
http_request {
url: "https://google.com/"
http_method: GET
headers {
key: "User-Agent"
value: "Google-Cloud-Tasks"
}
}
schedule_time {
seconds: 1613189391
nanos: 486293000
}
create_time {
seconds: 1613189391
}
dispatch_deadline {
seconds: 600
}
view: BASIC

Queue purged
Queue deleted
```

4. Run the test in the project in the native-image mode

```
$ LOCATION_ID=us-east1 mvn test -P native
...
[INFO] -------------------------------------------------------
[INFO] T E S T S
[INFO] -------------------------------------------------------
[INFO] Running com.example.tasks.ITNativeImageTasksSample
...
[INFO] --- native-maven-plugin:0.9.9:test (test-native) @ native-image-sample ---
[INFO] ====================
[INFO] Initializing project: native-image-sample
...
com.example.tasks.ITNativeImageTasksSample > testRunSampleApplication SUCCESSFUL


Test run finished after 1025 ms
[ 3 containers found ]
[ 0 containers skipped ]
[ 3 containers started ]
[ 0 containers aborted ]
[ 3 containers successful ]
[ 0 containers failed ]
[ 1 tests found ]
[ 0 tests skipped ]
[ 1 tests started ]
[ 0 tests aborted ]
[ 1 tests successful ]
[ 0 tests failed ]

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 02:27 min
...
```
150 changes: 150 additions & 0 deletions samples/native-image-sample/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
<?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>
<groupId>com.example.tasks</groupId>
<artifactId>native-image-sample</artifactId>
<name>Native Image Sample</name>

<!--
The parent pom defines common style checks and testing strategies for our samples.
Removing or replacing it should not affect the execution of the samples in anyway.
-->
<parent>
<groupId>com.google.cloud.samples</groupId>
<artifactId>shared-configuration</artifactId>
<version>1.2.0</version>
</parent>

<properties>
<!-- Java 8 because the Kokoro Sample test uses that Java version -->
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>libraries-bom</artifactId>
<version>24.2.0</version>

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the version that works with native-image-support 0.10.0.

<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-core</artifactId>
</dependency>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-tasks</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.truth</groupId>
<artifactId>truth</artifactId>
<version>1.1.3</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<!-- These plugins enable building the application to a JAR *not* using Native Image -->
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.example.TasksSampleApplication</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>

<profiles>
<profile>
<id>native</id>
<dependencies>
<dependency>
<!-- TODO: remove this when it's no longer needed -->
<groupId>com.google.cloud</groupId>
<artifactId>native-image-support</artifactId>
<version>0.10.0</version>
</dependency>

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No JUnit-related dependencies because this project doesn't use JUnit.

<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<version>5.8.2</version>
</dependency>
<dependency>
<groupId>org.graalvm.buildtools</groupId>
<artifactId>junit-platform-native</artifactId>
<version>0.9.9</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<!-- Must use older version of surefire plugin for native-image testing. -->
<version>2.22.2</version>
<configuration>
<includes>
<include>**/IT*</include>
</includes>
</configuration>
</plugin>
<plugin>
<groupId>org.graalvm.buildtools</groupId>
<artifactId>native-maven-plugin</artifactId>
<version>0.9.9</version>
<extensions>true</extensions>
<configuration>
<mainClass>com.example.tasks.TasksSampleApplication</mainClass>
<buildArgs>
<buildArg>--no-fallback</buildArg>
<buildArg>--no-server</buildArg>
<buildArg>--initialize-at-build-time</buildArg>
<buildArg>--features=com.google.cloud.nativeimage.features.ProtobufMessageFeature</buildArg>
</buildArgs>
</configuration>
<executions>
<execution>
<id>build-native</id>
<goals>
<goal>build</goal>
<goal>test</goal>
</goals>
<phase>package</phase>
</execution>
<execution>
<id>test-native</id>
<goals>
<goal>test</goal>
</goals>
<phase>test</phase>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* Copyright 2022 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.tasks;

import com.google.cloud.ServiceOptions;
import com.google.cloud.tasks.v2.CloudTasksClient;
import com.google.cloud.tasks.v2.CreateQueueRequest;
import com.google.cloud.tasks.v2.HttpMethod;
import com.google.cloud.tasks.v2.HttpRequest;
import com.google.cloud.tasks.v2.LocationName;
import com.google.cloud.tasks.v2.Queue;
import com.google.cloud.tasks.v2.QueueName;
import com.google.cloud.tasks.v2.RateLimits;
import com.google.cloud.tasks.v2.Task;
import java.io.IOException;
import java.util.UUID;

/** Sample application demonstrating Native Image compatibility with Google Cloud Tasks APIs. */
public class TasksSampleApplication {
/**
* Queue name randomness added to avoid FAILED_PRECONDITION: The queue cannot be created because a
* queue with this name existed too recently.
*/
private static final String GRAALVM_TEST_QUEUE_NAME = "graal-test-queue-";

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we replace "graalvm" with "native" or "native-image"?


private static final String LOCATION_ID = System.getenv("LOCATION_ID");

/** Runs the Cloud Tasks sample application. */
public static void main(String[] args) throws IOException {
String projectId = ServiceOptions.getDefaultProjectId();
LocationName parent = LocationName.of(projectId, LOCATION_ID);
QueueName queueName =
QueueName.of(
parent.getProject(),
parent.getLocation(),
GRAALVM_TEST_QUEUE_NAME + UUID.randomUUID().toString());

try (CloudTasksClient client = CloudTasksClient.create()) {
// Create queue
Queue queue =
Queue.newBuilder()
.setName(queueName.toString())
.setRateLimits(RateLimits.newBuilder().setMaxConcurrentDispatches(1).build())
.build();

CreateQueueRequest createQueueRequest =
CreateQueueRequest.newBuilder().setParent(parent.toString()).setQueue(queue).build();

Queue createdQueue = client.createQueue(createQueueRequest);
System.out.println("Test queue ready: " + createdQueue);

// Create task
HttpRequest taskTarget =
HttpRequest.newBuilder()
.setUrl("https://google.com")
.setHttpMethod(HttpMethod.GET)
.build();

Task taskRequest = Task.newBuilder().setHttpRequest(taskTarget).build();
Task task = client.createTask(queueName, taskRequest);
System.out.println("Created task: " + task);

// Cleanup
client.purgeQueue(queueName);
System.out.println("Queue purged");

client.deleteQueue(queueName);
System.out.println("Queue deleted");
}
}
}
Loading