Java Code Examples for com.amazonaws.services.lambda.runtime.events.SNSEvent#getRecords()

The following examples show how to use com.amazonaws.services.lambda.runtime.events.SNSEvent#getRecords() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SnsServiceImpl.java    From Serverless-Programming-Cookbook with MIT License 6 votes vote down vote up
@Override
public final Boolean processEvent(final SNSEvent event, final String outputQueueURL, final LambdaLogger logger) {

    try {

        logger.log("Number of records in event: " + event.getRecords().size());

        Collection<SendMessageBatchRequestEntry> entries = new ArrayList<>();

        int idVal = 1;
        for (SNSRecord r : event.getRecords()) {
            logger.log("Adding message: " + r.getSNS().getMessage());
            entries.add(new SendMessageBatchRequestEntry("id_" + idVal, r.getSNS().getMessage()));
            idVal++;
        }

        final SendMessageBatchRequest sendBatchRequest = new SendMessageBatchRequest()
                .withQueueUrl(outputQueueURL)
                .withEntries(entries);
        this.sqsClient.sendMessageBatch(sendBatchRequest);

    } catch (Exception e) {
        final String errorMessage = "Error occurred: " + e.getMessage();
        logger.log(errorMessage);
        return false;
    }

    return true;

}
 
Example 2
Source File: Driver.java    From AWS-MIMIC-IIItoOMOP with Apache License 2.0 6 votes vote down vote up
public String lambda(SNSEvent event, Context context)
{
    try
    {   
        List<String> list = new ArrayList<String>();
        
        for(SNSEvent.SNSRecord record : event.getRecords())
        {
            String table = record.getSNS().getMessage().split(" ")[0];
            
            processor = new FileProcessor(table);
            processor.readFiles();
       
            processor.closeConnection();
            
            list.add(table);
        }
        
        return list.toString() + " successfully loaded";
    }
    catch(IOException | ClassNotFoundException | SQLException ex){ return ex.toString(); }
}
 
Example 3
Source File: Lambda.java    From github-bucket with ISC License 5 votes vote down vote up
public Integer handleRequest(SNSEvent event, Context context) {
    try {
        // SNS Events could be possible more than one even if this looks a bit unusual for the deploy case.
        for (SNSEvent.SNSRecord record : event.getRecords()) {
            SNSEvent.SNS sns = record.getSNS();
            // Check SNS header for event type.
            SNSEvent.MessageAttribute attr = sns.getMessageAttributes().get(X_GITHUB_EVENT);
            // Only watch pushes to master.
            if (EVENT_PUSH.equalsIgnoreCase(attr.getValue())) {
                PushPayload value = MAPPER.readValue(sns.getMessage(), PushPayload.class);
                if (config.isWatchedBranch(new Branch(value.getRef()))) {
                    LOG.info(format("Processing '%s' on '%s': '%s'", attr.getValue(), value.getRef(), value.getHeadCommit().getId()));
                    switch (worker.call()) {
                        case SUCCESS:
                            return HttpStatus.SC_OK;
                        case FAILED:
                            return HttpStatus.SC_BAD_REQUEST;
                    }
                }
                // Different branch was found.
                else {
                    LOG.info(format("Push received for: '%s'", value.getRef()));
                }
            }
            // Different event was found.
            else {
                LOG.info(format("Event was: '%s'", attr.getValue()));
            }
        }
    }
    catch (Exception e) {
        LOG.error(e.getMessage(), e);
        return HttpStatus.SC_INTERNAL_SERVER_ERROR;
    }
    return HttpStatus.SC_BAD_REQUEST;
}
 
Example 4
Source File: SnsRequestObjectHandler.java    From jrestless with Apache License 2.0 5 votes vote down vote up
@Override
public Void handleRequest(SNSEvent snsEvent, Context context) {
	for (SNSRecord snsRecord : snsEvent.getRecords()) {
		delegateRequest(new SnsRecordAndLambdaContext(snsRecord, context));
	}
	return null;
}
 
Example 5
Source File: Lambda.java    From github-bucket with ISC License 5 votes vote down vote up
public Integer handleRequest(SNSEvent event, Context context) {
    try {
        // SNS Events could be possible more than one even if this looks a bit unusual for the deploy case.
        for (SNSEvent.SNSRecord record : event.getRecords()) {
            SNSEvent.SNS sns = record.getSNS();
            // Check SNS header for event type.
            SNSEvent.MessageAttribute attr = sns.getMessageAttributes().get(X_GITHUB_EVENT);
            // Only watch pushes to master.
            if (EVENT_PUSH.equalsIgnoreCase(attr.getValue())) {
                PushPayload value = MAPPER.readValue(sns.getMessage(), PushPayload.class);
                if (config.isWatchedBranch(new Branch(value.getRef()))) {
                    LOG.info(format("Processing '%s' on '%s': '%s'", attr.getValue(), value.getRef(), value.getHeadCommit().getId()));
                    switch (worker.call()) {
                        case SUCCESS:
                            return HttpStatus.SC_OK;
                        case FAILED:
                            return HttpStatus.SC_BAD_REQUEST;
                    }
                }
                // Different branch was found.
                else {
                    LOG.info(format("Push received for: '%s'", value.getRef()));
                }
            }
            // Different event was found.
            else {
                LOG.info(format("Event was: '%s'", attr.getValue()));
            }
        }
    }
    catch (Exception e) {
        LOG.error(e.getMessage(), e);
        return HttpStatus.SC_INTERNAL_SERVER_ERROR;
    }
    return HttpStatus.SC_BAD_REQUEST;
}
 
Example 6
Source File: SNSS3Handler.java    From bender with Apache License 2.0 4 votes vote down vote up
@Override
public void handler(SNSEvent event, Context context) throws HandlerException {
  if (!initialized) {
    init(context);
    SNSS3HandlerConfig handlerConfig = (SNSS3HandlerConfig) this.config.getHandlerConfig();
    this.logTrigger = handlerConfig.getLogSnsTrigger();
  }

  this.source = this.sources.get(0);
  this.inputFiles = new ArrayList<String>(0);

  if (this.logTrigger) {
    logger.info("trigger: " + gson.toJson(event));
  }

  for (SNSRecord record : event.getRecords()) {
    /*
     * Parse SNS as a S3 notification
     */
    String json = record.getSNS().getMessage();
    S3EventNotification s3Event = S3EventNotification.parseJson(json);

    /*
     * Validate the S3 file matches the regex
     */
    List<S3EventNotificationRecord> toProcess =
        new ArrayList<S3EventNotificationRecord>(s3Event.getRecords());
    for (S3EventNotificationRecord s3Record : s3Event.getRecords()) {
      String s3Path = String.format("s3://%s/%s", s3Record.getS3().getBucket().getName(),
          s3Record.getS3().getObject().getKey());
      try {
        this.source = SourceUtils.getSource(s3Path, this.sources);
      } catch (SourceNotFoundException e) {
        logger.warn("skipping processing " + s3Path);
        toProcess.remove(s3Record);
      }
    }

    if (toProcess.size() == 0) {
      logger.warn("Nothing to process");
      return;
    }

    this.inputFiles.addAll(toProcess.stream().map(m -> {
      return m.getS3().getObject().getKey();
    }).collect(Collectors.toList()));

    this.recordIterator =
        new S3EventIterator(new LambdaContext(context), toProcess, s3ClientFactory);

    super.process(context);
  }
}