Amazon web services SNSEvent对象';s getRecords()返回null
我创建并部署了一个用Java编写的简单Lambda函数。我似乎有几个问题 首先,SNSEvent对象的getRecords()方法调用返回null,我不知道为什么。我试着在SNS内部进行测试,但由于第二个问题,这不起作用 其次,Cloudwatch似乎没有从Lambda函数接收任何日志。如果没有Cloudwatch日志,我在SNS测试时无法看到任何日志 下面是Lambda函数的相关代码Amazon web services SNSEvent对象';s getRecords()返回null,amazon-web-services,aws-sdk,amazon-sns,aws-lambda,Amazon Web Services,Aws Sdk,Amazon Sns,Aws Lambda,我创建并部署了一个用Java编写的简单Lambda函数。我似乎有几个问题 首先,SNSEvent对象的getRecords()方法调用返回null,我不知道为什么。我试着在SNS内部进行测试,但由于第二个问题,这不起作用 其次,Cloudwatch似乎没有从Lambda函数接收任何日志。如果没有Cloudwatch日志,我在SNS测试时无法看到任何日志 下面是Lambda函数的相关代码 public void articleHandler(SNSEvent articleMsg, Context
public void articleHandler(SNSEvent articleMsg, Context context) {
LambdaLogger logger = context.getLogger();
if (articleMsg.getRecords() == null) {
logger.log("Getrecords is null");
System.exit(1);
}
for (SNSEvent.SNSRecord msg : articleMsg.getRecords()) {
try {
logger.log("Getting headline records");
JsonObject headline = getMessageAsJsonObject(msg.getSNS().getMessage());
if (headline.has("link") && headline.get("link").isJsonPrimitive() && headline.get("link").getAsJsonPrimitive().isString()) {
headline.addProperty("body", getArticleBody(headline.get("link").getAsString()));
logger.log(headline.toString());
} else {
throw new InvalidJSONException("\"link\" field either doesn't exist or is not a String primitive", headline.toString());
}
} catch (InvalidJSONException ex) {
logger.log(ex.toString());
} catch (IOException | SAXException | TikaException ex) {
logger.log(ex.getMessage());
}
}
}
<dependencies>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-core</artifactId>
<version>1.0.0</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-events</artifactId>
<version>1.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.tika</groupId>
<artifactId>tika-parsers</artifactId>
<version>1.11</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.5</version>
<type>jar</type>
</dependency>
</dependencies>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.3</version>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
下面是我的pom文件。我尝试将aws lambda java core和aws lambda java events设置为相同的版本,但这并没有改变任何东西,我将其放回1.0.0和1.1.0版本,因为这是在不同的lambda函数中工作的
public void articleHandler(SNSEvent articleMsg, Context context) {
LambdaLogger logger = context.getLogger();
if (articleMsg.getRecords() == null) {
logger.log("Getrecords is null");
System.exit(1);
}
for (SNSEvent.SNSRecord msg : articleMsg.getRecords()) {
try {
logger.log("Getting headline records");
JsonObject headline = getMessageAsJsonObject(msg.getSNS().getMessage());
if (headline.has("link") && headline.get("link").isJsonPrimitive() && headline.get("link").getAsJsonPrimitive().isString()) {
headline.addProperty("body", getArticleBody(headline.get("link").getAsString()));
logger.log(headline.toString());
} else {
throw new InvalidJSONException("\"link\" field either doesn't exist or is not a String primitive", headline.toString());
}
} catch (InvalidJSONException ex) {
logger.log(ex.toString());
} catch (IOException | SAXException | TikaException ex) {
logger.log(ex.getMessage());
}
}
}
<dependencies>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-core</artifactId>
<version>1.0.0</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-events</artifactId>
<version>1.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.tika</groupId>
<artifactId>tika-parsers</artifactId>
<version>1.11</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.5</version>
<type>jar</type>
</dependency>
</dependencies>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.3</version>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
由于Cloudwatch没有报告任何日志,我只能看到Lambda控制台报告的内容:
{
"errorMessage": "Process exited before completing request"
}
如果我从函数中删除If(articleMsg.getRecords()==null)语句,则在Lambda控制台中会出现以下错误
{ "errorMessage": "java.lang.NullPointerException", "errorType": "java.lang.NullPointerException", "stackTrace": [
"com.myapp.articlehandler.LambdaHandler.articleHandler(LambdaHandler.java:35)",
"sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)",
"sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)",
"sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)",
"java.lang.reflect.Method.invoke(Method.java:497)"]
}
LambdaHandler.java中的第35行(上面粘贴的代码)是指:
for (SNSEvent.SNSRecord msg : articleMsg.getRecords()) {
最后,当我单击错误“Execution result:failed(logs)”中的链接以查看Cloudwatch日志时,Cloudwatch会打开并给出以下错误,并且不会显示此Lambda函数的任何日志:
加载日志流时出错。请通过刷新重试
这一页
这两个问题似乎并不相关,尽管我不能确定
有人对其中一个/两个问题都有什么建议吗?我在为迪纳摩触发lambdas时遇到了同样的问题db@Tisha对不起,我不记得是怎么回事了。我应该发布一篇后续文章……我能说的是,通常当我遇到这样的问题时,这是因为发送到Lambda函数的JSON有问题。我在为dynamo触发lambdas时也面临同样的问题db@Tisha对不起,我不记得是怎么回事了。我应该发布一篇后续文章……我能说的是,通常当我遇到这样的问题时,这是因为发送到Lambda函数的JSON有问题。