Summary of common functions of SpringBoot integration (continuous update, recommended Collection)

At the same time, we look forward to communicating with you if there are mistakes. My contact information jenson_97@163.com

1, SpringBoot integrates asynchronous tasks

This function is usually used to deal with those non core tasks, such as shopping flow information, e-mail function, short message function, etc. this technology realizes the decoupling with the application to the greatest extent.

Code demonstration

Create a new service dedicated to asynchronous tasks:

@Slf4j
@Component
public class AsyncTask {

    //Inject Component
    @Autowired
    private OtherServiceImpl otherService;

    @Async
    public void makeLog(String content){
        log.info("[{}]Print the log with the following information:[{}]",DateUtil.now(),content);
    }

    @Async
    public void doSomething(){
        otherService.doSomething();
    }

}

This class is used to handle all other tasks that need asynchronous processing. The core annotation is @ Async

After that, @ EnableAsync is added to the main startup class of SpringBoot to enable the automatic configuration function of asynchronous tasks.

@EnableAsync
@SpringBootApplication
public class AsyncApplication {

    public static void main(String[] args) {
        SpringApplication.run(AsyncApplication.class, args);
    }

}

Writing a controller to test the asynchronous function

@Slf4j
@RestController
@RequestMapping("/test")
public class TestController {

    @Autowired
    private AsyncTask task;

    @GetMapping("/log")
    public String testMakeLog(String content){
        task.makeLog(content);
        return content;
    }

    @GetMapping("/dosomething")
    public String testDoSomething(){
        //... Other logic
        task.doSomething();
        return "OK";
    }
    
}

Use the browser to test the execution of asynchronous tasks:

Test passed

2, SpringBoot integrates scheduled tasks

Scheduled tasks are mainly used to process a task executed after a specified time. His function is equivalent to an alarm clock. After the alarm clock rings, I perform the task of getting up.

Code example

@Slf4j
@Configuration
//Turn on the automatic configuration function of scheduled tasks
@EnableScheduling
public class TimerTaskService {

    //Inject Component
    @Autowired
    private OtherServiceImpl otherService;
	
	//Start at 0 seconds and execute every 5 seconds
    @Scheduled(cron = "0/5 * * * * ? ")
    public void doSomething(){
        otherService.doSomething();
    }

}

Core notes: @ enableshcheduling, @ Scheduled,
@The enableshcheduling annotation is used to enable the automatic configuration function of scheduled tasks.
@Scheduled marks a method, which is used to execute the function specified by the annotation. The common attributes included in the @ scheduled annotation are: ① cron. Cron expression can specify a time point, and the task will be executed at that time point. ② fixedRate, which uses a millisecond value to specify how often a scheduled task is executed. For example, @ Scheduled(fixedRate=2000) means that the task is executed every two seconds. ③ fixedDelay, which controls the interval between the execution of the method. The timing starts from the execution of the last method (millisecond timing). For example, @ Scheduled(fixedDelay=5000) means that the next method will be executed after 5 seconds after the execution of the last method.

cron expressions can be generated using an online generator: Online Cron expression generator

test

Test passed

3, SpringBoot integrates RocketMQ

About to update

If I can help you, can you give us a third company to leave (◕‿◕✿)

Tags: Java Spring message queue

Posted by number67a on Fri, 13 May 2022 18:39:02 +0300