fi.evolver.basics.spring.messaging.MessageSenderTask Maven / Gradle / Ivy
package fi.evolver.basics.spring.messaging;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.Collections;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import fi.evolver.basics.spring.messaging.model.PendingTarget;
import fi.evolver.basics.spring.status.model.ComponentStatus;
import fi.evolver.basics.spring.status.model.Reportable;
@Component
public class MessageSenderTask implements Reportable {
private static final Logger LOG = LoggerFactory.getLogger(MessageSenderTask.class);
private final MessageRepository messageRepository;
private final IMessageSender messageSender;
private LocalDateTime lastRun;
@Autowired
public MessageSenderTask(MessageRepository messageRepository, IMessageSender messageSender) {
this.messageRepository = messageRepository;
this.messageSender = messageSender;
}
@Scheduled(fixedDelay = 10_000)
public void sendPendingMessages() {
try {
lastRun = LocalDateTime.now();
List targets = messageRepository.findTargetsWithPendingMessages();
for (PendingTarget target: targets)
messageSender.sendPendingMessages(target.getMessageTargetConfigId(), target.getMessageGroupId());
}
catch (Exception e) {
LOG.error("Issues scheduling message sending", e);
}
}
@Override
public List getStatus() {
return Collections.singletonList(new ComponentStatus(
"Bean",
getClass().getSimpleName(),
Collections.singletonMap("LastRunMsAgo", ChronoUnit.MILLIS.between(lastRun, LocalDateTime.now()))));
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy