[Java] Primitive 타입보다 VO 를 의존하라
·
JVM/Java
Kafka Record 에 들어갈 Record Key, Record Value 를 OffsetRecord 인터페이스로 추상화했다.OffsetRecord 는 오로지 오프셋을 저장하는 토픽에 쓰인다.예를 들면 이런식이다.Record KeyC:\\Users\M_Faclon\path\a.txtRecord value30/** * Domain interface used by OffsetManager and SourceConnector Producer. * Consists of object unique identifier and offset. * Stored in the Offset topic partition. */public interface OffsetRecord { /** * The unique ke..
[Java] Clean Code - index, array 대신 Iterator
·
JVM/Java
class Args { private String[] args; int currentIndex;}args 를 하나씩 파싱하는 메소드가 필요하다고 해보자,인자를 넘길때마다 이런 형식이 된다.Object parseArgument(String[] args, int index);인자 수는 적으면 적을수록 좋다. 인자가 많을수록 복잡하다. - 클린코드Args 클래스를 Iterator 로 리팩터링해보자.class Args { List args; Iterator currentElement;}이제 args 속 원소를 하나씩 넘길 수 있다.그저 currentElement 만 넘겨주면 된다.Ob..
[Spring] SpringBoot 수동 빈 등록 테스트
·
JVM/Spring
요구 사항아래와 같은 yaml 설정에 따라수동으로 사용할 빈을 등록하고싶다.app: storage: type: 'local' # [local, s3]type 항목에 따라 아래와 같이 사용할 클래스를 스프링 빈에 등록하고자한다.local -> LocalFileLister, LocalFileRepository's3' -> S3FileLister, S3FileRepositoryJava 로 수동 빈 등록 코드를 작성했다.@Configurationpublic class StorageRepositoryConfiguration { @Bean public FileValidator fileValidator(FiltersConfig filtersConfig) { return filtersConfig.toV..
[Spring] ConfigurationProperties POJOs 설계
·
JVM/Spring
File Storage -> Kafka 로 중복 없이 레코드를 전송하는 라이브러리를 만들고 있다.다음과 같이 Configuration yaml 을 디자인했다고 해보자.storage: type: local, s3, blob # here is deciding which storage is used. paths: - 'df' - 'df.log' filters: - type: Exclude expressions: - '*empty*' - type: Extension expressions: - .ndjson - .csv # So, bean injection is decided here s3: bucket: reg..
[Java] Closeable vs AutoCloseable
·
JVM/Java
개요앞에 Auto 라는 접두사 차이가 있다.언제 무슨 인터페이스를 쓰는게 적절한지 선택할 수 있도록 차이점을 알아본다.Closeablepublic interface Closeable extends AutoCloseable { public void close() throws IOException;}스트림을 닫고 리소스 사용을 중단하는 행위를 정의한다.이 인터페이스를 구현하는 클래스에는FileInputStream, FileOutputStream 등이 있다.IOExceptionIO 동작이 실패하거니 인터럽트가 발생함을 알리는 예외FileNotFoundException, UnsupportedEncodingException 등이 있다.IdempotentClose 는 반드시 멱등성을 보장해야한다.즉, 여러번 ..
[Java] 제네릭과 동적 타입 캐스팅
·
JVM/Java
개요Java 의 Json 데이터를 다루는 Jackson 라이브러리에서는런타임에 동적인 형 변환을 지원하고자 다음과 같은 메소드를 지원한다.public class ObjectMapper { public T convertValue(Object fromValue, Class toValueType) throws IllegalArgumentException { return (T) _convert(fromValue, _typeFactory.constructType(toValueType)); } public T convertValue(Object fromValue, TypeReference toValueTypeRef) throws IllegalArgume..