package com.sourcefellows.patterns.outbox;

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.Service;

@Service
public class OutboxProcessor {

    private static final Logger log = LoggerFactory.getLogger(OutboxProcessor.class);

    private final OutboxRepository outboxRepository;

    @Autowired
    public OutboxProcessor(OutboxRepository outboxRepository) {
        this.outboxRepository = outboxRepository;
    }

    @Scheduled(fixedRate = 5000)
    public void sendMessages() {
        log.info("sendMessages");
        this.outboxRepository.findAll().forEach(outbox -> {
            log.info("sendMessages " + outbox.toString());
            this.outboxRepository.delete(outbox);
        });
    }

}
