aws / aws-lambda-java-libs

Official mirror for interface definitions and helper classes for Java code running on the AWS Lambda platform.

Home Page:https://aws.amazon.com/lambda/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

RequestStreamHandler not working as expect

liewhite opened this issue · comments

commented

I use RequestStreamHandler to write response, but i receive first byte after lambda complete.

It should return a "1" every second

I access function with curl:

curl -N  https://xxxxxxxxxxxxxx.lambda-url.ap-northeast-1.on.aws

Set function invoke mode to RESPONSE_STREAM

This is function code:

import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.LambdaLogger;
import com.amazonaws.services.lambda.runtime.RequestStreamHandler;

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

// Handler value: example.HandlerStream
public class HandlerStream implements RequestStreamHandler {

  @Override
  /*
   * Takes an InputStream and an OutputStream. Reads from the InputStream,
   * and copies all characters to the OutputStream.
   */
  public void handleRequest(InputStream inputStream, OutputStream outputStream, Context context) throws IOException
  {
    LambdaLogger logger = context.getLogger();
    int i = 0;
    try {
      for(;i<=3; i++) {
        outputStream.write('1');
        outputStream.flush();
        Thread.sleep(1000);
      }
    } catch (Exception e) {
      e.printStackTrace();
      logger.log(e.toString());
    } finally {
      inputStream.close();
      // String finalString = outputStream.toString();
      logger.log("end of stream: " + i);
      outputStream.close();
    }
  }
}

@liewhite Please can you provide more context? What is it you're actually trying to do / what is the problem you're trying to solve?

commented

Oh, it's a aws problem, i forgot closing this.