
Handlers in Java
As seen previously, the handler becomes the entry point to the Lambda function. The AWS Lambda functions in Java can have two types of handlers, discussed as follows. Please note that this holds true for any language that runs on the JVM.
Handlers implementing standard interfaces, in this approach, the Lambda function handler implements the standard interfaces that are defined in the aws-lambda-core library. There are two standard interfaces, as follows:
- RequestHandler: The following code block shows how a Lambda function is written when implementing the RequestHandler<I,O>interface. The RequestHandler<I,O> interface has a method definition, as follows:
public O handleRequest(I name, Context context);
The code with this variant for Lambda function with input as a String and output as a String is as follows:
package example;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import com.amazonaws.services.lambda.runtime.Context;
public class Hello implements RequestHandler<String, String> {
public String handleRequest(String name, Context context) {
String greetingString = String.format("Hello %s.", name);
return greetingString;
}
}
The function takes in a String, and returns a greeting with Hello prefixed.
RequestHandler is the preferred interface to implement when there are POJOs or primitives as input or output.
- RequestStreamHandler: The following code block shows how a Lambda function is written when implementing the RequestStreamHandler interface.
The RequestStreamHandler has a method definition, as follows:
public void handleRequest(InputStream inputStream, OutputStream outputStream, Context context)throws IOException;
The code for such a variant is as follows:
package example;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import com.amazonaws.services.lambda.runtime.RequestStreamHandler;
import com.amazonaws.services.lambda.runtime.Context;
public class Hello implements RequestStreamHandler {
public void handleRequest(InputStream inputStream, OutputStream outputStream, Context context)
throws IOException {
int letter;
while((letter = inputStream.read()) != -1)
{
outputStream.write(Character.toUpperCase(letter));
}
}
}
The preceding code is taken from the official AWS Lambda documentation. RequestStreamHandler is the preferred interface to implement when the input and output are best handled as streams.
- Custom Handler: If the developer chooses not to use the AWS SDKs provided as standard building blocks for Lambda, they can create a handler function without having the enclosing class implement an interface.
The following code block shows a Lambda function ingesting a name and returning a greeting that is created without implementing the predefined interfaces, as shown in the previous section:
package example;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.LambdaLogger;
public class Hello {
public String myHandler(String name, Context context) {
LambdaLogger logger = context.getLogger();
logger.log("Going to greet : " + name);
return String.format("Hello %s ",name);
}
}
There are slight differences between the two approaches, which we will explore when we create the deployment package.