com.dell.cpsd.rcm.evaluation.client.amqp.config.RcmEvaluationRabbitConfig Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of rcm-evaluation-service-api Show documentation
Show all versions of rcm-evaluation-service-api Show documentation
This repository contains the source code for the rcm evaluation service API.
Use this repository to share contracts between services so you can create POJOs using defined JSON schemas and to create message transformers for the RCM evaluation service.
The newest version!
/** * Copyright © 2017 Dell Inc. or its subsidiaries. All Rights Reserved. * Dell EMC Confidential/Proprietary Information */ package com.dell.cpsd.rcm.evaluation.client.amqp.config; import com.dell.cpsd.common.logging.ILogger; import com.dell.cpsd.common.rabbitmq.MessageAnnotationProcessor; import com.dell.cpsd.common.rabbitmq.MessageAnnotationProcessorCallback; import com.dell.cpsd.common.rabbitmq.config.IRabbitMqPropertiesConfig; import com.dell.cpsd.rcm.evaluation.client.log.RFESLoggingManager; import com.dell.cpsd.rcm.evaluation.client.log.RFESMessageCode; import com.dell.cpsd.rcm.evaluation.service.api.EvaluateSystemRcmMessage; import com.dell.cpsd.rcm.evaluation.service.api.RcmEvaluationErrorMessage; import com.dell.cpsd.rcm.evaluation.service.api.SystemRcmEvaluationMessage; import com.dell.cpsd.service.common.client.context.IConsumerContextConfig; import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; import org.springframework.amqp.core.AmqpAdmin; import org.springframework.amqp.core.Binding; import org.springframework.amqp.core.BindingBuilder; import org.springframework.amqp.core.Queue; import org.springframework.amqp.core.TopicExchange; import org.springframework.amqp.rabbit.connection.ConnectionFactory; import org.springframework.amqp.rabbit.core.RabbitAdmin; import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.amqp.support.converter.ClassMapper; import org.springframework.amqp.support.converter.DefaultClassMapper; import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter; import org.springframework.amqp.support.converter.MessageConverter; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.env.Environment; import org.springframework.retry.backoff.ExponentialBackOffPolicy; import org.springframework.retry.policy.SimpleRetryPolicy; import org.springframework.retry.support.RetryTemplate; import java.net.InetAddress; import java.net.UnknownHostException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; /** * This is the configuration for the RabbitMQ artifacts used by a client. * *
for the message converter. * * @since SINCE-TBD */ @Bean ClassMapper rcmEvaluationClassMapper() { //stub final DefaultClassMapper classMapper = new DefaultClassMapper(); final Map* Copyright © 2017 Dell Inc. or its subsidiaries. All Rights Reserved. * Dell EMC Confidential/Proprietary Information *
* * @version 1.0 * * @since SINCE-TBD */ @Configuration public class RcmEvaluationRabbitConfig { /* * The logger for this class. */ private static final ILogger LOGGER = RFESLoggingManager.getLogger(RcmEvaluationRabbitConfig.class); /* * The retry template information for the client. */ private static final int MAX_ATTEMPTS = 10; private static final int INITIAL_INTERVAL = 100; private static final double MULTIPLIER = 2.0; private static final int MAX_INTERVAL = 50000; private static final String ISO8601_DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSSX"; /* * The binding key to the service message queue. */ private static final String BINDING_RFES_RCM_EVALUATION_RESPONSE = "dell.cpsd.rfes.rcm.evaluation.response"; /* * The fragment of the service message queue name. */ private static final String QUEUE_RFES_RCM_EVALUATION_RESPONSE = "queue.dell.cpsd.rfes.rcm.evaluation.response"; /* * The name of the service exchange. */ private static final String EXCHANGE_RFES_RCM_EVALUATION_RESPONSE = "exchange.dell.cpsd.rfes.rcm.evaluation.response"; /* * The routing key to the service request message queue. */ private static final String ROUTING_RFES_RCM_EVALUATION_REQUEST = "dell.cpsd.rfes.rcm.evaluation.request"; /* * The name of the service request exchange. */ private static final String EXCHANGE_RFES_RCM_EVALUATION_REQUEST = "exchange.dell.cpsd.rfes.rcm.evaluation.request"; /* * The RabbitMQ connection factory. */ @Autowired @Qualifier("rabbitConnectionFactory") private ConnectionFactory rabbitConnectionFactory; /* * The configuration properties for the client. */ @Autowired @Qualifier("rabbitPropertiesConfig") private IRabbitMqPropertiesConfig propertiesConfig; /* * The configuration properties for the client. */ @Autowired private IConsumerContextConfig consumerContextConfig; /* * The environment properties. */ @Autowired private Environment environment; /** * This returns the RabbitMQ template used by the producer. * * @return The RabbitMQ the rabbit template used by the producer. * * @since SINCE-TBD */ @Bean RabbitTemplate rabbitTemplate() { RabbitTemplate template = new RabbitTemplate(rabbitConnectionFactory); template.setMessageConverter(rcmEvaluationMessageConverter()); template.setRetryTemplate(retryTemplate()); return template; } /** * This returns theRetryTemplate
for theRabbitTemplate *
. * * @return TheRetryTemplate
. * * @since SINCE-TBD */ @Bean RetryTemplate retryTemplate() { RetryTemplate retryTemplate = new RetryTemplate(); ExponentialBackOffPolicy backOffPolicy = new ExponentialBackOffPolicy(); backOffPolicy.setInitialInterval(INITIAL_INTERVAL); backOffPolicy.setMultiplier(MULTIPLIER); backOffPolicy.setMaxInterval(MAX_INTERVAL); SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy(); retryPolicy.setMaxAttempts(MAX_ATTEMPTS); retryTemplate.setBackOffPolicy(backOffPolicy); retryTemplate.setRetryPolicy(retryPolicy); return retryTemplate; } /** * This returns theMessageConverter
for the *RabbitTemplate
. * * @return TheMessageConverter
for the template. * * @since SINCE-TBD */ @Bean public MessageConverter rcmEvaluationMessageConverter() { Jackson2JsonMessageConverter messageConverter = new Jackson2JsonMessageConverter(); messageConverter.setClassMapper(rcmEvaluationClassMapper()); messageConverter.setCreateMessageIds(true); final ObjectMapper objectMapper = new ObjectMapper(); // use ISO8601 format for dates objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); objectMapper.setDateFormat(new SimpleDateFormat(ISO8601_DATE_FORMAT)); // ignore properties we don't need or aren't expecting objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); messageConverter.setJsonObjectMapper(objectMapper); return messageConverter; } /** * This returns theClassMapper
for the message converter. * * @return TheClassMapper > classMappings = new HashMap<>(); List > messageClasses = new ArrayList >(); messageClasses.add(EvaluateSystemRcmMessage.class); messageClasses.add(SystemRcmEvaluationMessage.class); messageClasses.add(RcmEvaluationErrorMessage.class); final MessageAnnotationProcessor messageAnnotationProcessor = new MessageAnnotationProcessor(); messageAnnotationProcessor.process(new MessageAnnotationProcessorCallback() { @Override public void found(String messageType, Class messageClass) { classMappings.put(messageType, messageClass); } }, messageClasses); classMapper.setIdClassMapping(classMappings); return classMapper; } /** * This returns the host name for the client. * First it tries to use "container.id" system property, if not available local host name is used. * * @return The host name for the client. * @since 1.0 */ @Bean @Qualifier("hostName") String hostName() { String containerId = null; try { containerId = System.getProperty("container.id"); LOGGER.debug("System property container.id=" + containerId); } catch (final Exception e) { LOGGER.error("System property container.id unavailable"); } if(containerId == null) { try { containerId = InetAddress.getLocalHost().getHostName(); LOGGER.debug("Localhost name=" + containerId); } catch (final UnknownHostException eh) { throw new RuntimeException("Unable to identify hostname", eh); } } return containerId; } /** * This returns the TopicExchange
for the service response * messages. * * @return TheTopicExchange
for the service responses. * * @since SINCE-TBD */ @Bean TopicExchange rcmEvaluationResponseExchange() { return new TopicExchange(EXCHANGE_RFES_RCM_EVALUATION_RESPONSE); } /** * This returns theTopicExchange
for the service request * messages. * * @return TheTopicExchange
for message from the service. * * @since SINCE-TBD */ @Bean TopicExchange rcmEvaluationRequestExchange() { return new TopicExchange(EXCHANGE_RFES_RCM_EVALUATION_REQUEST); } /** * This returns theQueue
for response messages from the * service. * * @return TheQueue
for service response messages. * * @since SINCE-TBD */ @Bean Queue rcmEvaluationResponseQueue() { String bindingPostFix = this.getResponseQueuePostfix(); StringBuilder builder = new StringBuilder(); builder.append(QUEUE_RFES_RCM_EVALUATION_RESPONSE); builder.append("."); builder.append(bindingPostFix); String queueName = builder.toString(); Object[] lparams = {queueName}; LOGGER.info(RFESMessageCode.SERVICE_RESPONSE_QUEUE_I.getMessageCode(), lparams); boolean stateful = this.consumerContextConfig.stateful(); Queue queue = new Queue(queueName, true, false, !stateful); return queue; } /** * This returns theBinding
for the service response message * queue and exchange. * * @return TheBinding
for the service response message queue. * * @since SINCE-TBD */ @Bean public Binding rcmEvaluationResponseQueueBinding() { String bindingPostFix = this.getResponseQueuePostfix(); StringBuilder builder = new StringBuilder(); builder.append(BINDING_RFES_RCM_EVALUATION_RESPONSE); builder.append("."); builder.append(bindingPostFix); String binding = builder.toString(); Object[] lparams = {binding}; LOGGER.info(RFESMessageCode.SERVICE_RESPONSE_BINDING_I.getMessageCode(), lparams); return BindingBuilder.bind(rcmEvaluationResponseQueue()).to(rcmEvaluationResponseExchange()).with(binding); } /** * The returns the routing key for the service request message queue. * * @return The routing key for the service request message queue. * * @since SINCE-TBD */ @Bean public String rcmEvaluationRequestRoutingKey() { return ROUTING_RFES_RCM_EVALUATION_REQUEST; } /** * This returns theAmqpAdmin
for the connection factory. * * @return The AMQP admin object for the connection factory. * * @since SINCE-TBD */ @Bean AmqpAdmin amqpAdmin() { return new RabbitAdmin(rabbitConnectionFactory); } /* * This returns the generated postfix that is appended to the service * response message queue and exchange binding. * * @return The generated postfix. * * @since SINCE-TBD */ private String getResponseQueuePostfix() { return this.consumerContextConfig.consumerUuid(); } }