diff --git a/playedu-api/src/main/java/xyz/playedu/api/PlayeduApiApplication.java b/playedu-api/src/main/java/xyz/playedu/api/PlayeduApiApplication.java index d4cf1d3..f2f7593 100644 --- a/playedu-api/src/main/java/xyz/playedu/api/PlayeduApiApplication.java +++ b/playedu-api/src/main/java/xyz/playedu/api/PlayeduApiApplication.java @@ -20,6 +20,7 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.ComponentScan; import org.springframework.scheduling.annotation.EnableAsync; +import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.transaction.annotation.EnableTransactionManagement; import xyz.playedu.common.config.UniqueNameGeneratorConfig; @@ -27,6 +28,7 @@ import xyz.playedu.common.config.UniqueNameGeneratorConfig; @SpringBootApplication @EnableAsync @EnableTransactionManagement +@EnableScheduling @ComponentScan( basePackages = {"xyz.playedu"}, nameGenerator = UniqueNameGeneratorConfig.class) diff --git a/playedu-api/src/main/java/xyz/playedu/api/schedule/LDAPSchedule.java b/playedu-api/src/main/java/xyz/playedu/api/schedule/LDAPSchedule.java new file mode 100644 index 0000000..bd1414b --- /dev/null +++ b/playedu-api/src/main/java/xyz/playedu/api/schedule/LDAPSchedule.java @@ -0,0 +1,61 @@ +/* + * Copyright (C) 2023 杭州白书科技有限公司 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package xyz.playedu.api.schedule; + +import lombok.extern.slf4j.Slf4j; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Component; + +import xyz.playedu.common.bus.LDAPBus; + +@Component +@Slf4j +public class LDAPSchedule { + + @Autowired private LDAPBus ldapBus; + + private int times; + + @Scheduled(fixedRate = 3600000) + public void sync() { + if (!ldapBus.enabledLDAP()) { + log.info("未配置LDAP服务"); + return; + } + + // 系统刚启动不执行 + if (times == 0) { + times++; + return; + } + + try { + ldapBus.departmentSync(); + } catch (Exception e) { + log.error("LDAP-部门同步失败", e); + } + + try { + ldapBus.userSync(); + } catch (Exception e) { + log.error("LDAP-学员同步失败", e); + } + + log.info("LDAP同步成功"); + } +}