简体   繁体   English

带有 SNS 的 Lambda -> SQS AWS

[英]Lambda with SNS -> SQS AWS

Im have a aws lambda which will be triggered from SNS -> SQS.我有一个 aws lambda,它将从 SNS -> SQS 触发。 It means, I have a SNS and then SQS is a subcriber.这意味着,我有一个 SNS,然后 SQS 是一个订阅者。 Then I have a lambda triggered by SQS.然后我有一个由 SQS 触发的 lambda。

I have build a simple Lambda as below我已经构建了一个简单的 Lambda 如下

public class EnhancedCanonicalLambdaHandler implements RequestHandler<SQSEvent, String> {
    static final Logger log = LogManager.getLogger(EnhancedCanonicalLambdaHandler.class);
    static final ObjectMapper objectMapper = new ObjectMapper();

    @Override
    public String handleRequest(SQSEvent sqsEvent, Context context) {
        log.info("There are {} records to be processing ...", sqsEvent.getRecords().size());

        for(SQSEvent.SQSMessage msg : sqsEvent.getRecords()) {
// I want to map msg.getBody()
        }
    }

And the msg.getBody will have json like below并且 msg.getBody 将具有如下所示的 json

{
   "Type":"Notification",
   "MessageId":"d3056919-1db0-5bcb-b5ce-3df0eb234dd9",
   "TopicArn":"arn:aws:sns:us-east-2:enhance-canonical-topic",
   "Subject":"nghia do subject",
   "Message":"{\n\"groupRuleSourceLocation\" : \"groupRuleSourceLocation\",\n\"canonicalSourceLocation\": \"canonicalSourceLocation\"\n}",
   "Timestamp":"2019-02-22T13:08:35.147Z",
   "SignatureVersion":"1",
   "SigningCertURL":"https://sns.us-east-2.amazonaws.com/.pem",
   "UnsubscribeURL":"https://sns.us-east-2.amazonaws.com/?Action=Unsubscribe&SubscriptionArn=arn:aws:sns:us-east-2:057351434671:enhance-canonical-topic:",
   "MessageAttributes":{}
}

I would like to see if AWS they have anyway to map this body message to an Pojo defined already?我想看看 AWS 是否有能力将此正文消息映射到已定义的 Pojo? or we need to use json or mapper to map to an object we define?或者我们需要使用 json 或 mapper 来映射到我们定义的对象?

Thanks,谢谢,

You'll have to map it yourself or use mappers (like you mentioned) such as Gson or Jackson您必须自己映射或使用映射器(如您所提到的),例如 Gson 或 Jackson

Here's an example of how you can achieve this using Gson:以下是如何使用 Gson 实现此目的的示例:

Gson gson = new Gson();
String snsOrSqsMessageBody = "{\r\n   \"Type\":\"Notification\",\r\n   \"MessageId\":\"d3056919-1db0-5bcb-b5ce-3df0eb234dd9\",\r\n   \"TopicArn\":\"arn:aws:sns:us-east-2:enhance-canonical-topic\",\r\n   \"Subject\":\"nghia do subject\",\r\n   \"Message\":\"{\\n\\\"groupRuleSourceLocation\\\" : \\\"groupRuleSourceLocation\\\",\\n\\\"canonicalSourceLocation\\\": \\\"canonicalSourceLocation\\\"\\n}\",\r\n   \"Timestamp\":\"2019-02-22T13:08:35.147Z\",\r\n   \"SignatureVersion\":\"1\",\r\n   \"SigningCertURL\":\"https:\/\/sns.us-east-2.amazonaws.com\/.pem\",\r\n   \"UnsubscribeURL\":\"https:\/\/sns.us-east-2.amazonaws.com\/?Action=Unsubscribe&SubscriptionArn=arn:aws:sns:us-east-2:057351434671:enhance-canonical-topic:\",\r\n   \"MessageAttributes\":{}\r\n}";
MyCustomClass myCustomObject = gson.fromJson(snsOrSqsMessageBody, MyCustomClass.class);

Just for the sake of curiosity: what role is SQS playing in your application?只是出于好奇:SQS 在您的应用程序中扮演什么角色? You could simply subscribe your Lambda to your SNS topic, removing one layer of complexicity in your architecture.您可以简单地将您的 Lambda 订阅到您的 SNS 主题,从而消除您架构中的一层复杂性。

Hope this helps!希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM