package com.tistory.heowc.sender;

import com.tistory.heowc.common.Message;
import com.tistory.heowc.common.Constant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.StopWatch;

import java.util.stream.LongStream;

@Component
public class SendScheduler {

	private final RabbitTemplate rabbitTemplate;

	private static final Logger logger = LoggerFactory.getLogger(SendScheduler.class);

	public SendScheduler(RabbitTemplate rabbitTemplate) {
		this.rabbitTemplate = rabbitTemplate;
	}

	@Scheduled(cron = "0/3 * * * * *")
	public void onSend() {
		logger.info("Sending message... Start");
		StopWatch stopWatch = new StopWatch();
		stopWatch.start();

		LongStream.range(1, 15000)
			.parallel()
			.mapToObj(i -> Message.of(i, "Hello, RabbitMQ!"))
			.forEach(message -> rabbitTemplate.convertAndSend(Constant.QUEUE_NAME, message));

		stopWatch.stop();
		logger.info(stopWatch.toString());
		logger.info("Sending message... End");
	}
}
