Search in sources :

Example 1 with DBEventPublisherContext

use of org.apache.airavata.model.dbevent.DBEventPublisherContext in project airavata by apache.

the class RegistryServiceDBEventHandler method onMessage.

@Override
public void onMessage(MessageContext messageContext) {
    logger.info("RegistryServiceDBEventHandler | Received a new message!");
    try {
        // construct dbeventmessage thrift datamodel
        byte[] bytes = ThriftUtils.serializeThriftObject(messageContext.getEvent());
        DBEventMessage dbEventMessage = new DBEventMessage();
        ThriftUtils.createThriftFromBytes(bytes, dbEventMessage);
        logger.info("RegistryService received db-event-message from publisher: " + dbEventMessage.getPublisherService());
        // get publisher context
        DBEventPublisherContext publisherContext = dbEventMessage.getMessageContext().getPublisher().getPublisherContext();
        logger.info("RegistryService, Replicated Entity: " + publisherContext.getEntityType());
        // this try-block is mainly for catching DuplicateEntryException
        try {
            // check type of entity-type
            switch(publisherContext.getEntityType()) {
                // Gateway related operations
                case TENANT:
                    {
                        // construct gateway datamodel from message
                        Gateway gateway = new Gateway();
                        ThriftUtils.createThriftFromBytes(publisherContext.getEntityDataModel(), gateway);
                        // call service-methods based on CRUD type
                        switch(publisherContext.getCrudType()) {
                            case CREATE:
                                {
                                    logger.info("Replicating addGateway in Registry.");
                                    registryClient.addGateway(gateway);
                                    logger.info("addGateway Replication Success!");
                                    break;
                                }
                            case UPDATE:
                                {
                                    logger.info("Replicating updateGateway in Registry.");
                                    if (!registryClient.isGatewayExist(gateway.getGatewayId())) {
                                        logger.info("Gateway doesn't exist so adding instead of updating.");
                                        registryClient.addGateway(gateway);
                                    } else {
                                        registryClient.updateGateway(gateway.getGatewayId(), gateway);
                                    }
                                    logger.info("updateGateway Replication Success!");
                                    break;
                                }
                            case DELETE:
                                {
                                    logger.info("Replicating deleteGateway in Registry.");
                                    registryClient.deleteGateway(gateway.getGatewayId());
                                    logger.info("deleteGateway Replication Success!");
                                    break;
                                }
                        }
                        // break entity: gateway
                        break;
                    }
                // UserProfile related operations
                case USER_PROFILE:
                    {
                        // construct userprofile datamodel from message
                        UserProfile userProfile = new UserProfile();
                        ThriftUtils.createThriftFromBytes(publisherContext.getEntityDataModel(), userProfile);
                        // call service-methods based on CRUD type
                        switch(publisherContext.getCrudType()) {
                            case CREATE:
                                {
                                    logger.info("Replicating addUser in Registry.");
                                    registryClient.addUser(userProfile);
                                    logger.info("addUser Replication Success!");
                                    break;
                                }
                            case UPDATE:
                                {
                                    logger.info("Replicating updateGateway in Registry.");
                                    // TODO: find appropriate method
                                    break;
                                }
                            case DELETE:
                                {
                                    logger.info("Replicating deleteGateway in Registry.");
                                    // TODO: find appropriate method
                                    break;
                                }
                        }
                        // break entity: userprofile
                        break;
                    }
                // no handler for entity
                default:
                    {
                        logger.error("Handler not defined for Entity: " + publisherContext.getEntityType());
                    }
            }
        } catch (DuplicateEntryException ex) {
            // log this exception and proceed (do nothing)
            // this exception is thrown mostly when messages are re-consumed, hence ignore
            logger.warn("DuplicateEntryException while consuming db-event message, ex: " + ex.getMessage(), ex);
        }
        // send ack for received message
        logger.info("RegistryServiceDBEventHandler | Sending ack. Message Delivery Tag: " + messageContext.getDeliveryTag());
        RegistryServiceDBEventMessagingFactory.getDBEventSubscriber().sendAck(messageContext.getDeliveryTag());
    } catch (TException ex) {
        logger.error("Error processing message: " + ex, ex);
    } catch (ApplicationSettingsException ex) {
        logger.error("Error fetching application settings: " + ex, ex);
    } catch (AiravataException ex) {
        logger.error("Error sending ack. Message Delivery Tag: " + messageContext.getDeliveryTag(), ex);
    }
}
Also used : TException(org.apache.thrift.TException) DBEventMessage(org.apache.airavata.model.dbevent.DBEventMessage) ApplicationSettingsException(org.apache.airavata.common.exception.ApplicationSettingsException) UserProfile(org.apache.airavata.model.user.UserProfile) DBEventPublisherContext(org.apache.airavata.model.dbevent.DBEventPublisherContext) Gateway(org.apache.airavata.model.workspace.Gateway) DuplicateEntryException(org.apache.airavata.model.error.DuplicateEntryException) AiravataException(org.apache.airavata.common.exception.AiravataException)

Aggregations

AiravataException (org.apache.airavata.common.exception.AiravataException)1 ApplicationSettingsException (org.apache.airavata.common.exception.ApplicationSettingsException)1 DBEventMessage (org.apache.airavata.model.dbevent.DBEventMessage)1 DBEventPublisherContext (org.apache.airavata.model.dbevent.DBEventPublisherContext)1 DuplicateEntryException (org.apache.airavata.model.error.DuplicateEntryException)1 UserProfile (org.apache.airavata.model.user.UserProfile)1 Gateway (org.apache.airavata.model.workspace.Gateway)1 TException (org.apache.thrift.TException)1