-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathDefaultSNSSinglePublisher.java
More file actions
87 lines (77 loc) · 4.54 KB
/
DefaultSNSSinglePublisher.java
File metadata and controls
87 lines (77 loc) · 4.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package com.here.xyz.pub.impl;
import com.here.xyz.models.hub.Subscription;
import com.here.xyz.pub.mapper.IPubMsgMapper;
import com.here.xyz.pub.models.PubConfig;
import com.here.xyz.pub.models.PubLogConstants;
import com.here.xyz.pub.models.PubTransactionData;
import com.here.xyz.pub.models.PublishEntryDTO;
import com.here.xyz.pub.util.AwsUtil;
import com.here.xyz.pub.util.MessageUtil;
import com.here.xyz.pub.util.PubUtil;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import software.amazon.awssdk.services.sns.SnsAsyncClient;
import software.amazon.awssdk.services.sns.model.MessageAttributeValue;
import software.amazon.awssdk.services.sns.model.PublishRequest;
import software.amazon.awssdk.services.sns.model.PublishResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
public class DefaultSNSSinglePublisher implements IPublisher {
private static final Logger logger = LogManager.getLogger();
// Convert and publish transactions to desired SNS Topic
@Override
public void publishTransactions(final PubConfig pubCfg, final Subscription sub,
final List<PubTransactionData> txnList,
final PublishEntryDTO pubDTO) throws Exception {
final String subId = sub.getId();
final String spaceId = sub.getSource();
final String snsTopic = PubUtil.getSnsTopicARN(sub);
final long lotStartTS = System.currentTimeMillis();
// local counters
long publishedRecCnt = 0;
try {
final IPubMsgMapper msgMapper = MessageUtil.getMsgMapperInstance(sub);
// TODO : Support multi-region based on subscription configuration.
// We may require region specific publisher job for the respective subscriptions.
final SnsAsyncClient snsClient = AwsUtil.getSnsAsyncClient(pubCfg.AWS_DEFAULT_REGION);
// Publish all transactions on SNS Topic (in the same order they were fetched)
for (final PubTransactionData txnData : txnList) {
final long crtTxnId = txnData.getTxnId();
final long crtTxnRecId = txnData.getTxnRecId();
final long startTS = System.currentTimeMillis();
// Convert transaction payload into expected publishable format
final String pubFormat = msgMapper.mapToPublishableFormat(sub, txnData);
// Prepare SNS Notification message
final String msg = MessageUtil.compressAndEncodeToString(pubFormat);
final Map<String, MessageAttributeValue> msgAttrMap = new HashMap<>();
MessageUtil.addToAttributeMap(msgAttrMap, "action", txnData.getAction());
MessageUtil.addToAttributeMap(msgAttrMap, "space", spaceId);
MessageUtil.addToAttributeMap(msgAttrMap, "featureId", txnData.getFeatureId());
// Add other custom attributes
MessageUtil.addCustomFieldsToAttributeMap(msgAttrMap, sub, txnData.getJsonData());
// Publish message to SNS Topic
final PublishRequest request = PublishRequest.builder()
.message(msg)
.messageAttributes(msgAttrMap)
.topicArn(snsTopic)
.build();
final CompletableFuture<PublishResponse> futureResponse = snsClient.publish(request);
final PublishResponse result = futureResponse.join();
publishedRecCnt++;
final long timeTaken = System.currentTimeMillis() - startTS;
logger.debug("Message no. [{}], txnId={}, txnRecId={}, published in {}ms to SNS [{}] for subId [{}]. Status is {}.",
publishedRecCnt, crtTxnId, crtTxnRecId, timeTaken, snsTopic, subId, result.sdkHttpResponse().statusCode());
// Record last successfully published transaction Id's
pubDTO.setLastTxnId(crtTxnId);
pubDTO.setLastTxnRecId(crtTxnRecId);
}
}
finally {
final long lotTimeTaken = System.currentTimeMillis() - lotStartTS;
logger.info("Transaction publish stats for SNS [{}] [format => eventType,subId,spaceId,msgCount,timeTakenMs,lastTxnId,lastTxnRecId] - {} {} {} {} {} {} {}",
snsTopic, PubLogConstants.LOG_EVT_TXN_PUBLISH_STATS, subId, spaceId, publishedRecCnt, lotTimeTaken, pubDTO.getLastTxnId(), pubDTO.getLastTxnRecId());
}
}
}