聊聊PowerJob的UserService

本文主要研究一下PowerJob的UserService

UserService

tech/powerjob/server/core/service/UserService.java

@Service
public class UserService {

    @Resource
    private UserInfoRepository userInfoRepository;

    /**
     * 保存/修改 用户
     * @param userInfoDO user
     */
    public void save(UserInfoDO userInfoDO) {
        userInfoDO.setGmtCreate(new Date());
        userInfoDO.setGmtModified(userInfoDO.getGmtCreate());
        userInfoRepository.saveAndFlush(userInfoDO);
    }

    /**
     * 根据用户ID字符串获取用户信息详细列表
     * @param userIds 逗号分割的用户ID信息
     * @return 用户信息详细列表
     */
    public List<UserInfoDO> fetchNotifyUserList(String userIds) {
        if (StringUtils.isEmpty(userIds)) {
            return Lists.newLinkedList();
        }
        // 去重
        Set<Long> userIdList = Splitter.on(",").splitToList(userIds).stream().map(Long::valueOf).collect(Collectors.toSet());
        List<UserInfoDO> res = userInfoRepository.findByIdIn(Lists.newLinkedList(userIdList));
        res.forEach(x -> x.setPassword(null));
        return res;
    }
}

UserService提供了save及fetchNotifyUserList方法,基于UserInfoRepository来实现

UserInfoDO

tech/powerjob/server/persistence/remote/model/UserInfoDO.java

@Data
@Entity
@Table(indexes = {
        @Index(name = "uidx01_user_info", columnList = "username"),
        @Index(name = "uidx02_user_info", columnList = "email")
})
public class UserInfoDO {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO, generator = "native")
    @GenericGenerator(name = "native", strategy = "native")
    private Long id;

    private String username;

    private String password;
    /**
     * 手机号
     */
    private String phone;
    /**
     * 邮箱地址
     */
    private String email;
    /**
     * webHook
     */
    private String webHook;
    /**
     * 扩展字段
     */
    private String extra;

    private Date gmtCreate;

    private Date gmtModified;
}

UserInfoDO定义了username、email两个唯一键

UserInfoRepository

tech/powerjob/server/persistence/remote/repository/UserInfoRepository.java

public interface UserInfoRepository extends JpaRepository<UserInfoDO, Long> {

    List<UserInfoDO> findByUsernameLike(String username);

    List<UserInfoDO> findByIdIn(List<Long> userIds);
}

UserInfoRepository基于JpaRepository,声明了findByUsernameLike、findByIdIn方法

InstanceManager

tech/powerjob/server/core/instance/InstanceManager.java

    private void alert(Long instanceId, String alertContent) {
        InstanceInfoDO instanceInfo = instanceInfoRepository.findByInstanceId(instanceId);
        JobInfoDO jobInfo;
        try {
            jobInfo = instanceMetadataService.fetchJobInfoByInstanceId(instanceId);
        } catch (Exception e) {
            log.warn("[InstanceManager-{}] can't find jobInfo, alarm failed.", instanceId);
            return;
        }
        JobInstanceAlarm content = new JobInstanceAlarm();
        BeanUtils.copyProperties(jobInfo, content);
        BeanUtils.copyProperties(instanceInfo, content);
        List<UserInfoDO> userList = SpringUtils.getBean(UserService.class).fetchNotifyUserList(jobInfo.getNotifyUserIds());
        if (!StringUtils.isEmpty(alertContent)) {
            content.setResult(alertContent);
        }
        alarmCenter.alarmFailed(content, AlarmUtils.convertUserInfoList2AlarmTargetList(userList));
    }

InstanceManager的alert会根据jobInfo.getNotifyUserIds()区查找fetchNotifyUserList出来的userList,最后通过alarmCenter.alarmFailed给指定用户发送报警信息

小结

UserService提供了save及fetchNotifyUserList方法,基于UserInfoRepository来实现;InstanceManager的alert会根据jobInfo.getNotifyUserIds()区查找fetchNotifyUserList出来的userList,最后通过alarmCenter.alarmFailed给指定用户发送报警信息。

相关推荐

  1. 聊聊PowerJobUserService

    2024-01-22 08:50:03       38 阅读
  2. 聊聊PowerJobStoreStrategy

    2024-01-22 08:50:03       34 阅读
  3. 聊聊PowerJobAbstractScriptProcessor

    2024-01-22 08:50:03       39 阅读
  4. 聊聊PowerJobAbstractSqlProcessor

    2024-01-22 08:50:03       38 阅读
  5. 聊聊PowerJobDispatchStrategy

    2024-01-22 08:50:03       34 阅读
  6. 聊聊PowerJobTimingStrategyHandler

    2024-01-22 08:50:03       30 阅读
  7. 聊聊PowerJobUseCacheLock

    2024-01-22 08:50:03       44 阅读
  8. 聊聊PowerJobQueryConvertUtils

    2024-01-22 08:50:03       33 阅读
  9. 聊聊PowerJobAlarmable

    2024-01-22 08:50:03       24 阅读
  10. 聊聊PowerJobAliOssService

    2024-01-22 08:50:03       31 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-01-22 08:50:03       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-22 08:50:03       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-22 08:50:03       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-22 08:50:03       18 阅读

热门阅读

  1. 微信小程序中使用自定义 icon 图标

    2024-01-22 08:50:03       34 阅读
  2. 三、安全工程—安全架构(CISSP)

    2024-01-22 08:50:03       35 阅读
  3. 《繁花》中服饰公司的股票开盘价如何涨到了18.8

    2024-01-22 08:50:03       35 阅读
  4. Angular:引领未来的前端框架

    2024-01-22 08:50:03       36 阅读
  5. 流畅的Python(四)- 文本和字节序列

    2024-01-22 08:50:03       23 阅读
  6. 用大模型增强数据分析应用

    2024-01-22 08:50:03       32 阅读
  7. Spring三级缓存

    2024-01-22 08:50:03       33 阅读
  8. 微前端:一种新型的前端架构方法

    2024-01-22 08:50:03       31 阅读
  9. XFTP会话日志

    2024-01-22 08:50:03       28 阅读
  10. ORACLE交集运算符是INTERSECT

    2024-01-22 08:50:03       26 阅读