11

Consider a simple Lambda written in Java:

import com.amazonaws.services.lambda.runtime.Context; 
import com.amazonaws.services.lambda.runtime.RequestHandler;

public class Hello implements RequestHandler<Integer, String>{
    public String handleRequest(int myCount, Context context) {
        return String.valueOf(myCount);
    }
}

The handler interface is defined as RequestHandler<InputType, OutputType>, but when my Lambda reacts to events and just does some side effects, is the output type unnecessary and I have to write something like this:

public class Hello implements RequestHandler<SNSEvent, Void>{
    public Void handleRequest(SNSEvent snsEvent, Context context) {
        ...
        return null;
    }
}

Which is annoying.

Is there an alternative to RequestHandler for a void handler?:

public class Hello implements EventHandler<SNSEvent>{
    public void handleEvent(SNSEvent snsEvent, Context context) {
        ...
    }
}
ttulka
  • 10,309
  • 7
  • 41
  • 52
  • https://docs.aws.amazon.com/lambda/latest/dg/java-programming-model-handler-types.html – Michael - sqlbot Mar 19 '19 at 14:59
  • @Michael-sqlbot Sorry, I don't see any answer to my question under the link... – ttulka Mar 19 '19 at 15:01
  • What exactly is your question @ttulka? You mean you don't want to return anything from your function, so rather than `Void` you want to use `void`? The link that Michael shared states that if your functions are invoked asynchronously, you can just use `void`. If they are invoked synchronously, however, they need to return a supported data type. – Thales Minussi Mar 19 '19 at 19:41
  • @Thales Yes, but `RequestHandler` forces me to use a return type, which is in the asynchronous case unnecessary. Or is there a different interface to use, with return type `void` (not `Void`) implicit? – ttulka Mar 19 '19 at 20:43
  • Even the name `RequestHandler` doesn’t make much sense as it references *request-response*, a pattern for synchronous communication. But unfortunately I don't see any `EventHandler` or similar... – ttulka Mar 19 '19 at 20:46
  • If I understood you correctly, this is not possible. You must always inform the return type in Java. – Thales Minussi Mar 19 '19 at 20:55
  • @Thales You're probably right. Sad. A `EventHandler` would be really handy... – ttulka Mar 20 '19 at 05:45

1 Answers1

14

You don't need to implement an interface for your Lambda entry point. Your handler class can just be a POJO with a signature that fulfils the requirements explained in the documentation.

For example:

package example;

import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.events.SNSEvent;

public class Hello {
  public void handleEvent(SNSEvent event, Context context) {
    // Process the event
  }
}

In this case you should use example.Hello::handleEvent as the handler configuration.

See also this example from the official docs:

package example;

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

public class Hello {
    public String myHandler(int myCount, Context context) {
        LambdaLogger logger = context.getLogger();
        logger.log("received : " + myCount);
        return String.valueOf(myCount);
    }
}
Ferdinand Beyer
  • 64,979
  • 15
  • 154
  • 145