2 * ================================================================================
3 * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
4 * Copyright (c) 2021 China Mobile Property. All rights reserved.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 * ============LICENSE_END=========================================================
21 package org.onap.dcae.analytics.web.config;
23 import java.util.LinkedHashMap;
26 import org.onap.dcae.analytics.model.AnalyticsProfile;
27 import org.onap.dcae.analytics.model.DmaapMrConstants;
28 import org.springframework.context.annotation.Bean;
29 import org.springframework.context.annotation.Configuration;
30 import org.springframework.context.annotation.Import;
31 import org.springframework.context.annotation.Profile;
32 import org.springframework.integration.channel.QueueChannel;
33 import org.springframework.integration.dsl.IntegrationFlow;
34 import org.springframework.integration.dsl.IntegrationFlows;
35 import org.springframework.integration.dsl.MessageChannels;
36 import org.springframework.integration.handler.LoggingHandler;
37 import org.springframework.integration.handler.advice.ErrorMessageSendingRecoverer;
38 import org.springframework.integration.handler.advice.RequestHandlerRetryAdvice;
39 import org.springframework.integration.store.BasicMessageGroupStore;
40 import org.springframework.integration.store.MessageGroupQueue;
41 import org.springframework.messaging.MessageHandlingException;
42 import org.springframework.messaging.PollableChannel;
43 import org.springframework.retry.RetryPolicy;
44 import org.springframework.retry.backoff.BackOffPolicy;
45 import org.springframework.retry.backoff.ExponentialBackOffPolicy;
46 import org.springframework.retry.policy.SimpleRetryPolicy;
47 import org.springframework.retry.support.RetryTemplate;
48 import org.springframework.web.client.HttpStatusCodeException;
49 import org.springframework.web.client.RestClientException;
52 * @author Rajiv Singla
55 @Profile(AnalyticsProfile.DMAAP_PROFILE_NAME)
56 @Import(MessageStoreConfig.class)
57 public class DmaapRetryConfig {
60 public QueueChannel errorChannel() {
61 return MessageChannels.queue().get();
65 public IntegrationFlow loggingFlow() {
66 return IntegrationFlows.from(errorChannel())
67 .log(LoggingHandler.Level.ERROR)
72 public ErrorMessageSendingRecoverer errorMessageSendingRecoverer(final PollableChannel recoveryChannel) {
73 final ErrorMessageSendingRecoverer errorMessageSendingRecoverer = new ErrorMessageSendingRecoverer();
74 errorMessageSendingRecoverer.setChannel(recoveryChannel);
75 return errorMessageSendingRecoverer;
79 public PollableChannel recoveryChannel(final BasicMessageGroupStore basicMessageGroupStore) {
80 return MessageChannels.queue(new MessageGroupQueue(basicMessageGroupStore,
81 DmaapMrConstants.DMAAP_MR_PUBLISHER_RECOVERY_MESSAGE_STORE_GROUP_ID)).get();
85 public RequestHandlerRetryAdvice requestHandlerRetryAdvice(final RetryTemplate retryTemplate,
86 final ErrorMessageSendingRecoverer
87 errorMessageSendingRecoverer) {
88 final RequestHandlerRetryAdvice requestHandlerRetryAdvice = new RequestHandlerRetryAdvice();
89 requestHandlerRetryAdvice.setRetryTemplate(retryTemplate);
90 requestHandlerRetryAdvice.setRecoveryCallback(errorMessageSendingRecoverer);
91 return requestHandlerRetryAdvice;
95 public RetryTemplate retryTemplate(final RetryPolicy retryPolicy,
96 final BackOffPolicy backOffPolicy) {
97 final RetryTemplate retryTemplate = new RetryTemplate();
98 retryTemplate.setRetryPolicy(retryPolicy);
99 retryTemplate.setBackOffPolicy(backOffPolicy);
100 return retryTemplate;
104 public RetryPolicy retryPolicy() {
105 final Map<Class<? extends Throwable>, Boolean> retryableExceptions = new LinkedHashMap<>();
106 retryableExceptions.put(MessageHandlingException.class, true);
107 retryableExceptions.put(HttpStatusCodeException.class, true);
108 retryableExceptions.put(RestClientException.class, true);
109 return new SimpleRetryPolicy(DmaapMrConstants.DEFAULT_NUM_OF_RETRIES_ON_FAILURE, retryableExceptions);
113 public BackOffPolicy backOffPolicy() {
114 final ExponentialBackOffPolicy backOffPolicy = new ExponentialBackOffPolicy();
115 backOffPolicy.setInitialInterval(DmaapMrConstants.DEFAULT_RETRY_INITIAL_INTERVAL);
116 backOffPolicy.setMultiplier(DmaapMrConstants.DEFAULT_RETRY_MULTIPLIER);
117 backOffPolicy.setMaxInterval(DmaapMrConstants.DEFAULT_RETRY_MAX_INTERVAL);
118 return backOffPolicy;