├── .gitignore ├── LICENSE ├── README.md ├── appengine-modules-ear ├── build.gradle ├── nbactions.xml ├── pom.xml └── src │ └── main │ └── application │ └── META-INF │ ├── MANIFEST.MF │ ├── appengine-application.xml │ ├── application.xml │ └── maven-application.xml ├── appengine-modules-guestbook ├── build.gradle ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── google │ │ │ └── appengine │ │ │ └── demos │ │ │ └── guestbook │ │ │ ├── GuestbookServlet.java │ │ │ └── SignGuestbookServlet.java │ └── webapp │ │ ├── WEB-INF │ │ ├── appengine-web.xml │ │ ├── datastore-indexes.xml │ │ ├── logging.properties │ │ └── web.xml │ │ ├── guestbook.jsp │ │ └── stylesheets │ │ └── main.css │ └── test │ └── java │ └── com │ └── google │ └── appengine │ └── demos │ └── guestbook │ ├── GuestbookServletTest.java │ └── SignGuestbookServletTest.java ├── appengine-modules-shardedcounter ├── build.gradle ├── pom.xml └── src │ └── main │ ├── java │ └── com │ │ └── google │ │ └── appengine │ │ └── demos │ │ └── shardedcounter │ │ ├── java │ │ ├── v1 │ │ │ ├── CounterPage.java │ │ │ └── ShardedCounter.java │ │ └── v2 │ │ │ ├── CounterPage.java │ │ │ └── ShardedCounter.java │ │ └── jdo │ │ ├── PMF.java │ │ ├── v1 │ │ ├── Counter.java │ │ ├── CounterPage.java │ │ └── LimitedCounterShard.java │ │ ├── v2 │ │ ├── Counter.java │ │ ├── CounterFactory.java │ │ ├── CounterPage.java │ │ ├── CounterShard.java │ │ └── ShardedCounter.java │ │ └── v3 │ │ ├── CounterFactory.java │ │ ├── CounterPage.java │ │ ├── DatastoreCounter.java │ │ ├── DatastoreCounterShard.java │ │ └── ShardedCounter.java │ ├── resources │ └── META-INF │ │ └── jdoconfig.xml │ └── webapp │ ├── WEB-INF │ ├── appengine-web.xml │ └── web.xml │ ├── favicon.ico │ └── index.html ├── build.gradle ├── pom.xml └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | target/ 2 | build/ 3 | .gradle 4 | datanucleus.log 5 | .DS_Store 6 | out 7 | *.iml 8 | *.ipr 9 | *.iws 10 | .idea 11 | .settings 12 | .classpath 13 | .project -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright [yyyy] [name of copyright owner] 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. 203 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ![status: inactive](https://img.shields.io/badge/status-inactive-red.svg) 2 | 3 | This project is no longer actively developed or maintained. 4 | 5 | For new work on this check out [java-docs-samples](https://github.com/GoogleCloudPlatform/java-docs-samples) and [getting-started-java](https://github.com/GoogleCloudPlatform/getting-started-java). 6 | 7 | 8 | # appengine-modules-sample-java 9 | 10 | 11 | It has 2 web apps each deployed to a different module: guestbook and shardedcounter. 12 | 13 | Usage (After changing the app id in the appengine-modules-ear/src/main/application/META-INF/appengine-application.xml): 14 | 15 | git clone https://github.com/GoogleCloudPlatform/appengine-modules-sample-java.git 16 | cd appengine-modules-sample-java 17 | mvn install 18 | cd appengine-modules-ear 19 | #to test it locally: 20 | mvn appengine:devserver 21 | #or to deploy it: 22 | mvn appengine:update 23 | 24 | ============================= 25 | -------------------------------------------------------------------------------- /appengine-modules-ear/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'ear' 2 | apply plugin: 'appengine' 3 | 4 | buildscript { 5 | repositories { 6 | mavenCentral() 7 | } 8 | 9 | dependencies { 10 | classpath 'com.google.appengine:gradle-appengine-plugin:1.8.6' 11 | } 12 | } 13 | 14 | dependencies { 15 | appengineSdk 'com.google.appengine:appengine-java-sdk:1.8.6' 16 | deploy project(path: ':appengine-modules-guestbook', configuration: 'archives') 17 | deploy project(path: ':appengine-modules-shardedcounter', configuration: 'archives') 18 | } 19 | 20 | appengine { 21 | downloadSdk = true 22 | } 23 | -------------------------------------------------------------------------------- /appengine-modules-ear/nbactions.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | CUSTOM-devserver 5 | devserver 6 | 7 | appengine:devserver 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /appengine-modules-ear/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4.0.0 4 | 5 | com.google.appengine.sample 6 | appengine-modules-sample-java 7 | 1.0 8 | 9 | 10 | appengine-modules-ear 11 | 1.0 12 | ear 13 | 14 | appengine-modules-ear 15 | 16 | 17 | 18 | 19 | org.apache.maven.plugins 20 | maven-compiler-plugin 21 | 2.5.1 22 | 23 | 1.7 24 | 1.7 25 | 26 | 27 | 28 | org.apache.maven.plugins 29 | maven-ear-plugin 30 | 2.9 31 | 32 | 5 33 | lib 34 | war 35 | ${project.basedir}/src/main/application/META-INF/maven-application.xml 36 | 37 | 38 | 39 | com.google.appengine 40 | appengine-maven-plugin 41 | ${appengine.target.version} 42 | 43 | 44 | 45 | 46 | 47 | 48 | com.google.appengine.sample 49 | appengine-modules-guestbook 50 | 1.0 51 | war 52 | 53 | 54 | com.google.appengine.sample 55 | appengine-modules-shardedcounter 56 | 1.0 57 | war 58 | 59 | 60 | 61 | 62 | -------------------------------------------------------------------------------- /appengine-modules-ear/src/main/application/META-INF/MANIFEST.MF: -------------------------------------------------------------------------------- 1 | Manifest-Version: 1.0 2 | 3 | -------------------------------------------------------------------------------- /appengine-modules-ear/src/main/application/META-INF/appengine-application.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | change-to-your-app-id 4 | -------------------------------------------------------------------------------- /appengine-modules-ear/src/main/application/META-INF/application.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | appengine-modules-ear 5 | 6 | 7 | 12 | appengine-modules-guestbook-1.0 13 | appengine-modules-guestbook 14 | 15 | 16 | 17 | 18 | appengine-modules-shardedcounter-1.0 19 | appengine-modules-shardedcounter 20 | 21 | 22 | lib 23 | 24 | -------------------------------------------------------------------------------- /appengine-modules-ear/src/main/application/META-INF/maven-application.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | appengine-modules-ear 4 | 5 | 6 | appengine-modules-guestbook-1.0.war 7 | /appengine-modules-guestbook 8 | 9 | 10 | 11 | 12 | appengine-modules-shardedcounter-1.0.war 13 | /appengine-modules-shardedcounter 14 | 15 | 16 | lib 17 | -------------------------------------------------------------------------------- /appengine-modules-guestbook/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'war' 2 | 3 | dependencies { 4 | compile 'com.google.appengine:appengine-api-1.0-sdk:1.8.5' 5 | compile 'javax.servlet:servlet-api:2.5' 6 | compile 'jstl:jstl:1.2' 7 | testCompile 'com.google.appengine:appengine-testing:1.8.5' 8 | testCompile 'com.google.appengine:appengine-api-stubs:1.8.5' 9 | testCompile 'junit:junit:4.10' 10 | testCompile 'org.mockito:mockito-all:1.9.0' 11 | } 12 | -------------------------------------------------------------------------------- /appengine-modules-guestbook/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4.0.0 4 | 5 | com.google.appengine.sample 6 | appengine-modules-sample-java 7 | 1.0 8 | 9 | 10 | appengine-modules-guestbook 11 | war 12 | 1.0 13 | 14 | appengine-modules-guestbook 15 | 16 | 17 | 18 | 19 | 20 | 21 | com.google.appengine 22 | appengine-api-1.0-sdk 23 | ${appengine.target.version} 24 | 25 | 26 | javax.servlet 27 | servlet-api 28 | 2.5 29 | provided 30 | 31 | 32 | jstl 33 | jstl 34 | 1.2 35 | 36 | 37 | 38 | 39 | junit 40 | junit 41 | 4.10 42 | test 43 | 44 | 45 | org.mockito 46 | mockito-all 47 | 1.9.0 48 | test 49 | 50 | 51 | com.google.appengine 52 | appengine-testing 53 | ${appengine.target.version} 54 | test 55 | 56 | 57 | com.google.appengine 58 | appengine-api-stubs 59 | ${appengine.target.version} 60 | test 61 | 62 | 63 | 64 | 65 | 66 | 67 | org.apache.maven.plugins 68 | 2.5.1 69 | maven-compiler-plugin 70 | 71 | 1.7 72 | 1.7 73 | 74 | 75 | 76 | 77 | org.apache.maven.plugins 78 | maven-war-plugin 79 | 2.3 80 | 81 | 82 | 83 | com.google.appengine 84 | appengine-maven-plugin 85 | ${appengine.target.version} 86 | 87 | 88 | 89 | 90 | 91 | -------------------------------------------------------------------------------- /appengine-modules-guestbook/src/main/java/com/google/appengine/demos/guestbook/GuestbookServlet.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2012 Google Inc. All Rights Reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.google.appengine.demos.guestbook; 18 | 19 | import com.google.appengine.api.users.User; 20 | import com.google.appengine.api.users.UserService; 21 | import com.google.appengine.api.users.UserServiceFactory; 22 | 23 | import java.io.IOException; 24 | 25 | import javax.servlet.http.HttpServlet; 26 | import javax.servlet.http.HttpServletRequest; 27 | import javax.servlet.http.HttpServletResponse; 28 | 29 | public class GuestbookServlet extends HttpServlet { 30 | @Override 31 | public void doGet(HttpServletRequest req, HttpServletResponse resp) 32 | throws IOException { 33 | 34 | final UserService userService = UserServiceFactory.getUserService(); 35 | final User currentUser = userService.getCurrentUser(); 36 | 37 | if (currentUser != null) { 38 | resp.setContentType("text/plain"); 39 | resp.getWriter().println("Hello, " + currentUser.getNickname()); 40 | } else { 41 | resp.sendRedirect(userService.createLoginURL(req.getRequestURI())); 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /appengine-modules-guestbook/src/main/java/com/google/appengine/demos/guestbook/SignGuestbookServlet.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2012 Google Inc. All Rights Reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.google.appengine.demos.guestbook; 18 | 19 | import com.google.appengine.api.datastore.DatastoreService; 20 | import com.google.appengine.api.datastore.DatastoreServiceFactory; 21 | import com.google.appengine.api.datastore.Entity; 22 | import com.google.appengine.api.datastore.Key; 23 | import com.google.appengine.api.datastore.KeyFactory; 24 | import com.google.appengine.api.users.User; 25 | import com.google.appengine.api.users.UserService; 26 | import com.google.appengine.api.users.UserServiceFactory; 27 | 28 | import java.io.IOException; 29 | import java.util.Date; 30 | 31 | import javax.servlet.http.HttpServlet; 32 | import javax.servlet.http.HttpServletRequest; 33 | import javax.servlet.http.HttpServletResponse; 34 | 35 | public class SignGuestbookServlet extends HttpServlet { 36 | @Override 37 | public void doPost(HttpServletRequest req, HttpServletResponse resp) 38 | throws IOException { 39 | final UserService userService = UserServiceFactory.getUserService(); 40 | final User user = userService.getCurrentUser(); 41 | 42 | final String guestbookName = req.getParameter("guestbookName"); 43 | final Key guestbookKey = KeyFactory.createKey("Guestbook", guestbookName); 44 | final String content = req.getParameter("content"); 45 | final Date date = new Date(); 46 | final Entity greeting = new Entity("Greeting", guestbookKey); 47 | greeting.setProperty("user", user); 48 | greeting.setProperty("date", date); 49 | greeting.setProperty("content", content); 50 | 51 | final DatastoreService datastore = DatastoreServiceFactory.getDatastoreService(); 52 | datastore.put(greeting); 53 | 54 | resp.sendRedirect("/guestbook.jsp?guestbookName=" + guestbookName); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /appengine-modules-guestbook/src/main/webapp/WEB-INF/appengine-web.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | guestbook 4 | 1 5 | true 6 | 7 | 11 | 12 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /appengine-modules-guestbook/src/main/webapp/WEB-INF/datastore-indexes.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /appengine-modules-guestbook/src/main/webapp/WEB-INF/logging.properties: -------------------------------------------------------------------------------- 1 | # A default java.util.logging configuration. 2 | # (All App Engine logging is through java.util.logging by default). 3 | # 4 | # To use this configuration, copy it into your application's WEB-INF 5 | # folder and add the following to your appengine-web.xml: 6 | # 7 | # 8 | # 9 | # 10 | # 11 | 12 | # Set the default logging level for all loggers to WARNING 13 | .level = WARNING 14 | -------------------------------------------------------------------------------- /appengine-modules-guestbook/src/main/webapp/WEB-INF/web.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | sign 9 | com.google.appengine.demos.guestbook.SignGuestbookServlet 10 | 11 | 12 | sign 13 | /sign 14 | 15 | 16 | guestbook.jsp 17 | 18 | 19 | -------------------------------------------------------------------------------- /appengine-modules-guestbook/src/main/webapp/guestbook.jsp: -------------------------------------------------------------------------------- 1 | <%@ page contentType="text/html;charset=UTF-8" language="java" %> 2 | <%@ page import="com.google.appengine.api.datastore.DatastoreService" %> 3 | <%@ page import="com.google.appengine.api.datastore.DatastoreServiceFactory" %> 4 | <%@ page import="com.google.appengine.api.datastore.Entity" %> 5 | <%@ page import="com.google.appengine.api.datastore.FetchOptions" %> 6 | <%@ page import="com.google.appengine.api.datastore.Key" %> 7 | <%@ page import="com.google.appengine.api.datastore.KeyFactory" %> 8 | <%@ page import="com.google.appengine.api.datastore.Query" %> 9 | <%@ page import="com.google.appengine.api.users.User" %> 10 | <%@ page import="com.google.appengine.api.users.UserService" %> 11 | <%@ page import="com.google.appengine.api.users.UserServiceFactory" %> 12 | <%@ page import="java.util.List" %> 13 | <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %> 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | <% 23 | String guestbookName = request.getParameter("guestbookName"); 24 | if (guestbookName == null) { 25 | guestbookName = "default"; 26 | } 27 | pageContext.setAttribute("guestbookName", guestbookName); 28 | final UserService userService = UserServiceFactory.getUserService(); 29 | final User user = userService.getCurrentUser(); 30 | if (user != null) { 31 | pageContext.setAttribute("user", user); 32 | %> 33 |

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

35 | <% 36 | } else { 37 | %> 38 |

Hello! 39 | Sign in 40 | to include your name with greetings you post.

41 | <% 42 | } 43 | %> 44 | 45 | <% 46 | final DatastoreService datastore = DatastoreServiceFactory.getDatastoreService(); 47 | final Key guestbookKey = KeyFactory.createKey("Guestbook", guestbookName); 48 | // Run an ancestor query to ensure we see the most up-to-date 49 | // view of the Greetings belonging to the selected Guestbook. 50 | final Query query = new Query("Greeting", guestbookKey).addSort("date", Query.SortDirection.DESCENDING); 51 | final List greetings = datastore.prepare(query).asList(FetchOptions.Builder.withLimit(5)); 52 | if (greetings.isEmpty()) { 53 | %> 54 |

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

55 | <% 56 | } else { 57 | %> 58 |

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

59 | <% 60 | for (final Entity greeting : greetings) { 61 | pageContext.setAttribute("greeting_content", 62 | greeting.getProperty("content")); 63 | if (greeting.getProperty("user") == null) { 64 | %> 65 |

An anonymous person wrote:

66 | <% 67 | } else { 68 | pageContext.setAttribute("greeting_user", 69 | greeting.getProperty("user")); 70 | %> 71 |

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

72 | <% 73 | } 74 | %> 75 |
${fn:escapeXml(greeting_content)}
76 | <% 77 | } 78 | } 79 | %> 80 | 81 |
82 |
83 |
84 | 85 |
86 | 87 |
88 |
89 |
90 |
91 | 92 | 93 | 94 | -------------------------------------------------------------------------------- /appengine-modules-guestbook/src/main/webapp/stylesheets/main.css: -------------------------------------------------------------------------------- 1 | body { 2 | font-family: Verdana, Helvetica, sans-serif; 3 | background-color: #FFFFCC; 4 | } 5 | -------------------------------------------------------------------------------- /appengine-modules-guestbook/src/test/java/com/google/appengine/demos/guestbook/GuestbookServletTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2012 Google Inc. All Rights Reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.google.appengine.demos.guestbook; 18 | 19 | import static junit.framework.Assert.assertEquals; 20 | import static org.mockito.Mockito.mock; 21 | import static org.mockito.Mockito.when; 22 | 23 | import com.google.appengine.api.users.User; 24 | import com.google.appengine.api.users.UserServiceFactory; 25 | import com.google.appengine.tools.development.testing.LocalServiceTestHelper; 26 | import com.google.appengine.tools.development.testing.LocalUserServiceTestConfig; 27 | 28 | import org.junit.After; 29 | import org.junit.Before; 30 | import org.junit.Test; 31 | 32 | import java.io.IOException; 33 | import java.io.PrintWriter; 34 | import java.io.StringWriter; 35 | 36 | import javax.servlet.http.HttpServletRequest; 37 | import javax.servlet.http.HttpServletResponse; 38 | 39 | public class GuestbookServletTest { 40 | 41 | private GuestbookServlet guestbookServlet; 42 | 43 | private final LocalServiceTestHelper helper = 44 | new LocalServiceTestHelper(new LocalUserServiceTestConfig()) 45 | .setEnvIsLoggedIn(true) 46 | .setEnvAuthDomain("localhost") 47 | .setEnvEmail("test@localhost"); 48 | 49 | @Before 50 | public void setupGuestBookServlet() { 51 | helper.setUp(); 52 | guestbookServlet = new GuestbookServlet(); 53 | } 54 | 55 | @After 56 | public void tearDownHelper() { 57 | helper.tearDown(); 58 | } 59 | 60 | @Test 61 | public void testDoGet() throws IOException { 62 | final HttpServletRequest request = mock(HttpServletRequest.class); 63 | final HttpServletResponse response = mock(HttpServletResponse.class); 64 | 65 | final StringWriter stringWriter = new StringWriter(); 66 | 67 | when(response.getWriter()).thenReturn(new PrintWriter(stringWriter)); 68 | 69 | guestbookServlet.doGet(request, response); 70 | 71 | final User currentUser = UserServiceFactory.getUserService().getCurrentUser(); 72 | 73 | assertEquals("Hello, " + currentUser.getNickname() + "\n", stringWriter.toString()); 74 | } 75 | 76 | } 77 | -------------------------------------------------------------------------------- /appengine-modules-guestbook/src/test/java/com/google/appengine/demos/guestbook/SignGuestbookServletTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2012 Google Inc. All Rights Reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.google.appengine.demos.guestbook; 18 | 19 | import static junit.framework.Assert.assertEquals; 20 | import static junit.framework.Assert.assertTrue; 21 | import static org.mockito.Mockito.mock; 22 | import static org.mockito.Mockito.verify; 23 | import static org.mockito.Mockito.when; 24 | 25 | import com.google.appengine.api.datastore.DatastoreServiceFactory; 26 | import com.google.appengine.api.datastore.Entity; 27 | import com.google.appengine.api.datastore.EntityNotFoundException; 28 | import com.google.appengine.api.datastore.Query; 29 | import com.google.appengine.api.users.User; 30 | import com.google.appengine.api.users.UserServiceFactory; 31 | import com.google.appengine.tools.development.testing.LocalDatastoreServiceTestConfig; 32 | import com.google.appengine.tools.development.testing.LocalServiceTestHelper; 33 | 34 | import org.junit.After; 35 | import org.junit.Before; 36 | import org.junit.Test; 37 | 38 | import java.io.IOException; 39 | import java.util.Date; 40 | 41 | import javax.servlet.http.HttpServletRequest; 42 | import javax.servlet.http.HttpServletResponse; 43 | 44 | public class SignGuestbookServletTest { 45 | 46 | private SignGuestbookServlet signGuestbookServlet; 47 | 48 | private final LocalServiceTestHelper helper = 49 | new LocalServiceTestHelper(new LocalDatastoreServiceTestConfig()) 50 | .setEnvIsLoggedIn(true) 51 | .setEnvAuthDomain("localhost") 52 | .setEnvEmail("test@localhost"); 53 | 54 | @Before 55 | public void setupSignGuestBookServlet() { 56 | helper.setUp(); 57 | signGuestbookServlet = new SignGuestbookServlet(); 58 | } 59 | 60 | @After 61 | public void tearDownHelper() { 62 | helper.tearDown(); 63 | } 64 | 65 | @Test 66 | public void testDoPost() throws IOException, EntityNotFoundException { 67 | final HttpServletRequest request = mock(HttpServletRequest.class); 68 | final HttpServletResponse response = mock(HttpServletResponse.class); 69 | 70 | final String guestbookName = "TestGuestbook"; 71 | final String testContent = "Test Content"; 72 | 73 | when(request.getParameter("guestbookName")).thenReturn(guestbookName); 74 | when(request.getParameter("content")).thenReturn(testContent); 75 | 76 | final Date priorToRequest = new Date(); 77 | 78 | signGuestbookServlet.doPost(request, response); 79 | 80 | final Date afterRequest = new Date(); 81 | 82 | verify(response).sendRedirect("/guestbook.jsp?guestbookName=TestGuestbook"); 83 | 84 | final User currentUser = UserServiceFactory.getUserService().getCurrentUser(); 85 | 86 | final Entity greeting = DatastoreServiceFactory.getDatastoreService().prepare(new Query()).asSingleEntity(); 87 | 88 | assertEquals(guestbookName, greeting.getKey().getParent().getName()); 89 | assertEquals(testContent, greeting.getProperty("content")); 90 | assertEquals(currentUser, greeting.getProperty("user")); 91 | 92 | final Date date = (Date) greeting.getProperty("date"); 93 | assertTrue("The date in the entity [" + date + "] is prior to the request being performed", 94 | priorToRequest.before(date) || priorToRequest.equals(date)); 95 | assertTrue("The date in the entity [" + date + "] is after to the request completed", 96 | afterRequest.after(date) || afterRequest.equals(date)); 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /appengine-modules-shardedcounter/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'war' 2 | 3 | dependencies { 4 | compile 'com.google.appengine:appengine-api-1.0-sdk:1.8.5' 5 | compile 'javax.servlet:servlet-api:2.5' 6 | compile 'javax.jdo:jdo-api:2.5' 7 | compile 'net.sf.jsr107cache:jsr107cache:1.1' 8 | compile 'org.datanucleus:datanucleus-core:3.1.4' 9 | compile 'com.google.appengine.orm:datanucleus-appengine:2.1.2' 10 | compile 'org.datanucleus:datanucleus-api-jdo:3.1.3' 11 | testCompile 'com.google.appengine:appengine-testing:1.8.5' 12 | testCompile 'com.google.appengine:appengine-api-stubs:1.8.5' 13 | } 14 | -------------------------------------------------------------------------------- /appengine-modules-shardedcounter/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4.0.0 4 | 5 | com.google.appengine.sample 6 | appengine-modules-sample-java 7 | 1.0 8 | 9 | 10 | appengine-modules-shardedcounter 11 | war 12 | 1.0 13 | 14 | appengine-modules-shardedcounter 15 | 16 | 17 | 18 | 19 | 20 | 21 | com.google.appengine 22 | appengine-api-1.0-sdk 23 | ${appengine.target.version} 24 | 25 | 26 | javax.servlet 27 | servlet-api 28 | 2.5 29 | provided 30 | 31 | 32 | javax.jdo 33 | jdo-api 34 | 3.0.1 35 | 36 | 37 | net.sf.jsr107cache 38 | jsr107cache 39 | 1.1 40 | 41 | 42 | org.datanucleus 43 | datanucleus-core 44 | 3.1.4 45 | 46 | 47 | com.google.appengine.orm 48 | datanucleus-appengine 49 | 2.1.2 50 | 51 | 52 | org.datanucleus 53 | datanucleus-api-jdo 54 | 3.1.3 55 | 56 | 57 | 58 | 59 | com.google.appengine 60 | appengine-testing 61 | ${appengine.target.version} 62 | test 63 | 64 | 65 | com.google.appengine 66 | appengine-api-stubs 67 | ${appengine.target.version} 68 | test 69 | 70 | 71 | 72 | 73 | 74 | 75 | org.apache.maven.plugins 76 | 2.5.1 77 | maven-compiler-plugin 78 | 79 | 1.7 80 | 1.7 81 | 82 | 83 | 84 | org.apache.maven.plugins 85 | maven-war-plugin 86 | 2.3 87 | 88 | false 89 | 90 | 91 | 92 | ${basedir}/src/main/webapp/WEB-INF 93 | true 94 | WEB-INF 95 | 96 | 97 | 98 | 99 | 100 | com.google.appengine 101 | appengine-maven-plugin 102 | ${appengine.target.version} 103 | 104 | true 105 | 106 | 107 | 108 | compile 109 | 110 | enhance 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | -------------------------------------------------------------------------------- /appengine-modules-shardedcounter/src/main/java/com/google/appengine/demos/shardedcounter/java/v1/CounterPage.java: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2011 Google Inc. 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package com.google.appengine.demos.shardedcounter.java.v1; 17 | 18 | import java.io.IOException; 19 | import javax.servlet.http.HttpServlet; 20 | import javax.servlet.http.HttpServletRequest; 21 | import javax.servlet.http.HttpServletResponse; 22 | 23 | /** 24 | * Servlet provides the HTML for the counter web page. 25 | */ 26 | public class CounterPage extends HttpServlet { 27 | @Override 28 | public void doGet(HttpServletRequest req, HttpServletResponse resp) 29 | throws IOException { 30 | resp.getWriter().println(""); 31 | resp.getWriter().println(" "); 32 | 33 | final ShardedCounter counter = new ShardedCounter(); 34 | 35 | resp.getWriter().println( 36 | "

Current count: " + counter.getCount() + "

"); 37 | 38 | resp.getWriter().println("
"); 39 | resp.getWriter().println("
"); 40 | resp.getWriter().println("
"); 41 | 42 | resp.getWriter().println(" "); 43 | resp.getWriter().println(""); 44 | } 45 | 46 | @Override 47 | public void doPost(HttpServletRequest req, HttpServletResponse resp) 48 | throws IOException { 49 | final ShardedCounter counter = new ShardedCounter(); 50 | counter.increment(); 51 | doGet(req, resp); 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /appengine-modules-shardedcounter/src/main/java/com/google/appengine/demos/shardedcounter/java/v1/ShardedCounter.java: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2011 Google Inc. 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package com.google.appengine.demos.shardedcounter.java.v1; 17 | 18 | import com.google.appengine.api.datastore.DatastoreService; 19 | import com.google.appengine.api.datastore.DatastoreServiceFactory; 20 | import com.google.appengine.api.datastore.Entity; 21 | import com.google.appengine.api.datastore.EntityNotFoundException; 22 | import com.google.appengine.api.datastore.Key; 23 | import com.google.appengine.api.datastore.KeyFactory; 24 | import com.google.appengine.api.datastore.Query; 25 | import com.google.appengine.api.datastore.Transaction; 26 | 27 | import java.util.Random; 28 | 29 | /** 30 | * This initial implementation simply counts all instances of the 31 | * SimpleCounterShard kind in the datastore. The only way to increment the 32 | * number of shards is to add another shard by creating another entity in the 33 | * datastore. 34 | */ 35 | public class ShardedCounter { 36 | 37 | private static final DatastoreService ds = DatastoreServiceFactory 38 | .getDatastoreService(); 39 | 40 | /** 41 | * Default number of shards. 42 | */ 43 | private static final int NUM_SHARDS = 20; 44 | 45 | /** 46 | * A random number generator, for distributing writes across shards. 47 | */ 48 | private final Random generator = new Random(); 49 | 50 | /** 51 | * Retrieve the value of this sharded counter. 52 | * 53 | * @return Summed total of all shards' counts 54 | */ 55 | public long getCount() { 56 | long sum = 0; 57 | 58 | final Query query = new Query("SimpleCounterShard"); 59 | for (final Entity e : ds.prepare(query).asIterable()) { 60 | sum += (Long) e.getProperty("count"); 61 | } 62 | 63 | return sum; 64 | } 65 | 66 | /** 67 | * Increment the value of this sharded counter. 68 | */ 69 | public void increment() { 70 | final int shardNum = generator.nextInt(NUM_SHARDS); 71 | final Key shardKey = KeyFactory.createKey("SimpleCounterShard", 72 | Integer.toString(shardNum)); 73 | 74 | final Transaction tx = ds.beginTransaction(); 75 | Entity shard; 76 | try { 77 | shard = ds.get(tx, shardKey); 78 | final long count = (Long) shard.getProperty("count"); 79 | shard.setUnindexedProperty("count", count + 1L); 80 | } catch (EntityNotFoundException e) { 81 | shard = new Entity(shardKey); 82 | shard.setUnindexedProperty("count", 1L); 83 | } 84 | ds.put(tx, shard); 85 | tx.commit(); 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /appengine-modules-shardedcounter/src/main/java/com/google/appengine/demos/shardedcounter/java/v2/CounterPage.java: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2011 Google Inc. 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package com.google.appengine.demos.shardedcounter.java.v2; 17 | 18 | import java.io.IOException; 19 | import javax.servlet.http.HttpServlet; 20 | import javax.servlet.http.HttpServletRequest; 21 | import javax.servlet.http.HttpServletResponse; 22 | 23 | /** 24 | * Renders an HTML page showing current count of the sharded counter. 25 | * 26 | * Through the form, the user can increment the counter or add new shards. 27 | */ 28 | public class CounterPage extends HttpServlet { 29 | 30 | @Override 31 | public void doGet(HttpServletRequest req, HttpServletResponse resp) 32 | throws IOException { 33 | displayPageStart(resp); 34 | final ShardedCounter counter = new ShardedCounter("test-counter"); 35 | displayCounts(counter, resp); 36 | displayInputFormAndClose(resp); 37 | } 38 | 39 | @Override 40 | public void doPost(HttpServletRequest req, HttpServletResponse resp) 41 | throws IOException { 42 | displayPageStart(resp); 43 | final ShardedCounter counter = new ShardedCounter("test-counter"); 44 | if (Boolean.parseBoolean(req.getParameter("addShard"))) { 45 | final int inc = Integer.parseInt(req.getParameter("inc")); 46 | displayCount(counter, resp); 47 | displayNumberOfShards(counter.addShards(inc), resp); 48 | } else { 49 | counter.increment(); 50 | displayCount(counter, resp); 51 | displayNumberOfShards(counter.getShardCount(), resp); 52 | } 53 | displayInputFormAndClose(resp); 54 | } 55 | 56 | private void displayPageStart(HttpServletResponse resp) throws IOException { 57 | resp.getWriter().println(""); 58 | resp.getWriter().println(" "); 59 | } 60 | 61 | private void displayCount(ShardedCounter counter, HttpServletResponse resp) 62 | throws IOException { 63 | resp.getWriter() 64 | .println("

Current count: " + counter.getCount() + "

"); 65 | } 66 | 67 | private void displayCounts(ShardedCounter counter, HttpServletResponse resp) 68 | throws IOException { 69 | displayCount(counter, resp); 70 | displayNumberOfShards(counter.getShardCount(), resp); 71 | } 72 | 73 | private void displayNumberOfShards(long shards, HttpServletResponse resp) 74 | throws IOException { 75 | resp.getWriter().print("

Counter has " + shards); 76 | if (shards == 1) { 77 | resp.getWriter().println(" shard

"); 78 | } else { 79 | resp.getWriter().println(" shards

"); 80 | } 81 | } 82 | 83 | private void displayInputFormAndClose(HttpServletResponse resp) 84 | throws IOException { 85 | resp.getWriter().println("
"); 86 | resp.getWriter().println("
"); 87 | resp.getWriter().println("
"); 88 | 89 | resp.getWriter().println("
"); 90 | resp.getWriter().println( 91 | " "); 92 | resp.getWriter().println(" "); 93 | resp.getWriter().println( 94 | "
"); 95 | resp.getWriter().println("
"); 96 | 97 | resp.getWriter().println(" "); 98 | resp.getWriter().println(""); 99 | } 100 | } 101 | -------------------------------------------------------------------------------- /appengine-modules-shardedcounter/src/main/java/com/google/appengine/demos/shardedcounter/java/v2/ShardedCounter.java: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2011 Google Inc. 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package com.google.appengine.demos.shardedcounter.java.v2; 17 | 18 | import com.google.appengine.api.datastore.DatastoreService; 19 | import com.google.appengine.api.datastore.DatastoreServiceFactory; 20 | import com.google.appengine.api.datastore.Entity; 21 | import com.google.appengine.api.datastore.EntityNotFoundException; 22 | import com.google.appengine.api.datastore.Key; 23 | import com.google.appengine.api.datastore.KeyFactory; 24 | import com.google.appengine.api.datastore.Query; 25 | import com.google.appengine.api.datastore.Transaction; 26 | import com.google.appengine.api.memcache.Expiration; 27 | import com.google.appengine.api.memcache.MemcacheService; 28 | import com.google.appengine.api.memcache.MemcacheService.SetPolicy; 29 | import com.google.appengine.api.memcache.MemcacheServiceFactory; 30 | 31 | import java.util.Random; 32 | 33 | /** 34 | * A counter which can be incremented rapidly. 35 | * 36 | * Capable of incrementing the counter and increasing the number of shards. When 37 | * incrementing, a random shard is selected to prevent a single shard from being 38 | * written too frequently. If increments are being made too quickly, increase 39 | * the number of shards to divide the load. Performs datastore operations using 40 | * the low level datastore API. 41 | */ 42 | public class ShardedCounter { 43 | 44 | /** 45 | * Convenience class which contains constants related to a named sharded 46 | * counter. The counter name provided in the constructor is used as 47 | * the entity key. 48 | */ 49 | private static final class Counter { 50 | /** 51 | * Entity kind representing a named sharded counter. 52 | */ 53 | private static final String KIND = "Counter"; 54 | 55 | /** 56 | * Property to store the number of shards in a given {@value #KIND} named 57 | * sharded counter. 58 | */ 59 | private static final String SHARD_COUNT = "shard_count"; 60 | } 61 | 62 | /** 63 | * Convenience class which contains constants related to the counter shards. 64 | * The shard number (as a String) is used as the entity key. 65 | */ 66 | private static final class CounterShard { 67 | /** 68 | * Entity kind prefix, which is concatenated with the counter name to form 69 | * the final entity kind, which represents counter shards. 70 | */ 71 | private static final String KIND_PREFIX = "CounterShard_"; 72 | 73 | /** 74 | * Property to store the current count within a counter shard. 75 | */ 76 | private static final String COUNT = "count"; 77 | } 78 | 79 | private static final DatastoreService ds = DatastoreServiceFactory 80 | .getDatastoreService(); 81 | 82 | /** 83 | * Default number of shards. 84 | */ 85 | private static final int INITIAL_SHARDS = 5; 86 | 87 | /** 88 | * The name of this counter. 89 | */ 90 | private final String counterName; 91 | 92 | /** 93 | * A random number generating, for distributing writes across shards. 94 | */ 95 | private final Random generator = new Random(); 96 | 97 | /** 98 | * The counter shard kind for this counter. 99 | */ 100 | private final String kind; 101 | 102 | private final MemcacheService mc = MemcacheServiceFactory 103 | .getMemcacheService(); 104 | 105 | /** 106 | * Constructor which creates a sharded counter using the provided counter 107 | * name. 108 | * 109 | * @param counterName name of the sharded counter 110 | */ 111 | public ShardedCounter(String counterName) { 112 | this.counterName = counterName; 113 | kind = CounterShard.KIND_PREFIX + counterName; 114 | } 115 | 116 | /** 117 | * Increase the number of shards for a given sharded counter. Will never 118 | * decrease the number of shards. 119 | * 120 | * @param count Number of new shards to build and store 121 | * @return the new number of shards 122 | */ 123 | public long addShards(int count) { 124 | final Key counterKey = KeyFactory.createKey(Counter.KIND, counterName); 125 | return incrementPropertyTx(counterKey, Counter.SHARD_COUNT, count, 126 | INITIAL_SHARDS + count); 127 | } 128 | 129 | /** 130 | * Retrieve the value of this sharded counter. 131 | * 132 | * @return Summed total of all shards' counts 133 | */ 134 | public long getCount() { 135 | final Long value = (Long) mc.get(kind); 136 | if (value != null) { 137 | return value; 138 | } 139 | 140 | long sum = 0; 141 | final Query query = new Query(kind); 142 | for (final Entity shard : ds.prepare(query).asIterable()) { 143 | sum += (Long) shard.getProperty(CounterShard.COUNT); 144 | } 145 | mc.put(kind, sum, Expiration.byDeltaSeconds(60), 146 | SetPolicy.ADD_ONLY_IF_NOT_PRESENT); 147 | 148 | return sum; 149 | } 150 | 151 | /** 152 | * Increment the value of this sharded counter. 153 | */ 154 | public void increment() { 155 | final int numShards = getShardCount(); 156 | 157 | final long shardNum = generator.nextInt(numShards); 158 | 159 | final Key shardKey = KeyFactory.createKey(kind, Long.toString(shardNum)); 160 | incrementPropertyTx(shardKey, CounterShard.COUNT, 1, 1); 161 | mc.increment(kind, 1); 162 | } 163 | 164 | /** 165 | * Get the number of shards in this counter. 166 | * 167 | * @return shard count 168 | */ 169 | public int getShardCount() { 170 | try { 171 | final Key counterKey = KeyFactory.createKey(Counter.KIND, counterName); 172 | final Entity counter = ds.get(counterKey); 173 | final Long shardCount = (Long) counter.getProperty(Counter.SHARD_COUNT); 174 | return shardCount.intValue(); 175 | } catch (EntityNotFoundException ignore) { 176 | return INITIAL_SHARDS; 177 | } 178 | } 179 | 180 | /** 181 | * Increment datastore property value inside a transaction. If the entity with 182 | * the provided key does not exist, instead create an entity with the supplied 183 | * initial property value. 184 | * 185 | * @param key the entity key to update or create 186 | * @param prop the property name to be incremented 187 | * @param increment the amount by which to increment 188 | * @param initialValue the value to use if the entity does not exist 189 | * @return the new value 190 | */ 191 | private long incrementPropertyTx(Key key, String prop, long increment, 192 | long initialValue) { 193 | final Transaction tx = ds.beginTransaction(); 194 | Entity thing; 195 | long value; 196 | try { 197 | thing = ds.get(tx, key); 198 | value = (Long) thing.getProperty(prop) + increment; 199 | } catch (EntityNotFoundException e) { 200 | thing = new Entity(key); 201 | value = initialValue; 202 | } 203 | thing.setUnindexedProperty(prop, value); 204 | ds.put(tx, thing); 205 | tx.commit(); 206 | return value; 207 | } 208 | } 209 | -------------------------------------------------------------------------------- /appengine-modules-shardedcounter/src/main/java/com/google/appengine/demos/shardedcounter/jdo/PMF.java: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2009 Google Inc. 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package com.google.appengine.demos.shardedcounter.jdo; 17 | 18 | import javax.jdo.JDOHelper; 19 | import javax.jdo.PersistenceManagerFactory; 20 | 21 | public final class PMF { 22 | private static final PersistenceManagerFactory pmfInstance = 23 | JDOHelper.getPersistenceManagerFactory("transactions-optional"); 24 | 25 | private PMF() {} 26 | 27 | public static PersistenceManagerFactory get() { 28 | return pmfInstance; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /appengine-modules-shardedcounter/src/main/java/com/google/appengine/demos/shardedcounter/jdo/v1/Counter.java: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2009 Google Inc. 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package com.google.appengine.demos.shardedcounter.jdo.v1; 17 | 18 | import com.google.appengine.demos.shardedcounter.jdo.PMF; 19 | 20 | import java.util.List; 21 | import javax.jdo.PersistenceManager; 22 | 23 | /** 24 | * This initial implementation simply counts all instances of the 25 | * LimitedCounterShard class in the datastore. The only way to increment the 26 | * counter is to add another shard (creating another entity in the datastore). 27 | * This is not the correct approach, since we're limited to the number of 28 | * objects we can fetch with the query, but this is a simple foundation showing 29 | * how to add a new shard. 30 | * 31 | */ 32 | public class Counter { 33 | 34 | public Counter() { 35 | } 36 | 37 | public int getCount() { 38 | int sum = 0; 39 | final PersistenceManager pm = PMF.get().getPersistenceManager(); 40 | List shards = null; 41 | try { 42 | final String query = "select from " + LimitedCounterShard.class.getName(); 43 | shards = (List) pm.newQuery(query).execute(); 44 | if (shards != null && !shards.isEmpty()) { 45 | for (final LimitedCounterShard shard : shards) { 46 | sum += shard.getCount(); 47 | } 48 | } 49 | } finally { 50 | pm.close(); 51 | } 52 | return sum; 53 | } 54 | 55 | public void addShard() { 56 | addShards(1); 57 | } 58 | 59 | public void addShards(int count) { 60 | final LimitedCounterShard newShard = new LimitedCounterShard(); 61 | newShard.setCount(Integer.valueOf(count)); 62 | 63 | final PersistenceManager pm = PMF.get().getPersistenceManager(); 64 | try { 65 | pm.makePersistent(newShard); 66 | } finally { 67 | pm.close(); 68 | } 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /appengine-modules-shardedcounter/src/main/java/com/google/appengine/demos/shardedcounter/jdo/v1/CounterPage.java: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2009 Google Inc. 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package com.google.appengine.demos.shardedcounter.jdo.v1; 17 | 18 | import java.io.IOException; 19 | import javax.servlet.http.HttpServlet; 20 | import javax.servlet.http.HttpServletRequest; 21 | import javax.servlet.http.HttpServletResponse; 22 | 23 | /** 24 | * Servlet provides the HTML for the counter web page. 25 | * 26 | */ 27 | public class CounterPage extends HttpServlet { 28 | @Override 29 | public void doGet(HttpServletRequest req, HttpServletResponse resp) 30 | throws IOException { 31 | resp.getWriter().println(""); 32 | resp.getWriter().println(" "); 33 | 34 | final Counter counter = new Counter(); 35 | 36 | resp.getWriter().println("

Current count: " + counter.getCount() 37 | + "

"); 38 | 39 | resp.getWriter().println("
"); 40 | resp.getWriter().println( 41 | "
"); 42 | resp.getWriter().println("
"); 43 | 44 | resp.getWriter().println(" "); 45 | resp.getWriter().println(""); 46 | } 47 | 48 | @Override 49 | public void doPost(HttpServletRequest req, HttpServletResponse resp) 50 | throws IOException { 51 | final Counter counter = new Counter(); 52 | counter.addShard(); 53 | doGet(req, resp); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /appengine-modules-shardedcounter/src/main/java/com/google/appengine/demos/shardedcounter/jdo/v1/LimitedCounterShard.java: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2009 Google Inc. 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package com.google.appengine.demos.shardedcounter.jdo.v1; 17 | 18 | import javax.jdo.annotations.IdGeneratorStrategy; 19 | import javax.jdo.annotations.IdentityType; 20 | import javax.jdo.annotations.PersistenceCapable; 21 | import javax.jdo.annotations.Persistent; 22 | import javax.jdo.annotations.PrimaryKey; 23 | 24 | /** 25 | * A JDO object representing a single shard belonging to the counter. 26 | * 27 | */ 28 | @PersistenceCapable(identityType = IdentityType.APPLICATION) 29 | public class LimitedCounterShard { 30 | @PrimaryKey 31 | @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY) 32 | private Long id; 33 | 34 | @Persistent 35 | private Integer count; 36 | 37 | public LimitedCounterShard() { 38 | this.count = Integer.valueOf(0); 39 | } 40 | 41 | public LimitedCounterShard(Integer count) { 42 | this.count = count; 43 | } 44 | 45 | public Long getId() { 46 | return id; 47 | } 48 | 49 | public Integer getCount() { 50 | return count; 51 | } 52 | 53 | public void setCount(Integer count) { 54 | this.count = count; 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /appengine-modules-shardedcounter/src/main/java/com/google/appengine/demos/shardedcounter/jdo/v2/Counter.java: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2009 Google Inc. 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package com.google.appengine.demos.shardedcounter.jdo.v2; 17 | 18 | import javax.jdo.annotations.IdGeneratorStrategy; 19 | import javax.jdo.annotations.IdentityType; 20 | import javax.jdo.annotations.PersistenceCapable; 21 | import javax.jdo.annotations.Persistent; 22 | import javax.jdo.annotations.PrimaryKey; 23 | 24 | /** 25 | * Represents a counter in the datastore and stores the number of shards. 26 | * 27 | */ 28 | @PersistenceCapable(identityType = IdentityType.APPLICATION) 29 | public class Counter { 30 | @PrimaryKey 31 | @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY) 32 | private Long id; 33 | 34 | @Persistent 35 | private final String counterName; 36 | 37 | @Persistent 38 | private Integer numShards; 39 | 40 | public Counter(String counterName) { 41 | this.counterName = counterName; 42 | this.numShards = Integer.valueOf(0); 43 | } 44 | 45 | public Counter(String counterName, Integer numShards) { 46 | this.counterName = counterName; 47 | this.numShards = numShards; 48 | } 49 | 50 | public Long getId() { 51 | return id; 52 | } 53 | 54 | public String getCounterName() { 55 | return counterName; 56 | } 57 | 58 | public Integer getShardCount() { 59 | return numShards; 60 | } 61 | 62 | public void setShardCount(int count) { 63 | this.numShards = Integer.valueOf(count); 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /appengine-modules-shardedcounter/src/main/java/com/google/appengine/demos/shardedcounter/jdo/v2/CounterFactory.java: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2009 Google Inc. 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package com.google.appengine.demos.shardedcounter.jdo.v2; 17 | 18 | import com.google.appengine.demos.shardedcounter.jdo.PMF; 19 | 20 | import javax.jdo.PersistenceManager; 21 | 22 | /** 23 | * Finds or creates a sharded counter with the desired name. 24 | * 25 | */ 26 | public class CounterFactory { 27 | 28 | public ShardedCounter getCounter(String name) { 29 | final ShardedCounter counter = new ShardedCounter(name); 30 | if (counter.isInDatastore()) { 31 | return counter; 32 | } else { 33 | return null; 34 | } 35 | } 36 | 37 | public ShardedCounter createCounter(String name) { 38 | final ShardedCounter counter = new ShardedCounter(name); 39 | 40 | final Counter counterEntity = new Counter(name, 0); 41 | final PersistenceManager pm = PMF.get().getPersistenceManager(); 42 | try { 43 | pm.makePersistent(counterEntity); 44 | } finally { 45 | pm.close(); 46 | } 47 | 48 | return counter; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /appengine-modules-shardedcounter/src/main/java/com/google/appengine/demos/shardedcounter/jdo/v2/CounterPage.java: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2009 Google Inc. 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package com.google.appengine.demos.shardedcounter.jdo.v2; 17 | 18 | import java.io.IOException; 19 | import javax.servlet.http.HttpServlet; 20 | import javax.servlet.http.HttpServletRequest; 21 | import javax.servlet.http.HttpServletResponse; 22 | 23 | /** 24 | * Renders an HTML page showing current count of the sharded counter. 25 | * 26 | * Through the form, the user can increment the counter or add new shards. 27 | * 28 | */ 29 | public class CounterPage extends HttpServlet { 30 | 31 | @Override 32 | public void doGet(HttpServletRequest req, HttpServletResponse resp) 33 | throws IOException { 34 | displayPageStart(resp); 35 | final ShardedCounter counter = getOrCreateCounter(resp); 36 | displayCounts(counter, resp); 37 | displayInputFormAndClose(resp); 38 | } 39 | 40 | @Override 41 | public void doPost(HttpServletRequest req, HttpServletResponse resp) 42 | throws IOException { 43 | displayPageStart(resp); 44 | final ShardedCounter counter = getOrCreateCounter(resp); 45 | if (Boolean.parseBoolean(req.getParameter("addShard"))) { 46 | displayCount(counter, resp); 47 | displayNumberOfShards(counter.addShard(), resp); 48 | } else { 49 | counter.increment(); 50 | displayCount(counter, resp); 51 | displayNumberOfShards(counter.getNumShards(), resp); 52 | } 53 | displayInputFormAndClose(resp); 54 | } 55 | 56 | private void displayPageStart(HttpServletResponse resp) throws IOException { 57 | resp.getWriter().println(""); 58 | resp.getWriter().println(" "); 59 | } 60 | 61 | /** 62 | * Creates the sharded counter if it does not yet exist. 63 | */ 64 | private ShardedCounter getOrCreateCounter(HttpServletResponse resp) 65 | throws IOException { 66 | final CounterFactory factory = new CounterFactory(); 67 | ShardedCounter counter = factory.getCounter("test-counter"); 68 | if (counter == null) { 69 | counter = factory.createCounter("test-counter"); 70 | counter.addShard(); 71 | resp.getWriter().println( 72 | "

No counter named 'test-counter', so we created one.

"); 73 | } 74 | return counter; 75 | } 76 | 77 | private void displayCount(ShardedCounter counter, HttpServletResponse resp) 78 | throws IOException { 79 | resp.getWriter().println( 80 | "

Current count: " + counter.getCount() + "

"); 81 | } 82 | 83 | private void displayCounts(ShardedCounter counter, HttpServletResponse resp) 84 | throws IOException { 85 | displayCount(counter, resp); 86 | displayNumberOfShards(counter.getNumShards(), resp); 87 | } 88 | 89 | private void displayNumberOfShards(int shards, HttpServletResponse resp) 90 | throws IOException { 91 | resp.getWriter().print("

Counter has " + shards); 92 | if (shards == 1) { 93 | resp.getWriter().println(" shard

"); 94 | } else { 95 | resp.getWriter().println(" shards

"); 96 | } 97 | } 98 | 99 | private void displayInputFormAndClose(HttpServletResponse resp) 100 | throws IOException { 101 | resp.getWriter().println("
"); 102 | resp.getWriter().println("
"); 103 | resp.getWriter().println("
"); 104 | 105 | resp.getWriter().println("
"); 106 | resp.getWriter().println( 107 | " "); 108 | resp.getWriter().println( 109 | "
"); 110 | resp.getWriter().println("
"); 111 | 112 | resp.getWriter().println(" "); 113 | resp.getWriter().println(""); 114 | } 115 | } 116 | -------------------------------------------------------------------------------- /appengine-modules-shardedcounter/src/main/java/com/google/appengine/demos/shardedcounter/jdo/v2/CounterShard.java: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2009 Google Inc. 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package com.google.appengine.demos.shardedcounter.jdo.v2; 17 | 18 | import javax.jdo.annotations.IdGeneratorStrategy; 19 | import javax.jdo.annotations.IdentityType; 20 | import javax.jdo.annotations.PersistenceCapable; 21 | import javax.jdo.annotations.Persistent; 22 | import javax.jdo.annotations.PrimaryKey; 23 | 24 | /** 25 | * One shard belonging to the named counter. 26 | * 27 | * An individual shard is written to infrequently to allow the counter in 28 | * aggregate to be incremented rapidly. 29 | * 30 | */ 31 | @PersistenceCapable(identityType = IdentityType.APPLICATION) 32 | public class CounterShard { 33 | @PrimaryKey 34 | @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY) 35 | private Long id; 36 | 37 | @Persistent 38 | private final Integer shardNumber; 39 | 40 | @Persistent 41 | private final String counterName; 42 | 43 | @Persistent 44 | private Integer count; 45 | 46 | public CounterShard(String counterName, int shardNumber) { 47 | this(counterName, shardNumber, 0); 48 | } 49 | 50 | public CounterShard(String counterName, int shardNumber, int count) { 51 | this.counterName = counterName; 52 | this.shardNumber = Integer.valueOf(shardNumber); 53 | this.count = Integer.valueOf(count); 54 | } 55 | 56 | public Long getId() { 57 | return id; 58 | } 59 | 60 | public String getCounterName() { 61 | return counterName; 62 | } 63 | 64 | public Integer getShardNumber() { 65 | return shardNumber; 66 | } 67 | 68 | public Integer getCount() { 69 | return count; 70 | } 71 | 72 | public void setCount(Integer count) { 73 | this.count = count; 74 | } 75 | 76 | public void increment(int amount) { 77 | count = Integer.valueOf(count.intValue() + amount); 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /appengine-modules-shardedcounter/src/main/java/com/google/appengine/demos/shardedcounter/jdo/v2/ShardedCounter.java: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2009 Google Inc. 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package com.google.appengine.demos.shardedcounter.jdo.v2; 17 | 18 | import com.google.appengine.demos.shardedcounter.jdo.PMF; 19 | 20 | import java.util.List; 21 | import java.util.Random; 22 | import javax.jdo.PersistenceManager; 23 | import javax.jdo.Query; 24 | 25 | /** 26 | * A counter which can be incremented rapidly. 27 | * 28 | * Capable of incrementing the counter and increasing the number of shards. 29 | * When incrementing, a random shard is selected to prevent a single shard 30 | * from being written to too frequently. If increments are being made too 31 | * quickly, increase the number of shards to divide the load. Performs 32 | * datastore operations using JDO. 33 | * 34 | */ 35 | public class ShardedCounter { 36 | private final String counterName; 37 | 38 | public ShardedCounter(String counterName) { 39 | this.counterName = counterName; 40 | } 41 | 42 | public String getCounterName() { 43 | return counterName; 44 | } 45 | 46 | private Counter getThisCounter(PersistenceManager pm) { 47 | Counter current = null; 48 | final Query thisCounterQuery = pm.newQuery(Counter.class, 49 | "counterName == nameParam"); 50 | thisCounterQuery.declareParameters("String nameParam"); 51 | final List counter = (List) thisCounterQuery.execute( 52 | counterName); 53 | if (counter != null && !counter.isEmpty()) { 54 | current = counter.get(0); 55 | } 56 | return current; 57 | } 58 | 59 | public boolean isInDatastore() { 60 | boolean counterStored = false; 61 | final PersistenceManager pm = PMF.get().getPersistenceManager(); 62 | try { 63 | if (getThisCounter(pm) != null) { 64 | counterStored = true; 65 | } 66 | } finally { 67 | pm.close(); 68 | } 69 | return counterStored; 70 | } 71 | 72 | public int getCount() { 73 | int sum = 0; 74 | final PersistenceManager pm = PMF.get().getPersistenceManager(); 75 | 76 | try { 77 | final Query shardsQuery = pm.newQuery(CounterShard.class, 78 | "counterName == nameParam"); 79 | shardsQuery.declareParameters("String nameParam"); 80 | final List shards = (List) shardsQuery.execute( 81 | counterName); 82 | if (shards != null && !shards.isEmpty()) { 83 | for (final CounterShard current : shards) { 84 | sum += current.getCount(); 85 | } 86 | } 87 | } finally { 88 | pm.close(); 89 | } 90 | return sum; 91 | } 92 | 93 | public int getNumShards() { 94 | int numShards = 0; 95 | final PersistenceManager pm = PMF.get().getPersistenceManager(); 96 | try { 97 | final Counter current = getThisCounter(pm); 98 | if (current != null) { 99 | numShards = current.getShardCount().intValue(); 100 | } 101 | } finally { 102 | pm.close(); 103 | } 104 | return numShards; 105 | } 106 | 107 | public int addShard() { 108 | return addShards(1); 109 | } 110 | 111 | public int addShards(int count) { 112 | int numShards = 0; 113 | PersistenceManager pm = PMF.get().getPersistenceManager(); 114 | try { 115 | final Counter current = getThisCounter(pm); 116 | if (current != null) { 117 | numShards = current.getShardCount().intValue(); 118 | current.setShardCount(numShards + count); 119 | pm.makePersistent(current); 120 | } 121 | } finally { 122 | pm.close(); 123 | } 124 | 125 | pm = PMF.get().getPersistenceManager(); 126 | try { 127 | for (int i = 0; i < count; i++) { 128 | final CounterShard newShard = new CounterShard(getCounterName(), numShards); 129 | pm.makePersistent(newShard); 130 | numShards++; 131 | } 132 | } finally { 133 | pm.close(); 134 | } 135 | return numShards; 136 | } 137 | 138 | public void increment() { 139 | increment(1); 140 | } 141 | 142 | public void increment(int count) { 143 | int shardCount = 0; 144 | PersistenceManager pm = PMF.get().getPersistenceManager(); 145 | try { 146 | final Counter current = getThisCounter(pm); 147 | shardCount = current.getShardCount(); 148 | } finally { 149 | pm.close(); 150 | } 151 | 152 | final Random generator = new Random(); 153 | final int shardNum = generator.nextInt(shardCount); 154 | 155 | pm = PMF.get().getPersistenceManager(); 156 | try { 157 | final Query randomShardQuery = pm.newQuery(CounterShard.class); 158 | randomShardQuery.setFilter( 159 | "counterName == nameParam && shardNumber == numParam"); 160 | randomShardQuery.declareParameters("String nameParam, int numParam"); 161 | final List shards = (List) randomShardQuery 162 | .execute(counterName, shardNum); 163 | if (shards != null && !shards.isEmpty()) { 164 | final CounterShard shard = shards.get(0); 165 | shard.increment(count); 166 | pm.makePersistent(shard); 167 | } 168 | } finally { 169 | pm.close(); 170 | } 171 | } 172 | } 173 | -------------------------------------------------------------------------------- /appengine-modules-shardedcounter/src/main/java/com/google/appengine/demos/shardedcounter/jdo/v3/CounterFactory.java: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2009 Google Inc. 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package com.google.appengine.demos.shardedcounter.jdo.v3; 17 | 18 | import com.google.appengine.demos.shardedcounter.jdo.PMF; 19 | 20 | import javax.jdo.PersistenceManager; 21 | 22 | /** 23 | * Finds or creates a sharded counter with the desired name. 24 | * 25 | */ 26 | public class CounterFactory { 27 | 28 | public ShardedCounter getCounter(String name) { 29 | final ShardedCounter counter = new ShardedCounter(name); 30 | if (counter.isInDatastore()) { 31 | return counter; 32 | } else { 33 | return null; 34 | } 35 | } 36 | 37 | public ShardedCounter createCounter(String name) { 38 | final ShardedCounter counter = new ShardedCounter(name); 39 | 40 | final DatastoreCounter counterEntity = new DatastoreCounter(name, 0); 41 | final PersistenceManager pm = PMF.get().getPersistenceManager(); 42 | try { 43 | pm.makePersistent(counterEntity); 44 | } finally { 45 | pm.close(); 46 | } 47 | 48 | return counter; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /appengine-modules-shardedcounter/src/main/java/com/google/appengine/demos/shardedcounter/jdo/v3/CounterPage.java: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2009 Google Inc. 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package com.google.appengine.demos.shardedcounter.jdo.v3; 17 | 18 | import java.io.IOException; 19 | import javax.servlet.http.HttpServlet; 20 | import javax.servlet.http.HttpServletRequest; 21 | import javax.servlet.http.HttpServletResponse; 22 | 23 | /** 24 | * Renders an HTML page showing current count of the sharded counter. 25 | * 26 | * Through the form, the user can increment the counter or add new shards. 27 | * 28 | */ 29 | public class CounterPage extends HttpServlet { 30 | 31 | @Override 32 | public void doGet(HttpServletRequest req, HttpServletResponse resp) 33 | throws IOException { 34 | displayPageStart(resp); 35 | final ShardedCounter counter = getOrCreateCounter(resp); 36 | displayCounts(counter, resp); 37 | displayInputFormAndClose(resp); 38 | } 39 | 40 | @Override 41 | public void doPost(HttpServletRequest req, HttpServletResponse resp) 42 | throws IOException { 43 | displayPageStart(resp); 44 | final ShardedCounter counter = getOrCreateCounter(resp); 45 | if (Boolean.parseBoolean(req.getParameter("addShard"))) { 46 | displayCount(counter, resp); 47 | displayNumberOfShards(counter.addShard(), resp); 48 | } else { 49 | counter.increment(); 50 | displayCount(counter, resp); 51 | displayNumberOfShards(counter.getNumShards(), resp); 52 | } 53 | displayInputFormAndClose(resp); 54 | } 55 | 56 | private void displayPageStart(HttpServletResponse resp) throws IOException { 57 | resp.getWriter().println(""); 58 | resp.getWriter().println(" "); 59 | } 60 | 61 | /** 62 | * Creates the sharded counter if it does not yet exist. 63 | */ 64 | private ShardedCounter getOrCreateCounter(HttpServletResponse resp) 65 | throws IOException { 66 | final CounterFactory factory = new CounterFactory(); 67 | ShardedCounter counter = factory.getCounter("test-counter"); 68 | if (counter == null) { 69 | counter = factory.createCounter("test-counter"); 70 | counter.addShard(); 71 | resp.getWriter().println( 72 | "

No counter named 'test-counter', so we created one.

"); 73 | } 74 | return counter; 75 | } 76 | 77 | private void displayCount(ShardedCounter counter, HttpServletResponse resp) 78 | throws IOException { 79 | resp.getWriter() 80 | .println("

Current count: " + counter.getCount() + "

"); 81 | } 82 | 83 | private void displayCounts(ShardedCounter counter, HttpServletResponse resp) 84 | throws IOException { 85 | displayCount(counter, resp); 86 | displayNumberOfShards(counter.getNumShards(), resp); 87 | } 88 | 89 | private void displayNumberOfShards(int shards, HttpServletResponse resp) 90 | throws IOException { 91 | resp.getWriter().print("

Counter has " + shards); 92 | if (shards == 1) { 93 | resp.getWriter().println(" shard

"); 94 | } else { 95 | resp.getWriter().println(" shards

"); 96 | } 97 | } 98 | 99 | private void displayInputFormAndClose(HttpServletResponse resp) 100 | throws IOException { 101 | resp.getWriter().println("
"); 102 | resp.getWriter().println("
"); 103 | resp.getWriter().println("
"); 104 | 105 | resp.getWriter().println("
"); 106 | resp.getWriter().println( 107 | " "); 108 | resp.getWriter().println( 109 | "
"); 110 | resp.getWriter().println("
"); 111 | 112 | resp.getWriter().println(" "); 113 | resp.getWriter().println(""); 114 | } 115 | } 116 | -------------------------------------------------------------------------------- /appengine-modules-shardedcounter/src/main/java/com/google/appengine/demos/shardedcounter/jdo/v3/DatastoreCounter.java: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2009 Google Inc. 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package com.google.appengine.demos.shardedcounter.jdo.v3; 17 | 18 | import javax.jdo.annotations.IdGeneratorStrategy; 19 | import javax.jdo.annotations.IdentityType; 20 | import javax.jdo.annotations.PersistenceCapable; 21 | import javax.jdo.annotations.Persistent; 22 | import javax.jdo.annotations.PrimaryKey; 23 | 24 | /** 25 | * Represents a counter in the datastore and stores the number of shards. 26 | * 27 | */ 28 | @PersistenceCapable(identityType = IdentityType.APPLICATION) 29 | public class DatastoreCounter { 30 | @PrimaryKey 31 | @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY) 32 | private Long id; 33 | 34 | @Persistent 35 | private final String counterName; 36 | 37 | @Persistent 38 | private Integer numShards; 39 | 40 | public DatastoreCounter(String counterName) { 41 | this.counterName = counterName; 42 | this.numShards = Integer.valueOf(0); 43 | } 44 | 45 | public DatastoreCounter(String counterName, Integer numShards) { 46 | this.counterName = counterName; 47 | this.numShards = numShards; 48 | } 49 | 50 | public Long getId() { 51 | return id; 52 | } 53 | 54 | public String getCounterName() { 55 | return counterName; 56 | } 57 | 58 | public Integer getShardCount() { 59 | return numShards; 60 | } 61 | 62 | public void setShardCount(int count) { 63 | this.numShards = Integer.valueOf(count); 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /appengine-modules-shardedcounter/src/main/java/com/google/appengine/demos/shardedcounter/jdo/v3/DatastoreCounterShard.java: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2009 Google Inc. 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package com.google.appengine.demos.shardedcounter.jdo.v3; 17 | 18 | import javax.jdo.annotations.IdGeneratorStrategy; 19 | import javax.jdo.annotations.IdentityType; 20 | import javax.jdo.annotations.PersistenceCapable; 21 | import javax.jdo.annotations.Persistent; 22 | import javax.jdo.annotations.PrimaryKey; 23 | 24 | /** 25 | * One shard belonging to the named counter. 26 | * 27 | * An individual shard is written to infrequently to allow the counter in 28 | * aggregate to be incremented rapidly. 29 | * 30 | */ 31 | @PersistenceCapable(identityType = IdentityType.APPLICATION) 32 | public class DatastoreCounterShard { 33 | @PrimaryKey 34 | @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY) 35 | private Long id; 36 | 37 | @Persistent 38 | private final Integer shardNumber; 39 | 40 | @Persistent 41 | private final String counterName; 42 | 43 | @Persistent 44 | private Integer count; 45 | 46 | public DatastoreCounterShard(String counterName, int shardNumber) { 47 | this(counterName, shardNumber, 0); 48 | } 49 | 50 | public DatastoreCounterShard(String counterName, int shardNumber, 51 | int count) { 52 | this.counterName = counterName; 53 | this.shardNumber = Integer.valueOf(shardNumber); 54 | this.count = Integer.valueOf(count); 55 | } 56 | 57 | public Long getId() { 58 | return id; 59 | } 60 | 61 | public String getCounterName() { 62 | return counterName; 63 | } 64 | 65 | public Integer getShardNumber() { 66 | return shardNumber; 67 | } 68 | 69 | public Integer getCount() { 70 | return count; 71 | } 72 | 73 | public void setCount(Integer count) { 74 | this.count = count; 75 | } 76 | 77 | public void increment(int amount) { 78 | count = Integer.valueOf(count.intValue() + amount); 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /appengine-modules-shardedcounter/src/main/java/com/google/appengine/demos/shardedcounter/jdo/v3/ShardedCounter.java: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2009 Google Inc. 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package com.google.appengine.demos.shardedcounter.jdo.v3; 17 | 18 | import com.google.appengine.demos.shardedcounter.jdo.PMF; 19 | 20 | import java.util.Collections; 21 | import java.util.List; 22 | import java.util.Random; 23 | 24 | import net.sf.jsr107cache.Cache; 25 | import net.sf.jsr107cache.CacheException; 26 | import net.sf.jsr107cache.CacheManager; 27 | import javax.jdo.PersistenceManager; 28 | import javax.jdo.Query; 29 | 30 | /** 31 | * A counter which can be incremented rapidly. 32 | * 33 | * Capable of incrementing the counter and increasing the number of shards. 34 | * When incrementing, a random shard is selected to prevent a single shard 35 | * from being written to too frequently. If increments are being made too 36 | * quickly, increase the number of shards to divide the load. Performs 37 | * datastore operations using JDO. 38 | * 39 | * Lookups are attempted using Memcache (Jcache). If the counter value is 40 | * not in the cache, the shards are read from the datastore and accumulated 41 | * to reconstruct the current count. 42 | * 43 | */ 44 | public class ShardedCounter { 45 | private final String counterName; 46 | private Cache cache; 47 | 48 | public ShardedCounter(String counterName) { 49 | this.counterName = counterName; 50 | cache = null; 51 | try { 52 | cache = CacheManager.getInstance().getCacheFactory().createCache( 53 | Collections.emptyMap()); 54 | } catch (CacheException e) { 55 | } 56 | } 57 | 58 | public String getCounterName() { 59 | return counterName; 60 | } 61 | 62 | private DatastoreCounter getThisCounter(PersistenceManager pm) { 63 | DatastoreCounter current = null; 64 | final Query thisCounterQuery = pm.newQuery(DatastoreCounter.class, 65 | "counterName == nameParam"); 66 | thisCounterQuery.declareParameters("String nameParam"); 67 | final List counter = 68 | (List) thisCounterQuery.execute(counterName); 69 | if (counter != null && !counter.isEmpty()) { 70 | current = counter.get(0); 71 | } 72 | return current; 73 | } 74 | 75 | public boolean isInDatastore() { 76 | boolean counterStored = false; 77 | final PersistenceManager pm = PMF.get().getPersistenceManager(); 78 | try { 79 | if (getThisCounter(pm) != null) { 80 | counterStored = true; 81 | } 82 | } finally { 83 | pm.close(); 84 | } 85 | return counterStored; 86 | } 87 | 88 | public int getCount() { 89 | if (cache != null) { 90 | final Integer cachedCount = (Integer) cache.get("count" + counterName); 91 | if (cachedCount != null) { 92 | return cachedCount.intValue(); 93 | } 94 | } 95 | 96 | int sum = 0; 97 | final PersistenceManager pm = PMF.get().getPersistenceManager(); 98 | 99 | try { 100 | final Query shardsQuery = pm.newQuery(DatastoreCounterShard.class, 101 | "counterName == nameParam"); 102 | shardsQuery.declareParameters("String nameParam"); 103 | final List shards = 104 | (List) shardsQuery.execute(counterName); 105 | if (shards != null && !shards.isEmpty()) { 106 | for (final DatastoreCounterShard current : shards) { 107 | sum += current.getCount(); 108 | } 109 | } 110 | } finally { 111 | pm.close(); 112 | } 113 | 114 | if (cache != null) { 115 | cache.put("count" + counterName, Integer.valueOf(sum)); 116 | } 117 | 118 | return sum; 119 | } 120 | 121 | public int getNumShards() { 122 | if (cache != null) { 123 | final Integer cachedCount = (Integer) cache.get("shards" + counterName); 124 | if (cachedCount != null) { 125 | return cachedCount.intValue(); 126 | } 127 | } 128 | 129 | int numShards = 0; 130 | final PersistenceManager pm = PMF.get().getPersistenceManager(); 131 | try { 132 | final DatastoreCounter current = getThisCounter(pm); 133 | if (current != null) { 134 | numShards = current.getShardCount().intValue(); 135 | } 136 | } finally { 137 | pm.close(); 138 | } 139 | 140 | if (cache != null) { 141 | cache.put("shards" + counterName, Integer.valueOf(numShards)); 142 | } 143 | 144 | return numShards; 145 | } 146 | 147 | public int addShard() { 148 | return addShards(1); 149 | } 150 | 151 | public int addShards(int count) { 152 | int numShards = 0; 153 | PersistenceManager pm = PMF.get().getPersistenceManager(); 154 | try { 155 | final DatastoreCounter current = getThisCounter(pm); 156 | if (current != null) { 157 | numShards = current.getShardCount().intValue(); 158 | current.setShardCount(numShards + count); 159 | pm.makePersistent(current); 160 | } 161 | } finally { 162 | pm.close(); 163 | } 164 | 165 | pm = PMF.get().getPersistenceManager(); 166 | try { 167 | for (int i = 0; i < count; i++) { 168 | final DatastoreCounterShard newShard = new DatastoreCounterShard( 169 | getCounterName(), numShards); 170 | pm.makePersistent(newShard); 171 | numShards++; 172 | } 173 | } finally { 174 | pm.close(); 175 | } 176 | 177 | if (cache != null) { 178 | cache.put("shards" + counterName, Integer.valueOf(numShards)); 179 | } 180 | 181 | return numShards; 182 | } 183 | 184 | public void increment() { 185 | increment(1); 186 | } 187 | 188 | public void increment(int count) { 189 | if (cache != null) { 190 | final Integer cachedCount = (Integer) cache.get("count" + counterName); 191 | if (cachedCount != null) { 192 | cache.put("count" + counterName, 193 | Integer.valueOf(count + cachedCount.intValue())); 194 | } 195 | } 196 | 197 | int shardCount = 0; 198 | PersistenceManager pm = PMF.get().getPersistenceManager(); 199 | try { 200 | final DatastoreCounter current = getThisCounter(pm); 201 | shardCount = current.getShardCount(); 202 | } finally { 203 | pm.close(); 204 | } 205 | 206 | final Random generator = new Random(); 207 | final int shardNum = generator.nextInt(shardCount); 208 | 209 | pm = PMF.get().getPersistenceManager(); 210 | try { 211 | final Query randomShardQuery = pm.newQuery(DatastoreCounterShard.class); 212 | randomShardQuery.setFilter( 213 | "counterName == nameParam && shardNumber == numParam"); 214 | randomShardQuery.declareParameters("String nameParam, int numParam"); 215 | final List shards = 216 | (List) randomShardQuery.execute( 217 | counterName, shardNum); 218 | if (shards != null && !shards.isEmpty()) { 219 | final DatastoreCounterShard shard = shards.get(0); 220 | shard.increment(count); 221 | pm.makePersistent(shard); 222 | } 223 | } finally { 224 | pm.close(); 225 | } 226 | } 227 | } 228 | -------------------------------------------------------------------------------- /appengine-modules-shardedcounter/src/main/resources/META-INF/jdoconfig.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /appengine-modules-shardedcounter/src/main/webapp/WEB-INF/appengine-web.xml: -------------------------------------------------------------------------------- 1 | 2 | yet-another-sharded-counter 3 | 1 4 | true 5 | 6 | shardedcounter 7 | 8 | 9 | -------------------------------------------------------------------------------- /appengine-modules-shardedcounter/src/main/webapp/WEB-INF/web.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | java-v1 8 | 9 | com.google.appengine.demos.shardedcounter.java.v1.CounterPage 10 | 11 | 12 | 13 | java-v2 14 | 15 | com.google.appengine.demos.shardedcounter.java.v2.CounterPage 16 | 17 | 18 | 19 | jdo-v1 20 | 21 | com.google.appengine.demos.shardedcounter.jdo.v1.CounterPage 22 | 23 | 24 | 25 | jdo-v2 26 | 27 | com.google.appengine.demos.shardedcounter.jdo.v2.CounterPage 28 | 29 | 30 | 31 | jdo-v3 32 | 33 | com.google.appengine.demos.shardedcounter.jdo.v3.CounterPage 34 | 35 | 36 | 37 | java-v1 38 | /java-v1/ 39 | 40 | 41 | java-v2 42 | /java-v2/ 43 | 44 | 45 | jdo-v1 46 | /jdo-v1/ 47 | 48 | 49 | jdo-v2 50 | /jdo-v2/ 51 | 52 | 53 | jdo-v3 54 | /jdo-v3/ 55 | 56 | 57 | index.html 58 | 59 | 60 | 61 | -------------------------------------------------------------------------------- /appengine-modules-shardedcounter/src/main/webapp/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/appengine-modules-sample-java/9e3cc9b273121497e86eea834992cdd74a4df922/appengine-modules-shardedcounter/src/main/webapp/favicon.ico -------------------------------------------------------------------------------- /appengine-modules-shardedcounter/src/main/webapp/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | App Engine Java Sharded Counter Demo 4 | 5 | 6 |

App Engine Java Sharded Counter Demo

7 |

Java sharded counter demos

8 | 14 |

JDO sharded counter demos

15 | 23 | 24 | -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | allprojects { 2 | apply plugin: 'maven' 3 | 4 | group = 'com.google.appengine.sample' 5 | version = '1.0' 6 | } 7 | 8 | subprojects { 9 | apply plugin: 'java' 10 | repositories { mavenCentral() } 11 | } 12 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4.0.0 4 | com.google.appengine.sample 5 | appengine-modules-sample-java 6 | 1.0 7 | pom 8 | appengine-modules-sample-java 9 | 10 | 11 | 1 12 | 1.9.50 13 | UTF-8 14 | 15 | 16 | appengine-modules-ear 17 | appengine-modules-guestbook 18 | appengine-modules-shardedcounter 19 | 20 | 21 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include 'appengine-modules-guestbook', 'appengine-modules-shardedcounter', 'appengine-modules-ear' 2 | --------------------------------------------------------------------------------