Skip to main content

spring_boot_starter_scheduling 0.4.2 copy "spring_boot_starter_scheduling: ^0.4.2" to clipboard
spring_boot_starter_scheduling: ^0.4.2 copied to clipboard

Scheduling starter for spring_boot_dart: @Scheduled methods with fixed rate, fixed delay or cron expressions, a TaskScheduler bean, stopped with the context.

example/example.dart

// Scheduled tasks in a Spring Boot application: @Scheduled methods on a
// bean; the generator registers them and picks the starter up
// (example.spring.dart, from `dart run build_runner build`).
//
//   dart run example/example.dart
//   dart run example/example.dart --heartbeat.rate=500ms

// An example: printing to the console is how it shows its result.
// ignore_for_file: avoid_print
import 'dart:async';

import 'package:spring_boot_starter_scheduling/spring_boot_starter_scheduling.dart';

import 'example.spring.dart';

@Component()
class Heartbeat {
  int beats = 0;

  /// Every second, or `heartbeat.rate`.
  @Scheduled(fixedRateString: r'${heartbeat.rate:1s}')
  void beat() => print('beat ${++beats}');

  /// Every 2 seconds, by a cron expression (with seconds).
  @Scheduled(cron: '*/2 * * * * *')
  Future<void> report() async => print('report at ${DateTime.now()}');
}

/// Code can schedule work too, through the `TaskScheduler` bean.
@Component()
class Reminder implements CommandLineRunner {
  Reminder(this.scheduler);

  final TaskScheduler scheduler;

  @override
  void run(List<String> args) => scheduler.schedule(
    () => print('one-off after 1.5 s'),
    OneShotTrigger(scheduler.now().add(const Duration(milliseconds: 1500))),
  );
}

@SpringBootApplication()
Future<void> main(List<String> args) async {
  // Scheduled tasks keep an application running; this one stops after
  // three seconds. Closing the context cancels every task.
  final context = runApplication(args);
  await Future<void>.delayed(const Duration(seconds: 3));
  final heartbeat = context.getBean<Heartbeat>();
  await context.closeAsync();
  print('${heartbeat.beats} beats');
}
0
likes
0
points
650
downloads

Publisher

verified publisherfarum-azula.org

Weekly Downloads

Scheduling starter for spring_boot_dart: @Scheduled methods with fixed rate, fixed delay or cron expressions, a TaskScheduler bean, stopped with the context.

Repository (GitHub)
View/report issues

Topics

#scheduling #cron #spring

License

unknown (license)

Dependencies

spring_boot_dart, spring_boot_starter_logging

More

Packages that depend on spring_boot_starter_scheduling