본문 바로가기

전체 글20

카프카 프로듀서 파티셔닝: 왜 라운드 로빈이 아니지? 카프카 프로듀서는 브로커에 토픽을 전송할 때 키가 있으면 해시 방식으로 보내고 키가 없으면 라운드 로빈 방식으로 전송한다고 알고 있었지만, 카프카 클라이언트 2.4.0 이후부터는 키가 없을 때 라운드 로빈 방식으로 동작하지 않다는 것을 알게 되었습니다. 문제 인식 과정 Spring Kafka(spring boot 3.1.2, kafka clients 3.4.1)를 이용하여 개발을 진행하던 중 @KafkaListener의 concurrency를 테스트하고 있었습니다. @KafkaListener(topics = ["sample"], groupId = "first", concurrency = "3") 파티션이 3개인 sample 토픽을 생성하고 위에 코드를 작성했습니다. concurrency를 3으로 설정하면.. 2023. 9. 4.
프록시 패턴(Proxy Pattern) 프록시는 대리인이라는 뜻으로 프록시 패턴은 클라이언트가 객체를 직접 참조하는 것이 아니라 proxy(대리인)를 통해 객체에 접근하는 패턴입니다. 구조와 글만 보면 이해하기 어려우니 예제를 통해 자세히 알아보겠습니다. public class Company { public void enter() { System.out.println("들어 가기"); } }해당 코드에서 만약 직원들만 회사에 들어갈 수 있다는 조건이 추가된다면 코드가 어떻게 수정될까요? public class Company { public void enter(String status) { if ("employee".equals(status) { System.out.println("들어 가기"); } } }if문을 추가해서 직원일 경우에만 들어.. 2022. 3. 7.
Item 53. 가변인수는 신중히 사용하라 가변인수 메서드는 명시한 타입의 인수를 0개 이상 받을 수 있습니다. private static int sum(int... args) { int[] arg = new int[3]; int sum = 0; for (int arg : args) { sum += arg; } return sum; }sum(1, 2, 3)은 6을 리턴, sum()은 0을 리턴합니다. 최솟값을 찾는 메서드는 인수가 최소 1개 이상이어야 할 수 있습니다. private static int min(int... args) { if (args.length == 0) { throw new IllegalArgumentException("인수가 1개 이상 필요합니다."); } int min = args[0]; for (int arg : arg.. 2022. 3. 6.
Item 52. 다중정의는 신중히 사용하라 public class CollectionClassifier { public static String classify(Set s) { return "집합"; } public static String classify(List list) { return "리스트"; } public static String classify(Collection c) { return "그 외"; } public static void main(String[] args) { Collection[] collections = { new HashSet(), new ArrayList(), new HashMap().values() }; for (Collection c : collections) { System.out.println(class.. 2022. 3. 6.