2388b3bf81626b7b80d0a6d064fa267b347cf4cc
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021,2023 Nordix Foundation.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.clamp.acm.participant.intermediary.comm;
22
23 import java.io.Closeable;
24 import java.util.TimerTask;
25 import java.util.concurrent.Executors;
26 import java.util.concurrent.ScheduledExecutorService;
27 import java.util.concurrent.TimeUnit;
28 import org.onap.policy.clamp.acm.participant.intermediary.handler.ParticipantHandler;
29 import org.onap.policy.clamp.acm.participant.intermediary.parameters.ParticipantParameters;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 import org.springframework.context.event.ContextRefreshedEvent;
33 import org.springframework.context.event.EventListener;
34 import org.springframework.stereotype.Component;
35
36 /**
37  * This class sends messages from participants to CLAMP.
38  */
39 @Component
40 public class MessageSender extends TimerTask implements Closeable {
41     private static final Logger LOGGER = LoggerFactory.getLogger(MessageSender.class);
42
43     private final ParticipantHandler participantHandler;
44     private final ScheduledExecutorService timerPool;
45     private final long interval;
46
47     /**
48      * Constructor, set the publisher.
49      *
50      * @param participantHandler the participant handler to use for gathering information
51      * @param parameters the parameters of the participant
52      */
53     public MessageSender(ParticipantHandler participantHandler, ParticipantParameters parameters) {
54         this.participantHandler = participantHandler;
55
56         // Kick off the timer
57         timerPool = makeTimerPool();
58         interval = parameters.getIntermediaryParameters().getReportingTimeIntervalMs();
59     }
60
61     @EventListener
62     public void handleContextRefreshEvent(ContextRefreshedEvent ctxRefreshedEvent) {
63         timerPool.scheduleAtFixedRate(this, interval, interval, TimeUnit.MILLISECONDS);
64     }
65
66     @Override
67     public void run() {
68         LOGGER.debug("Sent heartbeat to CLAMP");
69         participantHandler.sendHeartbeat();
70     }
71
72     @Override
73     public void close() {
74         timerPool.shutdown();
75     }
76
77     /**
78      * Makes a new timer pool.
79      *
80      * @return a new timer pool
81      */
82     protected ScheduledExecutorService makeTimerPool() {
83         return Executors.newScheduledThreadPool(1);
84     }
85 }