Search in sources :

Example 1 with AbstractMessagePublisher

use of co.cask.cdap.internal.app.runtime.messaging.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 {
                    if (!allowedTopic.equals(topicId)) {
                        throw new UnsupportedOperationException("Publish to topic '" + topicId.getTopic() + "' is not supported");
                    }
                    // Use storePayload
                    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(co.cask.cdap.api.messaging.MessagingContext) TopicId(co.cask.cdap.proto.id.TopicId) AbstractMessagePublisher(co.cask.cdap.internal.app.runtime.messaging.AbstractMessagePublisher)

Aggregations

MessagingContext (co.cask.cdap.api.messaging.MessagingContext)1 AbstractMessagePublisher (co.cask.cdap.internal.app.runtime.messaging.AbstractMessagePublisher)1 TopicId (co.cask.cdap.proto.id.TopicId)1 Iterator (java.util.Iterator)1