Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scheduling Tasks spring boot to Micronaut Framework [ci skip] #1308

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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);
}
}
21 changes: 21 additions & 0 deletions guides/spring-boot-to-micronaut-scheduling-tasks/metadata.json
Original file line number Diff line number Diff line change
@@ -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"]
}
]
}
Original file line number Diff line number Diff line change
@@ -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()));
}
}
}
Original file line number Diff line number Diff line change
@@ -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<ILoggingEvent> 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);
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
common:header-top.adoc[]

== Sample Project

You can link:@[email protected][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[]


Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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()));
}
}
}
Original file line number Diff line number Diff line change
@@ -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();
});
}
}
Original file line number Diff line number Diff line change
@@ -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();
}

}