默认情况下,Micronaut的处理程序是类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();
}
}
}
正在返回空的自定义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创建我的自定义处理程序作为Native Image?或者我真的只限于使用默认的MicronautLambdaHandler
?
能够使其工作。我需要明确地放置依赖项implementation("io.micronaut.aws:micronaut-function-aws")
implementation("io.micronaut.aws:micronaut-function-aws-custom-runtime")
我不明白的是,即使没有在build.gradle上指定,这些工件也已经存在于类路径中,这就是为什么我从来没有把它们放进去。