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

Failed to inject scheduler dependency when using Spring Boot bootJar / fatJar #588

Open
2 of 3 tasks
skhro87 opened this issue Dec 19, 2024 · 0 comments
Open
2 of 3 tasks

Comments

@skhro87
Copy link

skhro87 commented Dec 19, 2024

Prerequisites

Please answer the following questions for yourself before submitting an issue. YOU MAY DELETE THE PREREQUISITES SECTION.

  • I am running the latest version (I'm using 15.0.0)
  • I checked the documentation and found no answer
  • I checked to make sure that this issue has not already been filed

Current/Expected Behavior

I have a Spring Boot application with Spring Boot 2.6.5 and added com.github.kagkarlsson:db-scheduler-spring-boot-starter:15.0.0 and I'm successfully running multiple scheduled tasks.
I am now trying to add two simple APIs e.g. GET /scheduled_tasks and POST /scheduled_tasks/launch for Admin users to list/trigger the scheduled tasks.
However, I am struggling to inject the scheduler dependency into my controller/service.

I have first tried the following code:

@Service
class ScheduledTasksService {
    @Autowired
    private lateinit var scheduler: Scheduler

    fun index(): ScheduledTasksIndexResDto {
        return ScheduledTasksIndexResDto(
            tasks = scheduler.scheduledExecutions.map { execution ->
                ScheduledTasksIndexResItemDto(
                    task_name = execution.taskInstance.taskName
                )
            }
        )
    }

    fun launch(dtoReq: ScheduledTasksLaunchReqDto) {
        // find task
        val taskInstanceId = scheduler.scheduledExecutions.firstOrNull {
            it.taskInstance.taskName == dtoReq.task_name
        }
            ?.taskInstance
            ?: throw ResponseStatusException(HttpStatus.BAD_REQUEST, "unknown task name ${dtoReq.task_name}")

        // trigger once
        scheduler.reschedule(taskInstanceId, Instant.now())
    }
}

which results in

***************************
APPLICATION FAILED TO START
***************************

Description:

Field schedulerClient in backend.services.ScheduledTasksService required a bean of type 'com.github.kagkarlsson.scheduler.SchedulerClient' that could not be found.

The injection point has the following annotations:
	- @org.springframework.beans.factory.annotation.Autowired(required=true)


Action:

Consider defining a bean of type 'com.github.kagkarlsson.scheduler.SchedulerClient' in your configuration.

I have also tried to use a custom BeanUtil to load the scheduler, as suggested in this issue :

@Service
class ScheduledTasksService() {
    fun index(): ScheduledTasksIndexResDto {
        val scheduler = BeanUtil.getBean(Scheduler::class.java)

        return ScheduledTasksIndexResDto(
            tasks = scheduler.scheduledExecutions.map { execution ->
                ScheduledTasksIndexResItemDto(
                    task_name = execution.taskInstance.taskName
                )
            }
        )
    }

    fun launch(dtoReq: ScheduledTasksLaunchReqDto) {
        val scheduler = BeanUtil.getBean(Scheduler::class.java)

        // find task
        val taskInstanceId = scheduler.scheduledExecutions.firstOrNull {
            it.taskInstance.taskName == dtoReq.task_name
        }
            ?.taskInstance
            ?: throw ResponseStatusException(HttpStatus.BAD_REQUEST, "unknown task name ${dtoReq.task_name}")

        // trigger once
        scheduler.reschedule(taskInstanceId, Instant.now())
    }
}

This allows me to start the application, but when I actually try to call the API, I'm getting:

org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.github.kagkarlsson.scheduler.Scheduler' available\n\tat org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:351)\n\tat org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:342)\n\tat org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1175)\n\tat backend.util.BeanUtil.getBean(BeanUtil.kt:24)\n\tat 

The irritating thing is that when I run the application without bootJar, it works.
When I compile via bootJar, I get the errors above.

My bootJar configuration is the following:

tasks.getByName<org.springframework.boot.gradle.tasks.bundling.BootJar>("bootJar") {
    mainClass.set("backend.BackendApplication")
}

I am pretty sure it's a misconfiguration with the bootJar, but I can't figure out how to fix it.
I tried to search issues and discussions here, and googled for other information, but couldn't fix it.

I would highly appreciate if someone could point me in the right direction. 🙏🏻

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant