By default, Micronaut's handler is the class MicronautLambdaHandler
. But on the Micronaut guide it says that
The main API you will interact with is AbstractMicronautLambdaRuntime. An abstract class which you can extend to create your custom runtime mainClass.
So I tried creating a custom handler and runtime, basically copying the existing MicronautLambdaHandler
and MicronautLambdaRuntime
Custom MyRuntime
public class MyRuntime extends AbstractMicronautLambdaRuntime<AwsProxyRequest, AwsProxyResponse, AwsProxyRequest, AwsProxyResponse> {
@Override
protected RequestHandler<AwsProxyRequest, AwsProxyResponse> createRequestHandler(String... args) {
try {
return new MyHandler(createApplicationContextBuilderWithArgs(args));
} catch (ContainerInitializationException e) {
throw new ConfigurationException("Exception thrown instantiating MyHandler", e);
}
}
public static void main(String[] args) {
try {
new MyRuntime().run(args);
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
}
Custom MyHandler that is just returning null
@Introspected
public class MyHandler implements RequestHandler<AwsProxyRequest, AwsProxyResponse>,
ApplicationContextProvider, Closeable {
protected final MicronautLambdaContainerHandler handler;
public MyHandler() throws ContainerInitializationException {
this.handler = new MicronautLambdaContainerHandler();
}
public MyHandler(ApplicationContextBuilder applicationContextBuilder) throws ContainerInitializationException {
this.handler = new MicronautLambdaContainerHandler(applicationContextBuilder);
}
public MyHandler(ApplicationContext applicationContext) throws ContainerInitializationException {
this.handler = new MicronautLambdaContainerHandler(applicationContext);
}
@Override
public AwsProxyResponse handleRequest(AwsProxyRequest input, Context context) {
System.out.println("hello custom handler");
System.out.println(context.getAwsRequestId());
return null;
}
@Override
public ApplicationContext getApplicationContext() {
return this.handler.getApplicationContext();
}
@Override
public void close() {
this.getApplicationContext().close();
}
}
Then on my Lambda Function I set the method handler to example.micronaut.MyHandler
and the runtime to Custom Runtime on Amazon Linux 2
When testing my Lambda it doesn't read my custom handler. It always run the default MicronautLambdaHandler
.
Also, what's weird is that even if I set the method handler to some random words like this.is.not.a.handler
, it still run the MicronautLambdaHandler
by default.
Is there a way to create my custom handler for Micronaut Lambda as Native Image as the guide suggests? Or I'm just really limited to using the default MicronautLambdaHandler
?