Skip to content

Commit 488b7a0

Browse files
committed
Updated all samples to include a stream handler with relevant SAM templates to address #82. Updated wiki quick start documentation with all samples to close #89. Added a new Spring Boot sample.
1 parent dd85c06 commit 488b7a0

File tree

21 files changed

+665
-82
lines changed

21 files changed

+665
-82
lines changed

samples/jersey/pet-store/sam.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Resources:
55
PetStoreFunction:
66
Type: AWS::Serverless::Function
77
Properties:
8-
Handler: com.amazonaws.serverless.sample.jersey.LambdaHandler::handleRequest
8+
Handler: com.amazonaws.serverless.sample.jersey.StreamLambdaHandler::handleRequest
99
Runtime: java8
1010
CodeUri: target/serverless-jersey-example-1.0-SNAPSHOT.jar
1111
MemorySize: 512
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.amazonaws.serverless.sample.jersey;
2+
3+
4+
import com.amazonaws.serverless.proxy.internal.LambdaContainerHandler;
5+
import com.amazonaws.serverless.proxy.jersey.JerseyLambdaContainerHandler;
6+
import com.amazonaws.serverless.proxy.model.AwsProxyRequest;
7+
import com.amazonaws.serverless.proxy.model.AwsProxyResponse;
8+
import com.amazonaws.services.lambda.runtime.Context;
9+
import com.amazonaws.services.lambda.runtime.RequestStreamHandler;
10+
11+
import org.glassfish.jersey.jackson.JacksonFeature;
12+
import org.glassfish.jersey.server.ResourceConfig;
13+
14+
import java.io.IOException;
15+
import java.io.InputStream;
16+
import java.io.OutputStream;
17+
18+
19+
public class StreamLambdaHandler implements RequestStreamHandler {
20+
private final ResourceConfig jerseyApplication = new ResourceConfig()
21+
.packages("com.amazonaws.serverless.sample.jersey")
22+
.register(JacksonFeature.class);
23+
private final JerseyLambdaContainerHandler<AwsProxyRequest, AwsProxyResponse> handler
24+
= JerseyLambdaContainerHandler.getAwsProxyHandler(jerseyApplication);
25+
26+
@Override
27+
public void handleRequest(InputStream inputStream, OutputStream outputStream, Context context)
28+
throws IOException {
29+
30+
AwsProxyRequest request = LambdaContainerHandler.getObjectMapper().readValue(inputStream, AwsProxyRequest.class);
31+
32+
AwsProxyResponse resp = handler.proxy(request, context);
33+
34+
LambdaContainerHandler.getObjectMapper().writeValue(outputStream, resp);
35+
// just in case it wasn't closed by the mapper
36+
outputStream.close();
37+
}
38+
}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,17 @@ public class PetData {
3939
names.add("Daisy");
4040
names.add("Rocky");
4141
names.add("Maggie");
42-
names.add("Jake ");
42+
names.add("Jake");
4343
names.add("Sophie");
44-
names.add("Jack ");
44+
names.add("Jack");
4545
names.add("Sadie");
46-
names.add("Toby ");
46+
names.add("Toby");
4747
names.add("Chloe");
48-
names.add("Cody ");
48+
names.add("Cody");
4949
names.add("Bailey");
5050
names.add("Buster");
5151
names.add("Lola");
52-
names.add("Duke ");
52+
names.add("Duke");
5353
names.add("Zoe");
5454
names.add("Cooper");
5555
names.add("Abby");

samples/spark/pet-store/sam.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Resources:
55
PetStoreFunction:
66
Type: AWS::Serverless::Function
77
Properties:
8-
Handler: com.amazonaws.serverless.sample.spark.LambdaHandler::handleRequest
8+
Handler: com.amazonaws.serverless.sample.spark.StreamLambdaHandler::handleRequest
99
Runtime: java8
1010
CodeUri: target/serverless-spark-example-1.0-SNAPSHOT.jar
1111
MemorySize: 512

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

Lines changed: 1 addition & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,14 @@
1616
import com.amazonaws.serverless.proxy.model.AwsProxyRequest;
1717
import com.amazonaws.serverless.proxy.model.AwsProxyResponse;
1818
import com.amazonaws.serverless.proxy.spark.SparkLambdaContainerHandler;
19-
import com.amazonaws.serverless.sample.spark.model.Pet;
20-
import com.amazonaws.serverless.sample.spark.model.PetData;
2119
import com.amazonaws.services.lambda.runtime.Context;
2220
import com.amazonaws.services.lambda.runtime.RequestHandler;
2321

24-
import com.fasterxml.jackson.databind.ObjectMapper;
2522
import org.slf4j.Logger;
2623
import org.slf4j.LoggerFactory;
2724
import spark.Spark;
2825

29-
import javax.ws.rs.core.Response;
30-
import java.util.UUID;
31-
32-
import static spark.Spark.before;
33-
import static spark.Spark.get;
34-
import static spark.Spark.post;
35-
3626
public class LambdaHandler implements RequestHandler<AwsProxyRequest, AwsProxyResponse> {
37-
private ObjectMapper objectMapper = new ObjectMapper();
3827
private boolean isInitialized = false;
3928
private SparkLambdaContainerHandler<AwsProxyRequest, AwsProxyResponse> handler;
4029
private Logger log = LoggerFactory.getLogger(LambdaHandler.class);
@@ -45,7 +34,7 @@ public AwsProxyResponse handleRequest(AwsProxyRequest awsProxyRequest, Context c
4534
isInitialized = true;
4635
try {
4736
handler = SparkLambdaContainerHandler.getAwsProxyHandler();
48-
defineResources();
37+
SparkResources.defineResources();
4938
Spark.awaitInitialization();
5039
} catch (ContainerInitializationException e) {
5140
log.error("Cannot initialize Spark application", e);
@@ -54,52 +43,4 @@ public AwsProxyResponse handleRequest(AwsProxyRequest awsProxyRequest, Context c
5443
}
5544
return handler.proxy(awsProxyRequest, context);
5645
}
57-
58-
private void defineResources() {
59-
before((request, response) -> response.type("application/json"));
60-
61-
post("/pets", (req, res) -> {
62-
Pet newPet = objectMapper.readValue(req.body(), Pet.class);
63-
if (newPet.getName() == null || newPet.getBreed() == null) {
64-
return Response.status(400).entity(new Error("Invalid name or breed")).build();
65-
}
66-
67-
Pet dbPet = newPet;
68-
dbPet.setId(UUID.randomUUID().toString());
69-
70-
res.status(200);
71-
return dbPet;
72-
}, new JsonTransformer());
73-
74-
get("/pets", (req, res) -> {
75-
int limit = 10;
76-
if (req.queryParams("limit") != null) {
77-
limit = Integer.parseInt(req.queryParams("limit"));
78-
}
79-
80-
Pet[] outputPets = new Pet[limit];
81-
82-
for (int i = 0; i < limit; i++) {
83-
Pet newPet = new Pet();
84-
newPet.setId(UUID.randomUUID().toString());
85-
newPet.setName(PetData.getRandomName());
86-
newPet.setBreed(PetData.getRandomBreed());
87-
newPet.setDateOfBirth(PetData.getRandomDoB());
88-
outputPets[i] = newPet;
89-
}
90-
91-
res.status(200);
92-
return outputPets;
93-
}, new JsonTransformer());
94-
95-
get("/pets/:petId", (req, res) -> {
96-
Pet newPet = new Pet();
97-
newPet.setId(UUID.randomUUID().toString());
98-
newPet.setBreed(PetData.getRandomBreed());
99-
newPet.setDateOfBirth(PetData.getRandomDoB());
100-
newPet.setName(PetData.getRandomName());
101-
res.status(200);
102-
return newPet;
103-
}, new JsonTransformer());
104-
}
10546
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.amazonaws.serverless.sample.spark;
2+
3+
4+
import com.amazonaws.serverless.proxy.internal.LambdaContainerHandler;
5+
import com.amazonaws.serverless.sample.spark.model.Pet;
6+
import com.amazonaws.serverless.sample.spark.model.PetData;
7+
8+
import javax.ws.rs.core.Response;
9+
10+
import java.util.UUID;
11+
12+
import static spark.Spark.before;
13+
import static spark.Spark.get;
14+
import static spark.Spark.post;
15+
16+
17+
public class SparkResources {
18+
19+
public static void defineResources() {
20+
before((request, response) -> response.type("application/json"));
21+
22+
post("/pets", (req, res) -> {
23+
Pet newPet = LambdaContainerHandler.getObjectMapper().readValue(req.body(), Pet.class);
24+
if (newPet.getName() == null || newPet.getBreed() == null) {
25+
return Response.status(400).entity(new Error("Invalid name or breed")).build();
26+
}
27+
28+
Pet dbPet = newPet;
29+
dbPet.setId(UUID.randomUUID().toString());
30+
31+
res.status(200);
32+
return dbPet;
33+
}, new JsonTransformer());
34+
35+
get("/pets", (req, res) -> {
36+
int limit = 10;
37+
if (req.queryParams("limit") != null) {
38+
limit = Integer.parseInt(req.queryParams("limit"));
39+
}
40+
41+
Pet[] outputPets = new Pet[limit];
42+
43+
for (int i = 0; i < limit; i++) {
44+
Pet newPet = new Pet();
45+
newPet.setId(UUID.randomUUID().toString());
46+
newPet.setName(PetData.getRandomName());
47+
newPet.setBreed(PetData.getRandomBreed());
48+
newPet.setDateOfBirth(PetData.getRandomDoB());
49+
outputPets[i] = newPet;
50+
}
51+
52+
res.status(200);
53+
return outputPets;
54+
}, new JsonTransformer());
55+
56+
get("/pets/:petId", (req, res) -> {
57+
Pet newPet = new Pet();
58+
newPet.setId(UUID.randomUUID().toString());
59+
newPet.setBreed(PetData.getRandomBreed());
60+
newPet.setDateOfBirth(PetData.getRandomDoB());
61+
newPet.setName(PetData.getRandomName());
62+
res.status(200);
63+
return newPet;
64+
}, new JsonTransformer());
65+
}
66+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.amazonaws.serverless.sample.spark;
2+
3+
4+
import com.amazonaws.serverless.exceptions.ContainerInitializationException;
5+
import com.amazonaws.serverless.proxy.internal.LambdaContainerHandler;
6+
import com.amazonaws.serverless.proxy.model.AwsProxyRequest;
7+
import com.amazonaws.serverless.proxy.model.AwsProxyResponse;
8+
import com.amazonaws.serverless.proxy.spark.SparkLambdaContainerHandler;
9+
import com.amazonaws.services.lambda.runtime.Context;
10+
import com.amazonaws.services.lambda.runtime.RequestStreamHandler;
11+
12+
import org.slf4j.Logger;
13+
import org.slf4j.LoggerFactory;
14+
import spark.Spark;
15+
16+
import java.io.IOException;
17+
import java.io.InputStream;
18+
import java.io.OutputStream;
19+
20+
21+
public class StreamLambdaHandler implements RequestStreamHandler {
22+
private boolean isInitialized = false;
23+
private SparkLambdaContainerHandler<AwsProxyRequest, AwsProxyResponse> handler;
24+
private Logger log = LoggerFactory.getLogger(StreamLambdaHandler.class);
25+
26+
@Override
27+
public void handleRequest(InputStream inputStream, OutputStream outputStream, Context context)
28+
throws IOException {
29+
if (!isInitialized) {
30+
isInitialized = true;
31+
try {
32+
handler = SparkLambdaContainerHandler.getAwsProxyHandler();
33+
SparkResources.defineResources();
34+
Spark.awaitInitialization();
35+
} catch (ContainerInitializationException e) {
36+
log.error("Cannot initialize Spark application", e);
37+
return;
38+
}
39+
}
40+
41+
AwsProxyRequest request = LambdaContainerHandler.getObjectMapper().readValue(inputStream, AwsProxyRequest.class);
42+
43+
AwsProxyResponse resp = handler.proxy(request, context);
44+
45+
LambdaContainerHandler.getObjectMapper().writeValue(outputStream, resp);
46+
// just in case it wasn't closed by the mapper
47+
outputStream.close();
48+
}
49+
}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,17 @@ public class PetData {
3939
names.add("Daisy");
4040
names.add("Rocky");
4141
names.add("Maggie");
42-
names.add("Jake ");
42+
names.add("Jake");
4343
names.add("Sophie");
44-
names.add("Jack ");
44+
names.add("Jack");
4545
names.add("Sadie");
46-
names.add("Toby ");
46+
names.add("Toby");
4747
names.add("Chloe");
48-
names.add("Cody ");
48+
names.add("Cody");
4949
names.add("Bailey");
5050
names.add("Buster");
5151
names.add("Lola");
52-
names.add("Duke ");
52+
names.add("Duke");
5353
names.add("Zoe");
5454
names.add("Cooper");
5555
names.add("Abby");

samples/spring/pet-store/sam.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Resources:
55
PetStoreFunction:
66
Type: AWS::Serverless::Function
77
Properties:
8-
Handler: com.amazonaws.serverless.sample.spring.LambdaHandler::handleRequest
8+
Handler: com.amazonaws.serverless.sample.spring.StreamLambdaHandler::handleRequest
99
Runtime: java8
1010
CodeUri: target/serverless-spring-example-1.0-SNAPSHOT.jar
1111
MemorySize: 512

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33

44
import com.amazonaws.serverless.exceptions.ContainerInitializationException;
5+
import com.amazonaws.serverless.proxy.internal.LambdaContainerHandler;
56
import com.amazonaws.serverless.proxy.model.AwsProxyRequest;
67
import com.amazonaws.serverless.proxy.model.AwsProxyResponse;
78
import com.amazonaws.serverless.proxy.spring.SpringLambdaContainerHandler;
89
import com.amazonaws.services.lambda.runtime.Context;
910
import com.amazonaws.services.lambda.runtime.RequestStreamHandler;
1011

11-
import com.fasterxml.jackson.databind.ObjectMapper;
1212
import org.slf4j.Logger;
1313
import org.slf4j.LoggerFactory;
1414

@@ -19,7 +19,6 @@
1919

2020
public class StreamLambdaHandler implements RequestStreamHandler {
2121
private SpringLambdaContainerHandler<AwsProxyRequest, AwsProxyResponse> handler;
22-
private static ObjectMapper mapper = new ObjectMapper();
2322
private Logger log = LoggerFactory.getLogger(StreamLambdaHandler.class);
2423

2524
@Override
@@ -35,11 +34,11 @@ public void handleRequest(InputStream inputStream, OutputStream outputStream, Co
3534
}
3635
}
3736

38-
AwsProxyRequest request = mapper.readValue(inputStream, AwsProxyRequest.class);
37+
AwsProxyRequest request = LambdaContainerHandler.getObjectMapper().readValue(inputStream, AwsProxyRequest.class);
3938

4039
AwsProxyResponse resp = handler.proxy(request, context);
4140

42-
mapper.writeValue(outputStream, resp);
41+
LambdaContainerHandler.getObjectMapper().writeValue(outputStream, resp);
4342
// just in case it wasn't closed by the mapper
4443
outputStream.close();
4544
}

0 commit comments

Comments
 (0)