com.amazonaws.services.lambda.runtime.events.SQSEvent Java Examples

The following examples show how to use com.amazonaws.services.lambda.runtime.events.SQSEvent. 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: Handler.java    From aws-doc-sdk-examples with Apache License 2.0 6 votes vote down vote up
@Override
public String handleRequest(SQSEvent event, Context context)
{
  String response = new String();
  // call Lambda API
  logger.info("Getting account settings");
  CompletableFuture<GetAccountSettingsResponse> accountSettings = 
      lambdaClient.getAccountSettings(GetAccountSettingsRequest.builder().build());
  // log execution details
  logger.info("ENVIRONMENT VARIABLES: {}", gson.toJson(System.getenv()));
  logger.info("CONTEXT: {}", gson.toJson(context));
  logger.info("EVENT: {}", gson.toJson(event));
  // process event
  for(SQSMessage msg : event.getRecords()){
    logger.info(msg.getBody());
  }
  // process Lambda API response
  try {
    GetAccountSettingsResponse settings = accountSettings.get();
    response = gson.toJson(settings.accountUsage());
    logger.info("Account usage: {}", response);
  } catch(Exception e) {
    e.getStackTrace();
  }
  return response;
}
 
Example #2
Source File: SqsServiceImpl.java    From Serverless-Programming-Cookbook with MIT License 5 votes vote down vote up
@Override
public final Boolean processEvent(final SQSEvent event, final String outputQueueURL, final LambdaLogger logger) {

    try {

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

        logger.log("Output Queue URL: " + outputQueueURL);

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

        int idVal = 1;
        for (SQSMessage m : event.getRecords()) {
            logger.log("Adding message: " + m.getBody());
            entries.add(new SendMessageBatchRequestEntry("id_" + idVal, m.getBody()));
            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 #3
Source File: LambdaSqsEventHandler.java    From Serverless-Programming-Cookbook with MIT License 5 votes vote down vote up
/**
 * Handle request.
 *
 * @param sqsEvent  - SQS Event passed as input to lambda handler
 * @param context - context object
 * @return true if success, else false.
 */
public Boolean handleRequest(final SQSEvent sqsEvent, final Context context) {
    context.getLogger().log("Received SQS event: " + sqsEvent);

    final SqsService sqsService =  new SqsServiceImpl(this.sqsClient);
    // It is a good practice to prefix environment variables with a project specific prefix.
    // E.g. SPC is a prefix that denote Serverless Programming Cookbook.
    return sqsService.processEvent(sqsEvent, System.getenv("SPC_OUTPUT_QUEUE_URL"), context.getLogger());

}
 
Example #4
Source File: SQSEventDeserializer.java    From aws-doc-sdk-examples with Apache License 2.0 5 votes vote down vote up
@Override
public SQSEvent deserialize(JsonElement eventJson, Type typeOfT, JsonDeserializationContext context)
  throws JsonParseException {
    SQSEvent event = new SQSEvent();
    logger.info("DESERIALIZING TEST EVENT");
    logger.info("EVENT JSON: " + eventJson.toString());
    // Records key is capitalized in test event, but lowercase in type
    JsonArray recordsArray = eventJson.getAsJsonObject().get("Records").getAsJsonArray();
    ArrayList<SQSMessage> records = gson.fromJson(recordsArray, sqsMessageArray);
    event.setRecords(records);
    return event;
}
 
Example #5
Source File: InvokeTest.java    From aws-doc-sdk-examples with Apache License 2.0 5 votes vote down vote up
@Test
void invokeTest() {
  AWSXRay.beginSegment("blank-java-test");
  String path = "src/test/resources/event.json";
  String eventString = loadJsonFile(path);
  SQSEvent event = gson.fromJson(eventString, SQSEvent.class);
  Context context = new TestContext();
  String requestId = context.getAwsRequestId();
  Handler handler = new Handler();
  String result = handler.handleRequest(event, context);
  assertTrue(result.contains("totalCodeSize"));
  AWSXRay.endSegment();
}
 
Example #6
Source File: FunctionInvokerTests.java    From spring-cloud-function with Apache License 2.0 5 votes vote down vote up
@Bean
public Function<SQSEvent, String> inputSQSEvent() {
	return v -> {
		System.out.println("Received: " + v);
		return v.toString();
	};
}
 
Example #7
Source File: FunctionInvokerTests.java    From spring-cloud-function with Apache License 2.0 5 votes vote down vote up
@Bean
public Function<Message<SQSEvent>, String> inputSQSEventAsMessage() {
	return v -> {
		System.out.println("Received: " + v);
		return v.toString();
	};
}
 
Example #8
Source File: SqsService.java    From Serverless-Programming-Cookbook with MIT License 2 votes vote down vote up
/**
 * Process SQSEvent.
 * @param event SQS event received.
 * @param outputQueueURL Output queue URL.
 * @param logger Lambda Logger from context.
 * @return Response domain object.
 */
Boolean processEvent(SQSEvent event, String outputQueueURL, LambdaLogger logger);