AWS Lambda:如何从简单的java类调用lambda函数

编程入门 行业动态 更新时间:2024-10-10 19:19:33
本文介绍了AWS Lambda:如何从简单的java类调用lambda函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我创建了简单的Lambda函数,并且上传到AWS Lambda.

I have created simple Lambda function and upload this to AWS Lambda.

import com.amazonaws.services.lambda.runtime.Context; import com.amazonaws.services.lambda.runtime.RequestHandler; public class Hello implements RequestHandler<String, String> { @Override public String handleRequest(String input, Context context) { String output = "Bonjour, " + input + "!"; return output; } } }

我想使用Java类从其他项目中调用此Lambda函数.我正在使用aws-java-sdk-lambda-1.10.22调用该函数.但是我无法做到这一点.

I want to invoke this Lambda function from some other project using java class. I am using aws-java-sdk-lambda-1.10.22 to call the function. But I am not able to succeed in that.

这是我的InvokeLambda类,它是一个单独的项目.

Here is my InvokeLambda class which is a separate project.

import java.nio.ByteBuffer; import java.nio.charset.Charset; import org.apachemons.logging.Log; import org.apachemons.logging.LogFactory; import com.amazonaws.auth.AWSCredentials; import com.amazonaws.auth.BasicAWSCredentials; import com.amazonaws.regions.Region; import com.amazonaws.regions.Regions; import com.amazonaws.services.lambda.AWSLambdaClient; import com.amazonaws.services.lambda.model.InvokeRequest; public class InvokeLambda { private static final Log logger = LogFactory.getLog(InvokeLambda.class); private static final String awsAccessKeyId = "XXXXXX"; private static final String awsSecretAccessKey = "YYYY"; private static final String regionName = "us-west-2"; private static final String functionName = "Hello"; private static Region region; private static AWSCredentials credentials; private static AWSLambdaClient lambdaClient; /** * The entry point into the AWS lambda function. */ public static void main(String... args) { credentials = new BasicAWSCredentials(awsAccessKeyId, awsSecretAccessKey); lambdaClient = (credentials == null) ? new AWSLambdaClient() : new AWSLambdaClient(credentials); //lambdaClient.configureRegion(Regions.US_WEST_2); region = Region.getRegion(Regions.fromName(regionName)); lambdaClient.setRegion(region); try { InvokeRequest invokeRequest = new InvokeRequest(); invokeRequest.setFunctionName(functionName); invokeRequest.setPayload("\" AWS Lambda\""); System.out.println(byteBufferToString( lambdaClient.invoke(invokeRequest).getPayload(), Charset.forName("UTF-8"))); } catch (Exception e) { logger.error(e.getMessage()); // System.out.println(e.getMessage()); } } public static String byteBufferToString(ByteBuffer buffer, Charset charset) { byte[] bytes; if (buffer.hasArray()) { bytes = buffer.array(); } else { bytes = new byte[buffer.remaining()]; buffer.get(bytes); } return new String(bytes, charset); } }

如何使用Java调用lambda函数?

How to call the lambda function using java?

推荐答案

给出注释中的信息,调用该函数的客户端代码就可以了.问题似乎出在功能本身的配置上.具体来说,AWS Lambda无法找到您指定的处理程序(com.aws.HelloLambda::handleRequest),因为它与处理程序类(Hello)的名称和包以及该类中方法的名称(handleRequest).

Given the information in your comment, your client code to invoke the function is fine. The problem appears to be with the configuration of the function itself. Specifically, AWS Lambda is not able to find the handler you've specified (com.aws.HelloLambda::handleRequest) because that doesn't match the name and package of your handler class (Hello) and the name of your method in that class (handleRequest).

您可以通过AWS控制台更新功能处理程序名称.选择您的函数,然后选择配置"选项卡,然后选择处理程序"属性.

You can update the function handler name through the AWS Console. Choose your function, then the configuration tab and then the Handler property.

您可能希望将其从com.aws.HelloLambda::handleRequest更改为Hello::handleRequest.

You probably want to change it from com.aws.HelloLambda::handleRequest to Hello::handleRequest.

在通过Java客户端测试该功能之前,可以直接通过控制台对其进行测试,这将帮助您确保正确配置该功能.

Before testing the function from your Java client, you could test it directly through the console, this will help you ensure the function is configured correctly.

更多推荐

AWS Lambda:如何从简单的java类调用lambda函数

本文发布于:2023-11-28 02:56:53,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1640695.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   简单   Lambda   AWS   java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!