SpringCloud + quartz timing of alert system

SpringCloud + quartz timing of alert system

Before Springboot made by a regular query system, I wrote a few days ago through an upgraded version of SpringCloud, functional and almost before, and now look.

First, you need to pay attention to my friends test number, the system of unauthorized access by third-party micro-channel test number, scan the next Fanger Wei code to follow
image description

Into my test number after clicking the timed reminder concerned entering the system

clipboard.png

Since it is using SpringCloud, then it is surely put a project is divided into several modules to run it

clipboard.png

I was a student, only one cloud server, so put these four modules on the same server, and Wuguai Wuguai ~

The system is divided into four modules

  • Eureka Server service registry
  • quartz Timed Reminder Service
  • Users Consumer Services
  • Micro-channel test number and authorized third-party service to send a message template

Note: This system does not use a gateway zuul

Eureka Server I will not say, what a simple configuration can be directly utilized.

quartz regularly reminded of it, we need quartz this third-party libraries

        <dependency>
            <groupId>org.quartz-scheduler</groupId>
            <artifactId>quartz</artifactId>
            <version>2.3.0</version>
        </dependency>

But also need some spring-dependent

quartz simple task trigger and trigger complex tasks, the system uses a complex task trigger custom cron task scheduling

Writing a Job interface implementation class

clipboard.png

Realized inside the execute method, which is executed when the task is triggered, write what you want to perform inside the method.

After you configure the scheduler, when the project started to create a bean

@Configuration
public class QuartzConfigration {
    /**
     * attention:
     * Details:定义quartz调度工厂
     */
    @Bean(name = "scheduler")
    public SchedulerFactoryBean schedulerFactory() {
        SchedulerFactoryBean bean = new SchedulerFactoryBean();
        // 用于quartz集群,QuartzScheduler 启动时更新己存在的Job
        bean.setOverwriteExistingJobs(true);
        // 延时启动,应用启动1秒后
//        bean.setStartupDelay(5);
        // 注册触发器
//        bean.setTriggers(jobTrigger);
        return bean;
    }
}

Create a task (the interception part to write their own tools class and modify), you can write their own tools, to facilitate their own future calls.

 String event= (String) map.get("eventId");
        String date= (String) map.get("date");
        JobDataMap jobDataMap=new JobDataMap();
        jobDataMap.put("eventId",event);  //将参数传递到Job类中
        jobDataMap.put("date",date);
        JobDetail jobDetail= JobBuilder
                .newJob(MyBean.class)
                .withIdentity(key,"group1")
                .usingJobData(jobDataMap)
                .build();
        CronTrigger cronTrigger = TriggerBuilder.newTrigger()
                .withIdentity(key, "group1")
                .withSchedule(CronScheduleBuilder.cronSchedule(cron))
                .build();
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        log.info("创建事件成功,执行时间为:{}",format.format(scheduler.scheduleJob(jobDetail,cronTrigger)));

After you create a time if you want to modify so you need to write

 TriggerKey triggerKey=new TriggerKey(eventId,"group1");
        CronTriggerImpl cronTrigger= (CronTriggerImpl) scheduler.getTrigger(triggerKey);
        cronTrigger.setCronExpression(cron);
        scheduler.rescheduleJob(triggerKey,cronTrigger);

In Job implementation class which can not be injected into the Service, you can do so

ConfigurableApplicationContext run = SpringApplication.run(SpringcloudQuartzApplication.class, args);
        EventService eventService = run.getBean(EventService.class);
        MyBean.eventService=eventService;

This is part of the quartz module introduced.

Next is the consumer user module, calling quartz service, called by Feign, comparing pit thing is, Feign method parameters, can only pass an object parameter and a number of parameters, can not pass two object parameters, and passing in front of the object parameters also need to add comments

 @PostMapping("/update")
    public ResultVO update(@RequestBody(required = false)EventForm eventForm,
                           @RequestParam(name = "id") String id);

Ribbon Feign the default load balancing (Although the present system does not cluster ~)

Micro letter template third party licensors and send a message not to say, is the use of third-party libraries written

<dependency>
            <groupId>com.github.binarywang</groupId>
            <artifactId>weixin-java-mp</artifactId>
            <version>2.8.0</version>
        </dependency>

Micro-channel third-party authorized github also posted, you can see the API itself, click here github

Project results presentation

image description

image description

image description

Well, here to introduce to friends ~
What is the problem to send me mail questions it
[email protected]

Guess you like

Origin www.cnblogs.com/homehtml/p/12189486.html