Before because of

Warning: It takes 6 minutes to read this article

In Hanzo Mall, some users submitted orders but did not pay. At this time, I scanned the unpaid orders at 5 o ‘clock every day through the Quartz timed task, then read the user’s email address and sent an email to remind the user to pay as soon as possible. Here’s how I implemented my Quartz timed task.

Maven led package

Maven is a Maven search engine that relies on web sites to search for and copy packages.

<! --> <dependency> <groupId>org.springframework.boot</groupId> < artifactId > spring - the boot - starter - quartz < / artifactId > < version > 2.1.13. RELEASE < / version > < / dependency >Copy the code

Configuration QuartzConfig class

/ * * *@Description: Quartz Periodic task configuration *@AuthorBy Haoyu QAQ *@Date2020/3/23 19:51 * My understanding@ConfigurationIs a singleton pattern that is always present when the container is started.@ComponentInstantiate */ once
@Configuration
public class QuartzConfig {

    @Bean
    public JobDetail PayQuartz(a) {
        return JobBuilder.newJob(CallPayQuartzTask.class).withIdentity("CallPayQuartzTask").storeDurably().build();
    }

    @Bean
    public Trigger CallPayQuartzTaskTrigger(a) {
        // Execute once every 5 seconds
        // Create a trigger
// SimpleScheduleBuilder scheduleBuilder = SimpleScheduleBuilder.simpleSchedule()
// .withIntervalInSeconds(5)
// .repeatForever();
        // The crON mode is executed periodically every day
        // The asterisks are represented from left to right: * * * * * * *
        // Format: [second] [minute] [hour] [day] [month] [week] [year]
        return TriggerBuilder.newTrigger().forJob(PayQuartz())
                .withIdentity("CallPayQuartzTask")
                .withSchedule(CronScheduleBuilder.cronSchedule("0, 0, 17 * *?)) .build(); }}Copy the code

Write CallPayQuartzTask class

/ * * *@Description: Scan the unpaid orders regularly every day and send email reminders to pay *@AuthorBy Haoyu QAQ *@Date2020/3/24 he * /
@Slf4j
public class CallPayQuartzTask extends QuartzJobBean{

    @Resource
    private HanZoMallOrderService hanZoMallOrderService;
    @Resource
    private HanZoMallUserService hanZoMallUserService;
    @Resource
    private MailSendService mailSendService;
    @Override
    protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        // Scan the order to see if there is any order status is not paid
        List<HanZoMallOrder> orderListVOS = hanZoMallOrderService.getHanZoMallOrderByOrderStatus(HanZoMallOrderStatusEnum.ORDER_PRE_PAY.getOrderStatus());
        if(orderListVOS! =null) {// Traverse the data sending mailbox to remind the user to pay as soon as possible
            log.info("Start sending bulk order notifications via Quartz");
            for (HanZoMallOrder order : orderListVOS) {
                Long userId= order.getUserId();
                String orderNo = order.getOrderNo();
                int totalPrice =  order.getTotalPrice();
                Date createDate = order.getCreateTime();
                SimpleDateFormat sdf = new SimpleDateFormat("Yyyy year MM month DD day HH minute MM second");
                String date = sdf.format(createDate);
                HanZoMallUserVO hanZoMallUserVO = hanZoMallUserService.getByPrimaryKey(userId);
                if(hanZoMallUserVO! =null && !StringUtils.isEmpty(hanZoMallUserVO.getEmailAddress())){
                    // The user's mailbox is not empty
                    String subject = "[Hanzo Mall unpaid order reminder]";
                    String content = "Hello, there you are."+date+"The order number created is"+orderNo+"The order has not been paid, the amount to be paid is"+totalPrice+"Yuan, please pay as soon as possible.";
                    mailSendService.sendSimpleMail(hanZoMallUserVO.getEmailAddress(),subject,content);
                }else if(hanZoMallUserVO! =null&& hanZoMallUserVO.getLoginName()! =null) {// If the user's mailbox is empty, the mobile phone number can be sent to the user
                    log.info("User's mailbox is empty, so users can be sent SMS alerts.");
                    log.info("Ali Cloud does not allow individuals to open the SMS alert interface, temporarily do not send.");
                }else if (hanZoMallUserVO==null){
                    log.info("User info empty -- no operation --");
                }
            }
        }
    }
}
Copy the code

There are some search order information of the service layer mapper layer code is not posted, according to their own business development can be. The main thing is to share the process, the code implementation is not difficult.

Regularly send email results pictures

summary

At this point, the whole process of sending an email message is described, and knowledge is only valuable if shared. If you have any questions, please feel free to contact me through my email on my page.