use of org.apache.airavata.db.event.manager.messaging.DBEventManagerException in project airavata by apache.
the class DBEventMessageHandler method onMessage.
@Override
public void onMessage(MessageContext messageContext) {
log.info("Incoming DB event message. Message Id : " + messageContext.getMessageId());
try {
byte[] bytes = ThriftUtils.serializeThriftObject(messageContext.getEvent());
DBEventMessage dbEventMessage = new DBEventMessage();
ThriftUtils.createThriftFromBytes(bytes, dbEventMessage);
DBEventMessageContext dBEventMessageContext = dbEventMessage.getMessageContext();
switch(dbEventMessage.getDbEventType()) {
case SUBSCRIBER:
log.info("Registering " + dBEventMessageContext.getSubscriber().getSubscriberService() + " subscriber for " + dbEventMessage.getPublisherService());
DbEventManagerZkUtils.createDBEventMgrZkNode(curatorClient, dbEventMessage.getPublisherService(), dBEventMessageContext.getSubscriber().getSubscriberService());
break;
case PUBLISHER:
List<String> subscribers = DbEventManagerZkUtils.getSubscribersForPublisher(curatorClient, dbEventMessage.getPublisherService());
if (subscribers.isEmpty()) {
log.error("No Subscribers registered for the service");
throw new DBEventManagerException("No Subscribers registered for the service");
}
String routingKey = getRoutingKeyFromList(subscribers);
log.info("Publishing " + dbEventMessage.getPublisherService() + " db event to " + subscribers.toString());
MessageContext messageCtx = new MessageContext(dbEventMessage, MessageType.DB_EVENT, "", "");
messageCtx.setUpdatedTime(AiravataUtils.getCurrentTimestamp());
DBEventManagerMessagingFactory.getDBEventPublisher().publish(messageCtx, routingKey);
break;
}
log.info("Sending ack. Message Delivery Tag : " + messageContext.getDeliveryTag());
DBEventManagerMessagingFactory.getDBEventSubscriber().sendAck(messageContext.getDeliveryTag());
} catch (Exception e) {
log.error("Error processing message.", e);
}
}
Aggregations