From fc77a257df2456be2d03c1cdf9e0c71aea09d865 Mon Sep 17 00:00:00 2001 From: "Mikami, Takeshi" Date: Sat, 18 Aug 2018 11:27:23 +0900 Subject: [PATCH] add example --- README.md | 67 ++++++++++++++++++- example/build.gradle | 37 ++++++++++ example/src/main/java/sample/Application.java | 22 ++++++ example/src/main/java/sample/ItemEntity.java | 36 ++++++++++ .../src/main/java/sample/ObjectifyConfig.java | 64 ++++++++++++++++++ .../main/java/sample/SampleController.java | 52 ++++++++++++++ .../main/java/sample/ServletInitializer.java | 11 +++ .../src/main/webapp/WEB-INF/appengine-web.xml | 4 ++ example/src/main/webapp/WEB-INF/web.xml | 7 ++ 9 files changed, 299 insertions(+), 1 deletion(-) create mode 100644 example/build.gradle create mode 100644 example/src/main/java/sample/Application.java create mode 100644 example/src/main/java/sample/ItemEntity.java create mode 100644 example/src/main/java/sample/ObjectifyConfig.java create mode 100644 example/src/main/java/sample/SampleController.java create mode 100644 example/src/main/java/sample/ServletInitializer.java create mode 100644 example/src/main/webapp/WEB-INF/appengine-web.xml create mode 100644 example/src/main/webapp/WEB-INF/web.xml diff --git a/README.md b/README.md index f2c10a8..c7123ff 100644 --- a/README.md +++ b/README.md @@ -6,4 +6,69 @@ objectify-appengine-memcacheclient [![Codacy Badge](https://api.codacy.com/project/badge/Grade/e7ca8b4b11d44b30987979b0511e31ac)](https://www.codacy.com/app/takemikami/objectify-appengine-memcacheclient?utm_source=github.com&utm_medium=referral&utm_content=takemikami/objectify-appengine-memcacheclient&utm_campaign=Badge_Grade) [![Coverage Status](https://coveralls.io/repos/github/takemikami/objectify-appengine-memcacheclient/badge.svg?branch=master)](https://coveralls.io/github/takemikami/objectify-appengine-memcacheclient?branch=master) -AppEngine Memcache Client Service for Objectify6 +objectify-appengine-memcacheclient is AppEngine Memcache Client Service for Objectify v6+. +Objectify is a Java data access API specifically designed for the Google Cloud Datastore. +See the [GitHub Objectify Repository](https://github.com/objectify/objectify) for about Objectify. + +## How to setup + +Add objectify-appengine-memcacheclient to your application dependencies. + +build.gradle snippet + +``` +dependencies { + compile group: 'com.googlecode.objectify', name: 'objectify', version: '6.0.1' + runtime group: 'com.github.takemikami', name: 'objectify-appengine-memcacheclient', version: '0.0.1' + runtime group: 'com.google.appengine', name: 'appengine-api-1.0-sdk', version: '1.9.64' +} +``` + +Initialize Objectify with objectify-appengine-memcacheclient. + +Objectify init code snippet + +``` +final static String MEMCACHE_SERVICE = "com.github.takemikami.objectify.appengine.AppEngineMemcacheClientService"; +ObjectifyService.init(new ObjectifyFactory( + DatastoreOptions.newBuilder().setHost("http://localhost:8484") + .setProjectId("my-project") + .build().getService(), + (MemcacheService) Class.forName(MEMCACHE_SERVICE).getDeclaredConstructor().newInstance() +)); +``` + + +## How to execute Example Application + +Example Application helps you to understand objectify-appengine-memcacheclient. +You can execute example application by following process. + +install gcloud sdk. (see. https://cloud.google.com/sdk/docs/) + +install app-engine-java component. + +``` +gcloud components install app-engine-java +``` + +execute cloud datastore emulator. + +``` +gcloud beta emulators datastore start --host-port=localhost:8484 +``` + +change example directory. + +``` +cd example +``` + +execute sample application. + +``` +gradle appengineRun +``` + +access to sample application. http://localhost:8080/ + diff --git a/example/build.gradle b/example/build.gradle new file mode 100644 index 0000000..65b84fd --- /dev/null +++ b/example/build.gradle @@ -0,0 +1,37 @@ +buildscript { + repositories { + mavenCentral() + } + dependencies { + classpath("org.springframework.boot:spring-boot-gradle-plugin:2.0.2.RELEASE") + classpath('com.google.cloud.tools:appengine-gradle-plugin:1.3.3') + } +} +plugins { + id 'java' + id 'war' +} +apply plugin: 'org.springframework.boot' +apply plugin: 'io.spring.dependency-management' +apply plugin: 'com.google.cloud.tools.appengine' + +sourceCompatibility = 1.8 +targetCompatibility = 1.8 + +repositories { + mavenCentral() + maven { url 'https://jitpack.io/' } +} +dependencies { + // SpringBoot + compileOnly group: 'javax.servlet', name: 'javax.servlet-api', version: '3.1.0' + compile("org.springframework.boot:spring-boot-starter-web") { + exclude group: "org.springframework.boot", module: "spring-boot-starter-tomcat" + } + providedRuntime "org.springframework.boot:spring-boot-starter-jetty" + + // objectify + compile group: 'com.googlecode.objectify', name: 'objectify', version: '6.0.1' + runtime group: 'com.github.takemikami', name: 'objectify-appengine-memcacheclient', version: '0.0.1' + runtime group: 'com.google.appengine', name: 'appengine-api-1.0-sdk', version: '1.9.64' +} diff --git a/example/src/main/java/sample/Application.java b/example/src/main/java/sample/Application.java new file mode 100644 index 0000000..533686c --- /dev/null +++ b/example/src/main/java/sample/Application.java @@ -0,0 +1,22 @@ +package sample; + +import org.springframework.boot.CommandLineRunner; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.ApplicationContext; +import org.springframework.context.annotation.Bean; + +@SpringBootApplication +public class Application { + + public static void main(String[] args) { + SpringApplication.run(Application.class, args); + } + + @Bean + public CommandLineRunner commandLineRunner(ApplicationContext ctx) { + return args -> { + }; + } + +} diff --git a/example/src/main/java/sample/ItemEntity.java b/example/src/main/java/sample/ItemEntity.java new file mode 100644 index 0000000..a495fa2 --- /dev/null +++ b/example/src/main/java/sample/ItemEntity.java @@ -0,0 +1,36 @@ +package sample; + +import com.googlecode.objectify.annotation.Cache; +import com.googlecode.objectify.annotation.Entity; +import com.googlecode.objectify.annotation.Id; + +@Cache(expirationSeconds=600) +@Entity +public class ItemEntity { + + public ItemEntity() { + } + + public ItemEntity(String id, String name, String description) { + this.id = id; + this.name = name; + this.description = description; + } + + @Id + private String id; + + public String getId() {return id;} + public void setId(String id) {this.id = id;} + + private String name; + + public String getName() {return name;} + public void setName(String name) {this.name = name;} + + private String description; + + public String getDescription() {return description;} + public void setDescription(String description) {this.description = description;} + +} diff --git a/example/src/main/java/sample/ObjectifyConfig.java b/example/src/main/java/sample/ObjectifyConfig.java new file mode 100644 index 0000000..b98993e --- /dev/null +++ b/example/src/main/java/sample/ObjectifyConfig.java @@ -0,0 +1,64 @@ +package sample; + +import com.google.cloud.datastore.DatastoreOptions; +import com.googlecode.objectify.ObjectifyFactory; +import com.googlecode.objectify.ObjectifyFilter; +import com.googlecode.objectify.ObjectifyService; +import com.googlecode.objectify.cache.MemcacheService; +import java.lang.reflect.InvocationTargetException; +import javax.servlet.ServletContextEvent; +import javax.servlet.ServletContextListener; +import org.springframework.boot.web.servlet.FilterRegistrationBean; +import org.springframework.boot.web.servlet.ServletListenerRegistrationBean; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class ObjectifyConfig { + + final static String MEMCACHE_SERVICE = "com.github.takemikami.objectify.appengine.AppEngineMemcacheClientService"; + + @Bean + public FilterRegistrationBean objectifyFilterRegistration() { + final FilterRegistrationBean registration = new FilterRegistrationBean<>(); + registration.setFilter(new ObjectifyFilter()); + registration.addUrlPatterns("/*"); + registration.setOrder(1); + return registration; + } + + @Bean + public ServletListenerRegistrationBean listenerRegistrationBean() { + ServletListenerRegistrationBean bean = + new ServletListenerRegistrationBean<>(); + bean.setListener(new ObjectifyListener()); + return bean; + } + + public class ObjectifyListener implements ServletContextListener { + + @Override + public void contextInitialized(ServletContextEvent sce) { + try { + ObjectifyService.init(new ObjectifyFactory( + DatastoreOptions.newBuilder().setHost("http://localhost:8484") + .setProjectId("my-project") + .build().getService(), + (MemcacheService) Class.forName(MEMCACHE_SERVICE).getDeclaredConstructor().newInstance() + )); + } catch (ClassNotFoundException | IllegalAccessException | InstantiationException | NoSuchMethodException | InvocationTargetException ex) { + ex.printStackTrace(); + throw new RuntimeException(ex); + } + + ObjectifyService.register(ItemEntity.class); + } + + @Override + public void contextDestroyed(ServletContextEvent sce) { + + } + + } + +} \ No newline at end of file diff --git a/example/src/main/java/sample/SampleController.java b/example/src/main/java/sample/SampleController.java new file mode 100644 index 0000000..80949b6 --- /dev/null +++ b/example/src/main/java/sample/SampleController.java @@ -0,0 +1,52 @@ +package sample; + +import com.googlecode.objectify.ObjectifyService; +import java.time.LocalDateTime; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class SampleController { + + @RequestMapping("/") + public String index() { + return "Local Date Time: " + LocalDateTime.now().toString() + + ""; + } + + @RequestMapping("/put") + public String put() { + ObjectifyService.ofy().save().entity(new ItemEntity("001", "name", "desc")); + + return "put: id=001, name=desc" + + "
top"; + } + + @RequestMapping("/get") + public String get() { + ItemEntity e = ObjectifyService.ofy().cache(true).load().type(ItemEntity.class).id("001").now(); + if (e != null) { + return "get:" + String.format("%s: %s - %s", e.getId(), e.getName(), e.getDescription()) + + "
top"; + } + + return "no date.
top"; + } + + @RequestMapping("/del") + public String del() { + ItemEntity e = ObjectifyService.ofy().cache(true).load().type(ItemEntity.class).id("001").now(); + if (e != null) { + ObjectifyService.ofy().delete().entity(e); + return "delete: id=001" + + "
top"; + } + + return "no date.
top"; + } + +} diff --git a/example/src/main/java/sample/ServletInitializer.java b/example/src/main/java/sample/ServletInitializer.java new file mode 100644 index 0000000..a756460 --- /dev/null +++ b/example/src/main/java/sample/ServletInitializer.java @@ -0,0 +1,11 @@ +package sample; + +import org.springframework.boot.builder.SpringApplicationBuilder; +import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; + +public class ServletInitializer extends SpringBootServletInitializer { + @Override + protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { + return application.sources(Application.class); + } +} diff --git a/example/src/main/webapp/WEB-INF/appengine-web.xml b/example/src/main/webapp/WEB-INF/appengine-web.xml new file mode 100644 index 0000000..fa7858a --- /dev/null +++ b/example/src/main/webapp/WEB-INF/appengine-web.xml @@ -0,0 +1,4 @@ + + true + java8 + \ No newline at end of file diff --git a/example/src/main/webapp/WEB-INF/web.xml b/example/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 0000000..7046259 --- /dev/null +++ b/example/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,7 @@ + + + \ No newline at end of file