[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..
[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..
[Java] 람다 캡쳐와 Synthetic 람다 클래스
·
JVM/Java
개요HeapDump 파일에서 Class$$Lambda$2831+2938213123+0x00 과 같은 식이 나왔다.요녀석의 정체는 무엇일까? synthetic lambda class아래와 같이 단일 메소드를 갖는 인터페이스는 Lambda 표현식으로 호출이 가능하다.public class LambdaTest { interface Callback { void onComplete(String result); } static class Messanger { void test(String msg, Callback callback) { callback.onComplete(msg); } } @DisplayName("single method interface can be called ..
[Java] OutOfMemory 잡기
·
JVM/Java
🎯 GoalsOutOfMemory 발생 원인 파악 하는 법을 안다. 예제 코드JVM option 에 다음 설정을 추가한다.Intellij IDEA > Rum Configuration > Modify > Add VM Options-Xms20m -Xmx20m -XX:+HeapDumpOnOutOfMemoryError-Xms20m힙 최소 크기 20MB-Xmx20m힙 최대 크기 20MB 제한-XX:HeapDumpOnOutOfMemoryOOM 발생시 HeapDump 파일을 생성public class HeapOOM { static class OOMObject {} public static void main(String[] args) { List list = new ArrayList(); while (..