DynamicGroupsJob.java 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*
  2. * Copyright [2020] [MaxKey of copyright http://www.maxkey.top]
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.maxkey.jobs;
  17. import java.io.Serializable;
  18. import java.util.List;
  19. import org.maxkey.entity.Groups;
  20. import org.maxkey.persistence.service.GroupsService;
  21. import org.quartz.Job;
  22. import org.quartz.JobExecutionContext;
  23. import org.slf4j.Logger;
  24. import org.slf4j.LoggerFactory;
  25. import com.fasterxml.jackson.annotation.JsonIgnore;
  26. public class DynamicGroupsJob implements Job , Serializable {
  27. /**
  28. *
  29. */
  30. private static final long serialVersionUID = 8831626240807856084L;
  31. final static Logger _logger = LoggerFactory.getLogger(DynamicGroupsJob.class);
  32. private static GroupsService groupsService = null;
  33. public static class JOBSTATUS{
  34. public static int STOP = 0;
  35. public static int RUNNING = 1;
  36. public static int FINISHED = 2;
  37. }
  38. private static int jobStatus = JOBSTATUS.STOP;
  39. @Override
  40. public void execute(JobExecutionContext context){
  41. if(jobStatus == JOBSTATUS.RUNNING) {
  42. _logger.info("DynamicGroupsJob is in running . " );
  43. return;
  44. }
  45. _logger.debug("DynamicGroupsJob is running ... " );
  46. jobStatus = JOBSTATUS.RUNNING;
  47. try {
  48. if(groupsService == null) {
  49. groupsService = (GroupsService) context.getMergedJobDataMap().get("groupsService");
  50. }
  51. List<Groups> groupsList = groupsService.queryDynamicGroups(null);
  52. for(Groups group : groupsList) {
  53. _logger.debug("group " + group);
  54. groupsService.refreshDynamicGroups(group);
  55. }
  56. Thread.sleep(10 *1000);
  57. _logger.debug("DynamicGroupsJob is success " );
  58. }catch(Exception e) {
  59. _logger.error("Exception " ,e);
  60. jobStatus = JOBSTATUS.STOP;
  61. }
  62. jobStatus = JOBSTATUS.FINISHED;
  63. _logger.debug("DynamicGroupsJob is finished . " );
  64. }
  65. }