Lambda project code (Java)

Since we have already discussed Lambdas in detail earlier, I will show only important parts of the code and step details here. I will also not show error handling and other supportive code. Please refer to the code files for the complete code. 

We will create a Lambda that gets triggered when messages are put into an SQS queue, and it will then send the message to another as a batch. 

The Maven pom.xml file of the project should also define the following dependency:

<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-sqs</artifactId>
<version>${aws.sdk.version}</version>
</dependency>

We can create a Java Lambda project with the following structure:

Note that unlike previous recipes, we do not have request and response domain objects. While we were invoking the Lambda manually in previous recipes, in this recipe AWS will be invoking the Lambda and passing a predefined event object based on a trigger we configure.

SqsService.java is the interface for our service class. This is not a requirement; you can directly use the implementation class or even embed all logic within the Lambda handler class itself:

public interface SqsService {
Boolean processEvent(SQSEvent event, String outputQueueURL, LambdaLogger logger);
}

SqsServiceImpl.java is the actual service implementation. 

We can retrieve messages from the input SQSEvent and create a collection of SendMessageBatchRequestEntry objects:

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++;
}

We can create and send a batch request to the output queue, as follows:

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

LambdaSqsEventHandler.java is our Lambda handler class, where we initialize the SQS client and pass it to the service class along with the SQSEvent we received. 

We can initialize the SQS client as follows:

private final AmazonSQS sqsClient;
public LambdaSqsSdkReceiveSendBatchHandler() {
this.sqsClient = AmazonSQSClientBuilder.standard()
.withRegion(System.getenv("AWS_REGION"))
.build();
}

We then invoke the service method, passing the client along with the SQSEvent object:

public Boolean handleRequest(final SQSEvent sqsEvent, final Context context) {
context.getLogger().log("Received SQS event: " + sqsEvent);

final SqsService sqsService = new SqsServiceImpl(this.sqsClient);
return sqsService.processEvent(sqsEvent, System.getenv("SPC_OUTPUT_QUEUE_URL"), context.getLogger());

}

We will use an environment variable to specify the name of the output queue. In the previous recipes, we were sending it through the request object. Also, it is a good practice to prefix environment variables with a project-specific constant. For example, SPC is a prefix that denote Serverless Programming Cookbook.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.210.205