-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Adding Pub/Sub App engine Java 8 sample #865
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
46830ef
Adding Pub/Sub app engine sample
jabubake a4f8bee
updating README
jabubake 82a046f
Merge branch 'master' into appengine-pubsub-j8
lesv c0276f2
removing unit tests
jabubake 0912367
Merge branch 'master' of https://github.com/GoogleCloudPlatform/java-…
jabubake 5192847
Merge branch 'appengine-pubsub-j8' of https://github.com/GoogleCloudP…
jabubake File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
# Using Google Cloud Pub/Sub on App Engine Standard Java 8 Environment | ||
|
||
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-java8/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 beta 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. | ||
``` | ||
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> | ||
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>" | ||
``` | ||
|
||
## 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 appengine:deploy | ||
``` | ||
|
||
Direct your browser to `https://project-id.appspot.com`. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
<!-- | ||
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 project] --> | ||
<project> | ||
<modelVersion>4.0.0</modelVersion> | ||
<packaging>war</packaging> | ||
<version>1.0-SNAPSHOT</version> | ||
<groupId>com.example.flexible</groupId> | ||
<artifactId>appengine-pubsub</artifactId> | ||
|
||
<parent> | ||
<artifactId>appengine-java8-samples</artifactId> | ||
<groupId>com.google.cloud</groupId> | ||
<version>1.0.0</version> | ||
<relativePath>..</relativePath> | ||
</parent> | ||
|
||
<properties> | ||
<maven.compiler.target>1.8</maven.compiler.target> | ||
<maven.compiler.source>1.8</maven.compiler.source> | ||
<failOnMissingWebXml>false</failOnMissingWebXml> <!-- REQUIRED --> | ||
<appengine.maven.plugin>1.3.1</appengine.maven.plugin> | ||
<jetty>9.4.4.v20170414</jetty> | ||
</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.24.0-beta</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.google.cloud</groupId> | ||
<artifactId>google-cloud-datastore</artifactId> | ||
<version>1.6.0</version> | ||
</dependency> | ||
<!-- [END dependencies] --> | ||
|
||
<!-- Test Dependencies --> | ||
<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> | ||
</plugins> | ||
</build> | ||
</project> | ||
<!-- [END project] --> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"}} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this used anywhere? |
53 changes: 53 additions & 0 deletions
53
appengine-java8/pubsub/src/main/java/com/example/appengine/pubsub/Message.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/** | ||
* 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.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; | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
appengine-java8/pubsub/src/main/java/com/example/appengine/pubsub/MessageRepository.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/** | ||
* 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.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); | ||
} |
98 changes: 98 additions & 0 deletions
98
appengine-java8/pubsub/src/main/java/com/example/appengine/pubsub/MessageRepositoryImpl.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.appengine.pubsub; | ||
|
||
import com.google.cloud.datastore.Datastore; | ||
import com.google.cloud.datastore.DatastoreOptions; | ||
import com.google.cloud.datastore.Entity; | ||
import com.google.cloud.datastore.Key; | ||
import com.google.cloud.datastore.KeyFactory; | ||
import com.google.cloud.datastore.Query; | ||
import com.google.cloud.datastore.QueryResults; | ||
import com.google.cloud.datastore.StructuredQuery; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** Storage for Message objects using Cloud Datastore. */ | ||
public class MessageRepositoryImpl implements MessageRepository { | ||
|
||
private static MessageRepositoryImpl instance; | ||
|
||
private String messagesKind = "messages"; | ||
private KeyFactory keyFactory = getDatastoreInstance().newKeyFactory().setKind(messagesKind); | ||
|
||
@Override | ||
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.getMessageId()); | ||
|
||
if (message.getData() != null) { | ||
messageEntityBuilder = messageEntityBuilder.set("data", message.getData()); | ||
} | ||
|
||
if (message.getPublishTime() != null) { | ||
messageEntityBuilder = messageEntityBuilder.set("publishTime", message.getPublishTime()); | ||
} | ||
datastore.put(messageEntityBuilder.build()); | ||
} | ||
|
||
@Override | ||
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 Datastore getDatastoreInstance() { | ||
return DatastoreOptions.getDefaultInstance().getService(); | ||
} | ||
|
||
private MessageRepositoryImpl() { | ||
} | ||
|
||
// retrieve a singleton instance | ||
public static synchronized MessageRepositoryImpl getInstance() { | ||
if (instance == null) { | ||
instance = new MessageRepositoryImpl(); | ||
} | ||
return instance; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
how do I get/create a verification token? MacOS X, Windows, and Linux have
uuidgen
.