use of co.cask.cdap.api.messaging.MessagingContext in project cdap by caskdata.
the class MapReduceRunnerTestBase method getDataNotifications.
/**
* Returns a list of {@link Notification} object fetched from the data event topic in TMS that was published
* starting from the given time.
*/
protected List<Notification> getDataNotifications(long startTime) throws Exception {
// Get data notifications from TMS
List<Notification> notifications = new ArrayList<>();
MessagingContext messagingContext = new MultiThreadMessagingContext(injector.getInstance(MessagingService.class));
try (CloseableIterator<Message> messages = messagingContext.getMessageFetcher().fetch(NamespaceId.SYSTEM.getNamespace(), injector.getInstance(CConfiguration.class).get(Constants.Dataset.DATA_EVENT_TOPIC), 10, startTime)) {
while (messages.hasNext()) {
notifications.add(GSON.fromJson(new String(messages.next().getPayload(), StandardCharsets.UTF_8), Notification.class));
}
}
return notifications;
}
use of co.cask.cdap.api.messaging.MessagingContext 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();
}
};
}
use of co.cask.cdap.api.messaging.MessagingContext in project cdap by caskdata.
the class MessagingAppTestRun method testWithWorker.
@Test
public void testWithWorker() throws Exception {
ApplicationManager appManager = deployWithArtifact(NAMESPACE, MessagingApp.class, artifactJar);
final WorkerManager workerManager = appManager.getWorkerManager(MessagingApp.MessagingWorker.class.getSimpleName()).start();
MessagingContext messagingContext = getMessagingContext();
final MessagingAdmin messagingAdmin = getMessagingAdmin(NAMESPACE);
// Wait for the worker to create the topic
Tasks.waitFor(true, new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
try {
messagingAdmin.getTopicProperties(MessagingApp.TOPIC);
return true;
} catch (TopicNotFoundException e) {
return false;
}
}
}, 5, TimeUnit.SECONDS, 100, TimeUnit.MILLISECONDS);
// Publish a message
String message = "message";
MessagePublisher messagePublisher = messagingContext.getMessagePublisher();
messagePublisher.publish(NAMESPACE.getNamespace(), MessagingApp.TOPIC, message);
// The worker will publish back a message with payload as concat(message, message)
final MessageFetcher messageFetcher = messagingContext.getMessageFetcher();
Tasks.waitFor(message + message, new Callable<String>() {
@Override
public String call() throws Exception {
try (CloseableIterator<Message> iterator = messageFetcher.fetch(NAMESPACE.getNamespace(), MessagingApp.TOPIC, Integer.MAX_VALUE, 0L)) {
Message message = Iterators.getLast(iterator, null);
return message == null ? null : message.getPayloadAsString();
}
}
}, 5, TimeUnit.SECONDS, 100, TimeUnit.MILLISECONDS);
// Publish concat(message + message) to the app
messagePublisher.publish(NAMESPACE.getNamespace(), MessagingApp.TOPIC, message + message);
// timeout.
try {
Tasks.waitFor(message + message + message + message, new Callable<String>() {
@Override
public String call() throws Exception {
try (CloseableIterator<Message> iterator = messageFetcher.fetch(NAMESPACE.getNamespace(), MessagingApp.TOPIC, Integer.MAX_VALUE, 0L)) {
Message message = Iterators.getLast(iterator, null);
return message == null ? null : message.getPayloadAsString();
}
}
}, 2, TimeUnit.SECONDS, 100, TimeUnit.MILLISECONDS);
Assert.fail("Expected timeout exception");
} catch (TimeoutException e) {
// expected
}
// Now publish a message to the control topic, to unblock the transaction block.
messagePublisher.publish(NAMESPACE.getNamespace(), MessagingApp.CONTROL_TOPIC, message);
// Should expect a new message as concat(message, message, message, message)
Tasks.waitFor(message + message + message + message, new Callable<String>() {
@Override
public String call() throws Exception {
try (CloseableIterator<Message> iterator = messageFetcher.fetch(NAMESPACE.getNamespace(), MessagingApp.TOPIC, Integer.MAX_VALUE, 0L)) {
Message message = Iterators.getLast(iterator, null);
return message == null ? null : message.getPayloadAsString();
}
}
}, 5, TimeUnit.SECONDS, 100, TimeUnit.MILLISECONDS);
// Wait for the worker to finish and verify that it completes successfully.
workerManager.waitForRun(ProgramRunStatus.COMPLETED, 5, TimeUnit.SECONDS);
}
Aggregations