Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 66 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/

37 changes: 37 additions & 0 deletions example/build.gradle
Original file line number Diff line number Diff line change
@@ -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'
}
22 changes: 22 additions & 0 deletions example/src/main/java/sample/Application.java
Original file line number Diff line number Diff line change
@@ -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 -> {
};
}

}
36 changes: 36 additions & 0 deletions example/src/main/java/sample/ItemEntity.java
Original file line number Diff line number Diff line change
@@ -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;}

}
64 changes: 64 additions & 0 deletions example/src/main/java/sample/ObjectifyConfig.java
Original file line number Diff line number Diff line change
@@ -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<ObjectifyFilter> objectifyFilterRegistration() {
final FilterRegistrationBean<ObjectifyFilter> registration = new FilterRegistrationBean<>();
registration.setFilter(new ObjectifyFilter());
registration.addUrlPatterns("/*");
registration.setOrder(1);
return registration;
}

@Bean
public ServletListenerRegistrationBean<ObjectifyListener> listenerRegistrationBean() {
ServletListenerRegistrationBean<ObjectifyListener> 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) {

}

}

}
52 changes: 52 additions & 0 deletions example/src/main/java/sample/SampleController.java
Original file line number Diff line number Diff line change
@@ -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()
+ "<ul>"
+ "<li><a href='/put'>put entity</a>"
+ "<li><a href='/get'>get entity</a>"
+ "<li><a href='/del'>del entity</a>"
+ "</ul>";
}

@RequestMapping("/put")
public String put() {
ObjectifyService.ofy().save().entity(new ItemEntity("001", "name", "desc"));

return "put: id=001, name=desc"
+ "<br/><a href='/'>top</a>";
}

@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())
+ "<br/><a href='/'>top</a>";
}

return "no date.<br/><a href='/'>top</a>";
}

@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"
+ "<br/><a href='/'>top</a>";
}

return "no date.<br/><a href='/'>top</a>";
}

}
11 changes: 11 additions & 0 deletions example/src/main/java/sample/ServletInitializer.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
4 changes: 4 additions & 0 deletions example/src/main/webapp/WEB-INF/appengine-web.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
<threadsafe>true</threadsafe>
<runtime>java8</runtime>
</appengine-web-app>
7 changes: 7 additions & 0 deletions example/src/main/webapp/WEB-INF/web.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
</web-app>