Micronaut Integration

From Obsidian Scheduler
Revision as of 22:43, 27 December 2024 by Craig (talk | contribs) (Created page with "Though Obsidian works great on its own, Obsidian also supports seamless integration with [https://micronaut.io/ Micronaut]. This page outlines how to set up Micronaut integrat...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search

Though Obsidian works great on its own, Obsidian also supports seamless integration with Micronaut. This page outlines how to set up Micronaut integration and execute jobs which are configured as Micronaut components.

Dependency Injection via Micronaut

As of Obsidian 6.0.0, Obsidian has formalized the support and integration with your Micronaut applications, and now supports loading job implementations from the Micronaut context. Obsidian does not include any Micronaut libraries and will defer to the version and libraries you include with your application. It supports versions of Micronaut from 3.7 onward.

Obsidian leverages Micronaut's formal support including the @jakarta.inject.Singleton/@jakarta.inject.Scope and any extensions and specializations of those annotations. It is important to note that if a job class does not have one of these annotations, Obsidian will not use the Micronaut instance if it exists, and will instantiate the job directly.

Job classes which can be loaded from Micronaut can be either SchedulableJob implementations or annotated jobs. See Implementing Jobs for details on how to write and configure executable jobs.

Micronaut will need to be started and running wherever Obsidian scheduler runtimes exist, so it is recommended that you start Obsidian from your Application class. Below is a sample Application that starts Obsidian while also ensuring the Obsidian/Micronaut integration gets started via the @Import. This 'special sauce' is found in the com.carfey.ops.job.di.MicronautComponentLocator class.

import com.carfey.ops.job.SchedulerStarter;
import com.carfey.ops.job.SchedulerStarter.SchedulerMode;

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

@Import(packages = {"com.carfey.ops.job.di"})
public class Application {

	public static void main(String[] args) {
		ApplicationContext context = Micronaut.run(Application.class, args);

		SchedulerStarter.get(SchedulerMode.EMBEDDED);
	}
}

Sample Job:

import com.carfey.ops.job.Context;
import com.carfey.ops.job.SchedulableJob;

import jakarta.inject.Singleton;

@Singleton
public class MyMicronautSchedulerJob implements SchedulableJob {

	@Override
	public void execute(Context context) throws Exception {
		//do something here
		...
		context.saveJobResult("Job result", result);
	}

}