Skip to content

Commit ab31b09

Browse files
author
Jason Harris
committed
Demonstrating Java streaming response
1 parent e932d10 commit ab31b09

File tree

2 files changed

+13
-13
lines changed

2 files changed

+13
-13
lines changed

examples/powertools-examples-core/sam/src/main/java/helloworld/AppStream.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@
3535
import java.io.PrintWriter;
3636
import java.nio.charset.Charset;
3737

38+
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent;
39+
import software.amazon.lambda.powertools.tracing.TracingUtils;
40+
3841
public class AppStream implements RequestStreamHandler {
3942
private static final ObjectMapper mapper = new ObjectMapper();
4043
private final static Logger log = LogManager.getLogger(AppStream.class);
@@ -45,17 +48,15 @@ public class AppStream implements RequestStreamHandler {
4548
public void handleRequest(InputStream input, OutputStream output, Context context) throws IOException {
4649
BufferedReader reader = new BufferedReader(new InputStreamReader(input, Charset.forName("US-ASCII")));
4750
PrintWriter writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(output, Charset.forName("US-ASCII"))));
48-
int nextChar;
4951
try {
50-
while ((nextChar = reader.read()) != -1) {
51-
output.write(nextChar);
52-
}
52+
APIGatewayProxyResponseEvent response = new APIGatewayProxyResponseEvent()
53+
.withStatusCode(200)
54+
.withBody("output_body");
55+
output.write(response.toString().getBytes());
5356
} catch (IOException e) {
5457
log.error("Something has gone wrong: ", e);
5558
} finally {
5659
reader.close();
57-
String finalString = writer.toString();
58-
log.info("Final string result: " + finalString);
5960
writer.close();
6061
}
6162
}

examples/powertools-examples-core/sam/template.yaml

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,12 @@ Resources:
4444
Environment:
4545
Variables:
4646
POWERTOOLS_LOGGER_SAMPLE_RATE: 0.7
47-
48-
HelloWorldStreamFunctionUrl:
49-
Type: AWS::Lambda::Url
50-
Properties:
51-
TargetFunctionArn: !Ref HelloWorldStreamFunction
52-
AuthType: AWS_IAM
53-
InvokeMode: RESPONSE_STREAM
47+
Events:
48+
HelloWorld:
49+
Type: Api
50+
Properties:
51+
Path: /hellostream
52+
Method: get
5453

5554
Outputs:
5655
# ServerlessRestApi is an implicit API created out of Events key under Serverless::Function

0 commit comments

Comments
 (0)