Skip to content

Commit 2b962c8

Browse files
committed
Updated samples to use a static block to declared the handler (#109) and moved them all to the new streamProxy method (#118).
1 parent 361ae5a commit 2b962c8

File tree

5 files changed

+43
-62
lines changed

5 files changed

+43
-62
lines changed

README.md

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,22 @@ Below is the most basic AWS Lambda handler example that launches a Spring applic
1313

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

1927
@Override
2028
public void handleRequest(InputStream inputStream, OutputStream outputStream, Context context)
2129
throws IOException {
22-
if (handler == null) {
23-
try {
24-
handler = SpringLambdaContainerHandler.getAwsProxyHandler(PetStoreSpringAppConfig.class);
25-
} catch (ContainerInitializationException e) {
26-
log.error("Cannot initialize Spring container", e);
27-
outputStream.close();
28-
throw new RuntimeException(e);
29-
}
30-
}
31-
32-
AwsProxyRequest request = LambdaContainerHandler.getObjectMapper().readValue(inputStream, AwsProxyRequest.class);
33-
34-
AwsProxyResponse resp = handler.proxy(request, context);
30+
handler.proxyStream(inputStream, outputStream, context);
3531

36-
LambdaContainerHandler.getObjectMapper().writeValue(outputStream, resp);
3732
// just in case it wasn't closed by the mapper
3833
outputStream.close();
3934
}

samples/jersey/pet-store/src/main/java/com/amazonaws/serverless/sample/jersey/StreamLambdaHandler.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ public StreamLambdaHandler() {
3131
@Override
3232
public void handleRequest(InputStream inputStream, OutputStream outputStream, Context context)
3333
throws IOException {
34-
3534
handler.proxyStream(inputStream, outputStream, context);
3635

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

samples/spark/pet-store/src/main/java/com/amazonaws/serverless/sample/spark/StreamLambdaHandler.java

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
import com.amazonaws.services.lambda.runtime.Context;
1010
import com.amazonaws.services.lambda.runtime.RequestStreamHandler;
1111

12-
import org.slf4j.Logger;
13-
import org.slf4j.LoggerFactory;
1412
import spark.Spark;
1513

1614
import java.io.IOException;
@@ -19,8 +17,18 @@
1917

2018

2119
public class StreamLambdaHandler implements RequestStreamHandler {
22-
private SparkLambdaContainerHandler<AwsProxyRequest, AwsProxyResponse> handler;
23-
private Logger log = LoggerFactory.getLogger(StreamLambdaHandler.class);
20+
private static SparkLambdaContainerHandler<AwsProxyRequest, AwsProxyResponse> handler;
21+
static {
22+
try {
23+
handler = SparkLambdaContainerHandler.getAwsProxyHandler();
24+
SparkResources.defineResources();
25+
Spark.awaitInitialization();
26+
} catch (ContainerInitializationException e) {
27+
// if we fail here. We re-throw the exception to force another cold start
28+
e.printStackTrace();
29+
throw new RuntimeException("Could not initialize Spark container", e);
30+
}
31+
}
2432

2533
public StreamLambdaHandler() {
2634
// we enable the timer for debugging. This SHOULD NOT be enabled in production.
@@ -30,17 +38,6 @@ public StreamLambdaHandler() {
3038
@Override
3139
public void handleRequest(InputStream inputStream, OutputStream outputStream, Context context)
3240
throws IOException {
33-
if (handler == null) {
34-
try {
35-
handler = SparkLambdaContainerHandler.getAwsProxyHandler();
36-
SparkResources.defineResources();
37-
Spark.awaitInitialization();
38-
} catch (ContainerInitializationException e) {
39-
log.error("Cannot initialize Spark application", e);
40-
return;
41-
}
42-
}
43-
4441
handler.proxyStream(inputStream, outputStream, context);
4542

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

samples/spring/pet-store/src/main/java/com/amazonaws/serverless/sample/spring/StreamLambdaHandler.java

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,22 @@
99
import com.amazonaws.services.lambda.runtime.Context;
1010
import com.amazonaws.services.lambda.runtime.RequestStreamHandler;
1111

12-
import org.slf4j.Logger;
13-
import org.slf4j.LoggerFactory;
14-
1512
import java.io.IOException;
1613
import java.io.InputStream;
1714
import java.io.OutputStream;
1815

1916

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

2429
public StreamLambdaHandler() {
2530
// we enable the timer for debugging. This SHOULD NOT be enabled in production.
@@ -29,16 +34,6 @@ public StreamLambdaHandler() {
2934
@Override
3035
public void handleRequest(InputStream inputStream, OutputStream outputStream, Context context)
3136
throws IOException {
32-
if (handler == null) {
33-
try {
34-
handler = SpringLambdaContainerHandler.getAwsProxyHandler(PetStoreSpringAppConfig.class);
35-
} catch (ContainerInitializationException e) {
36-
log.error("Cannot initialize Spring container", e);
37-
outputStream.close();
38-
throw new RuntimeException(e);
39-
}
40-
}
41-
4237
handler.proxyStream(inputStream, outputStream, context);
4338

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

samples/springboot/pet-store/src/main/java/com/amazonaws/serverless/sample/springboot/StreamLambdaHandler.java

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,22 @@
99
import com.amazonaws.services.lambda.runtime.Context;
1010
import com.amazonaws.services.lambda.runtime.RequestStreamHandler;
1111

12-
import org.slf4j.Logger;
13-
import org.slf4j.LoggerFactory;
14-
1512
import java.io.IOException;
1613
import java.io.InputStream;
1714
import java.io.OutputStream;
1815

1916

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

2429
public StreamLambdaHandler() {
2530
// we enable the timer for debugging. This SHOULD NOT be enabled in production.
@@ -29,16 +34,6 @@ public StreamLambdaHandler() {
2934
@Override
3035
public void handleRequest(InputStream inputStream, OutputStream outputStream, Context context)
3136
throws IOException {
32-
if (handler == null) {
33-
try {
34-
handler = SpringBootLambdaContainerHandler.getAwsProxyHandler(Application.class);
35-
} catch (ContainerInitializationException e) {
36-
log.error("Cannot initialize Spring container", e);
37-
outputStream.close();
38-
throw new RuntimeException(e);
39-
}
40-
}
41-
4237
handler.proxyStream(inputStream, outputStream, context);
4338

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

0 commit comments

Comments
 (0)