What interface should a Serverless scheduled java function implement?

matt freake :

I'm trying out writing a Java AWS Lambda using Serverless. While my Lambdas triggered by HTTP endpoints deploy and work correctly, my attempt at at scheduled one fails:

The config looks like:

from serverless.yml

functions:
  timedHandler:
    handler: com.serverless.TimedHandler
    events:
     - schedule:
        rate(1 minute)

If I try to implement RequestHandler like:

public class TimedHandler implements RequestHandler<Request, Response> {
private static final Logger LOG = LogManager.getLogger(Handler.class);

@Override
public Response handleRequest(Request request, Context context) {
    LOG.info("Started up");
    return null;
}

The logs complain that:

An error occurred during JSON parsing: java.lang.RuntimeException java.lang.RuntimeException: An error occurred during JSON parsing
Caused by: java.io.UncheckedIOException: com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of com.amazonaws.Request

I assume because the scheduled event is not passing in a Request object but something specific to a scheduled trigger. But if I don't implement an interface (which the AWS docs suggest is fine) the AWS logs complain

Class does not implement an appropriate handler interface: com.serverless.TimedHandler

Is there a specific AWS interface I need to implement for a scheduled lambda that passes in appropriate arguments?

matt freake :

So it turns out that they should implement the RequestStreamHandler interface, for example:

public class TimedHandler implements RequestStreamHandler {
    private static final Logger LOG = LogManager.getLogger(TimedHandler.class);

    @Override
    public void handleRequest(InputStream inputStream, OutputStream outputStream, Context context) throws IOException {
        LOG.info("Started up");
        return;
    }

}

This now successfully runs and throws no exceptions

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=78337&siteId=1