默认情况下,Micronaut 的处理程序是 class MicronautLambdaHandler
。但是在 Micronaut 指南上它说
您将与之交互的主要 API 是 AbstractMicronautLambdaRuntime。一个抽象类,您可以扩展它来创建您的自定义运行时 mainClass。
所以我尝试创建一个自定义处理程序和运行时,基本上复制现有的MicronautLambdaHandler
和MicronautLambdaRuntime
自定义 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();
}
}
}
仅返回 null 的自定义 MyHandler
@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();
}
}
然后在我的 Lambda 函数上,我将方法处理程序设置为example.micronaut.MyHandler
,运行时设置为Custom Runtime on Amazon Linux 2
在测试我的 Lambda 时,它不会读取我的自定义处理程序。它始终运行默认值MicronautLambdaHandler
。
此外,奇怪的是,即使我将方法处理程序设置为一些随机单词,如this.is.not.a.handler
,它仍然MicronautLambdaHandler
默认运行。
有没有办法按照指南的建议为 Micronaut Lambda 创建我的自定义处理程序作为本机图像?还是我真的仅限于使用默认值MicronautLambdaHandler
?