Closed luhggit closed 4 years ago
It seems like the beanName
is absent. @fangjian0423 Could you please take a look at it?
It's a bug in the sentinel module of SCA[1].
For temporary solution don't introduce any bean without bean name(bean id).
In your code you can try:
@Slf4j
@Component("xxxTaskComponent") <<<----------------
public class XXXTask extends QuartzJobBean {
private static XXXService xxxService;
static {
xxxService = (XXXService) ApplicationUtils.getBean("xxxService"); <<<<------- It's not safe and you'd better replace it with @autowired or @resource or use bean dependency.
}
@Override
protected void executeInternal(JobExecutionContext jobExecutionContext) {
xxxService.xxxTask();
}
}
Related #1076.
I add two classes in sentinel-core-example:
@Configuration
public class QuartzConfig {
@Bean
public JobDetail xxxTask() {
return newJob(XXXTask.class).withIdentity("xxx").storeDurably().build();
}
@Bean
public Trigger xxxTrigger() {
//cron
return TriggerBuilder.newTrigger().forJob(xxxTask())
.withIdentity("xxx")
.withSchedule(CronScheduleBuilder.cronSchedule("0 0 */1 * * ?"))
.build();
}
}
@Component
public class XXXTask extends QuartzJobBean {
@Override
protected void executeInternal(JobExecutionContext jobExecutionContext) {
System.out.println("test");
}
}
Every thing is fine!
how to reproduce it?
@jasonjoo2010 Thank for you advice, but it doesn't work.
Even though I assign a component name to "XXXTask ", the bean name which was pass to SentinelBeanPostProcessor is still null
.
I trace this bug, and finally I find the reason, this bug cause by Quartz's AutowireCapableBeanJobFactory.class:
class AutowireCapableBeanJobFactory extends SpringBeanJobFactory {
private final AutowireCapableBeanFactory beanFactory;
AutowireCapableBeanJobFactory(AutowireCapableBeanFactory beanFactory) {
Assert.notNull(beanFactory, "Bean factory must not be null");
this.beanFactory = beanFactory;
}
@Override
protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception {
Object jobInstance = super.createJobInstance(bundle);
this.beanFactory.autowireBean(jobInstance);
// ↓↓↓↓↓↓↓↓ this line
this.beanFactory.initializeBean(jobInstance, null); <<<<------ this param of bean name is null
return jobInstance;
}
}
Now, what should I do is to rewrite the quartz job factory and replace the AutowireCapableBeanJobFactory
.Let't me try....
@fangjian0423 Oh, thank you , you give me a hint, when I replace the spring-cloud-starter-alibaba-sentinel
to sentinel-core
, my quartz job run successfully.
If you want to reproduce that bug, may be you should create a SpringCloud project and import the spring-cloud-starter-alibaba-sentinel
like this:
(pom.xml)
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
<version>2.0.0.RELEASE</version>
</dependency>
😂We might need to handle this scenario (though it's not the right way to register beans with null name).
@jasonjoo2010 Thank for you advice, but it doesn't work.
Even though I assign a component name to "XXXTask ", the bean name which was pass to SentinelBeanPostProcessor is still
null
.I trace this bug, and finally I find the reason, this bug cause by Quartz's AutowireCapableBeanJobFactory.class:
class AutowireCapableBeanJobFactory extends SpringBeanJobFactory { private final AutowireCapableBeanFactory beanFactory; AutowireCapableBeanJobFactory(AutowireCapableBeanFactory beanFactory) { Assert.notNull(beanFactory, "Bean factory must not be null"); this.beanFactory = beanFactory; } @Override protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception { Object jobInstance = super.createJobInstance(bundle); this.beanFactory.autowireBean(jobInstance); // ↓↓↓↓↓↓↓↓ this line this.beanFactory.initializeBean(jobInstance, null); <<<<------ this param of bean name is null return jobInstance; } }
Now, what should I do is to rewrite the quartz job factory and replace the
AutowireCapableBeanJobFactory
.Let't me try....
I think making your own patch on SCA is an easier way :)
@jasonjoo2010 Ok, if I have a time...
When I import alibaba sentinel into my SpringCloud project and start, My quartz job all fail initialize.How can I solve it? The Exception like this:
My enviorment(part of pom.xml):
Part of my quartz config: QuartzConfig:
Task: