or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

events.mdindex.mdruntime.mdscheduling.mdscopes.mdserver.mdshutdown.md
tile.json

tessl/maven-io-micronaut--micronaut-context

Context module for the Micronaut Framework that extends the micronaut-inject module with additional bean container services such as job scheduling with @Scheduled, event listeners with @EventListener, and immutable configuration properties

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
mavenpkg:maven/io.micronaut/micronaut-context@4.9.x

To install, run

npx @tessl/cli install tessl/maven-io-micronaut--micronaut-context@4.9.0

index.mddocs/

Micronaut Context

Micronaut Context is a core library module for the Micronaut Framework that extends the micronaut-inject module with additional bean container services. It provides advanced application runtime features including declarative job scheduling with @Scheduled annotations, reactive event handling with @EventListener annotations, and support for immutable configuration properties. The module serves as the foundation for building enterprise-grade JVM applications with features like application lifecycle management, graceful shutdown, configuration binding, and runtime bean management.

Package Information

  • Package Name: io.micronaut:micronaut-context
  • Package Type: maven
  • Language: Java
  • Installation: Add to your Maven pom.xml or Gradle build.gradle:

Maven:

<dependency>
    <groupId>io.micronaut</groupId>
    <artifactId>micronaut-context</artifactId>
    <version>4.9.4</version>
</dependency>

Gradle:

implementation 'io.micronaut:micronaut-context:4.9.4'

Core Imports

import io.micronaut.runtime.Micronaut;
import io.micronaut.context.ApplicationContext;
import io.micronaut.scheduling.annotation.Scheduled;
import io.micronaut.runtime.event.annotation.EventListener;
import io.micronaut.runtime.context.scope.Refreshable;

Basic Usage

import io.micronaut.runtime.Micronaut;
import io.micronaut.context.ApplicationContext;

public class Application {
    public static void main(String[] args) {
        // Start Micronaut application
        ApplicationContext context = Micronaut.run(Application.class, args);
        
        // Application runs until shutdown
        // Context automatically manages lifecycle
    }
}

// Scheduled task example
import io.micronaut.scheduling.annotation.Scheduled;
import jakarta.inject.Singleton;

@Singleton
public class TaskService {
    @Scheduled(fixedDelay = "30s")
    public void performMaintenanceTask() {
        System.out.println("Running maintenance task");
    }
    
    @Scheduled(cron = "0 0 2 * * ?") // Daily at 2 AM
    public void performDailyBackup() {
        System.out.println("Running daily backup");
    }
}

// Event listener example
import io.micronaut.runtime.event.annotation.EventListener;
import io.micronaut.runtime.server.event.ServerStartupEvent;
import jakarta.inject.Singleton;

@Singleton
public class StartupListener {
    @EventListener
    public void onStartup(ServerStartupEvent event) {
        System.out.println("Server started on: " + event.getSource().getURL());
    }
}

Architecture

Micronaut Context is built around several key architectural components:

  • Application Runtime: Core Micronaut class for bootstrapping and lifecycle management
  • Scheduling Engine: Annotation-driven task scheduling with CRON and fixed-rate support
  • Event System: Reactive event publishing and listening with @EventListener
  • Scope Management: Custom scopes including @Refreshable and @ThreadLocal
  • Server Abstraction: Embedded server lifecycle management and configuration
  • Graceful Shutdown: Coordinated shutdown of application components
  • Executor Framework: Thread pool management and task execution

Capabilities

Application Runtime

Core application bootstrapping and lifecycle management. The main entry point for running Micronaut applications with configuration support and embedded server management.

public class Micronaut extends DefaultApplicationContextBuilder {
    public static ApplicationContext run(String... args);
    public static ApplicationContext run(Class<?> cls, String... args);
    public static ApplicationContext run(Class<?>[] classes, String... args);
    public static Micronaut build(String... args);
    public ApplicationContext start();
}

public interface EmbeddedApplication<T> extends ApplicationContextLifeCycle<T> {
    ApplicationContext getApplicationContext();
    ApplicationConfiguration getApplicationConfiguration();
    Environment getEnvironment();
    boolean isServer();
    boolean isForceExit();
    boolean isShutdownHookNeeded();
}

Application Runtime

Task Scheduling

Declarative and programmatic task scheduling with CRON expressions, fixed delays, and custom executors. Supports both annotation-driven scheduling and programmatic task management.

@Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Repeatable(Schedules.class)
public @interface Scheduled {
    String cron() default "";
    String zoneId() default "";
    String fixedDelay() default "";
    String initialDelay() default "";
    String fixedRate() default "";
    String scheduler() default TaskExecutors.SCHEDULED;
    String condition() default "";
}

public interface TaskScheduler {
    ScheduledFuture<?> schedule(String cron, Runnable command);
    <V> ScheduledFuture<V> schedule(String cron, Callable<V> command);
    ScheduledFuture<?> schedule(Duration delay, Runnable command);
    ScheduledFuture<?> scheduleAtFixedRate(Duration initialDelay, Duration period, Runnable command);
    ScheduledFuture<?> scheduleWithFixedDelay(Duration initialDelay, Duration delay, Runnable command);
}

Task Scheduling

Event System

Application event publishing and listening with reactive support. Enables decoupled communication between application components through typed events.

@Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Adapter(ApplicationEventListener.class)
public @interface EventListener {
}

public class ApplicationStartupEvent extends AbstractEmbeddedApplicationEvent {
    public ApplicationStartupEvent(EmbeddedApplication<?> source);
}

public class ApplicationShutdownEvent extends AbstractEmbeddedApplicationEvent {
    public ApplicationShutdownEvent(EmbeddedApplication<?> source);
}

Event System

Scope Management

Custom bean scopes including refreshable configurations and thread-local storage. Enables advanced bean lifecycle management beyond singleton and prototype scopes.

@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Scope
@ScopedProxy
public @interface Refreshable {
    String[] value() default {};
}

@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Scope
@ScopedProxy
public @interface ThreadLocal {
    boolean lifecycle() default false;
}

public class RefreshEvent extends ApplicationEvent {
    public RefreshEvent(Map<String, Object> changes);
    public RefreshEvent();
    public Map<String, Object> getSource();
}

Scope Management

Server Lifecycle

Embedded server abstraction and lifecycle events. Provides unified interface for different server implementations with startup/shutdown coordination.

public interface EmbeddedServer extends EmbeddedApplication<EmbeddedServer> {
    int getPort();
    String getHost();
    String getScheme();
    URL getURL();
    URI getURI();
    URI getContextURI();
    boolean isKeepAlive();
}

public class ServerStartupEvent extends ApplicationStartupEvent {
    public ServerStartupEvent(EmbeddedServer embeddedServer);
    public EmbeddedServer getSource();
}

public class ServerShutdownEvent {
    // Event fired when EmbeddedServer shuts down
}

Server Lifecycle

Graceful Shutdown

Coordinated shutdown capabilities for application components. Enables clean resource cleanup and graceful termination of long-running operations.

public interface GracefulShutdownCapable {
    CompletionStage<?> shutdownGracefully();
    OptionalLong reportActiveTasks();
    static CompletionStage<?> allOf(Stream<CompletionStage<?>> stages);
    static CompletionStage<?> shutdownAll(Stream<? extends GracefulShutdownCapable> stages);
}

@Singleton
public class GracefulShutdownManager {
    public CompletionStage<?> shutdownGracefully();
    public OptionalLong reportActiveTasks();
}

Graceful Shutdown

Core Types

public class ApplicationConfiguration {
    public static final String PREFIX = "micronaut.application";
    public static final String APPLICATION_NAME = "micronaut.application.name";
    
    public Charset getDefaultCharset();
    public Optional<String> getName();
    public InstanceConfiguration getInstance();
}

public interface TaskExecutors {
    String IO = "io";
    String BLOCKING = "blocking";
    String VIRTUAL = "virtual";
    String SCHEDULED = "scheduled";
    String MESSAGE_CONSUMER = "consumer";
}

public class CronExpression {
    public static CronExpression create(String cronExpression);
    public ZonedDateTime nextExecution(ZonedDateTime lastExecution);
    public boolean matches(ZonedDateTime dateTime);
}

public enum LogLevel {
    ALL, TRACE, DEBUG, INFO, WARN, ERROR, OFF, NOT_SPECIFIED
}