我有一个Spring Boot应用,其中有5个RabbitListener。我也有2个定期运行的Quartz Scheduler作业。当我启动Spring Boot应用程序时,作业似乎运行了一次,但随后保持在“已触发”的“触发”状态,因此不再运行。我正在使用Spring Boot 2.1.2和Quartz 2.3.0。有人可以建议吗?
我以前有4个Rabbit Listener,但没有这个问题,但是添加第5个兔子会导致这种情况发生。我尝试删除现有的Rabbit侦听器,所以只有4个Rabbit侦听器,包括我的新侦听器,问题消失了。
这是我要添加的Rabbit侦听器:
@Slf4j
@Component
public class MyListener
{
@Autowired
private MyProcess process;
@RabbitListener(queues = "#{T(com.earthport.elm.amqp.RabbitBinding).LIQUIDITY_BALANCE_CREDITED.getQueue()}",
containerFactory = "noDeadLetterRabbitListenerContainerFactory")
public void onMessage(MyMessage message)
{
if (log.isDebugEnabled())
{
log.debug("Received message: {}", message);
}
try
{
process.run(message);
}
catch (Exception e)
{
throw new AmqpRejectAndDontRequeueException(e);
}
}
}
这是我的侦听器容器的出厂配置:
@Bean
public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory(
Jackson2JsonMessageConverter messageConverter,
RetryAdviceChainFactory deadLetterRetryAdviceChainFactory)
{
final SimpleRabbitListenerContainerFactory cf = new SimpleRabbitListenerContainerFactory();
cf.setConnectionFactory(connectionFactory);
cf.setConcurrentConsumers(1);
cf.setErrorHandler(defaultErrorHandler);
cf.setAdviceChain(deadLetterRetryAdviceChainFactory.createDefaultRetryChain());
cf.setChannelTransacted(true);
cf.setTaskExecutor(taskExecutor);
cf.setMessageConverter(messageConverter);
cf.setTransactionManager(transactionManager);
return cf;
}
@Bean
public SimpleRabbitListenerContainerFactory noDeadLetterRabbitListenerContainerFactory(
Jackson2JsonMessageConverter messageConverter,
RetryAdviceChainFactory loggingRetryAdviceChainFactory)
{
final SimpleRabbitListenerContainerFactory cf = new SimpleRabbitListenerContainerFactory();
cf.setConnectionFactory(connectionFactory);
cf.setConcurrentConsumers(1);
cf.setErrorHandler(defaultErrorHandler);
cf.setAdviceChain(loggingRetryAdviceChainFactory.createRetryChain(
new NoBackOffPolicy(), new NeverRetryPolicy()));
cf.setChannelTransacted(true);
cf.setTaskExecutor(taskExecutor);
cf.setMessageConverter(messageConverter);
cf.setTransactionManager(transactionManager);
return cf;
}
这是我的侦听器容器的出厂配置:
@Bean
public JobFactory jobFactory(ApplicationContext applicationContext)
{
AutowiringSpringBeanJobFactory jobFactory = new AutowiringSpringBeanJobFactory();
jobFactory.setApplicationContext(applicationContext);
return jobFactory;
}
@Bean
public SchedulerFactoryBean schedulerFactoryBean(
DataSource dataSource,
JobFactory jobFactory,
Executor taskExecutor,
PlatformTransactionManager platformTransactionManager)
{
Properties quartzProperties = new Properties();
quartzProperties.setProperty("org.quartz.scheduler.instanceName", "ELM-Scheduler");
quartzProperties.setProperty("org.quartz.scheduler.instanceId", "AUTO");
quartzProperties.setProperty("org.quartz.jobStore.class", "org.quartz.impl.jdbcjobstore.JobStoreTX");
quartzProperties.setProperty("org.quartz.jobStore.driverDelegateClass", "org.quartz.impl.jdbcjobstore.StdJDBCDelegate");
quartzProperties.setProperty("org.quartz.jobStore.useProperties", "true");
quartzProperties.setProperty("org.quartz.jobStore.tablePrefix", "QRTZ_");
quartzProperties.setProperty("org.quartz.jobStore.isClustered", "true");
SchedulerFactoryBean factory = new SchedulerFactoryBean();
factory.setOverwriteExistingJobs(true);
factory.setAutoStartup(true);
factory.setDataSource(dataSource);
factory.setJobFactory(jobFactory);
factory.setTaskExecutor(taskExecutor);
factory.setTransactionManager(platformTransactionManager);
factory.setQuartzProperties(quartzProperties);
if (jobs != null)
{
factory.setJobDetails(jobs.toArray(new JobDetail[jobs.size()]));
}
if (triggers != null && schedulerEnabled)
{
factory.setTriggers(triggers.toArray(new Trigger[triggers.size()]));
}
return factory;
}
@Bean
public Scheduler scheduler(SchedulerFactoryBean schedulerFactoryBean)
{
return schedulerFactoryBean.getScheduler();
}
public static SimpleTriggerFactoryBean createTrigger(JobDetail jobDetail, long pollFrequencyMs, long startDelayMillis)
{
SimpleTriggerFactoryBean factoryBean = new SimpleTriggerFactoryBean();
factoryBean.setJobDetail(jobDetail);
factoryBean.setStartDelay(startDelayMillis);
factoryBean.setRepeatInterval(pollFrequencyMs);
factoryBean.setRepeatCount(SimpleTrigger.REPEAT_INDEFINITELY);
factoryBean.setMisfireInstruction(SimpleTrigger.MISFIRE_INSTRUCTION_RESCHEDULE_NEXT_WITH_REMAINING_COUNT);
return factoryBean;
}
这是我的Quartz和RabbitConfig中使用的共享TaskExcutor:
@Configuration
@EnableAsync
public class AsyncConfig
{
@Value("${executor.maxPoolSize:50}")
private int maxPoolSize;
@Value("${executor.maxQueueCapcity:500}")
private int maxQueueCapacity;
@Bean
public Executor taskExecutor()
{
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(5);
executor.setMaxPoolSize(maxPoolSize);
executor.setQueueCapacity(maxQueueCapacity);
return executor;
}
}
我希望我的预定作业能够定期运行。当此方法起作用时,我可以在我的QRTZ_TRIGGERS表中看到TRIGGER_STATE未锁定。但是,添加我的第5个侦听器后,该值将变为BLOCKED并保持不变。