-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Pubsub flexible sample #592
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 18 commits
0722c03
3baa2d9
33fd597
b674933
79330cb
b4f932a
0a7781b
c60b4d9
0bd8196
3962632
2f5771d
411289d
873dbf5
88048cf
a964fc3
54c3798
c5c972b
dc771c0
fb45c47
ba77db1
dee7131
2fe47ac
58bf601
69c52b8
f9258ef
329ae0f
7256754
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| # App Engine Flexible Environment - Pub/Sub Sample | ||
|
|
||
| ## 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/flexible/pubsub | ||
| ``` | ||
|
|
||
| ## Setup | ||
|
|
||
| Make sure `gcloud` is installed and authenticated. | ||
|
|
||
| Create a topic | ||
| ``` | ||
| gcloud beta pubsub topics create <your-topic-name> | ||
| ``` | ||
|
|
||
| Create a subscription, which includes specifying the endpoint to which the Pub/Sub server should send requests. | ||
|
||
| ``` | ||
| gcloud beta 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 | ||
| ``` | ||
| ## Run | ||
|
|
||
| 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> | ||
| export PUBSUB_SUBSCRIPTION_ID=<your-subscription-id> | ||
| mvn jetty: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>" | ||
| ``` | ||
|
|
||
| ## Deploy | ||
|
|
||
| Update the environment variables `PUBSUB_TOPIC`, `PUBSUB_VERIFICATION_TOKEN` and `PUBSUB_SUBSCRIPTION_ID` in `app.yaml`, | ||
| then: | ||
|
|
||
| ``` | ||
| mvn appengine:deploy | ||
| ``` | ||
|
|
||
| The home page of this application provides a form to publish messages and also provides a view of the most recent messages | ||
| received over the push endpoint and persisted in storage. | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| <!-- | ||
| Copyright 2016 Google Inc. All Rights Reserved. | ||
|
||
|
|
||
| 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> | ||
| <modelVersion>4.0.0</modelVersion> | ||
| <packaging>war</packaging> | ||
| <version>1.0-SNAPSHOT</version> | ||
| <groupId>com.example.flexible</groupId> | ||
| <artifactId>flexible-pubsub</artifactId> | ||
|
|
||
| <parent> | ||
| <artifactId>doc-samples</artifactId> | ||
| <groupId>com.google.cloud</groupId> | ||
| <version>1.0.0</version> | ||
| <relativePath>../..</relativePath> | ||
| </parent> | ||
|
|
||
| <properties> | ||
| <appengine.maven.plugin>1.0.0</appengine.maven.plugin> | ||
|
||
| <maven.compiler.target>1.8</maven.compiler.target> | ||
| <maven.compiler.source>1.8</maven.compiler.source> | ||
| <failOnMissingWebXml>false</failOnMissingWebXml> | ||
| <jetty.maven.plugin>9.3.8.v20160314</jetty.maven.plugin> | ||
| </properties> | ||
|
|
||
| <dependencies> | ||
| <dependency> | ||
| <groupId>javax.servlet</groupId> | ||
| <artifactId>javax.servlet-api</artifactId> | ||
| <version>3.1.0</version> | ||
| <type>jar</type> | ||
| <scope>provided</scope> | ||
| </dependency> | ||
|
|
||
| <!-- [START dependencies] --> | ||
| <dependency> | ||
| <groupId>com.google.cloud</groupId> | ||
| <artifactId>google-cloud-pubsub</artifactId> | ||
| <version>0.11.1-alpha</version> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>com.google.cloud</groupId> | ||
| <artifactId>google-cloud-datastore</artifactId> | ||
| <version>0.11.1-beta</version> | ||
| </dependency> | ||
| <!-- [END dependencies] --> | ||
|
|
||
| <!-- Test Dependencies --> | ||
| <dependency> | ||
| <groupId>com.google.appengine</groupId> | ||
| <artifactId>appengine-api-stubs</artifactId> | ||
| <version>1.9.38</version> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>com.google.appengine</groupId> | ||
| <artifactId>appengine-tools-sdk</artifactId> | ||
| <version>1.9.38</version> | ||
| <scope>test</scope> | ||
| </dependency> | ||
|
|
||
| <dependency> | ||
| <groupId>com.google.truth</groupId> | ||
| <artifactId>truth</artifactId> | ||
| <version>0.30</version> | ||
| <scope>test</scope> | ||
| </dependency> | ||
|
|
||
| <dependency> | ||
| <groupId>junit</groupId> | ||
| <artifactId>junit</artifactId> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>org.mockito</groupId> | ||
| <artifactId>mockito-all</artifactId> | ||
| <version>1.10.19</version> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| </dependencies> | ||
| <build> | ||
| <!-- for hot reload of the web application --> | ||
| <outputDirectory>${project.build.directory}/${project.build.finalName}/WEB-INF/classes</outputDirectory> | ||
| <plugins> | ||
| <plugin> | ||
| <groupId>com.google.cloud.tools</groupId> | ||
| <artifactId>appengine-maven-plugin</artifactId> | ||
| <version>${appengine.maven.plugin}</version> | ||
| <configuration> | ||
| <!-- deploy configuration --> | ||
| </configuration> | ||
| </plugin> | ||
| <plugin> | ||
| <groupId>org.eclipse.jetty</groupId> | ||
| <artifactId>jetty-maven-plugin</artifactId> | ||
| <version>${jetty.maven.plugin}</version> | ||
| </plugin> | ||
| </plugins> | ||
| </build> | ||
| </project> | ||
| <!-- [END project] --> | ||
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| {"message":{"data":"dGVzdA==","attributes":{},"messageId":"91010751788941","publishTime":"2017-04-05T23:16:42.302Z"}} |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| # Copyright 2017 Google Inc. | ||
| # 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 appyaml] | ||
| runtime: java | ||
| env: flex | ||
|
|
||
| handlers: | ||
| - url: /.* | ||
| script: this field is required, but ignored | ||
| secure: always | ||
|
|
||
| # [START env_variables] | ||
| env_variables: | ||
| PUBSUB_TOPIC: test-topic-1 | ||
|
||
| PUBSUB_VERIFICATION_TOKEN: verification-token-123234 | ||
| PUBSUB_SUBSCRIPTION_NAME : subscription-name-1 | ||
| # [END env_variables] | ||
| # [END appyaml] | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| /** | ||
| * Copyright 2017 Google Inc. | ||
| * | ||
| * <p>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 | ||
| * | ||
| * <p>http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * <p>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.flexible.pubsub; | ||
|
|
||
| /** | ||
| * A message captures information from the Pubsub message received over the push endpoint and is | ||
| * persisted in storage. | ||
| */ | ||
| public class Message { | ||
| private String id; | ||
| private String publishTime; | ||
| private String data; | ||
|
|
||
| public Message() {} | ||
|
|
||
| public Message(String id) { | ||
| this.id = id; | ||
| } | ||
|
|
||
| public String getId() { | ||
| return id; | ||
| } | ||
|
|
||
| public void setId(String id) { | ||
| this.id = id; | ||
| } | ||
|
|
||
| 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,98 @@ | ||
| /** | ||
| * Copyright 2017 Google Inc. | ||
| * | ||
| * <p>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 | ||
| * | ||
| * <p>http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * <p>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.flexible.pubsub; | ||
|
|
||
| import com.google.cloud.datastore.*; | ||
| import java.util.ArrayList; | ||
| import java.util.Base64; | ||
| import java.util.List; | ||
|
|
||
| /** Storage for Message objects using Cloud Datastore. */ | ||
| public class MessageRepository { | ||
| private static String messagesKind = "messages"; | ||
| private KeyFactory keyFactory = getDatastoreInstance().newKeyFactory().setKind(messagesKind); | ||
|
|
||
| /** Create a lazy initialized singleton of MessageRepository. */ | ||
| private static class LazyInit { | ||
| private static final MessageRepository INSTANCE; | ||
|
|
||
| static { | ||
| INSTANCE = new MessageRepository(); | ||
| } | ||
| } | ||
|
|
||
| public static MessageRepository getInstance() { | ||
| return LazyInit.INSTANCE; | ||
| } | ||
|
|
||
| /** Save message id, data (decoded from base64), publish time */ | ||
| public void save(Message message) { | ||
| // Save message to "messages" | ||
| Datastore datastore = getDatastoreInstance(); | ||
| Key key = datastore.allocateId(keyFactory.newKey()); | ||
|
|
||
| Entity.Builder messageEntityBuilder = Entity.newBuilder(key).set("messageId", message.getId()); | ||
|
|
||
| if (message.getData() != null) { | ||
| messageEntityBuilder = messageEntityBuilder.set("data", decode(message.getData())); | ||
| } | ||
|
|
||
| if (message.getPublishTime() != null) { | ||
| messageEntityBuilder = messageEntityBuilder.set("publishTime", message.getPublishTime()); | ||
| } | ||
| datastore.put(messageEntityBuilder.build()); | ||
| } | ||
|
|
||
| /** | ||
| * Retrieve most recent messages. | ||
| * | ||
| * @param limit number of messages | ||
| * @return list of messages | ||
| */ | ||
| public List<Message> retrieve(int limit) { | ||
| // Get Message saved in Datastore | ||
| Datastore datastore = getDatastoreInstance(); | ||
| Query<Entity> query = | ||
| Query.newEntityQueryBuilder() | ||
|
||
| .setKind(messagesKind) | ||
| .setLimit(limit) | ||
| .addOrderBy(StructuredQuery.OrderBy.desc("publishTime")) | ||
| .build(); | ||
| QueryResults<Entity> results = datastore.run(query); | ||
|
|
||
| List<Message> messages = new ArrayList<>(); | ||
| while (results.hasNext()) { | ||
| Entity entity = results.next(); | ||
| Message message = new Message(entity.getString("messageId")); | ||
| String data = entity.getString("data"); | ||
| if (data != null) { | ||
| message.setData(data); | ||
| } | ||
| String publishTime = entity.getString("publishTime"); | ||
| if (publishTime != null) { | ||
| message.setPublishTime(publishTime); | ||
| } | ||
| messages.add(message); | ||
| } | ||
| return messages; | ||
| } | ||
|
|
||
| private String decode(String data) { | ||
| return new String(Base64.getDecoder().decode(data)); | ||
| } | ||
|
|
||
| private Datastore getDatastoreInstance() { | ||
| return DatastoreOptions.getDefaultInstance().getService(); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a link to Cloud SDK https://cloud.google.com/sdk/docs/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.