Search in sources :

Example 1 with ConnectionProperties

use of org.odpi.openmetadata.frameworks.connectors.properties.ConnectionProperties in project egeria-connector-sas-viya by odpi.

the class RepositoryEventMapper method initialize.

@Override
public void initialize(String repositoryEventMapperName, OMRSRepositoryConnector repositoryConnector) {
    super.initialize(repositoryEventMapperName, repositoryConnector);
    final String methodName = "initialize";
    auditLog.logMessage(methodName, AuditCode.EVENT_MAPPER_INITIALIZING.getMessageDefinition());
    this.mapper = new ObjectMapper();
    this.mapper.configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true);
    try {
        log.debug("Set up connection factory for RabbitMQ");
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.useSslProtocol();
        ConnectionProperties connProperties = this.connectionProperties;
        if (connProperties != null) {
            EndpointProperties connEndpoint = connProperties.getEndpoint();
            Map<String, Object> cfgProperties = connProperties.getConfigurationProperties();
            if (connEndpoint != null) {
                String host = "";
                String portAsString = "";
                String address = connEndpoint.getAddress();
                if (StringUtils.isNotEmpty(address)) {
                    if (address.contains(":")) {
                        String[] addressParts = address.split(":", 2);
                        host = addressParts[0];
                        portAsString = addressParts[1];
                    } else {
                        host = address;
                    }
                }
                if (StringUtils.isEmpty(host)) {
                    host = "sas-rabbitmq-server";
                }
                // RabbitMQ host was configured, so set it in ConnectionFactory
                log.debug("Setting RabbitMQ host to: " + host);
                connectionFactory.setHost(host);
                if (StringUtils.isEmpty(portAsString)) {
                    portAsString = "5672";
                }
                try {
                    int port = Integer.valueOf(portAsString);
                    // RabbitMQ port was configured, so set it in ConnectionFactory
                    log.debug("Setting RabbitMQ port to: " + portAsString);
                    connectionFactory.setPort(port);
                } catch (NumberFormatException nfe) {
                    log.error("Could not convert '{}' to a port number.  Default port will be used.", portAsString);
                }
                String username = System.getenv("RABBITMQ_USER");
                String password = System.getenv("RABBITMQ_PASS");
                if (cfgProperties != null) {
                    username = (String) cfgProperties.getOrDefault("username", username);
                    password = (String) cfgProperties.getOrDefault("password", password);
                }
                if (StringUtils.isNotEmpty(username)) {
                    // RabbitMQ username was configured, so set it in ConnectionFactory
                    log.debug("Setting RabbitMQ username");
                    connectionFactory.setUsername(username);
                }
                if (StringUtils.isNotEmpty(password)) {
                    // RabbitMQ password was configured, so set it in ConnectionFactory
                    log.debug("Setting RabbitMQ password");
                    connectionFactory.setPassword(password);
                }
            }
        }
        log.debug("Create RabbitMQ connection");
        Connection connection = connectionFactory.newConnection();
        log.debug("Create RabbitMQ channel");
        channel = connection.createChannel();
        log.debug("Declare RabbitMQ exchange: " + EXCHANGE_NAME);
        channel.exchangeDeclare(EXCHANGE_NAME, "topic", true);
        log.debug("Declare RabbitMQ queue");
        queueName = channel.queueDeclare().getQueue();
        log.debug("Bind RabbitMQ queue: {}", queueName);
        channel.queueBind(queueName, EXCHANGE_NAME, "application.integration.catalog.change.*.success");
        log.debug("Finished establishment of RabbitMQ connection");
    } catch (Exception e) {
        log.error("Failed to initialize RabbitMQ connection " + e.getMessage());
    }
    auditLog.logMessage(methodName, AuditCode.EVENT_MAPPER_INITIALIZED.getMessageDefinition(repositoryConnector.getServerName()));
}
Also used : ConnectionFactory(com.rabbitmq.client.ConnectionFactory) ConnectionProperties(org.odpi.openmetadata.frameworks.connectors.properties.ConnectionProperties) Connection(com.rabbitmq.client.Connection) SASCatalogObject(org.odpi.openmetadata.connector.sas.repository.connector.mapping.SASCatalogObject) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) EndpointProperties(org.odpi.openmetadata.frameworks.connectors.properties.EndpointProperties) IOException(java.io.IOException) ConnectorCheckedException(org.odpi.openmetadata.frameworks.connectors.ffdc.ConnectorCheckedException) RepositoryErrorException(org.odpi.openmetadata.repositoryservices.ffdc.exception.RepositoryErrorException)

Aggregations

ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Connection (com.rabbitmq.client.Connection)1 ConnectionFactory (com.rabbitmq.client.ConnectionFactory)1 IOException (java.io.IOException)1 SASCatalogObject (org.odpi.openmetadata.connector.sas.repository.connector.mapping.SASCatalogObject)1 ConnectorCheckedException (org.odpi.openmetadata.frameworks.connectors.ffdc.ConnectorCheckedException)1 ConnectionProperties (org.odpi.openmetadata.frameworks.connectors.properties.ConnectionProperties)1 EndpointProperties (org.odpi.openmetadata.frameworks.connectors.properties.EndpointProperties)1 RepositoryErrorException (org.odpi.openmetadata.repositoryservices.ffdc.exception.RepositoryErrorException)1