mirror of
https://github.com/fabioformosa/quartz-manager.git
synced 2026-05-15 06:10:29 +09:00
#63 replaced some "any" with actual type and conformed the log notifier to the progress notifier
This commit is contained in:
@@ -1,17 +0,0 @@
|
||||
package it.fabioformosa.quartzmanager.aspects;
|
||||
|
||||
import org.quartz.JobExecutionContext;
|
||||
import org.quartz.SchedulerException;
|
||||
|
||||
/**
|
||||
*
|
||||
* Notify the progress of the trigger to all consumers
|
||||
*
|
||||
* @author Fabio Formosa
|
||||
*
|
||||
*/
|
||||
public interface ProgressNotifier {
|
||||
|
||||
void send(JobExecutionContext jobExecutionContext) throws SchedulerException;
|
||||
|
||||
}
|
||||
@@ -1,54 +0,0 @@
|
||||
package it.fabioformosa.quartzmanager.aspects;
|
||||
|
||||
import it.fabioformosa.quartzmanager.dto.TriggerFiredBundleDTO;
|
||||
import org.quartz.*;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.messaging.simp.SimpMessageSendingOperations;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
*
|
||||
* Notify the progress of the trigger through websocket
|
||||
*
|
||||
* @author Fabio Formosa
|
||||
*
|
||||
*/
|
||||
//@Aspect
|
||||
@Component
|
||||
public class WebSocketProgressNotifier implements ProgressNotifier {
|
||||
|
||||
@Autowired
|
||||
private SimpMessageSendingOperations messagingTemplate;
|
||||
|
||||
//@AfterReturning("execution(* logAndSend(..))")
|
||||
// @Override
|
||||
// public void updateProgress(JoinPoint joinPoint) {
|
||||
// log.info("PROGRESS UPDATE!!!");
|
||||
// }
|
||||
|
||||
@Override
|
||||
public void send(JobExecutionContext jobExecutionContext) throws SchedulerException {
|
||||
TriggerFiredBundleDTO triggerFiredBundleDTO = new TriggerFiredBundleDTO();
|
||||
|
||||
Trigger trigger = jobExecutionContext.getTrigger();
|
||||
triggerFiredBundleDTO.setFinalFireTime(trigger.getFinalFireTime());
|
||||
triggerFiredBundleDTO.setNextFireTime(trigger.getNextFireTime());
|
||||
triggerFiredBundleDTO.setPreviousFireTime(trigger.getPreviousFireTime());
|
||||
|
||||
if (trigger instanceof SimpleTrigger) {
|
||||
SimpleTrigger simpleTrigger = (SimpleTrigger) trigger;
|
||||
triggerFiredBundleDTO.setRepeatCount(simpleTrigger.getRepeatCount() + 1);
|
||||
triggerFiredBundleDTO.setTimesTriggered(simpleTrigger.getTimesTriggered());
|
||||
} else if (trigger instanceof DailyTimeIntervalTrigger) {
|
||||
DailyTimeIntervalTrigger dailyTrigger = (DailyTimeIntervalTrigger) trigger;
|
||||
triggerFiredBundleDTO.setRepeatCount(dailyTrigger.getRepeatCount() + 1);
|
||||
}
|
||||
|
||||
JobDetail jobDetail = jobExecutionContext.getJobDetail();
|
||||
triggerFiredBundleDTO.setJobKey(jobDetail.getKey().getName());
|
||||
triggerFiredBundleDTO.setJobClass(trigger.getClass().getSimpleName());
|
||||
|
||||
messagingTemplate.convertAndSend("/topic/progress", triggerFiredBundleDTO);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,27 +1,27 @@
|
||||
package it.fabioformosa.quartzmanager.configuration;
|
||||
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
|
||||
import org.springframework.web.socket.config.annotation.AbstractWebSocketMessageBrokerConfigurer;
|
||||
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
|
||||
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
|
||||
|
||||
@Configuration
|
||||
@ComponentScan(basePackages = {"it.fabioformosa.quartzmanager.aspects"})
|
||||
@EnableWebSocketMessageBroker
|
||||
public class WebsocketConfig extends AbstractWebSocketMessageBrokerConfigurer {
|
||||
|
||||
@Override
|
||||
public void configureMessageBroker(MessageBrokerRegistry config) {
|
||||
config.enableSimpleBroker("/topic");
|
||||
config.setApplicationDestinationPrefixes("/job");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void registerStompEndpoints(StompEndpointRegistry registry) {
|
||||
registry.addEndpoint("/quartz-manager/logs").setAllowedOrigins("/**").withSockJS();
|
||||
registry.addEndpoint("/quartz-manager/progress").setAllowedOrigins("/**").withSockJS();
|
||||
}
|
||||
|
||||
}
|
||||
package it.fabioformosa.quartzmanager.configuration;
|
||||
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
|
||||
import org.springframework.web.socket.config.annotation.AbstractWebSocketMessageBrokerConfigurer;
|
||||
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
|
||||
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
|
||||
|
||||
@Configuration
|
||||
@ComponentScan(basePackages = {"it.fabioformosa.quartzmanager.websockets"})
|
||||
@EnableWebSocketMessageBroker
|
||||
public class WebsocketConfig extends AbstractWebSocketMessageBrokerConfigurer {
|
||||
|
||||
@Override
|
||||
public void configureMessageBroker(MessageBrokerRegistry config) {
|
||||
config.enableSimpleBroker("/topic");
|
||||
config.setApplicationDestinationPrefixes("/job");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void registerStompEndpoints(StompEndpointRegistry registry) {
|
||||
registry.addEndpoint("/quartz-manager/logs").setAllowedOrigins("/**").withSockJS();
|
||||
registry.addEndpoint("/quartz-manager/progress").setAllowedOrigins("/**").withSockJS();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,56 +0,0 @@
|
||||
package it.fabioformosa.quartzmanager.jobs;
|
||||
|
||||
import it.fabioformosa.quartzmanager.aspects.ProgressNotifier;
|
||||
import it.fabioformosa.quartzmanager.jobs.entities.LogRecord;
|
||||
import org.quartz.Job;
|
||||
import org.quartz.JobExecutionContext;
|
||||
import org.quartz.SchedulerException;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.messaging.simp.SimpMessageSendingOperations;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
/**
|
||||
* Extends this class to create a job that produces LogRecord to be displayed
|
||||
* into the GUI panel
|
||||
*
|
||||
* @author Fabio.Formosa
|
||||
*
|
||||
*/
|
||||
public abstract class AbstractLoggingJob implements Job {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(AbstractLoggingJob.class);
|
||||
|
||||
@Autowired
|
||||
private SimpMessageSendingOperations messagingTemplate;
|
||||
|
||||
@Resource
|
||||
private ProgressNotifier progressNotifier;
|
||||
|
||||
/**
|
||||
*
|
||||
* @param jobExecutionContext
|
||||
* @return final log
|
||||
*/
|
||||
public abstract LogRecord doIt(JobExecutionContext jobExecutionContext);
|
||||
|
||||
@Override
|
||||
public final void execute(JobExecutionContext jobExecutionContext) {
|
||||
try {
|
||||
LogRecord logMsg = doIt(jobExecutionContext);
|
||||
logAndSend(logMsg);
|
||||
progressNotifier.send(jobExecutionContext);
|
||||
} catch (SchedulerException e) {
|
||||
log.error("Error updating progress " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public void logAndSend(LogRecord logRecord) {
|
||||
log.info(logRecord.getMessage());
|
||||
logRecord.setThreadName(Thread.currentThread().getName());
|
||||
messagingTemplate.convertAndSend("/topic/logs", logRecord);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package it.fabioformosa.quartzmanager.jobs;
|
||||
|
||||
import it.fabioformosa.quartzmanager.dto.TriggerFiredBundleDTO;
|
||||
import it.fabioformosa.quartzmanager.jobs.entities.LogRecord;
|
||||
import it.fabioformosa.quartzmanager.websockets.WebSocketProgressNotifier;
|
||||
import it.fabioformosa.quartzmanager.websockets.WebhookSender;
|
||||
import org.quartz.Job;
|
||||
import org.quartz.JobExecutionContext;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
/**
|
||||
* Extends this class to create a job that produces LogRecord to be displayed
|
||||
* into the GUI panel
|
||||
*
|
||||
* @author Fabio.Formosa
|
||||
*/
|
||||
public abstract class AbstractQuartzManagerJob implements Job {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(AbstractQuartzManagerJob.class);
|
||||
|
||||
@Resource
|
||||
private WebhookSender webSocketProgressNotifier;
|
||||
|
||||
@Resource
|
||||
private WebhookSender webSocketLogsNotifier;
|
||||
|
||||
/**
|
||||
* @param jobExecutionContext
|
||||
* @return final log
|
||||
*/
|
||||
public abstract LogRecord doIt(JobExecutionContext jobExecutionContext);
|
||||
|
||||
@Override
|
||||
public final void execute(JobExecutionContext jobExecutionContext) {
|
||||
LogRecord logMsg = doIt(jobExecutionContext);
|
||||
log.info(logMsg.getMessage());
|
||||
|
||||
logMsg.setThreadName(Thread.currentThread().getName());
|
||||
webSocketLogsNotifier.send(logMsg);
|
||||
|
||||
TriggerFiredBundleDTO triggerFiredBundleDTO = WebSocketProgressNotifier.buildTriggerFiredBundle(jobExecutionContext);
|
||||
webSocketProgressNotifier.send(triggerFiredBundleDTO);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
package it.fabioformosa.quartzmanager.services;
|
||||
|
||||
import it.fabioformosa.quartzmanager.jobs.AbstractLoggingJob;
|
||||
import it.fabioformosa.quartzmanager.jobs.AbstractQuartzManagerJob;
|
||||
import lombok.Getter;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.reflections.Reflections;
|
||||
@@ -15,7 +15,7 @@ import java.util.stream.Collectors;
|
||||
public class JobService {
|
||||
|
||||
@Getter
|
||||
private List<Class<? extends AbstractLoggingJob>> jobClasses = new ArrayList<>();
|
||||
private List<Class<? extends AbstractQuartzManagerJob>> jobClasses = new ArrayList<>();
|
||||
|
||||
private List<String> jobClassPackages = new ArrayList<>();
|
||||
|
||||
@@ -30,14 +30,14 @@ public class JobService {
|
||||
|
||||
@PostConstruct
|
||||
public void initJobClassList() {
|
||||
List<Class<? extends AbstractLoggingJob>> foundJobClasses = jobClassPackages.stream().flatMap(jobClassPackage -> findJobClassesInPackage(jobClassPackage).stream()).collect(Collectors.toList());
|
||||
List<Class<? extends AbstractQuartzManagerJob>> foundJobClasses = jobClassPackages.stream().flatMap(jobClassPackage -> findJobClassesInPackage(jobClassPackage).stream()).collect(Collectors.toList());
|
||||
if (foundJobClasses.size() > 0)
|
||||
this.jobClasses.addAll(foundJobClasses);
|
||||
}
|
||||
|
||||
private static Set<Class<? extends AbstractLoggingJob>> findJobClassesInPackage(String packageStr) {
|
||||
private static Set<Class<? extends AbstractQuartzManagerJob>> findJobClassesInPackage(String packageStr) {
|
||||
Reflections reflections = new Reflections(packageStr);
|
||||
return reflections.getSubTypesOf(AbstractLoggingJob.class);
|
||||
return reflections.getSubTypesOf(AbstractQuartzManagerJob.class);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
package it.fabioformosa.quartzmanager.websockets;
|
||||
|
||||
import it.fabioformosa.quartzmanager.jobs.entities.LogRecord;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.messaging.simp.SimpMessageSendingOperations;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class WebSocketLogsNotifier implements WebhookSender<LogRecord> {
|
||||
|
||||
public static final String TOPIC_LOGS = "/topic/logs";
|
||||
|
||||
@Autowired
|
||||
private SimpMessageSendingOperations messagingTemplate;
|
||||
|
||||
@Override
|
||||
public void send(LogRecord logRecord) {
|
||||
messagingTemplate.convertAndSend(TOPIC_LOGS, logRecord);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package it.fabioformosa.quartzmanager.websockets;
|
||||
|
||||
import it.fabioformosa.quartzmanager.dto.TriggerFiredBundleDTO;
|
||||
import org.quartz.*;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.messaging.simp.SimpMessageSendingOperations;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* Notify the progress of the trigger through websocket
|
||||
*
|
||||
* @author Fabio Formosa
|
||||
*/
|
||||
@Component
|
||||
public class WebSocketProgressNotifier implements WebhookSender<TriggerFiredBundleDTO> {
|
||||
|
||||
public static final String TOPIC_PROGRESS = "/topic/progress";
|
||||
|
||||
@Autowired
|
||||
private SimpMessageSendingOperations messagingTemplate;
|
||||
|
||||
@Override
|
||||
public void send(TriggerFiredBundleDTO triggerFiredBundleDTO) {
|
||||
messagingTemplate.convertAndSend(TOPIC_PROGRESS, triggerFiredBundleDTO);
|
||||
}
|
||||
|
||||
public static TriggerFiredBundleDTO buildTriggerFiredBundle(JobExecutionContext jobExecutionContext) {
|
||||
TriggerFiredBundleDTO triggerFiredBundleDTO = new TriggerFiredBundleDTO();
|
||||
|
||||
Trigger trigger = jobExecutionContext.getTrigger();
|
||||
triggerFiredBundleDTO.setFinalFireTime(trigger.getFinalFireTime());
|
||||
triggerFiredBundleDTO.setNextFireTime(trigger.getNextFireTime());
|
||||
triggerFiredBundleDTO.setPreviousFireTime(trigger.getPreviousFireTime());
|
||||
|
||||
if (trigger instanceof SimpleTrigger) {
|
||||
SimpleTrigger simpleTrigger = (SimpleTrigger) trigger;
|
||||
triggerFiredBundleDTO.setRepeatCount(simpleTrigger.getRepeatCount() + 1);
|
||||
triggerFiredBundleDTO.setTimesTriggered(simpleTrigger.getTimesTriggered());
|
||||
} else if (trigger instanceof DailyTimeIntervalTrigger) {
|
||||
DailyTimeIntervalTrigger dailyTrigger = (DailyTimeIntervalTrigger) trigger;
|
||||
triggerFiredBundleDTO.setRepeatCount(dailyTrigger.getRepeatCount() + 1);
|
||||
}
|
||||
|
||||
JobDetail jobDetail = jobExecutionContext.getJobDetail();
|
||||
triggerFiredBundleDTO.setJobKey(jobDetail.getKey().getName());
|
||||
triggerFiredBundleDTO.setJobClass(trigger.getClass().getSimpleName());
|
||||
return triggerFiredBundleDTO;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package it.fabioformosa.quartzmanager.websockets;
|
||||
|
||||
/**
|
||||
*
|
||||
* Notify the progress of the trigger to all consumers
|
||||
*
|
||||
* @author Fabio Formosa
|
||||
*
|
||||
*/
|
||||
public interface WebhookSender<T> {
|
||||
|
||||
void send(T message);
|
||||
|
||||
}
|
||||
@@ -4,7 +4,7 @@ import it.fabioformosa.quartzmanager.jobs.entities.LogRecord;
|
||||
import it.fabioformosa.quartzmanager.jobs.entities.LogRecord.LogType;
|
||||
import org.quartz.JobExecutionContext;
|
||||
|
||||
public class SampleJob extends AbstractLoggingJob {
|
||||
public class SampleJob extends AbstractQuartzManagerJob {
|
||||
|
||||
@Override
|
||||
public LogRecord doIt(JobExecutionContext jobExecutionContext) {
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
package it.fabioformosa.samplepackage;
|
||||
|
||||
import it.fabioformosa.quartzmanager.jobs.AbstractLoggingJob;
|
||||
import it.fabioformosa.quartzmanager.jobs.AbstractQuartzManagerJob;
|
||||
import it.fabioformosa.quartzmanager.jobs.entities.LogRecord;
|
||||
import it.fabioformosa.quartzmanager.jobs.entities.LogRecord.LogType;
|
||||
import org.quartz.JobExecutionContext;
|
||||
|
||||
public class SampleExtraJob extends AbstractLoggingJob {
|
||||
public class SampleExtraJob extends AbstractQuartzManagerJob {
|
||||
|
||||
@Override
|
||||
public LogRecord doIt(JobExecutionContext jobExecutionContext) {
|
||||
|
||||
@@ -2,12 +2,12 @@ package it.fabioformosa.quartzmanager.jobs.myjobs;
|
||||
|
||||
import org.quartz.JobExecutionContext;
|
||||
|
||||
import it.fabioformosa.quartzmanager.jobs.AbstractLoggingJob;
|
||||
import it.fabioformosa.quartzmanager.jobs.AbstractQuartzManagerJob;
|
||||
import it.fabioformosa.quartzmanager.jobs.entities.LogRecord;
|
||||
import it.fabioformosa.quartzmanager.jobs.entities.LogRecord.LogType;
|
||||
|
||||
|
||||
public class SampleJob extends AbstractLoggingJob {
|
||||
public class SampleJob extends AbstractQuartzManagerJob {
|
||||
@Override
|
||||
public LogRecord doIt(JobExecutionContext jobExecutionContext) {
|
||||
return new LogRecord(LogType.INFO, "Hello World!");
|
||||
|
||||
@@ -4,18 +4,18 @@ import org.quartz.JobExecutionContext;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import it.fabioformosa.quartzmanager.jobs.AbstractLoggingJob;
|
||||
import it.fabioformosa.quartzmanager.jobs.AbstractQuartzManagerJob;
|
||||
import it.fabioformosa.quartzmanager.jobs.entities.LogRecord;
|
||||
import it.fabioformosa.quartzmanager.jobs.entities.LogRecord.LogType;
|
||||
|
||||
/**
|
||||
* This job can be used to test the misfire policy. It pretends to be a long
|
||||
* processing job (sleeping for a while)
|
||||
*
|
||||
*
|
||||
* @author Fabio.Formosa
|
||||
*
|
||||
*/
|
||||
public class MisfireTestJob extends AbstractLoggingJob {
|
||||
public class MisfireTestJob extends AbstractQuartzManagerJob {
|
||||
|
||||
private Logger log = LoggerFactory.getLogger(MisfireTestJob.class);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user