Skip to content

Commit

Permalink
feat : sample quartz job
Browse files Browse the repository at this point in the history
  • Loading branch information
rajadilipkolli committed Nov 4, 2023
1 parent cc15533 commit 0ee9919
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 91 deletions.
2 changes: 1 addition & 1 deletion scheduler/boot-scheduler-quartz/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
<artifactId>spring-boot-starter-quartz</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
Expand Down
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());
}
}

This file was deleted.

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);
}
}
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();
}
}

This file was deleted.

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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,8 @@ spring.jpa.properties.hibernate.query.in_clause_parameter_padding=true
spring.jpa.properties.hibernate.query.plan_cache_max_size=4096
spring.jpa.properties.hibernate.connection.provider_disables_autocommit=true
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true

spring.quartz.job-store-type=jdbc
spring.quartz.jdbc.initialize-schema=always
spring.quartz.properties.org.quartz.jobStore.useProperties=true
spring.quartz.properties.org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.PostgreSQLDelegate
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
package com.scheduler.quartz;
package com.scheduler.quartz.repository;

import static org.assertj.core.api.Assertions.assertThat;

import com.scheduler.quartz.common.ContainersConfig;
import com.zaxxer.hikari.HikariDataSource;
import javax.sql.DataSource;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.boot.testcontainers.context.ImportTestcontainers;

@DataJpaTest(properties = {"spring.jpa.hibernate.ddl-auto=validate", "spring.test.database.replace=none"})
@ImportTestcontainers(ContainersConfig.class)
@DataJpaTest(
properties = {
"spring.jpa.hibernate.ddl-auto=validate",
"spring.test.database.replace=none",
"spring.datasource.url=jdbc:tc:postgresql:16.0-alpine:///db"
})
class SchemaValidationTest {

@Autowired
Expand Down

0 comments on commit 0ee9919

Please sign in to comment.