替换AWS Lambda invokeAsync(不建议使用) [英] Replacement for AWS Lambda invokeAsync (deprecated)
问题描述
我正在尝试从另一个Java Lambda函数内部异步调用Java Lambda函数.我只是想触发并忘记,但是使用.invokeAsync(InvokeRequest),我必须在Future上调用.get(),然后阻止并中断触发并忘记"用例.
I am trying to call a Java Lambda function asynchronously from within another Java Lambda function. I simply want to fire and forget, but with .invokeAsync(InvokeRequest), I have to call .get() on the Future, which then blocks and breaks the 'fire and forget' use case.
这是我要使用的代码:
private void sendToDownloader(String payload) throws InterruptedException, ExecutionException {
log.info(payload);
InvokeRequest invoke = new InvokeRequest();
invoke.withFunctionName("LambdaTwo")
.withPayload(payload)
.withInvocationType(InvocationType.Event);
lambdaClient.invokeAsync(invoke).get();
}
如果我删除了.get()方法调用,则它实际上不会调用LambdaTwo.
If I remove the .get() method call, it does not actually invoke LambdaTwo.
应注意,此lambda函数在调用LambdaTwo后立即结束.
It should be noted, this lambda function ends immediately after invoking LambdaTwo.
同时,我尝试使用以下代码:
Meanwhile, I tried using the following code:
private void sendToDownloader(String payload) throws InterruptedException, ExecutionException {
log.info(payload);
InvokeAsyncRequest invoke = new InvokeAsyncRequest();
invoke.withFunctionName("LambdaTwo")
.withInvokeArgs(payload);
lambdaClient.invokeAsync(invoke);
}
此代码有效,但是.invokeAsync已过时,并且显示要查看"Invoke API",对此我找不到任何文档.
This code works, however .invokeAsync is deprecated and it says to see "Invoke API", for which I cannot find any documentation.
有人能引导我以一种纯粹的即发即弃"方式调用Lambda函数的正确方法吗?
Can anyone lead me to the right way to invoke a Lambda function in a purely 'fire and forget' fashion?
编辑1 根据Mark的建议,我重写了下面的代码,但是在执行LambdaTwo函数时似乎阻塞了.
EDIT 1 Per Mark's suggestion, I have rewritten the code below, however it seems to block while the LambdaTwo function executes.
public void routeEvent(String lambdaName, String cacheName) throws InterruptedException, ExecutionException, JsonProcessingException {
ObjectNode obj = new ObjectNode(JsonNodeFactory.instance);
obj.put("nodeName", cacheName);
InvokeRequest invoke = new InvokeRequest();
invoke.withFunctionName(lambdaName)
.withInvocationType(InvocationType.Event)
.withPayload(OBJECT_MAPPER.writeValueAsString(obj));
log.info(System.currentTimeMillis());
lambdaClient.invoke(invoke);
log.info(System.currentTimeMillis());
}
它将打印以下毫秒.
2016-11-28 03:41:35信息LambdaFunction:97-1480304495867
2016-11-28 03:41:41信息LambdaFunction:99-1480304501432
2016-11-28 03:41:35 INFO LambdaFunction:97 - 1480304495867
2016-11-28 03:41:41 INFO LambdaFunction:99 - 1480304501432
两者之间相差约5.5秒,Lambda持续时间证实了这一点.
There is a difference of about 5.5 seconds between the two and the Lambda duration confirms this.
编辑2 为了澄清起见,我正在尝试调用.invokeAsync(InvokeRequest)和.invoke(InvokeRequest)的AWSLambdaAsyncClient,但都没有用.调用invokeAsync时,除非我在其上调用.get(),否则它实际上不会调用Lambda函数,该函数随后在第二个Lambda函数执行时阻塞.
EDIT 2 Just to clarify, I was trying the AWSLambdaAsyncClient calling the .invokeAsync(InvokeRequest) and .invoke(InvokeRequest) and neither worked. When calling invokeAsync, it does not actually invoke the Lambda function unless I call .get() on it, which then blocks while the second Lambda function executes.
最终有效的方法是调用.invokeAsync(InvokeRequest),但是不对InvokeRequest对象调用.withInvocationType(InvocationType.Event).尚不清楚为什么会导致异步行为,但这是可行的,并且未使用不推荐使用的方法.
What finally worked was calling .invokeAsync(InvokeRequest), however NOT calling .withInvocationType(InvocationType.Event) on the InvokeRequest object. It's not clear why that caused the asynchronous behavior, but that's what worked and not using a deprecated method.
public void routeEventAsync2(String lambdaName, String cacheName) throws InterruptedException, ExecutionException, JsonProcessingException {
ObjectNode obj = new ObjectNode(JsonNodeFactory.instance);
obj.put("nodeName", cacheName);
InvokeRequest invoke = new InvokeRequest();
invoke.withFunctionName(lambdaName)
.withPayload(OBJECT_MAPPER.writeValueAsString(obj));
lambdaAsyncClient.invokeAsync(invoke);
}
推荐答案
有关弃用此方法的文档令人困惑.他们试图说的是使用AWSLambdaClient.invoke(request)
.您需要在请求对象上将InvocationType
设置为Event
,以便在不等待响应的情况下调用Lambda函数.
The documentation regarding the deprecation of this method is confusing. What they are trying to say is to use AWSLambdaClient.invoke(request)
. You will need to set the InvocationType
to Event
on the request object in order to invoke the Lambda function without waiting for a response.
这篇关于替换AWS Lambda invokeAsync(不建议使用)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!