-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cc15533
commit 0ee9919
Showing
9 changed files
with
82 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 2 additions & 44 deletions
46
...ot-scheduler-quartz/src/main/java/com/scheduler/quartz/config/GlobalExceptionHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,19 @@ | ||
package com.scheduler.quartz.config; | ||
|
||
import com.scheduler.quartz.exception.ResourceNotFoundException; | ||
import java.net.URI; | ||
import java.time.Instant; | ||
import java.util.Comparator; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import org.springframework.core.Ordered; | ||
import org.springframework.core.annotation.Order; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.HttpStatusCode; | ||
import org.springframework.http.ProblemDetail; | ||
import org.springframework.validation.FieldError; | ||
import org.springframework.web.bind.MethodArgumentNotValidException; | ||
import org.springframework.web.bind.annotation.ControllerAdvice; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.bind.annotation.ResponseStatus; | ||
|
||
@ControllerAdvice | ||
@Order(Ordered.HIGHEST_PRECEDENCE) | ||
public class GlobalExceptionHandler { | ||
|
||
@ExceptionHandler(MethodArgumentNotValidException.class) | ||
@ResponseStatus(HttpStatus.BAD_REQUEST) | ||
ProblemDetail onException(MethodArgumentNotValidException methodArgumentNotValidException) { | ||
ProblemDetail problemDetail = | ||
ProblemDetail.forStatusAndDetail(HttpStatusCode.valueOf(400), "Invalid request content."); | ||
problemDetail.setTitle("Constraint Violation"); | ||
List<ApiValidationError> validationErrorsList = methodArgumentNotValidException.getAllErrors().stream() | ||
.map(objectError -> { | ||
FieldError fieldError = (FieldError) objectError; | ||
return new ApiValidationError( | ||
fieldError.getObjectName(), | ||
fieldError.getField(), | ||
fieldError.getRejectedValue(), | ||
Objects.requireNonNull(fieldError.getDefaultMessage(), "")); | ||
}) | ||
.sorted(Comparator.comparing(ApiValidationError::field)) | ||
.toList(); | ||
problemDetail.setProperty("violations", validationErrorsList); | ||
return problemDetail; | ||
} | ||
|
||
@ExceptionHandler(Exception.class) | ||
ProblemDetail onException(Exception exception) { | ||
if (exception instanceof ResourceNotFoundException resourceNotFoundException) { | ||
ProblemDetail problemDetail = ProblemDetail.forStatusAndDetail( | ||
resourceNotFoundException.getHttpStatus(), resourceNotFoundException.getMessage()); | ||
problemDetail.setTitle("Not Found"); | ||
problemDetail.setType(URI.create("http://api.boot-scheduler-quartz.com/errors/not-found")); | ||
problemDetail.setProperty("errorCategory", "Generic"); | ||
problemDetail.setProperty("timestamp", Instant.now()); | ||
return problemDetail; | ||
} else { | ||
return ProblemDetail.forStatusAndDetail(HttpStatusCode.valueOf(500), exception.getMessage()); | ||
} | ||
} | ||
|
||
record ApiValidationError(String object, String field, Object rejectedValue, String message) {} | ||
return ProblemDetail.forStatusAndDetail(HttpStatusCode.valueOf(500), exception.getMessage()); | ||
} | ||
} |
14 changes: 0 additions & 14 deletions
14
...eduler-quartz/src/main/java/com/scheduler/quartz/exception/ResourceNotFoundException.java
This file was deleted.
Oops, something went wrong.
23 changes: 23 additions & 0 deletions
23
scheduler/boot-scheduler-quartz/src/main/java/com/scheduler/quartz/job/SampleJob.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.scheduler.quartz.job; | ||
|
||
import com.scheduler.quartz.service.OddEvenService; | ||
import org.quartz.Job; | ||
import org.quartz.JobExecutionContext; | ||
import org.quartz.JobExecutionException; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class SampleJob implements Job { | ||
|
||
private final OddEvenService oddEvenService; | ||
|
||
public SampleJob(OddEvenService oddEvenService) { | ||
this.oddEvenService = oddEvenService; | ||
} | ||
|
||
@Override | ||
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException { | ||
String jobName = jobExecutionContext.getJobDetail().getJobDataMap().getString("jobName"); | ||
oddEvenService.execute(jobName); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
...scheduler-quartz/src/main/java/com/scheduler/quartz/job/SchedulerRegistrationService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.scheduler.quartz.job; | ||
|
||
import static org.quartz.DateBuilder.futureDate; | ||
import static org.quartz.JobBuilder.newJob; | ||
import static org.quartz.TriggerBuilder.newTrigger; | ||
|
||
import org.quartz.*; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@Configuration(proxyBeanMethods = false) | ||
public class SchedulerRegistrationService { | ||
|
||
@Bean | ||
JobDetail registerOddJob() { | ||
return newJob(SampleJob.class) | ||
.withIdentity("sample-job") | ||
.usingJobData("jobName", "odd") | ||
.storeDurably() | ||
.build(); | ||
} | ||
|
||
@Bean | ||
Trigger triggerOddJob(JobDetail registerOddJob) { | ||
return newTrigger() | ||
.withIdentity("trigger-sample-job") | ||
.forJob(registerOddJob.getKey()) | ||
.startAt(futureDate(5, DateBuilder.IntervalUnit.SECOND)) | ||
.build(); | ||
} | ||
} |
27 changes: 0 additions & 27 deletions
27
.../boot-scheduler-quartz/src/main/java/com/scheduler/quartz/model/response/PagedResult.java
This file was deleted.
Oops, something went wrong.
13 changes: 13 additions & 0 deletions
13
...uler/boot-scheduler-quartz/src/main/java/com/scheduler/quartz/service/OddEvenService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.scheduler.quartz.service; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
@Slf4j | ||
public class OddEvenService { | ||
|
||
public void execute(String jobName) { | ||
log.info("JobName :{}", jobName); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 7 additions & 5 deletions
12
...-scheduler-quartz/src/test/java/com/scheduler/quartz/repository/SchemaValidationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters