Apache-Camel中的Quartz定时任务

需求:在camel的基础上增加两个定时任务,一个需要发邮件,一个只需要简单调用存储过程并拿回数据记录日志即可

package com.hkt.it.ds.dmg.eip.route;

import java.util.List;

import com.xxx.DmgCamelRouteConfig;
import com.xxx.DmgCamelRouteUriBuilder;
import com.xxx.dao.AccountDao;
import com.xxx.domain.*;
import com.xxx.service.EmailService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.camel.LoggingLevel;
import org.apache.camel.builder.endpoint.EndpointRouteBuilder;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

@Slf4j
@Component
@RequiredArgsConstructor
public class AccountExpirationSchedulerRoute extends EndpointRouteBuilder {
	// cron 表达式
    public final static String DAY_END_CRON_EXPRESSION = "0+0+0+*+*+?";

    private final @Qualifier(EchooCamelRouteConfig.DIRECT_TO_SEND_SUPPORT_EMAIL_VIA_SMTP) EchooCamelRouteUriBuilder sendSupportEmailViaSmtpRoute;
    public final AccountDao accountDao;
    private final EmailService emailService;

	// 常量
    public final static String NEARING_EXPIRATION_SUBJECT = "Runner nearing expiration notice.";
    public final static String NEARING_EXPIRATION_CONTENT= "<html><head><meta charset=\"utf-8\"></head><body>Dear Administrator,<br /><br />The password of [%s] is about to expire, please remind him/her to change the password as soon as possible.<br /><br />Best Regards</body></html>";
    @Override
    public void configure() {

        // 任务一:Account password haven't changed = 75 days

        from("quartz://nearingExpirationScheduler?cron="+ DAY_END_CRON_EXPRESSION)
                .log(LoggingLevel.INFO, "Email notification scheduler for account expiration starting!")
                .enrich()
                .simple("direct://get-nearing-expiration-runner-list") // 路由去 get-nearing-expiration-runner-list 
                .split(body()) // 取回的是一个List,把它拆分开一个个处理
                .process(exchange -> {
                    NearingExpirationAccount nearingExpirationAccount = exchange.getIn().getBody(NearingExpirationAccount.class);

                    log.info("Send email to notice [{}] admin [{}] that account [{}] is nearing expiration.",
                            nearingExpirationAccount .getCode(),
                            nearingExpirationAccount .getEmailListStr(),
                            nearingExpirationAccount .getUsername());

                    //send email
                    exchange.getIn().setBody(emailService.generateExpirationNotificationEmail(
                            nearingExpirationAccount .getEmailListStr(), NEARING_EXPIRATION_SUBJECT,
                            String.format(NEARING_EXPIRATION_CONTENT, nearingExpirationAccount .getUsername())));

                }).to(sendSupportEmailViaSmtpRoute.getUri()); // 路由到后续的发邮件部分

        from("direct://get-nearing-expiration-runner-list")
                .process(exchange -> {
                	// 从 DB 拿数据
                    List<NearingExpirationAccount> nearingExpirationAccountList= accountDao.getNearingExpirationRunnerList();
                    log.info("Nearing expiration account list: {}", nearingExpirationAccountList);
                    // 塞进body,进行后续的处理
                    exchange.getIn().setBody(nearingExpirationAccountList);
                });



        //  任务二:Account password haven't changed = 90 days
        from("quartz://expiredRunnerScheduler?cron=" + DAY_END_CRON_EXPRESSION)
                .log(LoggingLevel.INFO, "Expired account scanning scheduler starting!")
                .process(exchange -> {
                    List<ExpiredOrSuspendedAccount> expiredAccountList = accountDao.updateAndGetExpiredAccountList();
                    log.info("The status of these expired account {} has been set to 'EXPIRED'", expiredAccountList );
                });
    }
}

from("quartz://nearingExpirationScheduler?cron="+ DAY_END_CRON_EXPRESSION)
quartz://表示这是一个quartz定时任务
nearingExpirationScheduler是这个任务的名字
cron=后面跟的是cron定时表示式

在这里插入图片描述

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值