Java – replacement of AWS lambda invokeasync (not recommended)
I tried to call a Java lambda function asynchronously from another Java lambda function I just want to fire and forget, but use Invokeasync (invokerequest), which I must call on future Get (), and then block and break the 'fire and forget' use case
This is the code I tried to use:
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(); }
If I delete The get () method is called, and it doesn't actually call lambdatwo
It should be noted that this lambda function ends immediately after calling lambdatwo
At the same time, I try to use 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); }
But this code is very useful Not recommended Invokeasync and see "invoke API", I can't find any documents
Can anyone guide me to call lambda functions in the right way in a pure "fire and unforgettable" way?
Edit 1 according to mark's suggestion, I have rewritten the following code, but it seems to block the execution of lambdatwo function
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()); }
It prints out the following milliseconds
There is a difference of about 5.5 seconds between the two, and the lambda duration confirms this
Edit 2 just to clarify, I'm trying to call Invokeasync (invokerequest) and The AWS lambdaasyncclient of invoke (invokerequest) does not work When invokeasync is called, it doesn't actually call the lambda function unless I call on it Get() and block when the second lambda function executes
The final job is to call Invokeasync (invokerequest), but do not call on the invokerequest object withInvocationType(InvocationType.Event). It is not clear why this leads to asynchronous behavior, but it is effective instead of using the deprecated method
public void routeEventAsync2(String lambdaName,cacheName); InvokeRequest invoke = new InvokeRequest(); invoke.withFunctionName(lambdaName) .withPayload(OBJECT_MAPPER.writeValueAsString(obj)); lambdaAsyncClient.invokeAsync(invoke); }
Solution
The documentation on deprecating this method is confusing What they are trying to say is to use AWS lambdaclient Invoke You need to set invocationtype to event on the request object to call the lambda function without waiting for a response