Skip to content
This repository was archived by the owner on Dec 2, 2021. It is now read-only.

Defining a job as a prototype scoped bean causes a cyclic dependency on startup #11

Open
longwa opened this issue Nov 5, 2018 · 3 comments

Comments

@longwa
Copy link

longwa commented Nov 5, 2018

If you define a bean that implements SchwartzJob as prototype scoped in resources.groovy

    'testJob'(TestJob) { bean ->
        bean.scope = 'prototype'
    }

You will get the following on startup:

The dependencies of some of the beans in the application context form a cycle:

┌─────┐
|  testJob (field private com.agileorbit.schwartz.QuartzService schwartztest.TestJob.com_agileorbit_schwartz_SchwartzJob__quartzService)
↑     ↓
|  schwartzQuartzService (field protected com.agileorbit.schwartz.SchwartzJob[] com.agileorbit.schwartz.QuartzService.jobs)
└─────┘

I guess in the singleton case, the beans injected into the job array don't need to be initialize again, so it works.

The fix is simple, the quartzService reference in SchwartzJob just needs to be annotated with @Lazy:

@Lazy @Autowired QuartzService quartzService

Simple project to reproduce:
https://github.com/longwa/grails-schwartzTest

longwa pushed a commit to longwa/grails-schwartz that referenced this issue Nov 5, 2018
@longwa
Copy link
Author

longwa commented Nov 5, 2018

Weirdly, this fix works for the sample application but still fails in our actual application for what appears to to be the same reason. The injected jobs are only used in one place so maybe it would just be better to lookup the bean via grailsApplication and not inject them at all.

@longwa
Copy link
Author

longwa commented Nov 5, 2018

Actually, looks like if you declare the bean as bean.autowire = 'byName' it will still fail with the same exception. Presumably that overrides the @Autowired behavior on the service and supercedes the lazy-ness.

longwa pushed a commit to longwa/grails-schwartz that referenced this issue Nov 5, 2018
@longwa
Copy link
Author

longwa commented Nov 5, 2018

Updated the PR to just fetch the jobs on demand when schedule is called instead of injecting them. This makes it compatible regardless of how the user configures the wiring behavior.

longwa pushed a commit to longwa/grails-schwartz that referenced this issue Dec 17, 2020
longwa pushed a commit to longwa/grails-schwartz that referenced this issue Dec 17, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant