001// -------------------------------------------------------------------------------- 002// Copyright 2002-2026 Echo Three, LLC 003// 004// Licensed under the Apache License, Version 2.0 (the "License"); 005// you may not use this file except in compliance with the License. 006// You may obtain a copy of the License at 007// 008// http://www.apache.org/licenses/LICENSE-2.0 009// 010// Unless required by applicable law or agreed to in writing, software 011// distributed under the License is distributed on an "AS IS" BASIS, 012// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013// See the License for the specific language governing permissions and 014// limitations under the License. 015// -------------------------------------------------------------------------------- 016 017package com.echothree.util.common.service.job; 018 019import com.echothree.control.user.authentication.common.AuthenticationUtil; 020import com.echothree.control.user.authentication.common.result.GetJobUserVisitResult; 021import com.echothree.control.user.job.common.JobUtil; 022import com.echothree.model.control.job.common.workflow.JobStatusConstants; 023import com.echothree.model.data.user.common.pk.UserVisitPK; 024import javax.naming.NamingException; 025import org.slf4j.Logger; 026import org.slf4j.LoggerFactory; 027 028public abstract class BaseScheduledJob { 029 030 protected final Logger log = LoggerFactory.getLogger(this.getClass()); 031 032 protected String jobName; 033 034 protected UserVisitPK userVisitPK; 035 protected long startTime; 036 037 protected BaseScheduledJob(String jobName) { 038 this.jobName = jobName; 039 } 040 041 private void startJob(UserVisitPK userVisitPK) 042 throws NamingException { 043 var commandForm = JobUtil.getHome().getStartJobForm(); 044 045 commandForm.setJobName(jobName); 046 047 log.debug("Starting job: {}", jobName); 048 startTime = System.currentTimeMillis(); 049 JobUtil.getHome().startJob(userVisitPK, commandForm); 050 } 051 052 private void endJob(UserVisitPK userVisitPK) 053 throws NamingException { 054 var commandForm = JobUtil.getHome().getEndJobForm(); 055 056 commandForm.setJobName(jobName); 057 058 log.debug("Ending job: {}", jobName); 059 JobUtil.getHome().endJob(userVisitPK, commandForm); 060 log.info("Completed job: {}, elapsed: {}ms", jobName, System.currentTimeMillis() - startTime); 061 } 062 063 public void executeJob() 064 throws NamingException { 065 var commandForm = AuthenticationUtil.getHome().getGetJobUserVisitForm(); 066 067 commandForm.setJobName(jobName); 068 069 var commandResult = AuthenticationUtil.getHome().getJobUserVisit(commandForm); 070 071 if(commandResult.hasErrors()) { 072 log.error(commandResult.toString()); 073 } else { 074 var executionResult = commandResult.getExecutionResult(); 075 var getJobUserVisitResult = (GetJobUserVisitResult)executionResult.getResult(); 076 077 userVisitPK = getJobUserVisitResult.getUserVisitPK(); 078 079 if(userVisitPK != null) { 080 if(getJobUserVisitResult.getJob().getJobStatus().getWorkflowStep().getWorkflowStepName().equals(JobStatusConstants.WorkflowStep_ENABLED)) { 081 startJob(userVisitPK); 082 execute(); 083 endJob(userVisitPK); 084 } 085 086 AuthenticationUtil.getHome().invalidateUserVisit(userVisitPK); 087 } 088 } 089 } 090 091 public abstract void execute() 092 throws NamingException; 093 094}