Spring注解定时任务

时间:2021-05-14 07:48:57
xmlns:task="http://www.springframework.org/schema/task"

xsi:schemaLocation="
            http://www.springframework.org/schema/task
            http://www.springframework.org/schema/task/spring-task-3.0.xsd


 <task:scheduler id="myScheduler" pool-size="5"/>  
    <task:annotation-driven scheduler="myScheduler"/> 


实现定时任务
@Scheduled(cron = "0/5 * * * * ?")
    public void login()throws Exception{
        System.out.println("王华");
    }
        

 实现异步

@Async
    public void registerAsync(User user) throws Exception {
        SimpleDateFormat formate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String result1 = null;
        for (int i = 0; i < 3; i++) {
            result1 = ringMessage.register(user.getUserId(), user.getLoginPassword());
            if (result1.equals("success")) {
                user.setEasemobStatus("1");
                usersMapper.update(user);
                break;
            }
        }
        // 用戶角色為商家註冊環信群組
        if (result1.equals("success") && !user.getRoleId().equals("500")) {
            String groupId = null;
            ArrayNode members = JsonNodeFactory.instance.arrayNode();
            for (int i = 0; i < 5; i++) {
                groupId = ringMessage.createChatGroup("粉丝交流", "群组聊天", true, true, 200,

                        user.getUserId(), members);
                if (!groupId.equals("error")) {
                    break;
                }
            }
            // 注册群组时生成群组id
            String communityId = UUIDGenerator.getUUID();
            Community community = new Community();
            community.setRingGroupId(groupId);
            community.setOwnerId(user.getUserId());
            community.setMaxmenbernum("200");
            community.setCreateTime(formate.format(new Date()));
            community.setCName("粉丝交流");
            community.setCommunityId(communityId);
            communityMapper.insert(community);
        }
    }