gpt4 book ai didi

java - java中如何将参数从一个lambda传递到另一个?

转载 作者:行者123 更新时间:2023-11-30 05:26:38 31 4
gpt4 key购买 nike

我想使用一些参数从另一个 lambda 函数 B 调用 Lambda 函数 A。

以下是调用 lambda 函数。

@SpringBootApplication
public class Application extends SpringBootServletInitializer implements CommandLineRunner {
@Autowired
private ConfigurableApplicationContext context;
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}

@Override
public void run(String... args) {
DCService dcService = LambdaInvokerFactory.builder().lambdaFunctionNameResolver(
(method, lambdaFunction, lambdaInvokerFactoryConfig) -> "EventPlanDCFunction-Dev")
.build(DCService.class);
log.info("Response from DC service :: {}",dcService.getClass());
String[] params = new String[]{"Subir has invoked"};
dcService.run(params);
SpringApplication.exit(context);
}
}

以下是DCService.java文件的代码。

public interface DCService {
@LambdaFunction(functionName = "DeliveryCycleLambdaHandler",
invocationType = InvocationType.Event)
void run(String... params);
}

以下是我要调用的 lambda 函数的代码。

@SpringBootApplication
public class Application extends SpringBootServletInitializer implements CommandLineRunner {
@Autowired
private ConfigurableApplicationContext context;
@Autowired
private DeliveryCycleService deliveryCycleService;
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}

@Override
public void run(String... args) {
deliveryCycleService.printMessage(args[0]);
SpringApplication.exit(context);
}
}

如您所见,我尝试通过从调用方法创建字符串数组来传递参数,但在另一个方法中收到 ArrayOutOFBoundException ,这意味着参数实际上并未到达调用的方法。如果我不传递参数,它可以正常工作,但对于我的用例,我需要传递参数并异步调用该方法。

注意:两者的 lambdaHandle 代码是相同的。以下属于其中之一。

@Slf4j
public class DCInvokeHandler implements RequestStreamHandler {
private static final Logger LOGGER = LoggerFactory.getLogger(DCInvokeHandler.class);
private volatile SpringBootLambdaContainerHandler<AwsProxyRequest, AwsProxyResponse> handler;

@Override
public void handleRequest(InputStream inputStream, OutputStream outputStream, Context context) throws IOException {
if (handler == null) {
synchronized (this) {
if (handler == null) {
handler = initHandler();
}
}
}

handler.proxyStream(inputStream, outputStream, context);
}

private static SpringBootLambdaContainerHandler<AwsProxyRequest, AwsProxyResponse> initHandler() {
try {
return SpringBootLambdaContainerHandler.getAwsProxyHandler(Application.class, Env.getEnv().name());
} catch (ContainerInitializationException e) {
LOGGER.error("Failed to start spring boot lambda handler", e);
// if we fail here. We re-throw the exception to force another cold start
throw new IllegalStateException("Could not initialize Spring Boot application", e);
}
}
}

最佳答案

  • 这是从 lambda 函数调用另一个 lambda 的基本代码。 aws sdk doc
try {


InvokeRequest invokeRequest = new InvokeRequest();
invokeRequest.setFunctionName(FunctionName);
invokeRequest.setPayload(ipInput);


returnDetails = byteBufferToString(
lambdaClient.invoke(invokeRequest).getPayload(),
Charset.forName("UTF-8"),logger);
} catch (Exception e) {

logger.log(e.getMessage());
}
  • 要异步调用另一个 lambda 函数,请将 InitationType 设置为 Eventaws api docs

  • 以下是调用类型RequestResponse、Event、DryRun。

  • RequestResponse(默认)- 同步调用该函数。保持连接打开,直到函数返回响应或超时。 API响应包括函数响应和附加数据。

  • 事件 - 异步调用函数。将多次失败的事件发送到函数的死信队列(如果已配置)。 API 响应仅包含状态代码。

  • DryRun - 验证参数值并验证用户或角色是否有权调用该函数。

关于java - java中如何将参数从一个lambda传递到另一个?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58441507/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com