Search in sources :

Example 6 with DeleteMessageBatchRequest

use of com.amazonaws.services.sqs.model.DeleteMessageBatchRequest in project nifi by apache.

the class DeleteSQS method onTrigger.

@Override
public void onTrigger(final ProcessContext context, final ProcessSession session) {
    List<FlowFile> flowFiles = session.get(1);
    if (flowFiles.isEmpty()) {
        return;
    }
    final FlowFile firstFlowFile = flowFiles.get(0);
    final String queueUrl = context.getProperty(QUEUE_URL).evaluateAttributeExpressions(firstFlowFile).getValue();
    final AmazonSQSClient client = getClient();
    final DeleteMessageBatchRequest request = new DeleteMessageBatchRequest();
    request.setQueueUrl(queueUrl);
    final List<DeleteMessageBatchRequestEntry> entries = new ArrayList<>(flowFiles.size());
    for (final FlowFile flowFile : flowFiles) {
        final DeleteMessageBatchRequestEntry entry = new DeleteMessageBatchRequestEntry();
        entry.setReceiptHandle(context.getProperty(RECEIPT_HANDLE).evaluateAttributeExpressions(flowFile).getValue());
        entries.add(entry);
    }
    request.setEntries(entries);
    try {
        client.deleteMessageBatch(request);
        getLogger().info("Successfully deleted {} objects from SQS", new Object[] { flowFiles.size() });
        session.transfer(flowFiles, REL_SUCCESS);
    } catch (final Exception e) {
        getLogger().error("Failed to delete {} objects from SQS due to {}", new Object[] { flowFiles.size(), e });
        final List<FlowFile> penalizedFlowFiles = new ArrayList<>();
        for (final FlowFile flowFile : flowFiles) {
            penalizedFlowFiles.add(session.penalize(flowFile));
        }
        session.transfer(penalizedFlowFiles, REL_FAILURE);
    }
}
Also used : FlowFile(org.apache.nifi.flowfile.FlowFile) DeleteMessageBatchRequest(com.amazonaws.services.sqs.model.DeleteMessageBatchRequest) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) AmazonSQSClient(com.amazonaws.services.sqs.AmazonSQSClient) DeleteMessageBatchRequestEntry(com.amazonaws.services.sqs.model.DeleteMessageBatchRequestEntry)

Aggregations

DeleteMessageBatchRequest (com.amazonaws.services.sqs.model.DeleteMessageBatchRequest)6 AmazonSQSClient (com.amazonaws.services.sqs.AmazonSQSClient)3 DeleteMessageBatchRequestEntry (com.amazonaws.services.sqs.model.DeleteMessageBatchRequestEntry)3 Message (com.amazonaws.services.sqs.model.Message)3 ReceiveMessageRequest (com.amazonaws.services.sqs.model.ReceiveMessageRequest)3 ReceiveMessageResult (com.amazonaws.services.sqs.model.ReceiveMessageResult)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 Test (org.junit.Test)3 FlowFile (org.apache.nifi.flowfile.FlowFile)2 TransportException (co.krypt.krypton.exception.TransportException)1 NetworkMessage (co.krypt.krypton.protocol.NetworkMessage)1 AmazonSQSException (com.amazonaws.services.sqs.model.AmazonSQSException)1 MessageAttributeValue (com.amazonaws.services.sqs.model.MessageAttributeValue)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 Charset (java.nio.charset.Charset)1 List (java.util.List)1 Map (java.util.Map)1 OutputStreamCallback (org.apache.nifi.processor.io.OutputStreamCallback)1