diff --git a/buildSrc/src/main/java/io/micronaut/guides/feature/LogbackTest.java b/buildSrc/src/main/java/io/micronaut/guides/feature/LogbackTest.java new file mode 100644 index 0000000000..b98ec06433 --- /dev/null +++ b/buildSrc/src/main/java/io/micronaut/guides/feature/LogbackTest.java @@ -0,0 +1,34 @@ +package io.micronaut.guides.feature; + +import io.micronaut.core.annotation.NonNull; +import io.micronaut.starter.application.ApplicationType; +import io.micronaut.starter.application.generator.GeneratorContext; +import io.micronaut.starter.build.dependencies.Dependency; +import io.micronaut.starter.feature.Feature; +import jakarta.inject.Singleton; + +@Singleton +public class LogbackTest implements Feature { + public static final String GROUP_ID_LOGBACK = "ch.qos.logback"; + public static final String ARTIFACT_ID_LOGBACK_CLASSIC = "logback-classic"; + private static final Dependency LOGBACK_CLASSIC_TEST = Dependency.builder() + .groupId(GROUP_ID_LOGBACK) + .artifactId(ARTIFACT_ID_LOGBACK_CLASSIC) + .test() + .build(); + + @Override + public @NonNull String getName() { + return "logback-test"; + } + + @Override + public boolean supports(ApplicationType applicationType) { + return true; + } + + @Override + public void apply(GeneratorContext generatorContext) { + generatorContext.addDependency(LOGBACK_CLASSIC_TEST); + } +} diff --git a/guides/spring-boot-to-micronaut-scheduling-tasks/metadata.json b/guides/spring-boot-to-micronaut-scheduling-tasks/metadata.json new file mode 100644 index 0000000000..a9310bc3cc --- /dev/null +++ b/guides/spring-boot-to-micronaut-scheduling-tasks/metadata.json @@ -0,0 +1,21 @@ +{ + "title": "Application Class - Spring Boot to Micronaut Framework", + "intro": "This guide shows compares the Application Class of a Spring Boot application vs a Micronaut Framework application.", + "authors": ["Sergio del Amo"], + "tags": ["spring-boot"], + "categories": ["Spring Boot to Micronaut Framework"], + "publicationDate": "2022-09-13", + "languages": ["java"], + "apps": [ + { + "framework": "Spring Boot", + "testFramework": "junit", + "name": "springboot", + "features": ["spring-boot-starter-web", "mockito", "awaitility"] + }, + { + "name": "micronautframework", + "features": ["awaitility", "logback-test"] + } + ] +} diff --git a/guides/spring-boot-to-micronaut-scheduling-tasks/micronautframework/java/src/main/java/example/micronaut/ScheduledTasks.java b/guides/spring-boot-to-micronaut-scheduling-tasks/micronautframework/java/src/main/java/example/micronaut/ScheduledTasks.java new file mode 100644 index 0000000000..c4adddd27e --- /dev/null +++ b/guides/spring-boot-to-micronaut-scheduling-tasks/micronautframework/java/src/main/java/example/micronaut/ScheduledTasks.java @@ -0,0 +1,23 @@ +package example.micronaut; + +import java.text.SimpleDateFormat; +import java.util.Date; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import io.micronaut.scheduling.annotation.Scheduled; +import jakarta.inject.Singleton; + +@Singleton +public class ScheduledTasks { + private static final Logger LOG = LoggerFactory.getLogger(ScheduledTasks.class); + + private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("HH:mm:ss"); + + @Scheduled(fixedRate = "5s") + public void reportCurrentTime() { + if (LOG.isInfoEnabled()) { + LOG.info("The time is now {}", DATE_FORMAT.format(new Date())); + } + } +} \ No newline at end of file diff --git a/guides/spring-boot-to-micronaut-scheduling-tasks/micronautframework/java/src/test/java/example/micronaut/ScheduledTasksTest.java b/guides/spring-boot-to-micronaut-scheduling-tasks/micronautframework/java/src/test/java/example/micronaut/ScheduledTasksTest.java new file mode 100644 index 0000000000..72bc207502 --- /dev/null +++ b/guides/spring-boot-to-micronaut-scheduling-tasks/micronautframework/java/src/test/java/example/micronaut/ScheduledTasksTest.java @@ -0,0 +1,28 @@ +package example.micronaut; + +import io.micronaut.test.extensions.junit5.annotation.MicronautTest; +import org.awaitility.Durations; +import org.junit.jupiter.api.Test; +import ch.qos.logback.classic.spi.ILoggingEvent; +import ch.qos.logback.core.read.ListAppender; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import static org.awaitility.Awaitility.await; +import static org.junit.jupiter.api.Assertions.assertTrue; + +@MicronautTest +class ScheduledTasksTest { + @Test + void reportCurrentTime() { + ListAppender listAppender = new ListAppender<>(); + listAppender.start(); + Logger logger = LoggerFactory.getLogger(ScheduledTasks.class); + if (logger instanceof ch.qos.logback.classic.Logger logbackLogger) { + logbackLogger.addAppender(listAppender); + } + await().atMost(Durations.TEN_SECONDS).untilAsserted(() -> { + assertTrue(listAppender.list.size() >= 2); + }); + } +} \ No newline at end of file diff --git a/guides/spring-boot-to-micronaut-scheduling-tasks/spring-boot-to-micronaut-scheduling-tasks.adoc b/guides/spring-boot-to-micronaut-scheduling-tasks/spring-boot-to-micronaut-scheduling-tasks.adoc new file mode 100644 index 0000000000..8587d8c166 --- /dev/null +++ b/guides/spring-boot-to-micronaut-scheduling-tasks/spring-boot-to-micronaut-scheduling-tasks.adoc @@ -0,0 +1,31 @@ +common:header-top.adoc[] + +== Sample Project + +You can link:@sourceDir@.zip[download a sample application] with the code examples shown in this article. + + +https://spring.io/guides/gs/scheduling-tasks/[Spring Scheduling Tasks] + +== Introduction + +Spring Boot and Micronaut applications contain a simple application class which starts the application for you. + +== Spring Boot Application Class + +source:Application[app=springboot] + +TODO enabledScheduling + + +source:ScheduledTasks[app=springboot] + +source:ScheduledTasks[app=micronautframework] + +== Next steps + +Read more about https://micronaut-projects.github.io/micronaut-spring/latest/guide/[Micronaut Spring]. + +common:helpWithMicronaut.adoc[] + + diff --git a/guides/spring-boot-to-micronaut-scheduling-tasks/springboot/java/src/main/java/example/micronaut/Application.java b/guides/spring-boot-to-micronaut-scheduling-tasks/springboot/java/src/main/java/example/micronaut/Application.java new file mode 100644 index 0000000000..e43e2c1d21 --- /dev/null +++ b/guides/spring-boot-to-micronaut-scheduling-tasks/springboot/java/src/main/java/example/micronaut/Application.java @@ -0,0 +1,13 @@ +package example.micronaut; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.scheduling.annotation.EnableScheduling; + +@SpringBootApplication +@EnableScheduling +public class Application { + public static void main(String[] args) { + SpringApplication.run(Application.class, args); + } +} diff --git a/guides/spring-boot-to-micronaut-scheduling-tasks/springboot/java/src/main/java/example/micronaut/ScheduledTasks.java b/guides/spring-boot-to-micronaut-scheduling-tasks/springboot/java/src/main/java/example/micronaut/ScheduledTasks.java new file mode 100644 index 0000000000..7f606d8939 --- /dev/null +++ b/guides/spring-boot-to-micronaut-scheduling-tasks/springboot/java/src/main/java/example/micronaut/ScheduledTasks.java @@ -0,0 +1,22 @@ +package example.micronaut; + +import java.text.SimpleDateFormat; +import java.util.Date; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Component; + +@Component +public class ScheduledTasks { + private static final Logger LOG = LoggerFactory.getLogger(ScheduledTasks.class); + private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("HH:mm:ss"); + + @Scheduled(fixedRate = 5000) + public void reportCurrentTime() { + if (LOG.isInfoEnabled()) { + LOG.info("The time is now {}", DATE_FORMAT.format(new Date())); + } + } +} \ No newline at end of file diff --git a/guides/spring-boot-to-micronaut-scheduling-tasks/springboot/java/src/test/java/example/micronaut/ScheduledTasksTest.java b/guides/spring-boot-to-micronaut-scheduling-tasks/springboot/java/src/test/java/example/micronaut/ScheduledTasksTest.java new file mode 100644 index 0000000000..d027254d86 --- /dev/null +++ b/guides/spring-boot-to-micronaut-scheduling-tasks/springboot/java/src/test/java/example/micronaut/ScheduledTasksTest.java @@ -0,0 +1,25 @@ +package example.micronaut; + +import org.awaitility.Durations; +import org.junit.jupiter.api.Test; + +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.mock.mockito.SpyBean; + +import static org.awaitility.Awaitility.await; +import static org.mockito.Mockito.atLeast; +import static org.mockito.Mockito.verify; + +@SpringBootTest +public class ScheduledTasksTest { + + @SpyBean + ScheduledTasks tasks; + + @Test + public void reportCurrentTime() { + await().atMost(Durations.TEN_SECONDS).untilAsserted(() -> { + verify(tasks, atLeast(2)).reportCurrentTime(); + }); + } +} \ No newline at end of file diff --git a/guides/spring-boot-to-micronaut-scheduling-tasks/springboot/java/src/test/java/example/micronaut/SchedulingTasksApplicationTest.java b/guides/spring-boot-to-micronaut-scheduling-tasks/springboot/java/src/test/java/example/micronaut/SchedulingTasksApplicationTest.java new file mode 100644 index 0000000000..4c6f30409a --- /dev/null +++ b/guides/spring-boot-to-micronaut-scheduling-tasks/springboot/java/src/test/java/example/micronaut/SchedulingTasksApplicationTest.java @@ -0,0 +1,20 @@ +package example.micronaut; + +import static org.assertj.core.api.Assertions.assertThat; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +public class SchedulingTasksApplicationTest { + + @Autowired + private ScheduledTasks tasks; + + @Test + public void contextLoads() { + // Basic integration test that shows the context starts up properly + assertThat(tasks).isNotNull(); + } + +} \ No newline at end of file