Skip to content
Draft
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
107 changes: 107 additions & 0 deletions appengine-java21/ee8/pubsub/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
# Using Google Cloud Pub/Sub on App Engine Standard Java 21 Environment

<a href="https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/java-docs-samples&page=editor&open_in_editor=appengine-java21/pubsub/README.md">
<img alt="Open in Cloud Shell" src ="http://gstatic.com/cloudssh/images/open-btn.png"></a>

This sample demonstrates how to use [Google Cloud Pub/Sub][pubsub]
from [Google App Engine standard environment][ae-docs].

[pubsub]: https://cloud.google.com/pubsub/docs/
[ae-docs]: https://cloud.google.com/appengine/docs/java/

The home page of this application provides a form to publish messages using Google/Cloud PubSub. The application
then receives these published messages over a push subscription endpoint and then stores in Google Cloud Datastore.
The home page provides a view of the most recent messages persisted in storage.

## Clone the sample app

Copy the sample apps to your local machine, and cd to the pubsub directory:

```
git clone https://github.com/GoogleCloudPlatform/java-docs-samples
cd java-docs-samples/appengine-java21/ee8/pubsub
```

## Setup

- Make sure [`gcloud`](https://cloud.google.com/sdk/docs/) is installed and initialized:
```
gcloud init
```
- If this is the first time you are creating an App Engine project
```
gcloud app create
```
- For local development, [set up](https://cloud.google.com/docs/authentication/getting-started) authentication
- [Enable](https://console.cloud.google.com/launcher/details/google/pubsub.googleapis.com) Pub/Sub API

- Create a topic
```
gcloud pubsub topics create <your-topic-name>
```

- Create a push subscription, to send messages to a Google Cloud Project URL such as https://<your-project-id>.appspot.com/push.

The verification token is used to ensure that the end point only handles requests that are sent matching the verification token.
You can use `uuidgen` on MacOS X, Windows, and Linux to generate a unique verification token.

```
gcloud pubsub subscriptions create <your-subscription-name> \
--topic <your-topic-name> \
--push-endpoint \
https://<your-project-id>.appspot.com/pubsub/push?token=<your-verification-token> \
--ack-deadline 30
```

- Create a subscription for authenticated pushes to send messages to a Google Cloud Project URL such as https://<your-project-id>.appspot.com/authenticated-push.

The push auth service account must have Service Account Token Creator Role assigned, which can be done in the Cloud Console [IAM & admin](https://console.cloud.google.com/iam-admin/iam) UI.
`--push-auth-token-audience` is optional. If set, remember to modify the audience field check in [PubSubAuthenticatedPush.java](src/main/java/com/example/appengine/pubsub/PubSubAuthenticatedPush.java#L48).

```
gcloud pubsub subscriptions create <your-subscription-name> \
--topic <your-topic-name> \
--push-endpoint \
https://<your-project-id>.appspot.com/pubsub/authenticated-push?token=<your-verification-token> \
--ack-deadline 30 \
--push-auth-service-account=[your-service-account-email] \
--push-auth-token-audience=example.com
```

## Run locally
Set the following environment variables and run using shown Maven command. You can then
direct your browser to `http://localhost:8080/`

```
export PUBSUB_TOPIC=<your-topic-name>
export PUBSUB_VERIFICATION_TOKEN=<your-verification-token>
mvn appengine:run
```

## Send fake subscription push messages with:

```
curl -H "Content-Type: application/json" -i --data @sample_message.json
"localhost:8080/pubsub/push?token=<your-token>"
```

### Authenticated push notifications

Simulating authenticated push requests will fail because requests need to contain a Cloud Pub/Sub-generated JWT in the "Authorization" header.

```
curl -H "Content-Type: application/json" -i --data @sample_message.json
"localhost:8080/pubsub/authenticated-push?token=<your-token>"
```

## Deploy

Update the environment variables `PUBSUB_TOPIC` and `PUBSUB_VERIFICATION_TOKEN` in
[`appengine-web.xml`](src/main/webapp/WEB-INF/appengine-web.xml),
then:

```
mvn clean package appengine:deploy
```

Direct your browser to `https://project-id.appspot.com`.
109 changes: 109 additions & 0 deletions appengine-java21/ee8/pubsub/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<!--
Copyright 2017 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.
-->
<!-- [START project] -->
<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>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<groupId>com.example.appengine</groupId>
<artifactId>appengine-pubsub</artifactId>

<!--
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>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
<failOnMissingWebXml>false</failOnMissingWebXml> <!-- REQUIRED -->
</properties>

<!-- [START dependencies] -->
<!-- Using libraries-bom to manage versions.
See https://github.com/GoogleCloudPlatform/cloud-opensource-java/wiki/The-Google-Cloud-Platform-Libraries-BOM -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>libraries-bom</artifactId>
<version>26.28.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-pubsub</artifactId>
</dependency>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-datastore</artifactId>
</dependency>
<!-- [END dependencies] -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<type>jar</type>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.googlecode.jatl</groupId>
<artifactId>jatl</artifactId>
<version>0.2.3</version>
</dependency>
<!-- [START dependencies] -->
</dependencies>
<!-- [END dependencies] -->
<build>
<!-- for hot reload of the web application -->
<outputDirectory>${project.build.directory}/${project.build.finalName}/WEB-INF/classes</outputDirectory>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.4.0</version>
</plugin>
<plugin>
<groupId>com.google.cloud.tools</groupId>
<artifactId>appengine-maven-plugin</artifactId>
<version>2.5.0</version>
<configuration>
<!-- deploy configuration -->
<projectId>GCLOUD_CONFIG</projectId>
<version>GCLOUD_CONFIG</version>
</configuration>
</plugin>
<!-- for local testing of web application -->
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.4.53.v20231009</version>
</plugin>
</plugins>
</build>
</project>
<!-- [END project] -->
1 change: 1 addition & 0 deletions appengine-java21/ee8/pubsub/sample_message.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"message":{"data":"dGVzdA==","attributes":{},"messageId":"91010751788941","publishTime":"2017-09-25T23:16:42.302Z"}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright 2017 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.appengine.pubsub;

/**
* A message captures information from the Pubsub message received over the push endpoint and is
* persisted in storage.
*/
public class Message {
private String messageId;
private String publishTime;
private String data;

public Message(String messageId) {
this.messageId = messageId;
}

public String getMessageId() {
return messageId;
}

public void setMessageId(String messageId) {
this.messageId = messageId;
}

public String getPublishTime() {
return publishTime;
}

public void setPublishTime(String publishTime) {
this.publishTime = publishTime;
}

public String getData() {
return data;
}

public void setData(String data) {
this.data = data;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright 2017 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.appengine.pubsub;

import java.util.List;

public interface MessageRepository {

/** Save message to persistent storage. */
void save(Message message);

/**
* Retrieve most recent stored messages.
*
* @param limit number of messages
* @return list of messages
*/
List<Message> retrieve(int limit);

/** Save claim to persistent storage. */
void saveClaim(String claim);

/**
* Retrieve most recent stored claims.
*
* @param limit number of messages
* @return list of claims
*/
List<String> retrieveClaims(int limit);

/** Save token to persistent storage. */
void saveToken(String token);

/**
* Retrieve most recent stored tokens.
*
* @param limit number of messages
* @return list of tokens
*/
List<String> retrieveTokens(int limit);
}
Loading