This is the 30th day of my participation in the August Challenge
xxl-job job
Introduction of depend on
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<! -- xxl-job-core -->
<dependency>
<groupId>com.xuxueli</groupId>
<artifactId>xxl-job-core</artifactId>
<version>2.3.0</version>
</dependency>
Copy the code
The application.properties configuration file specifies the address of the scheduler admin
### xxl-job admin address list, such as "http://address" or "http://address01,http://address02"
xxl.job.admin.addresses=http://127.0.0.1:8080/xxl-job-admin
### xxl-job, access token
xxl.job.accessToken=
### xxl-job executor appname
xxl.job.executor.appname=xxl-job-executor-sample
### xxl-job executor registry-address: default use address to registry , otherwise use ip:port if address is null
xxl.job.executor.address=
### xxl-job executor server-info
xxl.job.executor.ip=
xxl.job.executor.port=9999
### xxl-job executor log-path
xxl.job.executor.logpath=/Users/rubble/logs/xxl-job/jobhandler
### xxl-job executor log-retention-days
xxl.job.executor.logretentiondays=30
Copy the code
Writing task
/** * Step 1: Create the Job method in Spring Bean; * 2, annotation configuration: add annotation to Job method"@XxlJob(value=" custom JobHandler name ", init =" jobHandler initializes method ", destroy =" JobHandler destroys method ") * 3. Run xxljobhelper. log to print execution logs. * 4. Task result: The default task result is "successful", which does not need to be set actively; If there are any demands, such as setting up task results for failure, can pass "XxlJobHelper. HandleFail/handleSuccess" setting the task results; * /
@Component
public class SampleXxlJob {
private static Logger logger = LoggerFactory.getLogger(SampleXxlJob.class);
/** * 1, simple task example (Bean mode) */
@XxlJob("demoJobHandler")
public void demoJobHandler(a) throws Exception {
XxlJobHelper.log("XXL-JOB, Hello World.");
for (int i = 0; i < 5; i++) {
XxlJobHelper.log("beat at:" + i);
TimeUnit.SECONDS.sleep(2);
}
// default success}}Copy the code
Create task 0/30 * * * *? * This command is executed every 30 seconds
Viewing Execution Logs
Execution time > task interval will block
Blocking processing strategy: the processing strategy when scheduling is too dense for the executor to process; Single-machine serial (default) : After scheduling requests enter the single-machine executor, scheduling requests enter the FIFO queue and run in serial mode. Discards subsequent scheduling requests: After a scheduling request enters the single-node executor, the request is discarded and marked as a failure if a scheduling task is found running on the executor. Overwriting the previous scheduling: After the scheduling request enters the single-machine executor, the running scheduling task is found, the running scheduling task is terminated, the queue is cleared, and the local scheduling task is executed.
GLUE mode (Shell)
Step 1: In the scheduling center, create a scheduling task
Set parameters for the new task by referring to “Detailed Configuration Properties” above. Select GLUE mode (Shell).
Step 2: Develop the task code:
Task code
@Slf4j
@Controller
public class IndexController {
@RequestMapping("/index")
@ResponseBody
String index(a) {
log.info("xxl job executor running.");
return "xxl job executor running."; }}Copy the code
Select the specified task and click the “GLUE” button on the right of the task to go to the Web IDE interface of the GLUE task, where the task code can be developed (you can also copy and paste it into the edit after the development is completed in the IDE).
The task in this mode is actually a “shell” script;
Enter the shell script task
#! /bin/bash
echo "xxl-job: hello shell"
echo "Script location:$0"
echo "Parameter 1:The $1"
curl -d "" The $1
sleep 1s
echo "Good bye!"
exit 0
Copy the code
View the execution log, response XXL job executor Running. The logs have been printed
2021-08-12 20:38:15 [com.xxl.job.core.thread.JobThread#run]-[130]-[Thread-66] ----------- xxl-job job execute start ----------- ----------- Param:http://localhost:8081/index 2021-08-12 20:38:15 [com.xxl.job.core.handler.impl.ScriptJobHandler#execute]-[80]-[Thread-66] ----------- script file:/Users/rubble/logs/xxl-job/jobhandler/gluesource/2_1628771461000.sh ----------- xxl-job: Hello shell script location: / Users/rubble/logs/XXL - job/jobhandler/gluesource / 2 _1628771461000. Sh parameter 1: http://localhost:8081/index % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0 100 25 100 25 0 0 4166 0 --:--:-- --:--:-- --:--:-- 4166 xxl job executor running.Good bye! 2021-08-12 20:38:16 [com.xxl.job.core.thread.JobThread#run]-[176]-[Thread-66] ----------- xxl-job job execute end(finish) ----------- ----------- Result: handleCode=200, handleMsg = null 2021-08-12 20:38:16 [com.xxl.job.core.thread.TriggerCallbackThread#callbackLog]-[197]-[xxl-job, executor TriggerCallbackThread] ----------- xxl-job job callback finish. [Load Log Finish]Copy the code
At this point, the two common methods for scheduling tasks in Java development are complete.