Skip to content

Commit

Permalink
Added get all jobs and get all jobs by group
Browse files Browse the repository at this point in the history
  • Loading branch information
juliuskrah committed Oct 12, 2017
1 parent abf16e4 commit 5268d5e
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,16 @@
package com.juliuskrah.quartz.service;

import static org.quartz.JobKey.jobKey;
import static org.quartz.impl.matchers.GroupMatcher.anyJobGroup;
import static org.quartz.impl.matchers.GroupMatcher.jobGroupEquals;

import java.util.HashSet;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;

import org.quartz.JobDetail;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -46,7 +51,49 @@ public abstract class AbstractJobService implements JobService {
*/
@Override
public abstract JobDescriptor createJob(String group, JobDescriptor descriptor);


/**
* {@inheritDoc}
*/
@Override
public Set<JobDescriptor> findJobs(){
Set<JobDescriptor> descriptors = new HashSet<>();
try {
Set<JobKey> keys = scheduler.getJobKeys(anyJobGroup());
for(JobKey key : keys) {
JobDetail jobDetail = scheduler.getJobDetail(key);
descriptors.add(
JobDescriptor.buildDescriptor(jobDetail,
scheduler.getTriggersOfJob(key)));
}
} catch (SchedulerException e) {
log.error("Could not find any jobs due to error - {}", e.getLocalizedMessage(), e);
throw new RuntimeException(e.getLocalizedMessage());
}
return descriptors;
}

/**
* {@inheritDoc}
*/
@Override
public Set<JobDescriptor> findGroupJobs(String group){
Set<JobDescriptor> descriptors = new HashSet<>();
try {
Set<JobKey> keys = scheduler.getJobKeys(jobGroupEquals(group));
for(JobKey key : keys) {
JobDetail jobDetail = scheduler.getJobDetail(key);
descriptors.add(
JobDescriptor.buildDescriptor(jobDetail,
scheduler.getTriggersOfJob(key)));
}
} catch (SchedulerException e) {
log.error("Could not find any jobs due to error - {}", e.getLocalizedMessage(), e);
throw new RuntimeException(e.getLocalizedMessage());
}
return descriptors;
}

/**
* {@inheritDoc}
*/
Expand Down
20 changes: 19 additions & 1 deletion src/main/java/com/juliuskrah/quartz/service/JobService.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package com.juliuskrah.quartz.service;

import java.util.Optional;
import java.util.Set;

import com.juliuskrah.quartz.model.JobDescriptor;

Expand All @@ -38,10 +39,27 @@ public interface JobService {
* @param descriptor
* the payload containing the Job and its associated Trigger(s).
* The name and group uniquely identifies the job.
* @return JobDescriptor this contains the JobDetail and Triggers of the
* @return JobDescriptor <br/> this contains the JobDetail and Triggers of the
* newly created job
*/
JobDescriptor createJob(String group, JobDescriptor descriptor);

/**
* Searches for all Jobs in the Scheduler
*
* @return Set of JobDescriptor <br/> this contains the JobDetail and Triggers of the
* newly created job
*/
Set<JobDescriptor> findJobs();

/**
* Searches for all Jobs in the Scheduler
* @param group the group to specify
*
* @return Set of JobDescriptor <br/> this contains the JobDetail and Triggers of the
* newly created job
*/
Set<JobDescriptor> findGroupJobs(String group);

/**
* Searches for a Job identified by the given {@code JobKey}
Expand Down
25 changes: 24 additions & 1 deletion src/main/java/com/juliuskrah/quartz/web/rest/EmailResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

import static org.springframework.http.HttpStatus.CREATED;

import java.util.Set;

import javax.validation.Valid;

import org.springframework.http.ResponseEntity;
Expand Down Expand Up @@ -52,7 +54,28 @@ public class EmailResource {
public ResponseEntity<JobDescriptor> createJob(@PathVariable String group, @Valid @RequestBody JobDescriptor descriptor) {
return new ResponseEntity<>(jobService.createJob(group, descriptor), CREATED);
}


/**
* GET /api/v1.0/groups/:group/jobs
*
* @param group
* @return
*/
@GetMapping(path = "/groups/{group}/jobs")
public ResponseEntity<Set<JobDescriptor>> findGroupJobs(@PathVariable String group) {
return ResponseEntity.ok(jobService.findGroupJobs(group));
}

/**
* GET /api/v1.0/jobs
*
* @return
*/
@GetMapping(path = "/jobs")
public ResponseEntity<Set<JobDescriptor>> findJobs() {
return ResponseEntity.ok(jobService.findJobs());
}

/**
* GET /api/v1.0/groups/:group/jobs/:name
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static org.springframework.http.HttpStatus.BAD_REQUEST;
import static org.springframework.http.HttpStatus.CONFLICT;
import static org.springframework.http.HttpStatus.METHOD_NOT_ALLOWED;
import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR;

import java.util.List;
Expand All @@ -11,6 +12,7 @@
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
Expand Down Expand Up @@ -71,6 +73,18 @@ public ErrorVO processDataIntegrityViolationError(DataIntegrityViolationExceptio
return dto;
}

@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
public ResponseEntity<ErrorVO> processMethodNotSupportedException(HttpRequestMethodNotSupportedException ex) {
ErrorVO dto = ImmutableErrorVO.builder()
.message("405: Method Not Allowed")
.description(ex.getMessage())
.build();
return ResponseEntity
.status(METHOD_NOT_ALLOWED)
.header("allow", ex.getSupportedMethods())
.body(dto);
}

@ExceptionHandler(Exception.class)
public ResponseEntity<ErrorVO> processException(Exception ex) throws Exception {
if (log.isDebugEnabled()) {
Expand All @@ -91,5 +105,4 @@ public ResponseEntity<ErrorVO> processException(Exception ex) throws Exception {
.build();
return ResponseEntity.status(INTERNAL_SERVER_ERROR).body(dto);
}

}

0 comments on commit 5268d5e

Please sign in to comment.