EL-ADMIN配置定时任务

前言

使用对应专栏前一篇文档中的邮箱账号来配置定时任务,实现每天获取一次邮件数据,并把简历同步到系统中。

创建定时任务Bean

eladmin的定时任务类放在eladmin-system中,包名为me.zhengjie.modules.quartz.task,先在这个包中创建一个测试类,仅仅查询并打印下邮箱账号信息:

package me.zhengjie.modules.quartz.task;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;

import lombok.extern.slf4j.Slf4j;
import me.zhengjie.xingchenlie.basedata.service.EmailAccountService;
import me.zhengjie.xingchenlie.basedata.service.dto.EmailAccountDto;
import me.zhengjie.xingchenlie.basedata.service.dto.EmailAccountQueryCriteria;

/**
 * 定时获取邮件
 */
@Slf4j
@Async
@Component
public class EmailTask {

	@Autowired EmailAccountService emailAccountService;
	
    public void run(){
        log.info("进入email定时任务");
        List<EmailAccountDto> accounts = emailAccountService.queryAll(new EmailAccountQueryCriteria());
        for(EmailAccountDto dto : accounts) {
        	System.out.println(JSON.toJSON(dto).toString());
        }
        
    }

}

其中,注解@Slf4j和@Component需要都有。

配置定时任务

重启后台,然后前端页面左侧目录的系统管理–任务调度中开始配置。
点击添加:
在这里插入图片描述
其中,任务名称和任务描述是给自己看的,Bean名称就是创建的类首字母小写,执行方法就是Bean中要调用的方法名称,Cron表达式官方文档有样例参考:

0 0 10,14,16 * * ? 每天上午10点,下午2点,4点 
0 0/30 9-17 * * ? 朝九晚五工作时间内每半小时 
0 0 12 ? * WED 表示每个星期三中午12点 
"0 0 12 * * ?" 每天中午12点触发 
"0 15 10 ? * *" 每天上午10:15触发 
"0 15 10 * * ?" 每天上午10:15触发 
"0 15 10 * * ? *" 每天上午10:15触发 
"0 15 10 * * ? 2005" 2005年的每天上午10:15触发 
"0 * 14 * * ?" 在每天下午2点到下午2:59期间的每1分钟触发 
"0 0/5 14 * * ?" 在每天下午2点到下午2:55期间的每5分钟触发 
"0 0/5 14,18 * * ?" 在每天下午2点到2:55期间和下午6点到6:55期间的每5分钟触发 
"0 0-5 14 * * ?" 在每天下午2点到下午2:05期间的每1分钟触发 
"0 10,44 14 ? 3 WED" 每年三月的星期三的下午2:10和2:44触发 
"0 15 10 ? * MON-FRI" 周一至周五的上午10:15触发 
"0 15 10 15 * ?" 每月15日上午10:15触发 
"0 15 10 L * ?" 每月最后一日的上午10:15触发 
"0 15 10 ? * 6L" 每月的最后一个星期五上午10:15触发 
"0 15 10 ? * 6L 2002-2005" 2002年至2005年的每月的最后一个星期五上午10:15触发 
"0 15 10 ? * 6#3" 每月的第三个星期五上午10:15触发

创建完成后,点击列表的执行,可以看到控制台打印了run方法中log的内容,表示已经调用成功了。
在这里插入图片描述

完善

至此已经实现了定时任务的配置,接下来将对应的任务方法完善,因为简历表还没有创建,此处先提供获取邮件的辅助类。

import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import javax.mail.Address;
import javax.mail.BodyPart;
import javax.mail.Flags;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Part;
import javax.mail.Session;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;

import com.lootaa.xcl.business.db.EmailAccount;
import com.lootaa.xcl.business.task.vo.EmailVo;
import com.sun.mail.imap.IMAPStore;

public class EmailUtil {

	// 如果date为null,查询全部邮件;如果date有值yyyy-MM-dd,查询指定日期邮件
	public static List<EmailVo> getEmailList(EmailAccount account, String date, List<String> messageIds) throws Exception {
		List<EmailVo> list = new ArrayList<>();
		Properties props = System.getProperties();
		props.setProperty(account.getHostKey(), account.getHost());
		props.setProperty(account.getPortKey(), account.getPort());
		props.setProperty(account.getProtocolKey(), account.getProtocol());
		props.setProperty("mail.imap.partialfetch", "false");
		props.setProperty("mail.imaps.partialfetch", "false");
		Session session = Session.getInstance(props);
		IMAPStore store = (IMAPStore) session.getStore(account.getProtocol());
		store.connect(account.getAccount(), account.getPassword());
		Folder folder = store.getFolder("INBOX");
		folder.open(Folder.READ_WRITE); // Folder.READ_ONLY:只读权限 Folder.READ_WRITE:可读可写(可以修改邮件的状态)
		Message[] messages = folder.getMessages();
		int count = messages.length;
		for (int i = count - 1; i >= 0; i--) {
			// 常规邮件这一行就可以,遇到退信或者类似部分平台发送的邮件会报错javax.mail.MessagingException: Unable to load BODYSTRUCTURE
			// 所以需要下面两行处理,复制出来一份后重新赋值
			MimeMessage msg2 = (MimeMessage) messages[i];
			MimeMessage msg = new MimeMessage(msg2);
			EmailVo vo = new EmailVo();
			String messageId = msg.getMessageID();
			if(messageIds!= null && messageIds.size() > 0 && messageIds.contains(messageId)) {
				continue;
			}
			vo.setMessageId(messageId);
			String sendTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(msg.getSentDate());
			if(date != null && date.length() > 0) {
				if(sendTime.compareTo(date) < 0) {
					break;
				} else if(!sendTime.startsWith(date)) {
					continue;
				}
			}
			System.out.println(i + ":" + sendTime);
			vo.setSendTime(sendTime);
			vo.setSubject(msg.getSubject());
			Address[] froms = msg.getFrom();
			if (froms.length > 0) {
				InternetAddress address = (InternetAddress) froms[0];
				vo.setFromEmail(address.getAddress());
				vo.setFromName(address.getPersonal());
			}
			Address[] addresss = msg.getAllRecipients();
			if (addresss != null && addresss.length > 0) {
				StringBuffer receiveAddress = new StringBuffer();
				for (Address address : addresss) {
					InternetAddress internetAddress = (InternetAddress) address;
					receiveAddress.append(internetAddress.toUnicodeString()).append(",");
				}
				receiveAddress.deleteCharAt(receiveAddress.length() - 1);
				vo.setReceive(receiveAddress.toString());
			}
			vo.setRead(msg.getFlags().contains(Flags.Flag.SEEN));
			boolean isContainerAttachment = isContainAttachment(msg);
			vo.setContainerAttachment(isContainerAttachment);
			StringBuffer content = new StringBuffer(100);
			getMailTextContent(msg, content);
			vo.setContent(content.toString());
			if (isContainerAttachment) {
				StringBuffer fileName = new StringBuffer(100);
				InputStream is = getAttachment(msg, fileName);
				System.out.println(fileName);
				byte[] byteBuffer = null;
				ByteArrayOutputStream outByte = new ByteArrayOutputStream();
				byte[] tmpByte = new byte[2048];
				int len = 0;
				while ((len = is.read(tmpByte)) != -1) {
				      outByte.write(tmpByte, 0, len);
				}
				byteBuffer = outByte.toByteArray();
				vo.setAttachmentFile(byteBuffer);
				vo.setFileName(fileName.toString());
			}
			list.add(vo);
			
//			if(list.size() > 100) {
//				folder.close(true);
//				store.close();
//				return list;
//			}
		}
		folder.close(true);
		store.close();
		return list;
	}

	private static InputStream getAttachment(Part part, StringBuffer fileName)
			throws UnsupportedEncodingException, MessagingException, FileNotFoundException, IOException {
		if (part.isMimeType("multipart/*")) {
			Multipart multipart = (Multipart) part.getContent();
			int partCount = multipart.getCount();
			for (int i = 1; i < partCount; i++) {
				BodyPart bodyPart = multipart.getBodyPart(i);
				if(bodyPart == null) {
					continue;
				}
				String disp = bodyPart.getDisposition();
				if (disp != null && (disp.equalsIgnoreCase(Part.ATTACHMENT) || disp.equalsIgnoreCase(Part.INLINE))) {
					InputStream is = bodyPart.getInputStream();
					fileName = fileName.append(MimeUtility.decodeText(bodyPart.getFileName()));
					return is;
				} else if (bodyPart.isMimeType("multipart/*")) {
					return getAttachment(bodyPart, fileName);
				} else {
					String contentType = bodyPart.getContentType();
					if (contentType.indexOf("name") != -1 || contentType.indexOf("application") != -1) {
						InputStream is = bodyPart.getInputStream();
						fileName = fileName.append(MimeUtility.decodeText(bodyPart.getFileName()));
						return is;
					}
				}
			}
		} else if (part.isMimeType("message/rfc822")) {
			return getAttachment((Part) part.getContent(), fileName);
		}
		return null;
	}

	private static void getMailTextContent(Part part, StringBuffer content) throws MessagingException, IOException {
		boolean isContainTextAttach = part.getContentType().indexOf("name") > 0;
		if (part.isMimeType("text/*") && !isContainTextAttach) {
			content.append(part.getContent().toString());
		} else if (part.isMimeType("message/rfc822")) {
			getMailTextContent((Part) part.getContent(), content);
		} else if (part.isMimeType("multipart/*")) {
			Multipart multipart = (Multipart) part.getContent();
			int partCount = multipart.getCount();
			for (int i = 0; i < partCount; i++) {
				BodyPart bodyPart = multipart.getBodyPart(i);
				getMailTextContent(bodyPart, content);
			}
		}
	}

	/**
	 * 判断邮件中是否包含附件
	 */
	private static boolean isContainAttachment(Part part) throws MessagingException, IOException {
		boolean flag = false;
		if (part.isMimeType("multipart/*")) {
			MimeMultipart multipart = (MimeMultipart) part.getContent();
			int partCount = multipart.getCount();
			for (int i = 0; i < partCount; i++) {
				BodyPart bodyPart = multipart.getBodyPart(i);
				String disp = bodyPart.getDisposition();
				if (disp != null && (disp.equalsIgnoreCase(Part.ATTACHMENT) || disp.equalsIgnoreCase(Part.INLINE))) {
					flag = true;
				} else if (bodyPart.isMimeType("multipart/*")) {
					flag = isContainAttachment(bodyPart);
				} else {
					String contentType = bodyPart.getContentType();
					if (contentType.indexOf("application") != -1) {
						flag = true;
					}
					if (contentType.indexOf("name") != -1) {
						flag = true;
					}
				}
				if (flag)
					break;
			}
		} else if (part.isMimeType("message/rfc822")) {
			flag = isContainAttachment((Part) part.getContent());
		}
		return flag;
	}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
El-Admin 是一个基于 Spring Boot 和 Vue.js 的开源管理系统,而 Mybatis 是一个优秀的 Java ORM 框架。将 Mybatis 整合到 El-Admin 中,可以方便地进行数据持久化操作。 以下是整合 Mybatis 的步骤: 1. 添加 Mybatis 依赖 在 pom.xml 文件中添加 Mybatis 和 Mybatis-Spring 的依赖: ```xml <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.4</version> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.2.0</version> </dependency> ``` 2. 配置数据源 在 application.properties 文件中配置数据源信息: ```properties spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/eladmin?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai spring.datasource.username=root spring.datasource.password=root ``` 3. 配置 Mybatis 在 application.properties 文件中配置 Mybatis 相关属性: ```properties mybatis.type-aliases-package=com.example.demo.entity mybatis.mapper-locations=classpath*:mapper/*.xml ``` 其中,type-aliases-package 属性指定实体类所在的包,mapper-locations 属性指定 Mapper 接口所在的包。 4. 编写 Mapper 接口 创建 Mapper 接口,并在接口中定义 SQL 语句: ```java @Mapper public interface UserMapper { @Select("SELECT * FROM sys_user WHERE username = #{username}") User findByUsername(String username); } ``` 5. 编写 Mapper XML 文件 在 resources/mapper 目录下创建 Mapper XML 文件,并编写 SQL 语句: ```xml <mapper namespace="com.example.demo.mapper.UserMapper"> <select id="findByUsername" parameterType="java.lang.String" resultType="com.example.demo.entity.User"> SELECT * FROM sys_user WHERE username = #{username} </select> </mapper> ``` 6. 在 Service 中调用 Mapper 在 Service 中注入 Mapper,并调用 Mapper 中定义的方法: ```java @Service public class UserServiceImpl implements UserService { @Autowired private UserMapper userMapper; @Override public User findByUsername(String username) { return userMapper.findByUsername(username); } } ``` 以上就是整合 Mybatis 的步骤,希望对你有所帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lootaa

你的鼓励是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值