Skip to content

Commit

Permalink
Updated samples to use a static block to declared the handler (#109) …
Browse files Browse the repository at this point in the history
…and moved them all to the new streamProxy method (#118).
  • Loading branch information
sapessi committed Feb 11, 2018
1 parent 361ae5a commit 2b962c8
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 62 deletions.
27 changes: 11 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,22 @@ Below is the most basic AWS Lambda handler example that launches a Spring applic

```java
public class StreamLambdaHandler implements RequestStreamHandler {
private SpringLambdaContainerHandler<AwsProxyRequest, AwsProxyResponse> handler;
private Logger log = LoggerFactory.getLogger(StreamLambdaHandler.class);
private static SpringLambdaContainerHandler<AwsProxyRequest, AwsProxyResponse> handler;
static {
try {
handler = SpringLambdaContainerHandler.getAwsProxyHandler(PetStoreSpringAppConfig.class);
} catch (ContainerInitializationException e) {
// if we fail here. We re-throw the exception to force another cold start
e.printStackTrace();
throw new RuntimeException("Could not initialize Spring framework", e);
}
}

@Override
public void handleRequest(InputStream inputStream, OutputStream outputStream, Context context)
throws IOException {
if (handler == null) {
try {
handler = SpringLambdaContainerHandler.getAwsProxyHandler(PetStoreSpringAppConfig.class);
} catch (ContainerInitializationException e) {
log.error("Cannot initialize Spring container", e);
outputStream.close();
throw new RuntimeException(e);
}
}

AwsProxyRequest request = LambdaContainerHandler.getObjectMapper().readValue(inputStream, AwsProxyRequest.class);

AwsProxyResponse resp = handler.proxy(request, context);
handler.proxyStream(inputStream, outputStream, context);

LambdaContainerHandler.getObjectMapper().writeValue(outputStream, resp);
// just in case it wasn't closed by the mapper
outputStream.close();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ public StreamLambdaHandler() {
@Override
public void handleRequest(InputStream inputStream, OutputStream outputStream, Context context)
throws IOException {

handler.proxyStream(inputStream, outputStream, context);

// just in case it wasn't closed by the mapper
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestStreamHandler;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import spark.Spark;

import java.io.IOException;
Expand All @@ -19,8 +17,18 @@


public class StreamLambdaHandler implements RequestStreamHandler {
private SparkLambdaContainerHandler<AwsProxyRequest, AwsProxyResponse> handler;
private Logger log = LoggerFactory.getLogger(StreamLambdaHandler.class);
private static SparkLambdaContainerHandler<AwsProxyRequest, AwsProxyResponse> handler;
static {
try {
handler = SparkLambdaContainerHandler.getAwsProxyHandler();
SparkResources.defineResources();
Spark.awaitInitialization();
} catch (ContainerInitializationException e) {
// if we fail here. We re-throw the exception to force another cold start
e.printStackTrace();
throw new RuntimeException("Could not initialize Spark container", e);
}
}

public StreamLambdaHandler() {
// we enable the timer for debugging. This SHOULD NOT be enabled in production.
Expand All @@ -30,17 +38,6 @@ public StreamLambdaHandler() {
@Override
public void handleRequest(InputStream inputStream, OutputStream outputStream, Context context)
throws IOException {
if (handler == null) {
try {
handler = SparkLambdaContainerHandler.getAwsProxyHandler();
SparkResources.defineResources();
Spark.awaitInitialization();
} catch (ContainerInitializationException e) {
log.error("Cannot initialize Spark application", e);
return;
}
}

handler.proxyStream(inputStream, outputStream, context);

// just in case it wasn't closed by the mapper
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,22 @@
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestStreamHandler;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;


public class StreamLambdaHandler implements RequestStreamHandler {
private SpringLambdaContainerHandler<AwsProxyRequest, AwsProxyResponse> handler;
private Logger log = LoggerFactory.getLogger(StreamLambdaHandler.class);
private static SpringLambdaContainerHandler<AwsProxyRequest, AwsProxyResponse> handler;
static {
try {
handler = SpringLambdaContainerHandler.getAwsProxyHandler(PetStoreSpringAppConfig.class);
} catch (ContainerInitializationException e) {
// if we fail here. We re-throw the exception to force another cold start
e.printStackTrace();
throw new RuntimeException("Could not initialize Spring framework", e);
}
}

public StreamLambdaHandler() {
// we enable the timer for debugging. This SHOULD NOT be enabled in production.
Expand All @@ -29,16 +34,6 @@ public StreamLambdaHandler() {
@Override
public void handleRequest(InputStream inputStream, OutputStream outputStream, Context context)
throws IOException {
if (handler == null) {
try {
handler = SpringLambdaContainerHandler.getAwsProxyHandler(PetStoreSpringAppConfig.class);
} catch (ContainerInitializationException e) {
log.error("Cannot initialize Spring container", e);
outputStream.close();
throw new RuntimeException(e);
}
}

handler.proxyStream(inputStream, outputStream, context);

// just in case it wasn't closed by the mapper
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,22 @@
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestStreamHandler;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;


public class StreamLambdaHandler implements RequestStreamHandler {
private SpringBootLambdaContainerHandler<AwsProxyRequest, AwsProxyResponse> handler;
private Logger log = LoggerFactory.getLogger(StreamLambdaHandler.class);
private static SpringBootLambdaContainerHandler<AwsProxyRequest, AwsProxyResponse> handler;
static {
try {
handler = SpringBootLambdaContainerHandler.getAwsProxyHandler(Application.class);
} catch (ContainerInitializationException e) {
// if we fail here. We re-throw the exception to force another cold start
e.printStackTrace();
throw new RuntimeException("Could not initialize Spring Boot application", e);
}
}

public StreamLambdaHandler() {
// we enable the timer for debugging. This SHOULD NOT be enabled in production.
Expand All @@ -29,16 +34,6 @@ public StreamLambdaHandler() {
@Override
public void handleRequest(InputStream inputStream, OutputStream outputStream, Context context)
throws IOException {
if (handler == null) {
try {
handler = SpringBootLambdaContainerHandler.getAwsProxyHandler(Application.class);
} catch (ContainerInitializationException e) {
log.error("Cannot initialize Spring container", e);
outputStream.close();
throw new RuntimeException(e);
}
}

handler.proxyStream(inputStream, outputStream, context);

// just in case it wasn't closed by the mapper
Expand Down

0 comments on commit 2b962c8

Please sign in to comment.