NutzCN Logo
问答 quartz自定义时间,执行时间相差1000倍。
发布于 2196天前 作者 hcxxiaomo 1907 次浏览 复制 上一个帖子 下一个帖子
标签:

项目是需要自定义时间发送信息,不是固定时间执行的cron,相关代码如下:
引用pom.xml

<dependency>
			<groupId>org.nutz</groupId>
			<artifactId>nutz</artifactId>
			<version>1.r.66</version>
		</dependency>
		
		<dependency>
			<groupId>org.nutz</groupId>
			<artifactId>nutz-integration-quartz</artifactId>
			<version>1.r.66</version>
		</dependency>
		
		<dependency>
			<groupId>org.quartz-scheduler</groupId>
			<artifactId>quartz</artifactId>
			<version>2.2.3</version>
			<exclusions>
				<exclusion>
					<groupId>c3p0</groupId>
					<artifactId>c3p0</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
		
		<dependency>
    <groupId>com.mchange</groupId>
    <artifactId>c3p0</artifactId>
    <version>0.9.5.2</version>
</dependency>

quartz.properties配置文件(配置了数据库的)

org.quartz.scheduler.instanceName = NutzbookScheduler
#\u7EBF\u7A0B\u6570\u91CF \u6267\u884C\u8C03\u5EA6\u6700\u5927\u7684\u7EBF\u7A0B\u6570
org.quartz.threadPool.threadCount = 5
#\u8C03\u5EA6\u5B9E\u4F8B\u5931\u6548\u7684\u68C0\u67E5\u65F6\u95F4\u95F4\u9694
org.quartz.jobStore.clusterCheckinInterval = 20000
#\u6570\u636E\u4FDD\u5B58\u65B9\u5F0F \u6301\u4E45\u5316\u5230\u6570\u636E\u5E93
org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX
#\u6570\u636E\u5E93\u5E73\u53F0 mysql
org.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.StdJDBCDelegate
#\u8868\u7684\u524D\u7F00
org.quartz.jobStore.tablePrefix = QRTZ_
#\u6570\u636E\u5E93\u522B\u540D spring\u914D\u7F6E\u6570\u636E\u6E90 \u5219\u8FD9\u91CC\u914D\u7F6E\u7684\u6570\u636E\u6E90\u4E0D\u8D77\u4F5C\u7528 \u5728spring\u4E2D\u6307\u5B9Aquartz\u7684\u6570\u636E\u6E90
org.quartz.jobStore.dataSource =qzDS
org.quartz.dataSource.qzDS.driver =com.mysql.jdbc.Driver
org.quartz.dataSource.qzDS.URL =jdbc:mysql://127.0.0.1:3306/watch_web_project?useUnicode=true&characterEncoding=UTF-8&useSSL=false
org.quartz.dataSource.qzDS.user =root
org.quartz.dataSource.qzDS.password =123456
org.quartz.dataSource.qzDS.maxConnections =5

调用的代码信息

@IocBean
	public class WatchService {

		private static final Log log = Logs.get();

		@Inject
		protected Dao dao;

		@Inject
		private QuartzManager quartzManager;

		public void quartzSet(Long imei) {

			Date now = new Date();
			Date start = DateUtil.offsetMinute(now, 3);
			Date end = DateUtil.offsetMinute(now, 10);
			JobDetail jobDetail = newJob(SendInfoToWatch.class).withIdentity("job1" + imei, "group1")
					// .setJobData(newJobDataMap)
					.usingJobData("imei", String.valueOf(imei)).usingJobData("info", "info---来自于数据中的信息呢").build();
			simpleSchedule();
			SimpleTrigger trigger = newTrigger().withIdentity("trigger1" + imei, "group1").startAt(start)
					.forJob(jobDetail)
//				 .usingJobData(newJobDataMap)
					.withSchedule(SimpleScheduleBuilder.repeatMinutelyForever(2)).endAt(end).build();
			JobKey jobKey = new JobKey("job1_trigger1~" + imei, "group1");
			QuartzJob qj = new QuartzJob(jobKey, trigger, jobDetail);
			quartzManager.add(qj);
			log.info(jobDetail.getKey() + " will run at: " + trigger.getStartTime() + " and repeat: "
					+ trigger.getRepeatCount() + " times, every " + trigger.getRepeatInterval() / 1000
					+ " seconds and end at :" + trigger.getEndTime());

		}

		public void sendNoticeToWatch(Long imei, String info) {
			log.errorf("Long imei = %s ,String info = %s ", imei, info);
		}

	}
@IocBean
public class SendInfoToWatch implements Job{
	
	@Inject
	private WatchService watchService;

	@Override
	public void execute(JobExecutionContext context) throws JobExecutionException {
		JobDataMap jdm = context.getJobDetail().getJobDataMap();
		String imei = jdm.getString("imei");
		String info = jdm.getString("info");
		watchService.sendNoticeToWatch(Long.valueOf(imei), info);
	}

}

调用之后发现,数据库里面保存的数据如下:
qrtz_simple_triggers表中为如下数据

# SCHED_NAME, TRIGGER_NAME, TRIGGER_GROUP, REPEAT_COUNT, REPEAT_INTERVAL, TIMES_TRIGGERED
NutzbookScheduler, job1_trigger1~10086, group1, -1, 120000000, 1

qrtz_triggers表中为如下数据

# SCHED_NAME, TRIGGER_NAME, TRIGGER_GROUP, JOB_NAME, JOB_GROUP, DESCRIPTION, NEXT_FIRE_TIME, PREV_FIRE_TIME, PRIORITY, TRIGGER_STATE, TRIGGER_TYPE, START_TIME, END_TIME, CALENDAR_NAME, MISFIRE_INSTR, JOB_DATA
NutzbookScheduler, job1_trigger1~10086, group1, job1_trigger1~10086, group1, , 1543757848586, 1543637848586, 5, WAITING, SIMPLE, 1543637848586, 0, , 0, ...

时间相差了1000倍,原来设置的是2分钟执行一次,但是数据中显示的是每2000分钟执行一次呢。

3 回复

quartz的cron表达式到秒,linux的cron表达式到分钟

找到原因了,org.nutz.integration.quartz.Quartzs中的makeSimpleTrigger 方法有问题,导致时间错误了。修改这个方法就能正常调用和执行了。
兽总,要不我提交到git上,你看看?

好,还不晓得写错了什么

添加回复
请先登陆
回到顶部