SNSEvent对象的getRecords()返回null

时间:2016-01-31 19:33:22

标签: 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 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());
        }
    }

}

下面是我的pom文件。我尝试将aws-lambda-java-core和aws-lambda-java-events设为相同的版本,但这并没有改变任何东西,我把它放回到1.0.0和1.1.0版本,因为这是有用的一个不同的Lambda函数。

<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>

我一直在使用以下JSON作为测试(从Lambda内部)调用此Lambda函数,我直接从SNS Json Generator获得。

{
  "default": "{\"id\":1636,\"unread\":true,\"marked\":false,\"published\":false,\"updated\":1454242851,\"is_updated\":false,\"title\":\"Dubai tourism allure undiminished by hotel fire: tourism chief\",\"link\":\"http://feeds.reuters.com/~r/Reuters/worldNews/~3/aOd7O2wZCwc/story01.htm\",\"feed_id\":\"3\",\"tags\":[\"worldnews\"],\"excerpt\":\"&hellip;\",\"labels\":[],\"feed_title\":\"Reuters: World News\",\"comments_count\":0,\"comments_link\":\"\",\"always_display_attachments\":false,\"author\":\"\",\"score\":0,\"note\":null,\"lang\":\"en\"}",
  "lambda": "{\"id\":1636,\"unread\":true,\"marked\":false,\"published\":false,\"updated\":1454242851,\"is_updated\":false,\"title\":\"Dubai tourism allure undiminished by hotel fire: tourism chief\",\"link\":\"http://feeds.reuters.com/~r/Reuters/worldNews/~3/aOd7O2wZCwc/story01.htm\",\"feed_id\":\"3\",\"tags\":[\"worldnews\"],\"excerpt\":\"&hellip;\",\"labels\":[],\"feed_title\":\"Reuters: World News\",\"comments_count\":0,\"comments_link\":\"\",\"always_display_attachments\":false,\"author\":\"\",\"score\":0,\"note\":null,\"lang\":\"en\"}"
}

由于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()) {

最后,当我点击错误“执行结果:失败(日志)”中的链接以查看Cloudwatch日志时,Cloudwatch会打开并给出以下错误,并且不显示此Lambda函数的任何日志:

  

加载Log Streams时出错。请刷新再试一次   这个页面。

这两个问题似乎并不相关,但我无法确定。

有没有人对这两个问题都有任何建议?

0 个答案:

没有答案