In-depth analysis of Job and JobDetail in Quartz
The editor below will bring you an in-depth analysis of Job and JobDetail in Quartz. The editor thinks it is quite good, so I will share it with you now and give it as a reference for everyone. Let’s follow the editor and take a look.
What can Quartz be used for?
Quartz is a task scheduling framework. For example, if you encounter this problem
You want to have your credit card repaid automatically on the 25th of every month
You want to send a message to the goddess you secretly loved on April 1st every year An anonymous greeting card
I want to back up my love action movie study notes to the cloud disk every hour
The summary of these questions is: Do something at a regular time. And the time trigger conditions can be very complex (such as 17:50 on the last working day of each month), so complex that a special framework is needed to do this. Quartz is here to do this kind of thing. You give it a definition of a trigger condition, and it is responsible for triggering the corresponding job to work at the time point.
No more nonsense, the code is very good. . .
public static void main(String[] args) { try { //创建scheduler Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler(); //定义一个Trigger Trigger trigger =TriggerBuilder.newTrigger().withIdentity("trigger1", "group1") //定义name/group .startNow()//一旦加入scheduler,立即生效 .withSchedule(SimpleScheduleBuilder.simpleSchedule() //使用SimpleTrigger .withIntervalInSeconds(1) //每隔一秒执行一次 .repeatForever()) //一直执行 .build(); //定义一个JobDetail JobDetail job =JobBuilder.newJob(HelloQuartz.class) //定义Job类为HelloQuartz类,这是真正的执行逻辑所在 .withIdentity("job1", "group1") //定义name/group .usingJobData("name", "quartz") //定义属性 .build(); //加入这个调度 scheduler.scheduleJob(job, trigger); //启动之 scheduler.start(); //运行一段时间后关闭 Thread.sleep(10000); scheduler.shutdown(true); } catch (Exception e) { e.printStackTrace(); } }
HelloQuartz class
public class HelloQuartz implements Job { public void execute(JobExecutionContext context) throws JobExecutionException { JobDetail detail = context.getJobDetail(); String name = detail.getJobDataMap().getString("name"); System.out.println("say hello to " + name + " at " + new Date()); } }
jar package:
This example covers the three most important basic elements of Quartz very well:
Scheduler: scheduler. All scheduling is controlled by it.
Trigger: Define the conditions for triggering. In the example, its type is SimpleTrigger, which is executed every 1 second (what is SimpleTrigger will be described in detail below).
JobDetail & Job: JobDetail defines task data, and the real execution logic is in the Job, in the example it is HelloQuartz. Why is it designed as JobDetail + Job instead of using Job directly? This is because tasks may be executed concurrently. If the Scheduler uses Job directly, there will be a problem of concurrent access to the same Job instance. In the JobDetail & Job method, every time sheduler is executed, a new Job instance will be created based on JobDetail, so that the problem of concurrent access can be avoided.
Scheduler
Scheduler is the brain of Quartz, and all tasks are implemented by it.
Schduelr contains two important components: JobStore and ThreadPool.
JobStore will store runtime information, including Trigger, Scheduler, JobDetail, business locks, etc. It has multiple implementations RAMJob (memory implementation), JobStoreTX (JDBC, transactions are managed by Quartz), JobStoreCMT (JDBC, using container transactions), ClusteredJobStore (cluster implementation), TerracottaJobStore (what is Terraractta).
ThreadPool is a thread pool, and Quartz has its own thread pool implementation. All tasks will be executed by the thread pool.
SchedulerFactory
SchdulerFactory, as the name suggests, is used to create Schduler. There are two implementations: DirectSchedulerFactory and StdSchdulerFactory. The former can be used to customize your own Schduler parameters in code. The latter is to directly read the quartz.properties configuration under the classpath (if it does not exist, use the default value ) to instantiate Schduler. Generally speaking, it is enough for us to use StdSchdulerFactory.
SchdulerFactory itself supports the creation of RMI stubs, which can be used to manage remote Schedulers. Its functions are the same as those of local ones, and it can submit jobs remotely.1.job
JobDetail job = JobBuilder.newJob(RemindJob.class) .withIdentity("job1", "group1").build();//创建一个任务 /** * 创建触发器 * 第一种方式 不太好 */ SimpleTrigger trigger = TriggerBuilder.newTrigger().withIdentity("myTrigger", "myTriggerGroup"). withSchedule(SimpleScheduleBuilder.simpleSchedule(). withIntervalInSeconds(3). repeatForever()). startAt(new Date(System.currentTimeMillis()+1000)).build(); /** * 创建触发器 * 第二种 方式 非常好 * 可以 好用 2013年每月的第三个星期五上午10:30触发 0 30 10 ? * 6#3 2013 * 2016年每月的第一个星期四下午16:17触发 0 17 16 ? * 5#1 2016 * 每天15点到16点每5分钟运行一次,此外,每天17点到18点每5分钟运行一次 */ /*CronTrigger trigger=TriggerBuilder.newTrigger() .withIdentity("myTrigger", "myTriggerGroup") .withSchedule(CronScheduleBuilder.cronSchedule("0 18 16 ? * 5#1 2016")).build();*/ SchedulerFactory sf=new StdSchedulerFactory();//创建调度者工厂 Scheduler scheduler = sf.getScheduler();//创建一个调度者 scheduler.scheduleJob(job,trigger);//注册并进行调度 scheduler.start();//启动调度 //Thread.sleep(millis) //scheduler.shutdown();//关闭调度
*/ public class RemindJob implements Job { private RemindService service=new RemindService(); @Override public void execute(JobExecutionContext context) throws JobExecutionException { service.printPlan("你好!"); Date date=new Date(); String time = date.toString(); System.out.println(time+"job is starting"); }
object.
JobDataMap
JobDataMap can contain an unlimited number of (serialized) data objects, and the data can be used when the job instance is executed; JobDataMap is Java An implementation of the Map interface that adds some additional methods to facilitate access to basic types of data.将job加入到scheduler之前,在构建JobDetail时,可以将数据放入JobDataMap,如下示例:
JobDetail job=JobBuilder.newJob(RemindJob.class) .withIdentity("job1", "group1") .usingJobData("hello", "we are family") .build();
在job的执行过程中,可以从JobDataMap中取出数据,如下示例:
@Override public void execute(JobExecutionContext context) throws JobExecutionException { service.printPlan("你好!"); JobKey key=context.getJobDetail().getKey(); JobDataMap map = context.getJobDetail().getJobDataMap(); String string = map.getString("hello"); System.out.println(key+"==========="+string); Date date=new Date(); String time = date.toString(); System.out.println(time+"job is starting"); }
如果你使用的是持久化的存储机制(本教程的JobStore部分会讲到),在决定JobDataMap中存放什么数据的时候需要小心,因为JobDataMap中存储的对象都会被序列化,因此很可能会导致类的版本不一致的问题;Java的标准类型都很安全,如果你已经有了一个类的序列化后的实例,某个时候,别人修改了该类的定义,此时你需要确保对类的修改没有破坏兼容性;更多细节,参考现实中的序列化问题。另外,你也可以配置JDBC-JobStore和JobDataMap,使得map中仅允许存储基本类型和String类型的数据,这样可以避免后续的序列化问题。
如果你在job类中,为JobDataMap中存储的数据的key增加set方法(如在上面示例中,增加setJobSays(String val)方法),那么Quartz的默认JobFactory实现在job被实例化的时候会自动调用这些set方法,这样你就不需要在execute()方法中显式地从map中取数据了。
在Job执行时,JobExecutionContext中的JobDataMap为我们提供了很多的便利。它是JobDetail中的JobDataMap和Trigger中的JobDataMap的并集,但是如果存在相同的数据,则后者会覆盖前者的值。
下面的示例,在job执行时,从JobExecutionContext中获取合并后的JobDataMap:
@Override public void execute(JobExecutionContext context) throws JobExecutionException { service.printPlan("你好!"); JobKey key=context.getJobDetail().getKey(); /* JobDataMap map = context.getJobDetail().getJobDataMap(); String string = map.getString("hello"); System.out.println(key+"==========="+string);*/ JobDataMap map = context.getMergedJobDataMap(); String string = map.getString("hello"); System.out.println(key+"--------------------- "+string);
The above is the detailed content of In-depth analysis of Job and JobDetail in Quartz. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



In-depth analysis of the role and application scenarios of HTTP status code 460 HTTP status code is a very important part of web development and is used to indicate the communication status between the client and the server. Among them, HTTP status code 460 is a relatively special status code. This article will deeply analyze its role and application scenarios. Definition of HTTP status code 460 The specific definition of HTTP status code 460 is "ClientClosedRequest", which means that the client closes the request. This status code is mainly used to indicate

iBatis and MyBatis: Differences and Advantages Analysis Introduction: In Java development, persistence is a common requirement, and iBatis and MyBatis are two widely used persistence frameworks. While they have many similarities, there are also some key differences and advantages. This article will provide readers with a more comprehensive understanding through a detailed analysis of the features, usage, and sample code of these two frameworks. 1. iBatis features: iBatis is an older persistence framework that uses SQL mapping files.

Detailed explanation of Oracle error 3114: How to solve it quickly, specific code examples are needed. During the development and management of Oracle database, we often encounter various errors, among which error 3114 is a relatively common problem. Error 3114 usually indicates a problem with the database connection, which may be caused by network failure, database service stop, or incorrect connection string settings. This article will explain in detail the cause of error 3114 and how to quickly solve this problem, and attach the specific code

Wormhole is a leader in blockchain interoperability, focused on creating resilient, future-proof decentralized systems that prioritize ownership, control, and permissionless innovation. The foundation of this vision is a commitment to technical expertise, ethical principles, and community alignment to redefine the interoperability landscape with simplicity, clarity, and a broad suite of multi-chain solutions. With the rise of zero-knowledge proofs, scaling solutions, and feature-rich token standards, blockchains are becoming more powerful and interoperability is becoming increasingly important. In this innovative application environment, novel governance systems and practical capabilities bring unprecedented opportunities to assets across the network. Protocol builders are now grappling with how to operate in this emerging multi-chain

[Analysis of the meaning and usage of midpoint in PHP] In PHP, midpoint (.) is a commonly used operator used to connect two strings or properties or methods of objects. In this article, we’ll take a deep dive into the meaning and usage of midpoints in PHP, illustrating them with concrete code examples. 1. Connect string midpoint operator. The most common usage in PHP is to connect two strings. By placing . between two strings, you can splice them together to form a new string. $string1=&qu

Analysis of new features of Win11: How to skip logging in to a Microsoft account. With the release of Windows 11, many users have found that it brings more convenience and new features. However, some users may not like having their system tied to a Microsoft account and wish to skip this step. This article will introduce some methods to help users skip logging in to a Microsoft account in Windows 11 and achieve a more private and autonomous experience. First, let’s understand why some users are reluctant to log in to their Microsoft account. On the one hand, some users worry that they

Detailed analysis and examples of exponential functions in C language Introduction: The exponential function is a common mathematical function, and there are corresponding exponential function library functions that can be used in C language. This article will analyze in detail the use of exponential functions in C language, including function prototypes, parameters, return values, etc.; and give specific code examples so that readers can better understand and use exponential functions. Text: The exponential function library function math.h in C language contains many functions related to exponentials, the most commonly used of which is the exp function. The prototype of exp function is as follows

Due to space limitations, the following is a brief article: Apache2 is a commonly used web server software, and PHP is a widely used server-side scripting language. In the process of building a website, sometimes you encounter the problem that Apache2 cannot correctly parse the PHP file, causing the PHP code to fail to execute. This problem is usually caused by Apache2 not configuring the PHP module correctly, or the PHP module being incompatible with the version of Apache2. There are generally two ways to solve this problem, one is
