An open API service indexing awesome lists of open source software.

https://github.com/auritylab/spring-gcp-tasks

GCP Cloud Tasks in Spring
https://github.com/auritylab/spring-gcp-tasks

cloud-tasks experimental kotlin spring-boot

Last synced: 16 days ago
JSON representation

GCP Cloud Tasks in Spring

Awesome Lists containing this project

README

        

# Spring GCP Cloud Tasks library

Spring-gcp-tasks is a small **experimental** library to integrate GCP Cloud Tasks into Spring projects.

Note: *This isn't production tested, but the library is in a finished state.*

---

* [Installation](https://github.com/AurityLab/spring-gcp-tasks#installation)
* [Compatibility](https://github.com/AurityLab/spring-gcp-tasks#compatibility)
* [Properties](https://github.com/AurityLab/spring-gcp-tasks#properties)
* [Usage](https://github.com/AurityLab/spring-gcp-tasks#usage)
* [Example](https://github.com/AurityLab/spring-gcp-tasks#example)

---

### Installation

Repository: `https://maven.pkg.github.com/AurityLab/spring-gcp-tasks`

Gradle:
```groovy
dependencies {
implementation "com.auritylab:spring-gcp-tasks:0.1.19"
}
```

Maven:
```xml

com.auritylab
spring-gcp-tasks
0.1.19

```

### Compatibility

This library is compatible with spring cloud `Hoxton` or higher (at least `Hoxton.M2`).

For everything below `Hoxton.M2` (`Greenwich` etc.) you **have** to use google-cloud-bom version `0.99.0-alpha` or higher:

Gradle:
```groovy
dependencyManagement {
imports {
mavenBom("org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}") {
bomProperty "google-cloud-bom.version", "0.99.0-alpha"
}
}
}
```

Maven:
```xml

com.google.cloud
google-cloud-bom
0.99.0-alpha
pom
import



org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import

```

Replace `${springCloudVersion}` with your version (e.g. `Greenwich.SR2`).

### Properties

Prefix for all properties: `com.auritylab.spring.gcp.tasks`

Property | Default | Required | Type | Description
------------ | ------------- | ------------- | ------------- | -------------
`signature-secret` | **nothing** | **yes** | `String` | Used for signing tasks before submitting to Cloud Tasks.
`default-project-id` | `null` | **no** | `String?` | Used when nothing specified in `@CloudTask` annotation.
`default-location-id` | `null` | **no** | `String?` | Used when nothing specified in `@CloudTask` annotation.
`default-queue-id` | `null` | **no** | `String?` | Used when nothing specified in `@CloudTask` annotation.
`default-worker-endpoint` | `null` | **no** | `String?` | Used when nothing specified in `@CloudTask` annotation.
`default-worker-endpoint-route` | `"/taskhandler"` | **default** | `String` | Used when nothing specified in `@CloudTask` annotation.
`default-worker-route` | `""` | **default** | `String` | Used when nothing specified in `@CloudTask` annotation.
`skip-cloud-tasks` | `false` | **default** | `Boolean` | Task submissions will be sent to the task's worker endpoint directly skipping Cloud Tasks.
`skip-task-endpoint` | `false` | **default** | `Boolean` | Task will be executed directly without networking (task's execute method blocks until task finished).
`queue-id-global-prefix` | `""` | **default** | `String` | Prefix used for all queue ids. Useful for testing or staging.

Note on types:
`String` -> non-nullable
`String?` -> nullable

Also note that `default-worker-endpoint-route` and `default-worker-route` are two different properties.
The former is used for the actual route to send the task to (in combination with `default-worker-endpoint`).
The latter is a *virtual* route to map the task to the right worker.

### Usage

There are two annotations for auto configuration:

`@EnableCloudTasks` and `@EnableCloudTasksWithEndpoint`

The former enables spring-gcp-tasks functionality, but disables the worker endpoint.

The latter also enables the worker endpoint via a rest controller mapped to the route specified
in `com.auritylab.spring.gcp.tasks.default-worker-endpoint-route`. A configured web server is needed for this.

```kotlin
@SpringBootApplication
// or @EnableCloudTasks to disable worker endpoint
@EnableCloudTasksWithEndpoint
class Application

fun main(args: Array) {
runApplication(*args)
}
```

### Example

Writing a task worker:

```kotlin
@CloudTask(route = "/notification")
class NotificationWorker : TaskWorker(Payload::class) {
override fun run(payload: Payload, id: UUID) {
println("New notification task: $payload")
}

data class Payload(val str: String)
}
```

Executing a task with worker above:

```kotlin
class NotificationTest(
private val notificationWorker: NotificationWorker
) {
fun execute() {
notificationWorker.execute(
NotificationWorker.Payload("test")
)
}
}
```