Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor/core kafka exceptions #69

Merged
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# La-Planete
# Joyeuse_Planete
재고음식을 크게 할인된 가격으로 살 수 있게 하는 플랫폼으로서 소비자와 공급자의 편익을 동시에 증진시키는 것을 지향하는 프로젝트입니다.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ plugins {
bootJar.enabled = false
jar.enabled = false

group = 'com.f-lab.la_planete'
group = 'com.f-lab.joyeuse_planete'
version = '0.0.1-SNAPSHOT'

subprojects {
apply plugin: 'java'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

group = 'com.f-lab.la_planete'
group = 'com.f-lab.joyeuse_planete'

java {
toolchain {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.f_lab.la_planete.core.aspect;
package com.f_lab.joyeuse_planete.core.aspect;

import com.f_lab.la_planete.core.exceptions.ApplicationException;
import com.f_lab.la_planete.core.exceptions.ErrorCode;
import com.f_lab.la_planete.core.util.time.TimeConstantsString;
import com.f_lab.joyeuse_planete.core.exceptions.JoyeusePlaneteApplicationException;
import com.f_lab.joyeuse_planete.core.exceptions.ErrorCode;
import com.f_lab.joyeuse_planete.core.util.time.TimeConstantsString;
import jakarta.persistence.LockTimeoutException;
import jakarta.persistence.PessimisticLockException;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -22,7 +22,7 @@ public class LockRetryAspect {
private static final int FIRST_WAIT_INTERVAL = Integer.parseInt(TimeConstantsString.ONE_SECOND);
private static final int MULTIPLIER = 2;

@Around("@annotation(com.f_lab.la_planete.core.aspect.RetryOnLockFailure)")
@Around("@annotation(com.f_lab.joyeuse_planete.core.aspect.RetryOnLockFailure)")
public Object lockRetry(ProceedingJoinPoint joinPoint) {
int attempts = 0, stopInterval = FIRST_WAIT_INTERVAL;

Expand All @@ -44,15 +44,15 @@ public Object lockRetry(ProceedingJoinPoint joinPoint) {
Thread.sleep(stopInterval);

} catch (InterruptedException ex) {
throw new ApplicationException(ErrorCode.LOCK_ACQUISITION_FAIL_EXCEPTION, e);
throw new JoyeusePlaneteApplicationException(ErrorCode.LOCK_ACQUISITION_FAIL_EXCEPTION, e);
}

} catch (Throwable e) {
log.error("예상치 못한 오류가 발생하였습니다. 다시 시도해 주세요", e);
throw new ApplicationException(ErrorCode.LOCK_ACQUISITION_FAIL_EXCEPTION, e);
throw new JoyeusePlaneteApplicationException(ErrorCode.LOCK_ACQUISITION_FAIL_EXCEPTION, e);
}
}

throw new ApplicationException(ErrorCode.LOCK_ACQUISITION_FAIL_EXCEPTION);
throw new JoyeusePlaneteApplicationException(ErrorCode.LOCK_ACQUISITION_FAIL_EXCEPTION);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.f_lab.la_planete.core.aspect;
package com.f_lab.joyeuse_planete.core.aspect;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.f_lab.la_planete.core.config;
package com.f_lab.joyeuse_planete.core.config;


import org.springframework.context.annotation.Configuration;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.f_lab.la_planete.core.config;
package com.f_lab.joyeuse_planete.core.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package com.f_lab.la_planete.core.domain;
import com.f_lab.la_planete.core.domain.base.BaseTimeEntity;
package com.f_lab.joyeuse_planete.core.domain;
import com.f_lab.joyeuse_planete.core.domain.base.BaseTimeEntity;

import jakarta.persistence.*;
import lombok.AllArgsConstructor;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.f_lab.la_planete.core.domain;
package com.f_lab.joyeuse_planete.core.domain;

import com.f_lab.la_planete.core.domain.base.BaseEntity;
import com.f_lab.joyeuse_planete.core.domain.base.BaseEntity;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.f_lab.la_planete.core.domain;
package com.f_lab.joyeuse_planete.core.domain;


import com.f_lab.la_planete.core.domain.base.BaseEntity;
import com.f_lab.joyeuse_planete.core.domain.base.BaseEntity;
import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.f_lab.la_planete.core.domain;
package com.f_lab.joyeuse_planete.core.domain;

import lombok.Getter;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.f_lab.la_planete.core.domain;
package com.f_lab.joyeuse_planete.core.domain;

import com.f_lab.la_planete.core.domain.base.BaseEntity;
import com.f_lab.joyeuse_planete.core.domain.base.BaseEntity;
import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.f_lab.la_planete.core.domain;
package com.f_lab.joyeuse_planete.core.domain;

import lombok.Getter;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.f_lab.la_planete.core.domain;
package com.f_lab.joyeuse_planete.core.domain;


import com.f_lab.la_planete.core.domain.base.BaseTimeEntity;
import com.f_lab.joyeuse_planete.core.domain.base.BaseTimeEntity;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.f_lab.la_planete.core.domain;
package com.f_lab.joyeuse_planete.core.domain;

import com.f_lab.la_planete.core.domain.base.BaseTimeEntity;
import com.f_lab.joyeuse_planete.core.domain.base.BaseTimeEntity;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.f_lab.la_planete.core.domain.base;
package com.f_lab.joyeuse_planete.core.domain.base;



Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.f_lab.la_planete.core.domain.base;
package com.f_lab.joyeuse_planete.core.domain.base;

import jakarta.persistence.Column;
import jakarta.persistence.EntityListeners;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.f_lab.la_planete.core.events;
package com.f_lab.joyeuse_planete.core.events;

import lombok.AllArgsConstructor;
import lombok.Builder;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.f_lab.la_planete.core.events;
package com.f_lab.joyeuse_planete.core.events;


import lombok.AllArgsConstructor;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.f_lab.la_planete.core.exceptions;
package com.f_lab.joyeuse_planete.core.exceptions;

import lombok.AllArgsConstructor;
import lombok.Getter;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.f_lab.joyeuse_planete.core.exceptions;

public class JoyeusePlaneteApplicationException extends RuntimeException {

public JoyeusePlaneteApplicationException() {
}

public JoyeusePlaneteApplicationException(ErrorCode errorCode) {
super(errorCode.getDescription());
}

public JoyeusePlaneteApplicationException(ErrorCode errorCode, Throwable cause) {
super(errorCode.getDescription(), cause);
}

public JoyeusePlaneteApplicationException(Throwable cause) {
super(cause);
}

public JoyeusePlaneteApplicationException(ErrorCode errorCode, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(errorCode.getDescription(), cause, enableSuppression, writableStackTrace);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.f_lab.la_planete.core.kafka.aspect;
package com.f_lab.joyeuse_planete.core.kafka.aspect;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.f_lab.la_planete.core.kafka.aspect;
package com.f_lab.joyeuse_planete.core.kafka.aspect;

import com.f_lab.la_planete.core.kafka.exceptions.NonRetryableException;
import com.f_lab.joyeuse_planete.core.exceptions.JoyeusePlaneteApplicationException;
import com.f_lab.joyeuse_planete.core.exceptions.ErrorCode;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
Expand All @@ -17,28 +18,25 @@ public class KafkaRetryAspect {
private int MAX_RETRY;
private static final int STOP_INTERVAL = 1000;

@Around("@annotation(com.f_lab.la_planete.core.kafka.aspect.KafkaRetry)")
@Around("@annotation(com.f_lab.joyeuse_planete.core.kafka.aspect.KafkaRetry)")
public Object kafkaRetry(ProceedingJoinPoint joinPoint) {
int attempt = 0;

while(attempt < MAX_RETRY) {
try {
return joinPoint.proceed();
} catch (NonRetryableException e) {
throw e;

} catch (Throwable e) {
log.warn("시도 횟수={}, 메서드={}", attempt, joinPoint.getSignature());
attempt++;

try {
Thread.sleep(STOP_INTERVAL);
} catch (InterruptedException ex) {
throw new NonRetryableException(ex);
throw new JoyeusePlaneteApplicationException(ErrorCode.KAFKA_RETRY_FAIL_EXCEPTION, ex);
}
}
}

throw new NonRetryableException("오류 발생! 잠시 후 다시 시도해주세요.");
throw new JoyeusePlaneteApplicationException(ErrorCode.KAFKA_RETRY_FAIL_EXCEPTION);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.f_lab.joyeuse_planete.core.kafka.config;

import com.f_lab.joyeuse_planete.core.kafka.exceptions.NonRetryableException;
import com.f_lab.joyeuse_planete.core.kafka.exceptions.RetryableException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.kafka.config.ConcurrentKafkaListenerContainerFactory;
import org.springframework.kafka.core.ConsumerFactory;
import org.springframework.kafka.core.DefaultKafkaConsumerFactory;
import org.springframework.kafka.listener.DeadLetterPublishingRecoverer;
import org.springframework.kafka.listener.DefaultErrorHandler;
import org.springframework.util.backoff.FixedBackOff;

import java.util.Map;

public abstract class KafkaConsumerConfig {

@Value("${spring.kafka.bootstrap-servers}")
protected String BOOTSTRAP_SERVERS;

@Value("${spring.kafka.consumer.enable-auto-commit}")
protected boolean AUTO_COMMIT;

@Value("${kafka.container.concurrency}")
protected int CONCURRENCY;

@Value("${spring.kafka.consumer.properties.spring.json.trusted.packages}")
protected String TRUSTED_PACKAGES;

@Value("${spring.kafka.consumer.isolation-level}")
protected String ISOLATION_LEVEL;


abstract public ConcurrentKafkaListenerContainerFactory<String, Object> kafkaListenerContainerFactory();

abstract protected Map<String, Object> consumerConfig();

protected DeadLetterPublishingRecoverer deadLetterPublishingRecoverer() {
return null;
}

public FixedBackOff defaultBackOffStrategy() {
return new FixedBackOff(1500L, 10);
}

public DefaultErrorHandler defaultErrorHandler() {
DefaultErrorHandler errorHandler = new DefaultErrorHandler(
deadLetterPublishingRecoverer(),
defaultBackOffStrategy());

errorHandler.addNotRetryableExceptions(NonRetryableException.class);
errorHandler.addRetryableExceptions(RetryableException.class);

return errorHandler;
}

public ConsumerFactory<String, Object> consumerFactory() {
return new DefaultKafkaConsumerFactory<>(consumerConfig());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.f_lab.joyeuse_planete.core.kafka.config;

import lombok.AccessLevel;
import lombok.Getter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.kafka.core.DefaultKafkaProducerFactory;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.core.ProducerFactory;
import org.springframework.kafka.transaction.KafkaTransactionManager;

import java.util.Map;

public abstract class KafkaProducerConfig {

@Value("${spring.kafka.bootstrap-servers}")
protected String BOOTSTRAP_SERVERS;

@Value("${spring.kafka.producer.ack:all}")
protected String ACK;

@Value("${spring.kafka.producer.enable.idempotence:true}")
protected String IDEMPOTENCE;

abstract protected Map<String, Object> producerConfig();

public KafkaTemplate<String, Object> kafkaTemplate() {
return new KafkaTemplate<>(producerFactory());
}

public KafkaTransactionManager<String, Object> kafkaTransactionManager() {
return new KafkaTransactionManager<>(producerFactory());
}

public ProducerFactory<String, Object> producerFactory() {
return new DefaultKafkaProducerFactory<>(producerConfig());
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.f_lab.joyeuse_planete.core.kafka.exceptions;


import com.f_lab.joyeuse_planete.core.exceptions.JoyeusePlaneteApplicationException;

public class NonRetryableException extends JoyeusePlaneteApplicationException {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.f_lab.joyeuse_planete.core.kafka.exceptions;

public class RetryableException extends RuntimeException {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.f_lab.joyeuse_planete.core.kafka.service;

import com.f_lab.joyeuse_planete.core.kafka.aspect.KafkaRetry;
import com.f_lab.joyeuse_planete.core.kafka.exceptions.RetryableException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.transaction.annotation.Transactional;

import static org.springframework.transaction.annotation.Propagation.REQUIRES_NEW;

@Slf4j
@RequiredArgsConstructor
public class KafkaService {

private final KafkaTemplate<String, Object> kafkaTemplate;

@KafkaRetry
@Transactional(propagation = REQUIRES_NEW)
public void sendKafkaEvent(String event, Object object) {
try {
kafkaTemplate.send(event, object);
} catch(Exception e) {
log.error("오류가 발생하였습니다. event = {}, message = {}", event, e.getMessage(), e);
throw new RetryableException();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.f_lab.la_planete.core.util.time;
package com.f_lab.joyeuse_planete.core.util.time;

public class TimeConstantsString {
public static final String FIVE_SECONDS = "5000";
Expand Down
Loading
Loading