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 will receive messages from one queue, send it to another as a batch, and then delete the retrieved messages. 

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 the Java Lambda project with the following structure:

The Request.java class will correspond to our input JSON:

@Data
public class Request {
private String inputQueueURL;
private String outputQueueURL;
private int maxMessagesToReceive;
private int delay;
}

Here, inputQueueURL is the URL for the input queue from which the Lambda will receive messages, outputQueueURL is the URL for the output queue to which the Lambda will send messages, maxMessagesToReceive is the maximum number of messages retrieved from the queue in every receive call, and delay is the time for which delivery of messages to the queue is postponed.

Response.java will contain a field to send the response back to the invoker:

@Data
@AllArgsConstructor
public class Response {
private String message;
}

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 {
Response sendMessage(Request request, LambdaLogger logger);
}

SqsServiceImpl.java is the actual service implementation. 

We can retrieve messages from the input queue as follows:

final ReceiveMessageRequest receiveMessageRequest = new ReceiveMessageRequest()
.withQueueUrl(request.getInputQueueURL())
.withMaxNumberOfMessages(request.getMaxMessagesToReceive());

final List<Message> messages = this.sqsClient.receiveMessage(receiveMessageRequest).getMessages();

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

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

int idVal = 1;
for (Message m : messages) {
logger.log("Adding message: " + m.getBody());
entries.add(new SendMessageBatchRequestEntry("id_" + idVal,
m.getBody()).withDelaySeconds(request.getDelay()));
idVal++;
}

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

Finally, we delete all messages received, as follows:

for (Message m : messages) {
this.sqsClient.deleteMessage(request.getInputQueueURL(), m.getReceiptHandle());
}

LambdaSqsSdkReceiveSendBatchHandler.java is our Lambda handler class, where we initialize the SQS client and pass it to the service class. 

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 Request object:

public Response handleRequest(final Request request, final Context context) {
final SqsService sqsService = new SqsServiceImpl(this.sqsClient);
return sqsService.sendMessage(request, context.getLogger());

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

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