本文整理汇总了Java中com.amazonaws.services.sqs.model.SendMessageBatchResult类的典型用法代码示例。如果您正苦于以下问题:Java SendMessageBatchResult类的具体用法?Java SendMessageBatchResult怎么用?Java SendMessageBatchResult使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
SendMessageBatchResult类属于com.amazonaws.services.sqs.model包,在下文中一共展示了SendMessageBatchResult类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testSend
import com.amazonaws.services.sqs.model.SendMessageBatchResult; //导入依赖的package包/类
/**
* Tests the {@link BatchSQSMsgSender#send(org.apache.flume.Channel)} method. Tests the happy path scenario.
*
* @throws Exception
*/
@Test
public void testSend() throws Exception {
BatchSQSMsgSender sqsMsgSender =
new BatchSQSMsgSender("https://some-fake/url", "us-east-1", "someAwsAccessKey", "someAwsSecretKey", 5, 10);
AmazonSQS mockSqs = Mockito.mock(AmazonSQS.class);
when(mockSqs.sendMessageBatch(any(SendMessageBatchRequest.class))).thenReturn(new SendMessageBatchResult());
sqsMsgSender.setAmazonSQS(mockSqs);
byte[] mockMsgPayload = {'A', 'b'};
Event mockEvent = Mockito.mock(Event.class);
when(mockEvent.getBody()).thenReturn(mockMsgPayload);
Channel mockChannel = Mockito.mock(Channel.class);
when(mockChannel.take()).thenReturn(mockEvent);
sqsMsgSender.send(mockChannel);
}
示例2: mockBatchResult
import com.amazonaws.services.sqs.model.SendMessageBatchResult; //导入依赖的package包/类
private SendMessageBatchResult mockBatchResult(int batchSize, int expectedSuccessCount) {
SendMessageBatchResult mockResult = Mockito.mock(SendMessageBatchResult.class);
List<SendMessageBatchResultEntry> successfulEntries = new ArrayList<SendMessageBatchResultEntry>();
for (int i = 0; i < expectedSuccessCount; i++) {
successfulEntries.add(new SendMessageBatchResultEntry().withId(String.valueOf(i + 1)));
}
when(mockResult.getSuccessful()).thenReturn(successfulEntries);
List<BatchResultErrorEntry> failedEntries = new ArrayList<BatchResultErrorEntry>();
for (int i = expectedSuccessCount; i < batchSize; i++) {
failedEntries.add(
new BatchResultErrorEntry().withId(String.valueOf(i + 1)).withCode("401").withSenderFault(true)
.withMessage("Invalid binary character"));
}
when(mockResult.getFailed()).thenReturn(failedEntries);
return mockResult;
}
示例3: shouldSendBatchesInSizeOfTen
import com.amazonaws.services.sqs.model.SendMessageBatchResult; //导入依赖的package包/类
@Test
public void shouldSendBatchesInSizeOfTen() throws Exception {
when(mockAmazonSQS.sendMessageBatch(any(SendMessageBatchRequest.class))).thenReturn(mock(SendMessageBatchResult.class));
ArgumentCaptor<SendMessageBatchRequest> captor = ArgumentCaptor.forClass(SendMessageBatchRequest.class);
messagePublisher.postBatch(messageBatch(10), subject);
verify(mockAmazonSQS, times(1)).sendMessageBatch(captor.capture());
assertThat(captor.getValue().getEntries()).hasSize(10);
messagePublisher.postBatch(messageBatch(20), subject);
verify(mockAmazonSQS, times(3)).sendMessageBatch(any(SendMessageBatchRequest.class));
messagePublisher.postBatch(messageBatch(11), subject);
verify(mockAmazonSQS, times(5)).sendMessageBatch(captor.capture());
assertThat(captor.getValue().getEntries()).hasSize(1);
messagePublisher.postBatch(messageBatch(9), subject);
verify(mockAmazonSQS, times(6)).sendMessageBatch(captor.capture());
assertThat(captor.getValue().getEntries()).hasSize(9);
}
示例4: publishMessages
import com.amazonaws.services.sqs.model.SendMessageBatchResult; //导入依赖的package包/类
void publishMessages(List<Message> messages) {
logger.info("Sending {} messages", messages.size());
SendMessageBatchRequest batch = new SendMessageBatchRequest(queueURL);
messages.stream().forEach(msg -> {
SendMessageBatchRequestEntry sendr = new SendMessageBatchRequestEntry(msg.getId(), msg.getPayload());
batch.getEntries().add(sendr);
});
logger.info("sending {}", batch.getEntries().size());
SendMessageBatchResult result = client.sendMessageBatch(batch);
logger.info("send result {}", result.getFailed().toString());
}
示例5: send
import com.amazonaws.services.sqs.model.SendMessageBatchResult; //导入依赖的package包/类
@Override
public int send(Channel channel) throws EventDeliveryException {
int eventProcessedCounter = 0;
// Create batch request
List<SendMessageBatchRequest> batchRequests = createBatches(channel);
for (SendMessageBatchRequest batchRequest : batchRequests) {
// Send batch request
SendMessageBatchResult result = null;
try {
result = this.amazonSQS.sendMessageBatch(batchRequest);
}
catch (AmazonServiceException ase) {
// Throw request reached to SQS but the whole batch was rejected for some reason. Let the whole batch
// be treated as "failed". Flume will retry the while batch
throw new EventDeliveryException("Failure sending batch message request to Amazon SQS, " +
"the request made it to SQS but was rejected for some reason.", ase);
}
catch (AmazonClientException ace) {
throw new EventDeliveryException("Failure sending batch message request to Amazon SQS.", ace);
}
// Handle the result of the SQS batch request i.e., log errors, or fail the whole batch by throwing
// EventDeliveryException in case of errors etc.
handleResult(batchRequest, result);
// The code reached here means there is nothing to rollback in this transaction. So increment the
// eventProcessedCounter by the number of successfully sent messages.
eventProcessedCounter += result.getSuccessful().size();
}
return eventProcessedCounter;
}
示例6: handleResult
import com.amazonaws.services.sqs.model.SendMessageBatchResult; //导入依赖的package包/类
/**
* Handles SQS send message batch result and throws EventDeliveryException to cause the flume transaction to fail
* and let flume retry the whole batch in case all the messages in the batch failed to be delivered to SQS.
* Currently, this method does just logs errors and skips the messages in case some messages from the batched failed
* to be delivered but some succeeded (i.e., partial batch failure).
* <p>
* TODO: Add retry logic instead letting flume drop the failed messages in case of partial batch failure
*
* @param batchRequest The SQS SendMessageBatchRequest
* @param batchResult The SQS SendMessageBatchResult
*
* @throws EventDeliveryException In case all the messages in the batch failed to be delivered to SQS
*/
protected void handleResult(SendMessageBatchRequest batchRequest, SendMessageBatchResult batchResult)
throws EventDeliveryException {
List<SendMessageBatchRequestEntry> batchRequestEntries = batchRequest.getEntries();
List<BatchResultErrorEntry> errors = batchResult.getFailed();
int attemptedCount = batchRequestEntries == null ? 0 : batchRequestEntries.size();
int errorCount = errors == null ? 0 : errors.size();
if (errorCount > 0) {
String errorMessage = buildErrorMessage(batchRequestEntries, errors);
if (attemptedCount == errorCount) {
// if it was a non-empty batch and if all the messages in the batch have errors then fail the whole
// batch and let flume rollback the transaction and retry it
// Just throw the EventDeliveryException. This will eventually cause the channel's transaction to
// rollback.
throw new EventDeliveryException(errorMessage);
}
else {
// TODO: Add retry logic instead letting flume drop the failed messages in case of partial batch failure
// Just log the error message and let flume drop failed messages in case of partial batch failures
LOG.error(errorMessage);
}
}
}
示例7: testSendPartialBatchFailure
import com.amazonaws.services.sqs.model.SendMessageBatchResult; //导入依赖的package包/类
/**
* Tests the {@link BatchSQSMsgSender#send(org.apache.flume.Channel)} method. Tests the failure scenario when
* certain messages in the batch failed to be delivered to SQS.
* <p>
* <pre>
* Expected:
* - No EventDeliveryException is thrown
* - The BatchSQSMsgSender returns successfully processed events count
* </pre>
*
* @throws Exception
*/
@Test
public void testSendPartialBatchFailure() throws Exception {
int batchSize = 5;
int failedMsgCount = 1;
int expectedSuccessCount = batchSize - failedMsgCount;
BatchSQSMsgSender sqsMsgSender =
new BatchSQSMsgSender("https://some-fake/url", "us-east-1", "someAwsAccessKey", "someAwsSecretKey",
batchSize, 100);
AmazonSQS mockSqs = Mockito.mock(AmazonSQS.class);
SendMessageBatchResult mockResult = mockBatchResult(batchSize, expectedSuccessCount);
when(mockSqs.sendMessageBatch(any(SendMessageBatchRequest.class))).thenReturn(mockResult);
sqsMsgSender.setAmazonSQS(mockSqs);
String msgBody = "Some message payload";
byte[] mockMsgPayload = msgBody.getBytes();
Event mockEvent = Mockito.mock(Event.class);
when(mockEvent.getBody()).thenReturn(mockMsgPayload);
Channel mockChannel = Mockito.mock(Channel.class);
when(mockChannel.take()).thenReturn(mockEvent);
int successCount = sqsMsgSender.send(mockChannel);
Assert.assertEquals(expectedSuccessCount, successCount);
}
示例8: testSendCompleteBatchFailure
import com.amazonaws.services.sqs.model.SendMessageBatchResult; //导入依赖的package包/类
/**
* Tests the {@link BatchSQSMsgSender#send(org.apache.flume.Channel)} method. Tests the failure scenario when all
* the messages in the batch failed to be delivered to SQS.
* <p>
* Expected: - EventDeliveryException is thrown - EventDeliveryException also contains the failed messages payload
* in the exception message
*
* @throws Exception
*/
@Test(expected = EventDeliveryException.class)
public void testSendCompleteBatchFailure() throws Exception {
int batchSize = 5;
int failedMsgCount = batchSize;
int expectedSuccessCount = batchSize - failedMsgCount;
BatchSQSMsgSender sqsMsgSender =
new BatchSQSMsgSender("https://some-fake/url", "us-east-1", "someAwsAccessKey", "someAwsSecretKey",
batchSize, 100);
AmazonSQS mockSqs = Mockito.mock(AmazonSQS.class);
SendMessageBatchResult mockResult = mockBatchResult(batchSize, expectedSuccessCount);
when(mockSqs.sendMessageBatch(any(SendMessageBatchRequest.class))).thenReturn(mockResult);
sqsMsgSender.setAmazonSQS(mockSqs);
String msgBody = "Some message payload";
byte[] mockMsgPayload = msgBody.getBytes();
Event mockEvent = Mockito.mock(Event.class);
when(mockEvent.getBody()).thenReturn(mockMsgPayload);
Channel mockChannel = Mockito.mock(Channel.class);
when(mockChannel.take()).thenReturn(mockEvent);
try {
sqsMsgSender.send(mockChannel);
}
catch (EventDeliveryException ede) {
// Make sure that the original payload is also part of the exception error messsage body
// to get the failed payloads logged along with errors
Assert.assertTrue(ede.getMessage().contains(msgBody));
//rethrow as the test is expecting this exception to be thrown
throw ede;
}
}
示例9: submitBatches
import com.amazonaws.services.sqs.model.SendMessageBatchResult; //导入依赖的package包/类
/***
* Submit the batches of messages
* @param messageBatches
*/
private void submitBatches(
final Map<String, List<SendMessageBatchRequestEntry>> messageBatches) {
for(Entry<String, List<SendMessageBatchRequestEntry>> queueBatchEntry : messageBatches.entrySet()){
final String queueUrl = this.queueAdmin.getQueueUrl(queueBatchEntry.getKey());
final SendMessageBatchRequest batch = new SendMessageBatchRequest()
.withQueueUrl(queueUrl)
.withEntries(queueBatchEntry.getValue());
final SendMessageBatchResult batchResult = this.sqs.sendMessageBatch(batch);
this.logFailures(batchResult.getFailed());
}
}
示例10: sendMessages
import com.amazonaws.services.sqs.model.SendMessageBatchResult; //导入依赖的package包/类
@Override
public SendMessageBatchResult sendMessages(SendMessageBatchRequest request,
ResultCapture<SendMessageBatchResult> extractor) {
ActionResult result = resource.performAction("SendMessages", request,
extractor);
if (result == null) return null;
return (SendMessageBatchResult) result.getData();
}
示例11: testQueueSubResourceAndAttributes
import com.amazonaws.services.sqs.model.SendMessageBatchResult; //导入依赖的package包/类
/**
* Tests sending messages using batch operation and retrieve them. Also
* tests setting the queue attributes and retrieving them.
*/
@Test
@Ignore
public void testQueueSubResourceAndAttributes() throws InterruptedException {
/**
* Trying to get the message which is deleted. Here there is no service
* call made, a new sub resource is created with the given handle. So,
* this wont be returning null.
*/
Message message = queue.getMessage("invalid-recepient-handle");
assertNotNull(message);
try {
message.getAttributes();
fail("An unsupported operation exception must be thrown as load operation is no supported on message attribute");
} catch (UnsupportedOperationException use) { }
SendMessageBatchResult sendMessageBatchResult = queue
.sendMessages(new SendMessageBatchRequest()
.withEntries(new SendMessageBatchRequestEntry("msg1",
TEST_MESSAGE)));
SendMessageBatchResultEntry sendMessageBatchResultEntry = sendMessageBatchResult
.getSuccessful().get(0);
List<Message> messages = waitForMessagesFromQueue(null);
assertNotNull(messages);
assertEquals(1, messages.size());
message = messages.get(0);
assertMessage(TEST_MESSAGE, sendMessageBatchResultEntry.getMessageId(),
sendMessageBatchResultEntry.getMD5OfMessageBody(), message);
queue.setAttributes(ImmutableMapParameter.of("MaximumMessageSize",
"2048"));
assertTrue(queue.getAttributes().containsKey("MaximumMessageSize"));
}
示例12: sendMessageBatch
import com.amazonaws.services.sqs.model.SendMessageBatchResult; //导入依赖的package包/类
private void sendMessageBatch(Collection<SendMessageBatchRequestEntry> messages) {
for (Collection<SendMessageBatchRequestEntry> batch : partition(messages, MAX_BATCH_SIZE)) {
final SendMessageBatchResult sendMessageBatchResult =
amazonSQS.sendMessageBatch(new SendMessageBatchRequest(queueUrl, new ArrayList<>(batch)));
final List<BatchResultErrorEntry> failed = sendMessageBatchResult.getFailed();
if (!failed.isEmpty()) {
try {
Set<String> failedMessageIds =
failed.stream().map(BatchResultErrorEntry::getId).collect(Collectors.toSet());
final Map<String, SendMessageBatchRequestEntry> failedMessageIdToMessage =
batch.stream().filter(failedMessageIds::contains).collect(Collectors.toMap(
SendMessageBatchRequestEntry::getId,
Function.identity()
));
failed.stream().forEach(failMessage -> {
final SendMessageBatchRequestEntry failedEntry =
failedMessageIdToMessage.get(failMessage.getId());
if (failedEntry != null) {
final String messageBody = failedEntry.getMessageBody();
LOG.error(
"Failed to send message, due to {}, message content : {} ",
failMessage,
messageBody
);
}
});
} catch (Exception e) {
LOG.error("Failed to log failed to send messages", e);
}
}
}
}
示例13: postBatchShouldSendMessagesWithSNSEnvelope
import com.amazonaws.services.sqs.model.SendMessageBatchResult; //导入依赖的package包/类
@Test
public void postBatchShouldSendMessagesWithSNSEnvelope() throws Exception {
// Arrange
when(mockAmazonSQS.sendMessageBatch(any(SendMessageBatchRequest.class))).thenReturn(mock(SendMessageBatchResult.class));
ArgumentCaptor<SendMessageBatchRequest> captor = ArgumentCaptor.forClass(SendMessageBatchRequest.class);
// Act
messagePublisher.postBatch(
Arrays.asList(
new TestMessage("Hello"), new TestMessage("world")
), "subject"
);
// Assert
verify(mockAmazonSQS).sendMessageBatch(captor.capture());
SendMessageBatchRequest sendMessageBatchRequest = captor.getValue();
assertThat(sendMessageBatchRequest.getQueueUrl()).isEqualTo("queueUrl");
List<SendMessageBatchRequestEntry> entries = sendMessageBatchRequest.getEntries();
assertThat(entries.size()).isEqualTo(2);
ObjectMapper mapper = new ObjectMapper();
AmazonSNSMessage msg1 = mapper.readValue(entries.get(0).getMessageBody(), AmazonSNSMessage.class);
assertThat(msg1.getSubject()).isEqualTo("subject");
assertThat(msg1.getMessage()).isEqualTo("{\"message\":\"Hello\"}");
AmazonSNSMessage msg2 = mapper.readValue(entries.get(1).getMessageBody(), AmazonSNSMessage.class);
assertThat(msg2.getSubject()).isEqualTo("subject");
assertThat(msg2.getMessage()).isEqualTo("{\"message\":\"world\"}");
}
示例14: sendMessageBatch
import com.amazonaws.services.sqs.model.SendMessageBatchResult; //导入依赖的package包/类
@Override
public SendMessageBatchResult sendMessageBatch(SendMessageBatchRequest sendMessageBatchRequest) throws AmazonClientException {
DirectorySQSQueue queue = getQueueFromUrl(sendMessageBatchRequest.getQueueUrl(), false);
//lists for reporting
List<BatchResultErrorEntry> batchResultErrorEntries = new ArrayList<>();
List<SendMessageBatchResultEntry> batchResultEntries = new ArrayList<>();
//attempt to change the visibility on each
for (SendMessageBatchRequestEntry batchRequestEntry : sendMessageBatchRequest.getEntries()) {
try {
final int invisibilityDelay = Objects.firstNonNull(batchRequestEntry.getDelaySeconds(), 0);//0 is amazon spec default
Message sentMessage = queue.send(batchRequestEntry.getMessageBody(), invisibilityDelay);
batchResultEntries.add(new SendMessageBatchResultEntry().
withId(batchRequestEntry.getId()).
withMessageId(sentMessage.getMessageId()).
withMD5OfMessageBody(sentMessage.getMD5OfBody()));
} catch (IOException e) {
BatchResultErrorEntry batchResultErrorEntry = new BatchResultErrorEntry().
withSenderFault(false).
withId(batchRequestEntry.getId()).
withMessage(e.getMessage());
batchResultErrorEntries.add(batchResultErrorEntry);
}
}
return new SendMessageBatchResult().
withFailed(batchResultErrorEntries).
withSuccessful(batchResultEntries);
}
示例15: testBulkSendDelete_shouldWork
import com.amazonaws.services.sqs.model.SendMessageBatchResult; //导入依赖的package包/类
@Test
public void testBulkSendDelete_shouldWork() {
// create queue
CreateQueueResult createdQueue = sqs.createQueue(new CreateQueueRequest().withQueueName("tea-earl-grey-queue"));
// send batch
SendMessageBatchRequestEntry firstRequest = new SendMessageBatchRequestEntry().withDelaySeconds(0).withId("one")
.withMessageGroupId("groupee").withMessageBody("{\"XOXO\":234}");
SendMessageBatchRequestEntry secondRequest = new SendMessageBatchRequestEntry().withDelaySeconds(0).withId("two")
.withMessageGroupId("groupee").withMessageBody("{\"Quinoa\":\"Readymade\",\"vegan\":true}");
SendMessageBatchRequestEntry thirdRequest = new SendMessageBatchRequestEntry().withDelaySeconds(0).withId("three")
.withMessageGroupId("groupee").withMessageBody("{\"VHS\":\"street art slow-carb\"}");
// verify send batch result
SendMessageBatchResult sendResult = sqs.sendMessageBatch(new SendMessageBatchRequest().withQueueUrl(createdQueue.getQueueUrl())
.withEntries(ImmutableList.of(firstRequest,secondRequest, thirdRequest)));
assertNotNull("verify that batch send returned ok", sendResult);
assertTrue("no request failed",sendResult.getFailed().isEmpty());
assertEquals("verify successfull message count", 3, sendResult.getSuccessful().size());
SendMessageBatchResultEntry firstResultEntry = sendResult.getSuccessful().stream().filter(msg -> msg.getId().equals("one")).findAny().get();
assertEquals("verify correct message MD5",getAwsMessageMD5("{\"XOXO\":234}"),firstResultEntry.getMD5OfMessageBody());
assertNotNull("verify message id exists",firstResultEntry.getMessageId());
ReceiveMessageResult receivedMessagesResult = sqs.receiveMessage(new ReceiveMessageRequest().withQueueUrl(createdQueue.getQueueUrl()).withMaxNumberOfMessages(4));
// delete batch
List<DeleteMessageBatchRequestEntry> deleteRequests = new ArrayList<>();
deleteRequests.add(new DeleteMessageBatchRequestEntry().withId("one").withReceiptHandle(receivedMessagesResult.getMessages().get(0).getReceiptHandle()));
deleteRequests.add(new DeleteMessageBatchRequestEntry().withId("two").withReceiptHandle(receivedMessagesResult.getMessages().get(0).getReceiptHandle()));
deleteRequests.add(new DeleteMessageBatchRequestEntry().withId("three").withReceiptHandle(receivedMessagesResult.getMessages().get(0).getReceiptHandle()));
DeleteMessageBatchResult deleteBatchResult = sqs.deleteMessageBatch(new DeleteMessageBatchRequest().withQueueUrl(createdQueue.getQueueUrl()).withEntries(deleteRequests));
// verify delete batch result
assertNotNull("verify that batch delete returned ok", deleteBatchResult);
assertTrue("no request failed",deleteBatchResult.getFailed().isEmpty());
assertEquals("verify successfull message count", 3, deleteBatchResult.getSuccessful().size());
assertTrue("queue must be empty after removal",getQueues().get("tea-earl-grey-queue").getMessageQueue().isEmpty());
for(Message message : receivedMessagesResult.getMessages()) {
assertTrue("invisibility-queue must be empty after removal",getQueues().get("tea-earl-grey-queue").getInvisibilityQueueFor(message.getReceiptHandle()).isEmpty());
}
// cleanup
getQueues().remove("tea-earl-grey-queue");
}