diff --git a/taskqueue/README.md b/taskqueue/README.md new file mode 100644 index 00000000000..a8cda57b7a3 --- /dev/null +++ b/taskqueue/README.md @@ -0,0 +1,5 @@ +# Task Queue Java Snippets + +These are Java samples for using the [Task Queue](https://cloud.google.com/appengine/docs/java/taskqueue/) + + diff --git a/taskqueue/deferred/README.md b/taskqueue/deferred/README.md new file mode 100644 index 00000000000..8ed90310827 --- /dev/null +++ b/taskqueue/deferred/README.md @@ -0,0 +1,24 @@ +App Engine Java Guestbook +Copyright (C) 2010-2012 Google Inc. + +## Sample guestbook for use with App Engine Java. + +Requires [Apache Maven](http://maven.apache.org) 3.1 or greater, and JDK 7+ in order to run. + +To build, run + + mvn package + +Building will run the tests, but to explicitly run tests you can use the test target + + mvn test + +To start the app, use the [App Engine Maven Plugin](http://code.google.com/p/appengine-maven-plugin/) that is already included in this demo. Just run the command. + + mvn appengine:devserver + +For further information, consult the [Java App Engine](https://developers.google.com/appengine/docs/java/overview) documentation. + +To see all the available goals for the App Engine plugin, run + + mvn help:describe -Dplugin=appengine \ No newline at end of file diff --git a/taskqueue/deferred/pom.xml b/taskqueue/deferred/pom.xml new file mode 100644 index 00000000000..172cce898b6 --- /dev/null +++ b/taskqueue/deferred/pom.xml @@ -0,0 +1,131 @@ + + + + 4.0.0 + war + 1.0-SNAPSHOT + + com.google.cloud.taskqueue.samples + defer-sample + + + 1.9.19 + 1 + UTF-8 + + + + 3.1.0 + + + + + + com.google.appengine + appengine-api-1.0-sdk + ${appengine.version} + + + javax.servlet + servlet-api + 2.5 + provided + + + jstl + jstl + 1.2 + + + + + junit + junit + 4.12-beta-1 + test + + + org.mockito + mockito-all + 1.9.5 + test + + + com.google.appengine + appengine-testing + ${appengine.version} + test + + + com.google.appengine + appengine-api-stubs + ${appengine.version} + test + + + + + + ${project.build.directory}/${project.build.finalName}/WEB-INF/classes + + + org.codehaus.mojo + versions-maven-plugin + 2.1 + + + compile + + display-dependency-updates + display-plugin-updates + + + + + + org.apache.maven.plugins + 3.1 + maven-compiler-plugin + + 1.7 + 1.7 + + + + + org.apache.maven.plugins + maven-war-plugin + 2.4 + + true + + + + ${basedir}/src/main/webapp/WEB-INF + true + WEB-INF + + + + + + + com.google.appengine + appengine-maven-plugin + 1.9.19 + + false + + + + + + + + + + diff --git a/taskqueue/deferred/src/main/java/com/google/cloud/taskqueue/samples/DeferSampleServlet.java b/taskqueue/deferred/src/main/java/com/google/cloud/taskqueue/samples/DeferSampleServlet.java new file mode 100644 index 00000000000..a70a059ea63 --- /dev/null +++ b/taskqueue/deferred/src/main/java/com/google/cloud/taskqueue/samples/DeferSampleServlet.java @@ -0,0 +1,60 @@ +/** + * Copyright 2015 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. + */ + +package com.google.cloud.taskqueue.samples; + +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; + +import com.google.appengine.api.taskqueue.DeferredTask; +import com.google.appengine.api.taskqueue.Queue; +import com.google.appengine.api.taskqueue.QueueFactory; +import com.google.appengine.api.taskqueue.TaskOptions; + +/** + * This small servlet demonstrates how to use the DeferredTask + * interface to background a task on the AppEngine task queues, + * without needing to create a separate URL handler. + */ +public class DeferSampleServlet extends HttpServlet { + + //[START defer] + public static class ExpensiveOperation implements DeferredTask { + @Override + public void run() { + System.out.println("Doing an expensive operation..."); + // expensive operation to be backgrounded goes here + } + } + + @Override + public void doGet(HttpServletRequest request, HttpServletResponse + resp) throws IOException { + // Add the task to the default queue. + Queue queue = QueueFactory.getDefaultQueue(); + + // Wait 5 seconds to run for demonstration purposes + queue.add(TaskOptions.Builder.withPayload(new ExpensiveOperation()) + .etaMillis(System.currentTimeMillis() + 5000)); + + resp.setContentType("text/plain"); + resp.getWriter().println("Task is backgrounded on queue!"); + } + //[END defer] + +} diff --git a/taskqueue/deferred/src/main/webapp/WEB-INF/appengine-web.xml b/taskqueue/deferred/src/main/webapp/WEB-INF/appengine-web.xml new file mode 100644 index 00000000000..6d4b396b5bb --- /dev/null +++ b/taskqueue/deferred/src/main/webapp/WEB-INF/appengine-web.xml @@ -0,0 +1,26 @@ + + + + + your-app-id + ${appengine.app.version} + true + + + + + diff --git a/taskqueue/deferred/src/main/webapp/WEB-INF/logging.properties b/taskqueue/deferred/src/main/webapp/WEB-INF/logging.properties new file mode 100644 index 00000000000..0c2ea51bc6d --- /dev/null +++ b/taskqueue/deferred/src/main/webapp/WEB-INF/logging.properties @@ -0,0 +1,13 @@ +# A default java.util.logging configuration. +# (All App Engine logging is through java.util.logging by default). +# +# To use this configuration, copy it into your application's WEB-INF +# folder and add the following to your appengine-web.xml: +# +# +# +# +# + +# Set the default logging level for all loggers to WARNING +.level = WARNING diff --git a/taskqueue/deferred/src/main/webapp/WEB-INF/web.xml b/taskqueue/deferred/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 00000000000..81cc85ee959 --- /dev/null +++ b/taskqueue/deferred/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,30 @@ + + + + + defer-sample-servlet + com.google.cloud.taskqueue.samples.DeferSampleServlet + + + defer-sample-servlet + /defer + + diff --git a/taskqueue/deferred/src/main/webapp/guestbook.jsp b/taskqueue/deferred/src/main/webapp/guestbook.jsp new file mode 100644 index 00000000000..13097933e3e --- /dev/null +++ b/taskqueue/deferred/src/main/webapp/guestbook.jsp @@ -0,0 +1,110 @@ +<%-- + Copyright 2015 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. + +--%> + +<%@ page contentType="text/html;charset=UTF-8" language="java" %> +<%@ page import="com.google.appengine.api.datastore.DatastoreService" %> +<%@ page import="com.google.appengine.api.datastore.DatastoreServiceFactory" %> +<%@ page import="com.google.appengine.api.datastore.Entity" %> +<%@ page import="com.google.appengine.api.datastore.FetchOptions" %> +<%@ page import="com.google.appengine.api.datastore.Key" %> +<%@ page import="com.google.appengine.api.datastore.KeyFactory" %> +<%@ page import="com.google.appengine.api.datastore.Query" %> +<%@ page import="com.google.appengine.api.users.User" %> +<%@ page import="com.google.appengine.api.users.UserService" %> +<%@ page import="com.google.appengine.api.users.UserServiceFactory" %> +<%@ page import="java.util.List" %> +<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %> + + + + + + + + +<% + String guestbookName = request.getParameter("guestbookName"); + if (guestbookName == null) { + guestbookName = "default"; + } + pageContext.setAttribute("guestbookName", guestbookName); + UserService userService = UserServiceFactory.getUserService(); + User user = userService.getCurrentUser(); + if (user != null) { + pageContext.setAttribute("user", user); +%> +

Hello, ${fn:escapeXml(user.nickname)}! (You can + sign out.)

+<% +} else { +%> +

Hello! + Sign in + to include your name with greetings you post.

+<% + } +%> + +<% + DatastoreService datastore = DatastoreServiceFactory.getDatastoreService(); + Key guestbookKey = KeyFactory.createKey("Guestbook", guestbookName); + // Run an ancestor query to ensure we see the most up-to-date + // view of the Greetings belonging to the selected Guestbook. + Query query = new Query("Greeting", guestbookKey).addSort("date", Query.SortDirection.DESCENDING); + List greetings = datastore.prepare(query).asList(FetchOptions.Builder.withLimit(5)); + if (greetings.isEmpty()) { +%> +

Guestbook '${fn:escapeXml(guestbookName)}' has no messages.

+<% +} else { +%> +

Messages in Guestbook '${fn:escapeXml(guestbookName)}'.

+<% + for (Entity greeting : greetings) { + pageContext.setAttribute("greeting_content", + greeting.getProperty("content")); + if (greeting.getProperty("user") == null) { +%> +

An anonymous person wrote:

+<% +} else { + pageContext.setAttribute("greeting_user", + greeting.getProperty("user")); +%> +

${fn:escapeXml(greeting_user.nickname)} wrote:

+<% + } +%> +
${fn:escapeXml(greeting_content)}
+<% + } + } +%> + +
+
+
+ +
+ +
+
+
+
+ + +