最近,我开始使用具有以下属性的 Quartz 持久作业存储:
org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX
org.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.PostgreSQLDelegate
Run Code Online (Sandbox Code Playgroud)
我在 spring 中使用基于 cron 的触发器定义了一个示例作业:
<bean id="sampleCronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="sampleJobDetail"/>
<property name="cronExpression" value="0/5 * * * * ?"/>
Run Code Online (Sandbox Code Playgroud)
我看到作业确实每 5 秒执行一次,一切都很好。现在我停止该程序,这是一个简单的控制台应用程序,没有 Web 容器或任何其他内容,等待约 30 秒并重新运行我的程序。我看到的是,当调度程序启动时,作业会被触发很多次。例如,如果作业执行如下:
public class SampleJob implements Serializable, Job {
@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
System.out.println("Executing the job Job " + new Date());
}
Run Code Online (Sandbox Code Playgroud)
}
重启后输出如下:
Executing the job Job Mon Mar 31 08:34:18 IDT 2014
Executing the job Job Mon Mar 31 08:34:18 IDT …Run Code Online (Sandbox Code Playgroud) 我想为我的工作创建 cron 表达式,但我不知道如何从表达式中排除一周中的特定日期。
triggerBuilder
.ForJob(jobKey)
.WithSchedule(CronScheduleBuilder.CronSchedule("????")).InTimeZone(TimeZoneInfo.Utc))
.StartNow()
.WithIdentity(triggerKey)
.Build();
Run Code Online (Sandbox Code Playgroud)
例如:除了星期一之外,我每天都想解雇我的工作。
我找到了一种方法,我们可以将工作时间间隔设置为每天中午(12 点)解雇:
0 0 0 ? * MON-FRI或者0 0 0 ? * 1-5
但是从这个间隔中排除特定的一天怎么样,例如“星期四”。
感谢您的帮助:)
恕我直言,这个问题quartz并不是很清楚:repeatInterval
如果该方法花费的时间比 长repeatInterval,会发生什么,即使当前方法没有完成,它是否也会触发触发器?如果该方法创建数据源对象,是否会导致连接池问题?
说明该方法通常需要5几秒钟才能完成,但可能会激增到10几秒钟,并且repeatInterval设置为8000(8 秒)
下次触发时会发生什么?我做了一些示例测试,看起来它会在16th第二次发生,因为第一次尝试8000ms 失败了
是这样的吗?对服务器性能有影响吗?
我正在创建一个使用 Quartz 的计时器应用程序,还使用 spring 从 schema.sql 文件初始化我的数据库。当应用程序启动时,我希望在创建 Scheduler bean 之前初始化数据库。
@Bean
public Scheduler scheduler() throws SchedulerException {
Scheduler scheduler;
final StdSchedulerFactory stdSchedulerFactory = new StdSchedulerFactory("application.properties");
stdSchedulerFactory.initialize();
scheduler = stdSchedulerFactory.getScheduler();
scheduler.start();
return scheduler;
}
Run Code Online (Sandbox Code Playgroud)
Scheduler bean 位于 TimerConfiguration.java 中,它被添加到 TimerApplication 中,例如
@SpringBootApplication
@Import({TimerConfiguration.class})
公共类 TimerApplication {
有办法实现这一点吗?
我有 Sprint Boot - Java 8 应用程序,它有一个石英作业,我在启动时配置它并设置时间表。该作业按照计划自动运行,就像您对石英作业所期望的那样。但是,现在我希望能够允许用户通过单击前端的按钮来手动触发这些作业,而不会扰乱该作业的正常调度。这是我所有的相关文件。
application.yml
quartz:
fooCron: 0 0 1 * * ?
fooGroup: foo-quartz-group
Run Code Online (Sandbox Code Playgroud)
QuartzConfig.java
@Configuration
@ConfigurationProperties(prefix = "quartz")
public class QuartzConfig {
private String fooCron;
private String fooGroup;
@Autowired
private ApplicationContext applicationContext;
@Autowired
private PlatformTransactionManager transactionManager;
@Autowired
private DataSource dataSource;
@Bean
public SchedulerFactoryBean quartzScheduler() {
AutowiringSpringBeanJobFactory jobFactory = new AutowiringSpringBeanJobFactory();
jobFactory.setApplicationContext(applicationContext);
Trigger[] triggers = {fooTrigger().getObject()};
SchedulerFactoryBean quartzScheduler = new SchedulerFactoryBean();
quartzScheduler.setJobFactory(jobFactory);
quartzScheduler.setTransactionManager(transactionManager);
quartzScheduler.setDataSource(dataSource);
quartzScheduler.setOverwriteExistingJobs(true);
quartzScheduler.setSchedulerName("foo-scheduler");
quartzScheduler.setQuartzProperties(quartzProperties());
quartzScheduler.setTriggers(triggers);
return quartzScheduler;
}
@Bean
public CronTriggerFactoryBean …Run Code Online (Sandbox Code Playgroud) 在 Spring Scheduler 上,这就是我想要实现的目标:
@Scheduled(initialDelay = 1000, fixedDelay = 5000)
Run Code Online (Sandbox Code Playgroud)
我正在转向quartz,但我似乎无法找到初始延迟的等效 API。
TriggerBuilder.newTrigger().withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInMilliseconds(5000));
Run Code Online (Sandbox Code Playgroud)
有什么想法可以添加延迟吗?
我有一个 Spring Boot 应用程序,它使用 Quartz 调度程序和 PostGreSQL 数据库作为存储。我正在将其从使用自己的数据库与public模式运行迁移到针对每个应用程序使用一个模式的共享数据库运行。该架构由Flyway管理。
在测试期间(使用testcontainers)应用程序启动失败,并显示
Caused by: org.postgresql.util.PSQLException: ERROR: relation "qrtz_locks" does not exist
Position: 15
Run Code Online (Sandbox Code Playgroud)
尽管 Flyway 之前已在架构中创建了该表app_test_hub_scheduler_v0。
配置是
spring:
jpa.properties:
hibernate.default_schema: app_test_hub_scheduler_v0
flyway:
enabled: true
schemas: app_test_hub_scheduler_v0
jpa:
properties:
hibernate:
default_schema: app_test_hub_scheduler_v0
quartz:
jdbc:
schema: app_test_hub_scheduler_v0
jdbcUrlParams: ?currentSchema=app_test_hub_scheduler_v0
Run Code Online (Sandbox Code Playgroud)
石英的性质是
org.quartz.scheduler.instanceName=test-hub-scheduler
org.quartz.scheduler.instanceId=AUTO
org.quartz.threadPool.class=org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount=50
org.quartz.jobStore.misfireThreshold=60000
org.quartz.jobStore.class=org.quartz.impl.jdbcjobstore.JobStoreTX
org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.PostgreSQLDelegate
org.quartz.jobStore.useProperties=true
org.quartz.jobStore.dataSource=myDS
org.quartz.jobStore.tablePrefix=qrtz_
org.quartz.jobStore.isClustered=false
Run Code Online (Sandbox Code Playgroud)
石英似乎没有获得模式名称。我该如何设置?
我们在 Spring Boot 应用程序中使用石英。为了存储作业信息,我使用了 jdbc 存储。在application.properties文件中,我有以下内容:
spring.quartz.job-store-type=jdbc
spring.quartz.jdbc.initialize-schema=always
Run Code Online (Sandbox Code Playgroud)
但是每次重新启动后,表都会被初始化,并且所有存储的信息都会丢失。如果我将 spring.quartz.jdbc.initialize-schema 更改为 never,则在创建表时会出现异常。
我不想在重启后删除石英表数据。你能帮助我吗 ?
问候, 斯蒂芬
我正在使用 Quartz 来安排工作。在执行时有时我会遇到一些奇怪的问题。首先我只是解释我的用例,我们需要一个将在预定时间触发的作业,并且在调度期间我们需要再次重新安排相同的作业但在不同的时间。因此,有时我们会遇到第一个触发器进入 ACQUIRE 状态,并且在执行时它将进入 QUARTZ 表中的 ERROR 状态。或者有时它会成功运行,但在重新安排作业后,它会再次进入 ERROR 状态。有时我们的代码运行成功,但它不会在控制台中打印 INFO 日志,有时会打印。
注意:: 我有相同的调度程序并配置我的其他环境,在该环境中它会正常工作。
所以这可能是环境特定问题吗?或者如何解决 QUARTZ 的此类有线问题。
我有一个带有石英依赖项的 Spring Boot Web 应用程序,并且正在运行 Spring 执行器。但 Actuator 只发布 14 个端点。我需要启用什么才能让执行器发布石英端点吗?
父项目是
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.0</version>
</parent>
Run Code Online (Sandbox Code Playgroud)
并具有以下 spring 依赖项
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-quartz</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)
以及以下执行器属性:
management.endpoint.shutdown.enabled=true
management.endpoint.quartz.enabled=true
management.endpoints.web.exposure.include=*
Run Code Online (Sandbox Code Playgroud)
尽管存在石英依赖性,但尚未发布针对石英的弹簧执行器端点。/actuator 端点报告以下内容:
{
"_links": {
"self": {
"href": "https://localhost:8080/actuator",
"templated": false
},
"beans": {
"href": "https://localhost:8080/actuator/beans",
"templated": false
},
"caches-cache": {
"href": "https://localhost:8080/actuator/caches/{cache}",
"templated": true
},
"caches": {
"href": "https://localhost:8080/actuator/caches",
"templated": false
},
"health": {
"href": "https://localhost:8080/actuator/health",
"templated": false
}, …Run Code Online (Sandbox Code Playgroud) java spring quartz-scheduler spring-boot spring-boot-actuator
quartz-scheduler ×10
java ×7
spring ×7
spring-boot ×6
quartz ×3
c# ×1
cron ×1
flyway ×1
javabeans ×1