Search in sources :

Example 1 with AbstractMessagePublisher

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();
        }
    };
}
Also used : Iterator(java.util.Iterator) MessagingContext(io.cdap.cdap.api.messaging.MessagingContext) TopicId(io.cdap.cdap.proto.id.TopicId) AbstractMessagePublisher(io.cdap.cdap.messaging.context.AbstractMessagePublisher)

Aggregations

MessagingContext (io.cdap.cdap.api.messaging.MessagingContext)1 AbstractMessagePublisher (io.cdap.cdap.messaging.context.AbstractMessagePublisher)1 TopicId (io.cdap.cdap.proto.id.TopicId)1 Iterator (java.util.Iterator)1