use of io.cdap.cdap.messaging.context.AbstractMessagePublisher in project cdap by caskdata.
the class BasicMapReduceTaskContext method getMessagingContext.
@Override
protected MessagingContext getMessagingContext() {
// Override to have transactional publisher use "store" instead of "payload"
// since a task is executed with long transaction.
// The actual publish will be done in the MR driver.
final MessagingContext context = super.getMessagingContext();
// TODO: CDAP-7807 Make it available for any topic
final TopicId allowedTopic = NamespaceId.SYSTEM.topic(cConf.get(Constants.Dataset.DATA_EVENT_TOPIC));
return new MessagingContext() {
@Override
public MessagePublisher getMessagePublisher() {
return new AbstractMessagePublisher() {
@Override
protected void publish(TopicId topicId, Iterator<byte[]> payloads) throws IOException, TopicNotFoundException, UnauthorizedException {
if (!allowedTopic.equals(topicId)) {
throw new UnsupportedOperationException("Publish to topic '" + topicId.getTopic() + "' is not supported");
}
// If there is transaction, use storePayload. Otherwise publish without transaction
if (transaction == null) {
getMessagingService().publish(StoreRequestBuilder.of(topicId).addPayloads(payloads).build());
} else {
getMessagingService().storePayload(StoreRequestBuilder.of(topicId).setTransaction(transaction.getWritePointer()).addPayloads(payloads).build());
}
}
};
}
@Override
public MessagePublisher getDirectMessagePublisher() {
return context.getDirectMessagePublisher();
}
@Override
public MessageFetcher getMessageFetcher() {
return context.getMessageFetcher();
}
};
}
Aggregations