Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding java8 spanner sample #711

Merged
merged 3 commits into from
Jun 15, 2017
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
8 changes: 6 additions & 2 deletions appengine-java8/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,16 @@
<module>multitenancy</module>
<module>oauth2</module>
<module>requests</module>
<module>search</module>
<module>sendgrid</module>

<module>remote-client</module>
<module>remote-server</module>

<module>search</module>

<module>sendgrid</module>

<module>spanner</module>

<module>static-files</module>

<module>taskqueues-deferred</module>
Expand Down
54 changes: 54 additions & 0 deletions appengine-java8/spanner/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Google Cloud Spanner Sample

This sample demonstrates how to use [Google Cloud Spanner][spanner-docs]
from [Google App Engine standard environment][ae-docs].

[spanner-docs]: https://cloud.google.com/spanner/docs/
[ae-docs]: https://cloud.google.com/appengine/docs/java/


## Setup
- Install the [Google Cloud SDK](https://cloud.google.com/sdk/) and run:
```
gcloud init
```
If this is your first time creating an App engine application:
```
gcloud app create
```
- [Create a Spanner instance](https://cloud.google.com/spanner/docs/quickstart-console#create_an_instance).

- Update `SPANNER_INSTANCE` value in `[appengine-web.xml](src/main/webapp/WEB-INF/appengine-web.xml).

## Endpoints
- `/spanner` : will run sample operations against the spanner instance in order. Individual tasks can be run
using the `task` query parameter. See [SpannerTasks](src/main/java/com/example/appengine/spanner/SpannerTasks.java)
for supported set of tasks.
Note : by default all the spanner example operations run in order, this operation may take a while to return.

## Running locally
- Authorize the local application:
```
gcloud auth application-default login
```
You may also [create and use service account credentials](https://cloud.google.com/docs/authentication/getting-started#creating_the_service_account).

- App Engine Maven plugins do not work correctly for this sample for local testing.
Here is the [tracking issue](https://github.com/GoogleCloudPlatform/google-cloud-java/issues/2155).
As a workaround to run locally, this sample uses the [Maven Jetty plugin](http://www.eclipse.org/jetty/documentation/9.4.x/jetty-maven-plugin.html).
```
mvn -DSPANNER_INSTANCE=my-spanner-instance jetty:run
```

To see the results of the local application, open
[http://localhost:8080/run](http://localhost:8080/run) in a web browser.
Note : by default all the spanner example operations run in order, this operation may take a while to show results.

## Deploying

$ mvn clean appengine:deploy

To see the results of the deployed sample application, open
`https://spanner-dot-PROJECTID.appspot.com/run` in a web browser.
Note : by default all the spanner example operations run in order, this operation may take a while to show results.

92 changes: 92 additions & 0 deletions appengine-java8/spanner/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright 2017 Google Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<groupId>com.example.appengine</groupId>
<modelVersion>4.0.0</modelVersion>
<artifactId>appengine-spanner-j8</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<failOnMissingWebXml>false</failOnMissingWebXml>
</properties>

<parent>
<artifactId>appengine-java8-samples</artifactId>
<groupId>com.google.cloud</groupId>
<version>1.0.0</version>
<relativePath>..</relativePath>
</parent>

<dependencies>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-spanner</artifactId>
<version>0.19.0-beta</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.google.appengine</groupId>
<artifactId>appengine-api-1.0-sdk</artifactId>
<version>1.9.53</version>
</dependency>
</dependencies>

<build>
<outputDirectory>
${project.build.directory}/${project.build.finalName}/WEB-INF/classes
</outputDirectory>

<plugins>
<!-- START Using Jetty plugin as workaround for local testing.
https://github.com/GoogleCloudPlatform/google-cloud-java/issues/2155:
This sample does not work with the AppEngine Maven Plugins for local testing
-->
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.4.6.v20170531</version>
</plugin>
<!-- END -->

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<version>3.5.1</version>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>com.google.cloud.tools</groupId>
<artifactId>appengine-maven-plugin</artifactId>
<version>1.3.1</version>
<configuration>
<deploy.promote>true</deploy.promote>
<deploy.stopPreviousVersion>true</deploy.stopPreviousVersion>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
/**
* Copyright 2017 Google Inc.
*
* <p>Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of the License at
*
* <p>http://www.apache.org/licenses/LICENSE-2.0
*
* <p>Unless required by applicable law or agreed to in writing, software distributed under the
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.appengine.spanner;

import com.google.cloud.spanner.DatabaseAdminClient;
import com.google.cloud.spanner.DatabaseClient;
import com.google.cloud.spanner.DatabaseId;
import com.google.cloud.spanner.Spanner;
import com.google.cloud.spanner.SpannerOptions;
import java.io.IOException;
import java.util.UUID;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

// With @WebListener annotation the webapp/WEB-INF/web.xml is no longer required.
@WebListener
public class SpannerClient implements ServletContextListener {

private static String PROJECT_ID;
private static String INSTANCE_ID;
private static String DATABASE_ID;

// The initial connection can be an expensive operation -- We cache this Connection
// to speed things up. For this sample, keeping them here is a good idea, for
// your application, you may wish to keep this somewhere else.
private static Spanner spanner = null;
private static DatabaseAdminClient databaseAdminClient = null;
private static DatabaseClient databaseClient = null;

private static ServletContext sc;

private static void connect() throws IOException {
if (INSTANCE_ID == null) {
if (sc != null) {
sc.log("environment variable SPANNER_INSTANCE need to be defined.");
}
return;
}
SpannerOptions options = SpannerOptions.newBuilder().build();
PROJECT_ID = options.getProjectId();
spanner = options.getService();
databaseAdminClient = spanner.getDatabaseAdminClient();
}

static DatabaseAdminClient getDatabaseAdminClient() {
if (databaseAdminClient == null) {
try {
connect();
} catch (IOException e) {
if (sc != null) {
sc.log("getDatabaseAdminClient ", e);
}
}
}
if (databaseAdminClient == null) {
if (sc != null) {
sc.log("Spanner : Unable to connect");
}
}
return databaseAdminClient;
}

static DatabaseClient getDatabaseClient() {
if (databaseClient == null) {
databaseClient =
spanner.getDatabaseClient(DatabaseId.of(PROJECT_ID, INSTANCE_ID, DATABASE_ID));
}
return databaseClient;
}

@Override
public void contextInitialized(ServletContextEvent event) {
if (event != null) {
sc = event.getServletContext();
if (INSTANCE_ID == null) {
INSTANCE_ID = sc.getInitParameter("SPANNER_INSTANCE");
}
}
//try system properties
if (INSTANCE_ID == null) {
INSTANCE_ID = System.getProperty("SPANNER_INSTANCE");
}

if (DATABASE_ID == null) {
DATABASE_ID = "db-" + UUID.randomUUID().toString().substring(0, 25);
}

try {
connect();
} catch (IOException e) {
if (sc != null) {
sc.log("SpannerConnection - connect ", e);
}
}
if (databaseAdminClient == null) {
if (sc != null) {
sc.log("SpannerConnection - No Connection");
}
}
if (sc != null) {
sc.log("ctx Initialized: " + INSTANCE_ID + " " + DATABASE_ID);
}
}

@Override
public void contextDestroyed(ServletContextEvent servletContextEvent) {
// App Engine does not currently invoke this method.
databaseAdminClient = null;
}

static String getInstanceId() {
return INSTANCE_ID;
}

static String getDatabaseId() {
return DATABASE_ID;
}
}
Loading