하루종일 영문이력서를 작성했습니다.

Chatgpt를 활용했는데 생각보다 도움이 되서 놀랐어요.

chatgpt가 도와주는 이력서 작성

위에처럼 문장 하나하나씩 물어보다가

chatgpt가 주는 이력서 꿀팁

well-organized and presents a clear picture of your work experience and technical skills라고 칭찬받았습니다.

 

인공지능을 배우고 싶은 맘이 불쑥 들었습니다. 파이선으로 코딩테스트를 준비해야하나요 ㅎㅎ

부트캠프에서 제공한 개발자 우수 이력서 양식들을 보고 놀라서 자빠져있다가 일어나서 이력서 초안을 작성했습니다. 비지니스 이력서, 포맷이 정해진 이력서를 작성하고 심지어 부업으로 그런 이력서들을 첨삭해주는 일도 했던 저에게는 기절 초풍할 노릇이었습니다. 노션으로 이력서를 작성할 수도 있다는 사실은 알고 있었는데 '안녕하세요'가 들어간 이력서라니...🤦‍♀️🤦‍♀️

 

 우선은 부트캠프에서 제공한 이력서 양식을 참조하되 제가 아는 방식으로 조금 더 포멀하게 만들었습니다. 깃헙을 검색해서 나온 이력서들도 참조했습니다. 내용은 여전히 어떻게 채워야 할지는 모르겠습니다. 최종 프로젝트를 6주동안 진행했는데 아무래도 제가 구현하지 않은 부분도 있다 보니 어떤부분을 넣을지, 어떤부분을 강조할 지에 대해서는 피드백이 필요하다고 판단했습니다. 우선 부트캠프에서 제공하는 이력서 피드백 서비스를 신청했습니다.

개발자 이력서 초안

 

 

원래는 보통 학력이 맨위에 올라가는데, 학력을 맨밑에 넣는 일이 생겼습니다..  전공이 달라 슬픕니다. 첨부터 공대갈걸..........ㅠㅠ

QueryDSL을 이용해서 조회를 하고 데이터를 불러올 때 페이징 처리를 하는 방법에 대해 알아보겠습니다. QueryDSL로 페이징을 처리하는 방법은 의외로 생각 보다 더 간단했습니다. 

 

JPQL Pageable

기존에는 Spring Data JPA에서 제공하는 기능인 Pageable 기능을 사용하여 매우 쉽게 처리 했었는데 아래와 같습니다.

List<Post> findByTitleContainsOrContentsContains(Pageable pageable, String search, String searchContents);

Pageable 을 깊게 들어가면 아래와 같이 나옵니다.

/*
 * Copyright 2008-2022 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.springframework.data.domain;

import java.util.Optional;

import org.springframework.util.Assert;

/**
 * Abstract interface for pagination information.
 *
 * @author Oliver Gierke
 * @author Mark Paluch
 */
public interface Pageable {

   /**
    * Returns a {@link Pageable} instance representing no pagination setup.
    *
    * @return
    */
   static Pageable unpaged() {
      return Unpaged.INSTANCE;
   }

   /**
    * Creates a new {@link Pageable} for the first page (page number {@code 0}) given {@code pageSize} .
    *
    * @param pageSize the size of the page to be returned, must be greater than 0.
    * @return a new {@link Pageable}.
    * @since 2.5
    */
   static Pageable ofSize(int pageSize) {
      return PageRequest.of(0, pageSize);
   }

   /**
    * Returns whether the current {@link Pageable} contains pagination information.
    *
    * @return
    */
   default boolean isPaged() {
      return true;
   }

   /**
    * Returns whether the current {@link Pageable} does not contain pagination information.
    *
    * @return
    */
   default boolean isUnpaged() {
      return !isPaged();
   }

   /**
    * Returns the page to be returned.
    *
    * @return the page to be returned or throws {@link UnsupportedOperationException} if the object is
    *         {@link #isUnpaged()}.
    * @throws UnsupportedOperationException if the object is {@link #isUnpaged()}.
    */
   int getPageNumber();

   /**
    * Returns the number of items to be returned.
    *
    * @return the number of items of that page or throws {@link UnsupportedOperationException} if the object is
    *         {@link #isUnpaged()}.
    * @throws UnsupportedOperationException if the object is {@link #isUnpaged()}.
    */
   int getPageSize();

   /**
    * Returns the offset to be taken according to the underlying page and page size.
    *
    * @return the offset to be taken or throws {@link UnsupportedOperationException} if the object is
    *         {@link #isUnpaged()}.
    * @throws UnsupportedOperationException if the object is {@link #isUnpaged()}.
    */
   long getOffset();

   /**
    * Returns the sorting parameters.
    *
    * @return
    */
   Sort getSort();

   /**
    * Returns the current {@link Sort} or the given one if the current one is unsorted.
    *
    * @param sort must not be {@literal null}.
    * @return
    */
   default Sort getSortOr(Sort sort) {

      Assert.notNull(sort, "Fallback Sort must not be null");

      return getSort().isSorted() ? getSort() : sort;
   }

   /**
    * Returns the {@link Pageable} requesting the next {@link Page}.
    *
    * @return
    */
   Pageable next();

   /**
    * Returns the previous {@link Pageable} or the first {@link Pageable} if the current one already is the first one.
    *
    * @return
    */
   Pageable previousOrFirst();

   /**
    * Returns the {@link Pageable} requesting the first page.
    *
    * @return
    */
   Pageable first();

   /**
    * Creates a new {@link Pageable} with {@code pageNumber} applied.
    *
    * @param pageNumber
    * @return a new {@link PageRequest} or throws {@link UnsupportedOperationException} if the object is
    *         {@link #isUnpaged()} and the {@code pageNumber} is not zero.
    * @since 2.5
    * @throws UnsupportedOperationException if the object is {@link #isUnpaged()}.
    */
   Pageable withPage(int pageNumber);

   /**
    * Returns whether there's a previous {@link Pageable} we can access from the current one. Will return
    * {@literal false} in case the current {@link Pageable} already refers to the first page.
    *
    * @return
    */
   boolean hasPrevious();

   /**
    * Returns an {@link Optional} so that it can easily be mapped on.
    *
    * @return
    */
   default Optional<Pageable> toOptional() {
      return isUnpaged() ? Optional.empty() : Optional.of(this);
   }

}

 

 

 

 

 

 

QueryDSL 페이징 처리 하기

 

@Override

    public List<Post> findByTitleContainsOrContentsContains(Pageable pageable, String search, String searchContents) {

        return jpaQueryFactory.selectFrom(post)

                .where(post.title.contains(search).or(post.title.contains(searchContents)))

                .offset(pageable.getOffset())

                .limit(pageable.getPageSize())

                .fetch();

    }

 

offset 과 limit으로 페이징 처리를 합니다.

 

 

 

정작 프로젝트는....

복잡한 쿼리(삭제, 업데이트)에만 QueryDSL을 사용하기로 했으므로 QueryDSL을 사용한 페이징 처리는 하지 않았습니다.

 

 

 

 

TIL QueryDSL 마스터가 되다 (조회, 수정, 삭제, Optional) 230208

QueryDSL 마스터가 되었습니다. 설치하는 과정은 지난 포스팅을 참조해주세요. TIL QueryDSL 5.0.0 적용하기 230206 QueryDSL의 최신 버전(2021.07.22 release)인 5.0 버전을 사용했습니다. 블로그 자료들에는 outdat

pizzathedeveloper.tistory.com

 

 

TIL QueryDSL 5.0.0 적용하기 230206

QueryDSL의 최신 버전(2021.07.22 release)인 5.0 버전을 사용했습니다. 블로그 자료들에는 outdated된 것들이 많아서 최대한 공식 문서와 github을 참조하려고 했습니다. QueryDSL의 github에는 데이터 타입 별

pizzathedeveloper.tistory.com

 

 

 

 

나름 이력서 코칭하는 알바도 했었지만! 개발자의 이력서는 어떻게 써야하는지 알지 못했던 저에게 유용한 세선을 오전내내 듣고 이력서를 작성하는 시간을 가졌습니다. 퇴사하고 많은일을 했었지만 개발관련된 것들은 부트캠프를 제외하면 없기 때문에 공백을 설명하는 것이 걱정일 거 같긴 합니다.

 

 

멘토님의 모의 면접

갑자기 멘토님이 면접질문을 하시겠다고 해서 당황했지만 질문하신 내역을 정리해 보았습니다.

 

  • ORM 이란?
  • JPA란?
  • QueryDSL이란? (ORM이랑 묶어서 )
  • MyBatis?
  • elastic search란? (개념 알기)
  • Index란?
  • gradle 이란?
  • Hibernate는 뭐야?
  • JPA는 뭐지
  • MySQL 왜 도입했는지?
  • 인증, 인가, 쿠키, 세션, 토큰 
  • Oauth 대충이해하기
  •  SSE vs Websocket
  • CDN, Nginx  flow 알기 
  • Spring 구조, 작동 원리

 

 

답변은 추후 작성하기로 하겠습니다.

 

 

기타 멘토링 사항

기술은 구구절절 나열하지 않기.

내가 어떤 문제 해결을 위해서 어떤 기술을 썼습니다 라고 하면 됩니다. 

여유로운 사람을 찾게 됩니다. 과시적인 사람은 알맹이가 오히려 없습니다. 

구구절절은 지양하기.

 

 

이력서 쓰기

개발자 이력서는 처음이라 어떤 포맷으로 해야할지도 고민이 됩니다. 최종 프로젝트가 커서 할 말이 많아 다행입니다. 내일까지 이력서 제출하고 피드백을 받는 일정입니다. 취업까지 화이팅!

 

화이팅!

최종발표가 끝났습니다.

감개무량합니다. 

 

긍정적인 피드백이 많았습니다.

지원해달라는 협력사분들도 있었고 다들 칭찬을 많이 해주셔서 기분이 좋았습니다.

 

기념 사진

반사람들 다같이 최종발표 기념사진을 찍었습니다.

 

CRUD를 갓 배운 상태에서 다양한 기능들을 접하게 되어 처음에는 압도되서 부담이 되었지만 step-by-step으로 기술 하나하나 적용했고 발생하는 에러들을 해결하다보니 어느새 멋진 프로젝트가 완성되어 모두 앞에서 선보일 수 있게 되었습니다. 실력적으로도 경험적으로도 크게 성장했습니다. 좋은 팀원들을 만나서 운이 좋았다고 생각합니다. 끝까지 노력한 저와 팀원들 모두에게 감사합니다.

oncounter 최종발표영상 입니다.

10분 초과되서 제가 급하게 영상 컷편집을 했습니다.

내일은 진짜 발표날입니다!!

 

이와중에 Elastic Search 성공한 거 실화입니까ㅏㅏㅏㅏ

QueryDSL 마스터가 되었습니다. 설치하는 과정은 지난 포스팅을 참조해주세요.

 

 

TIL QueryDSL 5.0.0 적용하기 230206

QueryDSL의 최신 버전(2021.07.22 release)인 5.0 버전을 사용했습니다. 블로그 자료들에는 outdated된 것들이 많아서 최대한 공식 문서와 github을 참조하려고 했습니다. QueryDSL의 github에는 데이터 타입 별

pizzathedeveloper.tistory.com

 

QueryDSL 조회하기 (Optional)

@Override
public Optional<PostLike> findByPostLikedIdAndMemberId(Long postLikedId, Long memberId) {
    PostLike postLike1 = jpaQueryFactory.select(postLike)
            .from(postLike)
            .where(postLike.postLiked.id.eq(postLikedId).and(postLike.member.id.eq(memberId)))
            .fetchOne();
    return Optional.ofNullable(postLike1);
}

 

 

QueryDSL 수정하기

@Override
public void updateNickname(String before, String after) {
    jpaQueryFactory
            .update(comment)
            .where(comment.nickname.eq(before))
            .set(comment.nickname, after)
            .execute();
}

 

 

 

QueryDSL 삭제하기

@Override
public void deleteAllByPost(Post post) {
    jpaQueryFactory
            .delete(comment)
            .where(comment.post.eq(post))
            .execute();
}

 

생각보다 어려워서 미루다가 밤산책 한번 갔다 머리 refresh하고 보니까 갑자기 쏙쏙 들어와서 반 정도 하고 잤습니다. 다른 조들은 기능 추가 없이 이력서를 쓰고 다른 공부를 하고 계시더군요. 면접준비도 슬슬해야하고 이력서용 사진도 찍어야 합니다. 화이팅....!

JPQL을 QueryDSL로 변경하기

우선 Repository를 많이 파야 한다는 단점이 있습니다. 저희 프로젝트에서는 JPArepository도 따로 파놨기 때문에 총 4개 Repository가 있습니다.

 

한 도메인당! 4개! 

여기다가 나중에 elastic search(현재 구현중)을 더하면 5개가 됩니다.

 

Querydsl repository

관련해서는 다음 TIL에 정리하도록 할게요.

 

짧게 요약하자면,

저희는 JPA를 나눠서 썼기 때문에 

기본 Repository

JPA Repository

Custom Repository

Custom Repository Impl

 

4개가 필요합니다.

 

 

 

QueryDSL의 최신 버전(2021.07.22 release)인 5.0 버전을 사용했습니다. 블로그 자료들에는 outdated된 것들이 많아서 최대한 공식 문서와 github을 참조하려고 했습니다. QueryDSL의 github에는 데이터 타입 별 튜토리얼도 있습니다. 저도 언젠가는 이런 오픈소스에 기여할 수 있는 개발자가 되고 싶습니다. 제가 개발자라는 직업에 매력을 느끼는 이유기도 합니다. 

 

QueryDSL 도입 이유

현재 JPQL을 사용하고 있는데 3가지 문제점을 발견하였습니다.

  1. 문자열(String)로 처리가 되다 보니 띄어쓰기 하나에도 오류가 날 수 있음
  2. Compile 단계에서 오류 체크가 불가능함
  3. Runtime 단계에서 오류를 발견할 수 있어 비효율적임

 

포트폴리오적으로도, 새로운 기술을 써본다는 측면으로도 QueryDSL를 사용해보고 싶었지만 위에 말한 3가지 문제를 해결할 수 있어서 QueryDSL를 도입하기로 결정했습니다. QueryDSL은 아래와 같은 장점을 가지고 있습니다.

  1. QueryDSL은 모든 쿼리에 대한 내용이 함수 형태로 제공 됨
  2. 위의 이유 덕분에, complie 단계에서 오류 체크(Type-check)가 가능함
  3. 커스터마이징하기 쉽고 유연한 코드를 작성할 수 있음

QueryDSL의 단점은 코드 라인 수가 길어진다는 것이 있지만, 함수 형태이기 때문에 가독성이 좋고 IDE의 도움(코드 자동완성)을 받을 수 있다는 게 장점입니다. 2번 이유에 대해서 멘토님께 발표할 때 human error라고 했는데 무슨 말인지 못알아들으셔서 "오타요!" 라고 외치고 말았습니다.

build.gradle 설정하기

buildscript {
    ext {
        queryDslVersion = "5.0.0"
    }
}
plugins {
 	...
 	id "com.ewerk.gradle.plugins.querydsl" version "1.0.10"
 	...
 }
 ...
 dependencies {
 	// querydsl 추가
 	implementation "com.querydsl:querydsl-jpa:5.0.0"
 	implementation "com.querydsl:querydsl-apt:5.0.0"
     ...
 }
 // Qtype 생성 경로
 def querydslDir = "$buildDir/generated/querydsl"
 querydsl {
 	jpa = true
 	querydslSourcesDir = querydslDir
 }
 sourceSets {
 	main.java.srcDir querydslDir
 }
 compileQuerydsl{
 	options.annotationProcessorPath = configurations.querydsl
 }
 configurations {
 	compileOnly {
 		extendsFrom annotationProcessor
 	}
 	querydsl.extendsFrom compileClasspath
 }

최신 버전인 5.0.0 버전을 사용하기로 했습니다.  다른 블로그를 참조하실 때 조심하세요! 버전이 다 다릅니다. 저는 지마켓 기술 블로그를 참조했습니다. 

 

낮은 버전이라도 버전 숫자만 바꾸면 되겠지 하다가 엄청 해맸습니다. 기술에 대해서 완전히 이해하고 코드 쓰기로 다짐했으면서 바로 질러버리는 행동을 해서 반성합니다....

 

 

Q 클래스

QueryDSL로 쿼리를 작성할 때에는 QType을 이용해서 쿼리를 Type-safe하게 작성합니다.

사이드 바에 있는 Gradle을 클릭하면 아래처럼 윈도우가 열립니다. 

Tasks-> other -> complieQuerydsl 을 실행합니다.

 

Q클래스가 생성된 것을  확인 할 수 있습니다.

Q타입 생성

 

Configuration 설정하기

@Configuration
public class QuerydslConfig {
        @PersistenceContext
        private EntityManager entityManager;

        @Bean
        public JPAQueryFactory jpaQueryFactory() {
            return new JPAQueryFactory(entityManager);
        }

}

JPAQueryFactory를 Bean으로 등록해서 프로젝트 전역에서 QueryDSL을 작성할 수 있습니다.

 

 

 참조문서

 

 

GitHub - querydsl/querydsl: Unified Queries for Java

Unified Queries for Java. Contribute to querydsl/querydsl development by creating an account on GitHub.

github.com

 

Querydsl - Unified Queries for Java

5.0 5.0.0 (22.7.2021) This release of QueryDSL targets Java 8 minimally and comes with various improvements to make QueryDSL ready for the modern Java ecosystem. This version also removes joda-time:joda-time, com.google.guava:guava and com.google.code.find

querydsl.com

 

 

 

Spring Boot에 QueryDSL을 사용해보자

1. QueryDSL PostRepository.java Spring Data JPA가 기본적으로 제공해주는 CRUD 메서드 및 쿼리 메서드 기능을 사용하더라도, 원하는 조건의 데이터를 수집하기 위해서는 필연적으로 JPQL…

tecoble.techcourse.co.kr

 

주니어 개발자의 QueryDSL 찔러보기

안녕하세요. Fulfillment Engineering 팀의 입사한 지 1년이 얼마 지나지 않은 싱싱한(?) 주니어 개발자 백정현입니다. 최근 들어 JAVA를 기반으로 한 Spring boot + JPA 또는 Spring Data JPA를 이용한 프로젝트가

dev.gmarket.com

일요일에 오랜만에 노트북을 들지 않고 외출을 했습니다. 몸은 가벼운데 마음은 무거워져서 내일은 더 열심히 해야지 다짐해봅니다.

 

 

 

TIL 왜 또 안되니 SSE 230130

에러가 마구마구 터지는 중. 해결이 안되는데 일단 새벽 3시가 넘어서 잠.

pizzathedeveloper.tistory.com

 

TIL AsyncConfigurerSupport 으로 비동기 설정 230131

오늘은 계획된 배포일이지만 아무도 오늘 가능할 거라고 생각하지 못합니다. 일단은 @Async 의 Configuration 설정을 통해서 서버가 터지는 현상은 막았습니다. AsyncConfigurerSupport @Configuration @EnableAsync

pizzathedeveloper.tistory.com

 

TIL SSE 알림 기능 만들기 총정리 230201

SSE(Server-Sent-Event)를 사용해서 알림기능을 만들기로 결정을 하고 약 2주동안 고생하면서 결국에는 성공(?)을 시켰습니다. 완전하다고는 할 수 없지만 이제는 왠만하면 SSE를 사용하지는 않고 다른

pizzathedeveloper.tistory.com

 

 

TIL 배포하다 230202

드디어 배포를 했습니다. 🎙안녕하세요! 우연처럼, 운명처럼 하모니를 만들어가고 있는 oncounter team 입니다🎵 https://oncounter.co.kr 👆👆oncounter 방문하기👆👆 📌oncounter GUIDE 🎼Oncounter 서비스

pizzathedeveloper.tistory.com

 

TIL 모든 건 개발자 맘대로 230203

배포를 하고 유저 피드백을 받고 있습니다. 좋은 칭찬도 많고 도움이 되는 피드백도 많이 오고 있습니다. 시간을 내어 피드백을 써주시는 분들께 감사하지만 한 피드백을 보고 삐딱한(?) 맘이 들

pizzathedeveloper.tistory.com

 

'TIL' 카테고리의 다른 글

TIL JPQL을 QueryDSL로 변경하기 Repository 들 230207  (0) 2023.02.08
TIL QueryDSL 5.0.0 적용하기 230206  (0) 2023.02.07
TIL 멘토링 230204  (0) 2023.02.06
TIL 모든 건 개발자 맘대로 230203  (1) 2023.02.04
TIL 배포하다 230202  (0) 2023.02.03

오늘은 멘토님의 멘토링이 있는 날입니다. 

그동안 사용한 기술스택을 정리하고 회고하는 시간이었습니다.

'TIL' 카테고리의 다른 글

TIL QueryDSL 5.0.0 적용하기 230206  (0) 2023.02.07
TIL WIL 일주일 남음 230205  (0) 2023.02.06
TIL 모든 건 개발자 맘대로 230203  (1) 2023.02.04
TIL 배포하다 230202  (0) 2023.02.03
TIL SSE 알림 기능 만들기 총정리 230201  (0) 2023.02.02

배포를 하고 유저 피드백을 받고 있습니다. 좋은 칭찬도 많고 도움이 되는 피드백도 많이 오고 있습니다. 시간을 내어 피드백을 써주시는 분들께 감사하지만 한 피드백을 보고 삐딱한(?) 맘이 들었습니다. 아 삐딱한 이라기보다는 "개발자 빨리 할걸...."이라는 맘입니다. 

모든 건 개발자 맘대로

 

회원 가입시, abc@def.ghi로 가입이 됩니다

위와 같은 피드백이 왔을 때 든 맘은

 

"잉?" 

 

이었습니다. ghi 라는 도메인이 없다고 어떻게 장담해서 이걸 정규식을 도입을 안 했다고 말씀하시는 거지? 세상에 이메일 주소들이 무한대로 많은지언데! 

 

그래도 피드백이 들어왔으니 머리를 맡대고 조원들이랑 고민을 해봤는데 저희는 이미 프론트에서 정규식으로 한번, 백에서도 정규식 메서드로 한번 총 두번을 검증하고 있습니다. 골뱅이가 들어가고 알파벳과 숫자로만 이루어지는 이메일인지 확인을 하는 정규식 입니다. 백엔드에서는 아래와 같은 코드를 사용해서 유효성검사를 합니다.

 

public class Validator {
    public static boolean isValidEmail(String email){
        final String REGEX = "^[0-9a-zA-Z]([-_.]?[0-9a-zA-Z])*@[0-9a-zA-Z]([-_.]?[0-9a-zA-Z])*.[a-zA-Z]{2,3}$";

        return Pattern.matches(REGEX, email);
    }

 

그래서 정규식을 도입하지 않았다고 하는 피드백은 오해이고 이건 어쩔수 없는 문제라고 결론을 내리려다가 든 생각은,

 

"아, 이래서 이메일 인증을 하는구나!"

 

였습니다.

 

이메일 인증이 사용자가 입력한 이메일 값이 정확한 건지 거짓으로 입력한 건지를 알 수 있는 유일한 수단이었던 것입니다. 

 

아아, 안탑깝게도 저희의 프로젝트는 이제 1주일 밖에 남지 않았고 부트캠프도 2주 밖에 남지 않은 상태입니다. 당장 도입을 한다고 해도 물리적으로 시간이 가능할지 (백은 가능하지만 프론트는 계속해서 들어오는 피드백과 에러를 고치기에도 시간이 모자란 상태입니다.) 의문이 들었습니다. 그래서 제가 제안한 것은, 프로젝트 기한이 끝나고 부트캠프가 끝나도 계속해서 만나서 프로젝트의 완성도를 높혀나가자 였습니다. 고맙게도 열정과 욕심히 그득그득한 조원들 대부분이 찬성해주었습니다. 다들 6주라는 시간으로 인한 서비스의 완성도에 아쉬움을 가지고 있었거든요. 부트캠프 끝나고도 계속 할일이 생겼네요.

 


오늘 포스팅의 제목은 '모든 건 개발자 마음대로'인데요, 정말 개발자의 경력과 능력치에 따라서 서비스가 얼마나 완성도가 있어지는지를 새삼 더욱 더 많이~ 깨닫고 있는 요즘입니다. 이메일 인증같은 다소 사소해보이는 이 기능도 개발자가 생각해냈을테니까요. 저는 경영학과 출신이라 기획만 하고 아이디어를 생각만 하고 펀드만 끌어오지 구체적인 구현을 할 줄 몰라서 답답해 했었는데 이제는 '타이탄의 도구' 하나를 획득했습니다. 전세계 갑부들을 보면 일론 머스크, 마크 주커버그 등 대부분 본인이 프로그래밍을 직접할 줄 알아서 아이디어 구현까지 할 수 있었던 엔지니어들이 대부분입니다. 물론 워런버핏도 있지만, 워런버핏이 전세계 사람들의 삶에 영향을 끼칠만한 지대한 기여는 하지 않았으니까요. 

 

시간이 갈수록 엔지니어를 왜 진작하지 않았을까 하는 아쉬움이 밀려들지만, 앞으로 90년은 더 살아야하는 21세기 인간이기에 지금이라도 열심히 하려고 합니다. 

 

 

 

'TIL' 카테고리의 다른 글

TIL WIL 일주일 남음 230205  (0) 2023.02.06
TIL 멘토링 230204  (0) 2023.02.06
TIL 배포하다 230202  (0) 2023.02.03
TIL SSE 알림 기능 만들기 총정리 230201  (0) 2023.02.02
TIL AsyncConfigurerSupport 으로 비동기 설정 230131  (0) 2023.02.01

드디어 배포를 했습니다.

 

🎙안녕하세요!
우연처럼, 운명처럼 하모니를 만들어가고 있는 oncounter team 입니다🎵
 
👆👆oncounter 방문하기👆👆
 
 
 
 
🎼Oncounter 서비스 소개
  • 여러분의 창작물을 업로드하세요! 내가 만든 음악을 멋진 커버사진과 함께 업로드하고 다른 아티스트들에게 콜라보를 요청할 수 있습니다.
  • 다른 뮤지션들 업로드한 음악에 내가 만든 음악을 콜라보 요청을 할 수 있습니다. 콜라보가 승인이 된다면 함께 플레이 할 수 있습니다!
  • 마이페이지에서 내 자신을 알리세요! 나를 소개하고 자신의 SNS링크를 공유할 수 있을 뿐만 아니라, 내가 올린 음악과 보관한 음악을 조회할 수 있습니다. 마이페이지 편집 기능을 통해서 자신의 정보를 수정할 수 있습니다.
  • 팔로우를 통해 내가 좋아하는 뮤지션의 최신 업데이트를 받아볼 수 있습니다!
  • DM 기능을 통한 커뮤니티 형성! 내가 콜라보하고 싶은 뮤지션들에게 DM을 보내보세요!
 

🎉 설문 참여 이벤트 🎉

여러분의 피드백은 oncounter를 더욱 좋은 서비스로 발전시킵니다!
oncounter를 이용하시고 좋았던 점 또는 아쉬웠던 점에 대해서 피드백을 부탁드리겠습니다🙇‍♂️🙇‍♂️🙇‍♂️🙇‍♀️🙇‍♂️
oncounter의 발전에 큰 도움이 되는 설문조사에 참여해주신 분들께는 다음과 같이 선물🎁을 드립니다.

이벤트 참여 기간: 02.02(목) ~ 02.08(수) 자정까지

이벤트 당첨 확인 : 02월 08일 (수) 공식 인스타그램 @oncounter.official

이벤트 당첨 선물🎁 :

  🥇1등 (1명): World DJ Festival 월드디제이페스티벌 티켓🎧🎤

  🥈2등 (5명) : 스타벅스 아메리카노 기프티콘

이벤트 결과는 oncounter 공식인스타그램에 공지되며, 당첨자는 개별 연락드릴 예정입니다.

입력하신 개인정보는 상품 전달을 위해서만 사용됩니다.

 

 

 


피드백이 들어왔는데 

데이터 최적화는 어떻게 하는 걸까요?

 

ㅠㅠ

 

 

SSE(Server-Sent-Event)를 사용해서 알림기능을 만들기로 결정을 하고 약 2주동안 고생하면서 결국에는 성공(?)을 시켰습니다. 완전하다고는 할 수 없지만 이제는 왠만하면 SSE를 사용하지는 않고 다른 옵션을 시도해볼 것 같습니다. 로컬에서는 잘 돌아갔는데 프론트랑 연결하면서 팡팡팡팡팡! 에러가 하루가 멀다하고 터졌거든요. 산넘어 산이었습니다. 이번 포스팅에서는 다른 블로그들은 언급하지 않고 넘어가는 SSE 알림 기능 관련 에러 총정리를 하려고 합니다. 저처럼 헤매지 마시길.

 

SSE 알림 기능 관련 주요 TIL

230110 알림 기능 구현 뭘로 할까?

230111 SSE로 알림기능 구현하기

230120 SSE 프론트와 연결하기 (feat. 비관적락)

230124 SSE 에러: DB connection leak, open-in-view 설정

230125 SSE 또 connection leak triggered

230126 @Async 비동기처리

230126 읽지 않은 알림 갯수 반환하기

230127 @EventListener 알림 기능 강결합 제거

230131 AsyncConfigurerSupport로 비동기 설정하기

 

 

 

위 처럼 다양한 에러를 만나고 결국엔 해결했습니다. TIL에서 언급하지 않은 부분과 그래서 결국 최종 코드가 무엇인지에 대해 알려드리도록 하겠습니다. 하나 걸러 하나가 나오는 에러라 다 기록하지는 못했지만 앞으로는 더 꼼꼼히 기록해야겠다는 다짐을 하면서 우선 SSE 설정을 위한 헤더에 대해 알아봅시다.

 

NotificationController

@Tag(name = "SSE")
@ApiResponses(value = {
        @ApiResponse(responseCode = "2000", description = "SSE 연결 성공"),
        @ApiResponse(responseCode = "5000", description = "SSE 연결 실패")
})
@Operation(summary = "SSE 연결")
@GetMapping(value="/api/subscribe/{nickname}", produces = "text/event-stream")
public SseEmitter subscribe(
        @PathVariable String nickname,
        @RequestHeader(value="Last-Event-ID", required = false, defaultValue = "") String lastEventId,
        HttpServletResponse response){
    response.addHeader("X-Accel-Buffering", "no");
    response.addHeader("Content-Type", "text/event-stream");
    response.setHeader("Connection", "keep-alive");
    response.setHeader("Cache-Control", "no-cache");

    String encodedNickname = URLDecoder.decode(nickname, StandardCharsets.UTF_8);
    return notificationService.subscribe(lastEventId, encodedNickname);
}

Ngnix를 적용하고 에러가 나서,

 

response.addHeader("X-Accel-Buffering", "no");

를 적용해 주었고

 

아래와 같이 에러가 나서

EventSource's response has a Content-type specifying an unsupported type: application/json. Aborting the connection

EventSource's response has a Content-type specifying an unsupported type: application/json. Aborting the connection

response.addHeader("Content-Type", "text/event-stream");

 

를 적용해주었습니다.

 

ngnix 세팅 때문에 토큰을 헤더에 담아오지 못하게 되어서 nickname으로 파라미터를 변경해주었는데, 닉네임에 한글도 있어서 encoding관련해서 아래처럼 설정을 넣어주었습니다.

 

String encodedNickname = URLDecoder.decode(nickname, StandardCharsets.UTF_8);

 

 

NotificationService

    public SseEmitter subscribe(String lastEventId, String nickname) {
        Member member = memberRepository.findByNickname(nickname)
                .orElseThrow(()-> new NotFoundException(SSE, SERVICE, MEMBER_NOT_FOUND, "Nickname : " + nickname));
        Long memberId = member.getId();
        String emitterId = memberId + "_" + System.currentTimeMillis();
        SseEmitter emitter = emitterRepository.save(emitterId, new SseEmitter(DEFAULT_TIMEOUT));

        log.info("emitter created");

        emitter.onCompletion(() -> {
            synchronized (emitter){
            emitterRepository.deleteById(emitterId);}});
        emitter.onTimeout(() -> {
            emitter.complete();
            emitterRepository.deleteById(emitterId);});

        sendToClient(emitter, emitterId, "EventStream Created. [memberId=" + memberId + "]");

        if (!lastEventId.isEmpty()) {
            Map<String, Object> events = emitterRepository.findAllEventCacheStartWithByMemberId(String.valueOf(memberId));
            events.entrySet().stream()
                    .filter(entry -> lastEventId.compareTo(entry.getKey()) < 0)
                    .forEach(entry -> sendToClient(emitter, entry.getKey(), entry.getValue()));
        }

        return emitter;
    }

    @Async // 비동기 처리를 위한 어노테이션
    @Transactional
    public void send(Member receiver, Member sender, NotificationType notificationType, String content, RedirectionType type, Long typeId, Long postId) {
        Notification notification = notificationRepository.save(new Notification(receiver, notificationType, content, type, typeId, postId, sender));
        String memberId = String.valueOf(receiver.getId());

        Map<String, SseEmitter> sseEmitters = emitterRepository.findAllEmitterStartWithByMemberId(memberId);
        sseEmitters.forEach(
                (key, emitter) -> {
                    emitterRepository.saveEventCache(key, notification);
                    sendToClient(emitter, key, notification.getContent());
                }
        );
    }

    private void sendToClient(SseEmitter emitter, String emitterId, Object data) {
        try {
            log.warn("emitterId : " + emitterId);
            log.warn("data : " + data.toString());
            emitter.send(SseEmitter.event()
                    .id(emitterId)
                    .data(data));
            log.info(emitterId+"-emitter has been sent and completed");
        } catch (IOException exception) {
            log.error("Unable to emit");
            emitter.completeWithError(exception);
            emitterRepository.deleteById(emitterId);
        }
    }

    @Transactional
    public List<ResponseNotificationDto> getNotificationList(Member member) {
        List<Notification> notificationList = notificationRepository.findAllByReceiverOrderByCreatedAtDesc(member);
        List<ResponseNotificationDto> responseNotificationDtoList= new ArrayList<>();
        for (Notification notification: notificationList) {
            responseNotificationDtoList.add(SSE_MAPPER.NotificationtoResponseNotificationDto(notification));
        }
        return responseNotificationDtoList;
    }

    @Transactional
    public void readNotification(Long notificationid, Member member) {
        Notification notification = notificationRepository.findById(notificationid)
                .orElseThrow(()-> new NotFoundException(SSE, SERVICE, NOTIFICATION_NOT_FOUND, "Notification ID : " + notificationid));
        if(member.getId().equals(notification.getReceiver().getId())) {
            notification.read();
        }
        notificationRepository.save(notification);
    }

    @Transactional
    public ResponseCountNotificationDto countUnreadNotifications(Member member) {
        String nickname = member.getNickname();
        Long count = notificationRepository.countUnreadNotifications(nickname);

        return new ResponseCountNotificationDto(count);

    }
}

여러가지 메소드가 들어있어서 좀 깁니다. 

 

중요한 수정사항들을 몇가지 언급하자면,

 

synchornized() : 비동기를 사용했던 친구들이 완료되면 동기화 시키는 메서드
emitter.completeWithError(exception);

에미터가 발신 되지 않았을 경우 에러와 함께 종료 시키기

 

 

 

대댓글 알림 서비스 메서드 예시

제가 EventListener를 사용했다는 사실 알고 계시죠? (@EventListener 사용)  

아래와 같이 EventListener를 구현하였습니다.

@Component
@RequiredArgsConstructor
@Slf4j
public class NotificationListener {

    private final NotificationService notificationService;

    @TransactionalEventListener
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    @Async
    public void handleNotification(RequestNotificationDto requestNotificationDto){
        notificationService.send(requestNotificationDto.getReceiver(), requestNotificationDto.getSender(),requestNotificationDto.getNotificationType(),
                requestNotificationDto.getContent(), requestNotificationDto.getType(), requestNotificationDto.getTypeId(), requestNotificationDto.getPostId());
        log.info("EventListener has been operated. Sender Id: " + requestNotificationDto.getSender().getId() + " NotificationType: " +requestNotificationDto.getNotificationType());
    }

}

 

예시로는 대댓글을 작성하면 가는 알림입니다.

@Transactional
public void createComment(Long postId,Long parentId, CommentDto commentDto, String nickname) {

    Post post = postRepository.findById(postId).orElseThrow(
            () -> new NotFoundException(Domain.COMMENT, Layer.SERVICE, POST_NOT_FOUND, "Post ID : " + postId)
    );
    Comment parentComment = null;
    if (parentId != null) {
        parentComment = commentRepository.findById(parentId).orElseThrow(
                () -> new NotFoundException(Domain.COMMENT, Layer.SERVICE, COMMENT_NOT_FOUND, "Parent Comment ID : " + parentId)
        );
    }
    Member member = memberRepository.findByNickname(nickname)
            .orElseThrow(() -> new NotFoundException(COMMENT, SERVICE, MEMBER_NOT_FOUND, "Nickname : " + nickname));

    Comment comment = COMMENT_MAPPER.commentDtoToComment(commentDto, member, post, parentId);
    commentRepository.save(comment);

    //post 작성자에게 댓글 알림
    Member postMember = memberRepository.findByNickname(post.getNickname())
            .orElseThrow(() -> new NotFoundException(COMMENT, SERVICE, MEMBER_NOT_FOUND, "Nickname : " + nickname));

    if (!postMember.getNickname().equals(member.getNickname())) {
        String content = post.getTitle() + "에 " + nickname + "님이 댓글을 남겼습니다.";
        notify(postMember, member, NotificationType.COMMENT, content, RedirectionType.detail, postId, null);
    }

    //댓글 작성자에게 댓글 알림
    if (parentComment != null) {
        Member commentMember = memberRepository.findByNickname(parentComment.getNickname())
                .orElseThrow(() -> new NotFoundException(COMMENT, SERVICE, MEMBER_NOT_FOUND, "Nickname : " + comment.getNickname()));
        if (!commentMember.getNickname().equals(member.getNickname())) {
            String content = commentMember.getNickname() + "님의 댓글에 " + nickname + "님이 댓글을 남겼습니다.";
            notify(commentMember, member, NotificationType.COMMENT, content, RedirectionType.detail, postId, null);
        }
    }
}


private void notify(Member postMember, Member sender, NotificationType notificationType,
                    String content, RedirectionType type, Long typeId, Long postId){
    eventPublisher.publishEvent(new RequestNotificationDto(postMember,sender, notificationType,content,type, typeId, postId));

}

위처럼 notify 라는 메서드를 만들어서 event를 발행합니다. 해당 event가 발행되면 listener가 듣고 NotificationService의 send 메서드를 실행시켜줍니다. 이때, Eventlistener의 파라미터는 1개이어야 하기 때문에 RequestNotificationDto를 생성해서 하나의 dto에 정보를 담아서 보내주었습니다.

 

 

 

SSE 에러를 해결하면서 느낀 점

인터넷에 나오는 모든 정보가 정확한 것은 아닙니다. 에러가 있어도 언급을 하지 않는 경우가 있고, 내 컴퓨터에서는 안되는 경우가 더러 있습니다. 그리고 동료들이 있어서 함께 에러를 해결해나갈수 있었습니다. github의 다른 개발자들도 같은 고민을 한 적이 있고 각기 다른 방법으로 에러를 해결했다는 사실도 재미있습니다. 

 

에러는 로컬에서 발생하는 에러(error factor : ONLY myself), 프론트와 연결하면서 발생하는 에러(error factor: still ONLY ME)가 있습니다. ngnix 설정을 하면서 또 한번 다수의 에러가 생겼고, 강결합으로 인한 에러도 있었습니다. 이번을 계기로 http 통신에 대해서 좀 더 알게되었습니다. 

 

 

아직도 미해결 에러가 있습니다. 해협!!!

많은 시간 공들여서 찾아보았지만 아직 해결되지 않은 에러가 한가지 남아있습니다.

 

바로바로.....

 

net:: ERR_INCOMPLETE_CHUNKED_ENCODING 200

 

입니다.

 

구글링으로 나와있는 방법은 대부분 시도해보았습니다. 혹시 해결방안을 알고 계시는 분들은 댓글달아주세요...!!

 

라고 하자마자 해결방안이 나와서 에러가 이제 안납니다.

 

ngnix 설정을 추가해 줍니다.

proxy_buffering off;
proxy_cache off;
chunked_transfer_encoding off;

 

에러가 사라졌습니다.

위에 헤더로 ngnix 설정해줬다고 생각했었는데 아니었나봅니다....

 

오늘은 계획된 배포일이지만 아무도 오늘 가능할 거라고 생각하지 못합니다.

일단은 @Async 의 Configuration 설정을 통해서 서버가 터지는 현상은 막았습니다. 

AsyncConfigurerSupport 

@Configuration
@EnableAsync
public class AsyncConfig extends AsyncConfigurerSupport {

    @Override
    public Executor getAsyncExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(10);
        executor.setMaxPoolSize(30);
        executor.setQueueCapacity(50);
        executor.setThreadNamePrefix("NOTIFICATION-ASYNC");
        executor.initialize();
        return executor;
    }

    @Override
    public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
        return super.getAsyncUncaughtExceptionHandler();
    }
}

위와 같이 설정해주었는데 나중에 Chatgpt한테 물어보니 코드를 그대로 짜주더라고요. 살짝 개발자 커리어에 대한 불안감이 엄습해왔지만 당장 닥친 미래가 더 중요해서 집중을 했습니다. 무서운 세상입니다... 취업하면 MS 주식 풀매수... 

chatgpt

 

에러 넘어 에러!! ERROR!!

대댓글에는 알림이 가지 않는 현상을 발견했습니다. 생각을 깊게 못한 제 탓인가요. 기능이 하루걸러 추가되는 프로젝트였다 보니 그런거 같기도 합니다. 

에러가 마구마구 터지는 중.

해결이 안되는데

일단 새벽 3시가 넘어서 잠.

어느새 최종프로젝트도 배포일이 2일 남았습니다. MVP 기능은 예전에 끝이 났는데 프론트와 연결하고 배포하면서 일어나는 자잘한 에러들과 디자인 변경으로 인한 새로운 api들 여러개를 구현하다보니 시간이 훌쩍 지나갔습니다. SSE 파트를 맡은 저는 하루 걸러 새로 나오는 버그들에 정신이 없었던거 같습니다. SSE 관련해서 타임라인을 보면 아래와 같습니다. 관련해서 잘 정리해놓고 면접때 꼭 얘기하라는 멘토님의 말씀이 있기도 했습니다. 

 

 

 

TIL SSE 에러 230124

알림관련해서 성공한 줄 알았는데 아니었습니다. FE와 연결 시 발생한 SSE 관련 에러는 다음과 같습니다. 1. subscribe 성공 후, 알림 2~3번 수신하면 서버가 멈추는 현상 -> DB Connection Pool이 다 차서 con

pizzathedeveloper.tistory.com

 

 

TIL SSE 에러 트러블슈팅 230125

아직 해결중입니다. 버그가 쉽게 해결되지 않아서 스트레스가 밀려오지만 또 면접에서 할말 생겼다는 생각이 들어서 긍정적으로 생각하기로 했습니다. 집에서 공부하니까 집중이 잘 안되는 거

pizzathedeveloper.tistory.com

 

 

TIL @Async 비동기 동기 230126

결국 SSE 관련 버그를 처리했습니다. TIL SSE 에러 트러블슈팅 230125 아직 해결중입니다. 버그가 쉽게 해결되지 않아서 스트레스가 밀려오지만 또 면접에서 할말 생겼다는 생각이 들어서 긍정적으

pizzathedeveloper.tistory.com

 

 

TIL @EventListener 알림 기능 강한 결합 제거 230127

기존에는 NotificationService를 주입받아 적용했는데 서비스간의 의존성이 생기고 결합도가 높아서 이슈가 생길것이 염려되었습니다. 결국 관련해서 에러가 터지기도 했습니다. 의존성을 제거하기

pizzathedeveloper.tistory.com

 

 

TIL 에러 넘어 에러 (feat. 객체직렬화) 230128

에러 EventSource's response has a Content-Type specifying an unsupported type; 이라는 에러가 발생했습니다. 프론트로부터 위와 같은 에러를 전달 받았습니다. 이벤트 데이터를 저장할 때 원본 객체를 문자열로

pizzathedeveloper.tistory.com

 

 

덕분에 비동기, 동기 관련 개념을 알게 되었고 @EventListener라는 어노테이션을 활용할 수 있게 되었으며 Gson이라는 구글의 라이브러라리를 알게 되었습니다. 

최종프로젝트 로고

팀원들과 소통하며 각자의 파트에서 배운 것들을 공유했습니다. 또 facade 패턴을 도입하는 것도 고려 했었는데 멘토님이 SAGA 패턴이라는 것을 알려주셔서 패턴에 대해서도 공부를 했습니다. 하루하루 배울 것이 천지이고 알게 될수록 개발은 흥미롭기만 합니다.

 

(영어 2배속으로 들었는데 한번 더 들어야 이해가 될 듯 합니다. 영어는 영어이지만 외계어인듯.... Speak English Ma'am!!)

 

영어를 아는 것과 프로그래밍 언어를 아는 것은 다르고 컴퓨터 용어를 아는 것도 다릅니다... 배울게 넘  많아요!!

 

이제 2주 남짓한 시간이 남았는데 최종 프로젝트 끝나면 알고리즘 공부를 빡세게 해야겠습니다.... 마지막까지 화이팅🙏🙏🙏🙏

 

 

 

 

 

에러

EventSource 에러

EventSource's response has a Content-Type specifying an unsupported type;

 

이라는 에러가 발생했습니다. 프론트로부터 위와 같은 에러를 전달 받았습니다.

이벤트 데이터를 저장할 때 원본 객체를 문자열로 변환하지 않고 그대로 저장할 경우 EventSteam 연결이 즉시 종료되는 현상이 확인되었습니다. 그래서 여기저기 물어보고 구글링 해서 json 객체를 문자열로 변환하는 방법을 찾았습니다.

 

json 마샬링 언마샬링 하기에 대해서 아래글을 참조하세요.

 

JSON 마샬링/언마샬링 하기 Part1

JOSN 마샬링/언마샬링에 대해서 말씀 드리려고 합니다. 설명에 앞서 용어에 대한 정리를 간략하게 해보겠습니다. JSON (JavaScript Object Notation) JSON에 대한 정의는 아래와 같습니다. JSON (JavaScript Object

beyondj2ee.tumblr.com

 

 

객체직렬화 하기

@Async // 비동기 처리를 위한 어노테이션
@Transactional
public void send(Member receiver, Member sender, NotificationType notificationType, String content, RedirectionType type, Long typeId, Long postId) {
    Notification notification = notificationRepository.save(new Notification(receiver, notificationType, content, type, typeId, postId, sender));
    String memberId = String.valueOf(receiver.getId());

    Map<String, SseEmitter> sseEmitters = emitterRepository.findAllEmitterStartWithByMemberId(memberId);
    sseEmitters.forEach(
            (key, emitter) -> {
                emitterRepository.saveEventCache(key, notification);
                Gson gson = new Gson();
                String data = gson.toJson(notification);
                sendToClient(emitter, key, data);
            }
    );
}

 

Gson을 사용해서 객체직렬화를 하였습니다.

 

Gson gson = new Gson();
String data = gson.toJson(notification);

아직 프론트랑 연결은 안해봤는데 일단 string 형태로 보내니까 json 형태라는 에러는 안뜰거라고 예상합니다.

 

 

 

참고 자료

 

Google의 Gson 라이브러리를 사용한 Java 객체 직렬화

이 게시물은 Java 객체의 직렬화 및 역직렬화 Google의 Gson 라이브러리를 사용합니다. 오늘날 거의 모든 RESTful 웹 서비스는 XML 대신 JSON 데이터를 사용하고 생성합니다. 불행히도 Java SE는 JSON을 Java O

www.techiedelight.com

 

기존에는 NotificationService를 주입받아 적용했는데 서비스간의 의존성이 생기고 결합도가 높아서 이슈가 생길것이 염려되었습니다. 결국 관련해서 에러가 터지기도 했습니다. 의존성을 제거하기 위해 여러가지 방법을 찾아보았는데 EventListener가 있다는 사실을 알게 되었습니다. EventListener는 말 그대로 이벤트를 리스닝(?)하고 있다가 이벤트가 발생하면 처리하는 로직입니다. 

 

서비스간의 강한 결합, 강한 의존성을 낮출수 있는 방법입니다. 

eventlistener 알림

@TransactionalEventListener

@TransactionEventListener를 사용하면, 트랜잭션 흐름에 따라 이벤트를 제어할 수 있습니다.

@TransactionalEventListener는 4가지 옵션이 있습니다.

  • AFTER_COMMIT(default): 트랜잭션이 성공적으로 마무리(commit)이 되었을 때 이벤트를 실행합니다.
  • AFTER_ROLLBACK: 트랜잭션이 rollback이 되었을 때 이벤트를 실행합니다.
  • AFTER_COMPLETION: 트랜잭션이 마무리 되었을 때(commit or rollback) 이벤트를 실행합니다.
  • BEFORE_COMMIT: 트랜잭션의 커밋 전에 이벤트를 실행합니다.

 

문제점,

  • 이벤트 전달 시점을 트랜잭션 커밋 시점으로 설정한 경우 트랜잭션이 끝나 DB에 데이터를 저장하는 것이 불가능합니다.
    • 이때 @Transactional(propagation = Propagation.REQUIRES_NEW)으로 새로운 트랜잭션을 생성시켜 데이터를 저장하는 방식으로 문제를 해결합니다.

 

 

NotificationListener 구현 (1)

@Component
@RequiredArgsConstructor
@Slf4j
public class NotificationListener {

    private final NotificationService notificationService;

    @TransactionalEventListener
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    @Async
    public void handleNotification(RequestNotificationDto requestNotificationDto){
        notificationService.send(
                requestNotificationDto.getReceiver(), requestNotificationDto.getSender(), requestNotificationDto.getNotificationType(),
                requestNotificationDto.getContent(), requestNotificationDto.getType(), requestNotificationDto.getTypeId(),
                requestNotificationDto.getPostId());
    }

}

RequsetDto를 따로 만들어서 Event가 발생하면 받아오게 하였습니다.

 

BeanInitializationException

위의 핸들러를 실행시키자 다음과 같이 BeanInitializationException이 발생했습니다. 

BeanInitializationException

org.springframework.beans.factory.BeanInitializationException: Failed to process @EventListener annotation on bean with name 'notificationService'; nested exception is java.lang.IllegalStateException: Maximum one parameter is allowed for event listener method: public void com.bluehair.hanghaefinalproject.sse.service.NotificationService.send(com.bluehair.hanghaefinalproject.member.entity.Member,com.bluehair.hanghaefinalproject.member.entity.Member,com.bluehair.hanghaefinalproject.sse.entity.NotificationType,java.lang.String,com.bluehair.hanghaefinalproject.sse.entity.RedirectionType,java.lang.Long,java.lang.Long) at org.springframework.context.event.EventListenerMethodProcessor.afterSingletonsInstantiated(EventListenerMethodProcessor.java:157) ~[spring-context-5.3.24.jar:5.3.24] at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:974) ~[spring-beans-5.3.24.jar:5.3.24] at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918) ~[spring-context-5.3.24.jar:5.3.24] at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583) ~[spring-context-5.3.24.jar:5.3.24] at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:147) ~[spring-boot-2.7.6.jar:2.7.6] at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:731) ~[spring-boot-2.7.6.jar:2.7.6] at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:408) ~[spring-boot-2.7.6.jar:2.7.6] at org.springframework.boot.SpringApplication.run(SpringApplication.java:307) ~[spring-boot-2.7.6.jar:2.7.6] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1303) ~[spring-boot-2.7.6.jar:2.7.6] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1292) ~[spring-boot-2.7.6.jar:2.7.6] at com.bluehair.hanghaefinalproject.HanghaeFinalProjectApplication.main(HanghaeFinalProjectApplication.java:13) ~[main/:na] at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na] at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) ~[na:na] at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na] at java.base/java.lang.reflect.Method.invoke(Method.java:568) ~[na:na] at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) ~[spring-boot-devtools-2.7.6.jar:2.7.6] Caused by: java.lang.IllegalStateException: Maximum one parameter is allowed for event listener method: public void com.bluehair.hanghaefinalproject.sse.service.NotificationService.send(com.bluehair.hanghaefinalproject.member.entity.Member,com.bluehair.hanghaefinalproject.member.entity.Member,com.bluehair.hanghaefinalproject.sse.entity.NotificationType,java.lang.String,com.bluehair.hanghaefinalproject.sse.entity.RedirectionType,java.lang.Long,java.lang.Long) at org.springframework.context.event.ApplicationListenerMethodAdapter.resolveDeclaredEventTypes(ApplicationListenerMethodAdapter.java:127) ~[spring-context-5.3.24.jar:5.3.24] at org.springframework.context.event.ApplicationListenerMethodAdapter.<init>(ApplicationListenerMethodAdapter.java:117) ~[spring-context-5.3.24.jar:5.3.24] at org.springframework.transaction.event.TransactionalApplicationListenerMethodAdapter.<init>(TransactionalApplicationListenerMethodAdapter.java:65) ~[spring-tx-5.3.24.jar:5.3.24] at org.springframework.transaction.event.TransactionalEventListenerFactory.createApplicationListener(TransactionalEventListenerFactory.java:56) ~[spring-tx-5.3.24.jar:5.3.24] at org.springframework.context.event.EventListenerMethodProcessor.processBean(EventListenerMethodProcessor.java:200) ~[spring-context-5.3.24.jar:5.3.24] at org.springframework.context.event.EventListenerMethodProcessor.afterSingletonsInstantiated(EventListenerMethodProcessor.java:154) ~[spring-context-5.3.24.jar:5.3.24] ... 15 common frames omitted
Maximum one parameter is allowed for event listener method

EventListener는 최대 하나의 parameter만을 가진다고 합니다. 보니까 Singleton으로 설정되어 있어서 그렇습니다. 다시 코드를 변경해서 parameter 하나만 보내주는 것으로 notificationService.send를 변경했습니다.

 

NotificationListener 구현 (2)

아아... 그게 아니었습니다. 

@EventListener 어노테이션을 2군데 붙여놔서 그런거였습니다.

 

HandleNotification 메서드에만 붙어놔야 했는데 NotificationService안에도 붙여놔서 send 메서드에는 파라미터가 여러개여서 문제가 생긴거였습니다. send 메서드 위에 있던 @EventListener 어노테이션을 제거하니 제대로 작동이 되었습니다.

 

Service 구현

여러가지 서비스가 알림서비스를 이용하고 있었는데 게시글 좋아요 메서드 관련한 코드입니다. 

 

이렇게 고쳤습니다.

우선 ApplicationEventPublisher를 가져옵니다.

private final ApplicationEventPublisher eventPublisher;
@Transactional
public PostLikeDto postLike(Long postId, Member member){
    Post postliked = postRepository.findById(postId)
            .orElseThrow(()-> new NotFoundException(LIKE, SERVICE, POST_NOT_FOUND, "Post ID : " + postId)
            );
    PostLikeCompositeKey postLikeCompositeKey
            = new PostLikeCompositeKey(member.getId(), postliked.getId());
    boolean likecheck;
    Optional<PostLike> postLike= postLikeRepository.findByPostLikedIdAndMemberId(postliked.getId(), member.getId());

    if(postLike.isPresent()){
        postLikeRepository.deleteById(postLikeCompositeKey);
        postliked.unLike();
        postRepository.save(postliked);
        likecheck = false;

        return new PostLikeDto(likecheck, postliked.getLikeCount());
    }

    postLikeRepository.save(new PostLike(postLikeCompositeKey, member, postliked));
    likecheck=true;
    postliked.like();
    postRepository.save(postliked);


    Member postMember = memberRepository.findByNickname(postliked.getNickname())
            .orElseThrow(() -> new NotFoundException(COMMENT, SERVICE, MEMBER_NOT_FOUND, "Nickname : " + postliked.getNickname()));
    if(!postMember.getNickname().equals(member.getNickname())) {
        String content = postliked.getTitle() + "을(를) " + member.getNickname() + "님이 좋아합니다.";
        notify(postMember, member, NotificationType.POST_LIKED, content, RedirectionType.detail, postId, null);
    }

    return new PostLikeDto(likecheck, postliked.getLikeCount());

}

 

notify 메서드를 만들어서 event를 publish 합니다.

 

private void notify(Member postMember, Member sender, NotificationType notificationType,
                        String content, RedirectionType type, Long typeId, Long postId){
    eventPublisher.publishEvent(new RequestNotificationDto(postMember,sender, notificationType,content,type, typeId, postId));

}

 

 

 

이제 notify가 실행되면서 Eventlistener가 NotificationService의 send 메서드를 실행해줍니다.

 

@Component
@RequiredArgsConstructor
@Slf4j
public class NotificationListener {

    private final NotificationService notificationService;

    @TransactionalEventListener
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    @Async
    public void handleNotification(RequestNotificationDto requestNotificationDto){
        notificationService.send(requestNotificationDto.getReceiver(), requestNotificationDto.getSender(),requestNotificationDto.getNotificationType(),
                requestNotificationDto.getContent(), requestNotificationDto.getType(), requestNotificationDto.getTypeId(), requestNotificationDto.getPostId());
        log.info("EventListener has been operated. Sender Id: " + requestNotificationDto.getSender().getId() + "NotificationType: " +requestNotificationDto.getNotificationType());
    }

}

 

참고 문서

 

알림 기능을 구현해보자 - SSE(Server-Sent-Events)!

시작하기에 앞서 이번에 개발을 진행하면서 알림에 대한 요구사항을 만족시켜야하는 상황이 발생했다. 여기서 말하는 알림이 무엇인지 자세하게 살펴보자. A라는 사람이 스터디를 생성했고 B라

gilssang97.tistory.com

 

결국 SSE 관련 버그를 처리했습니다. 

 

TIL SSE 에러 트러블슈팅 230125

아직 해결중입니다. 버그가 쉽게 해결되지 않아서 스트레스가 밀려오지만 또 면접에서 할말 생겼다는 생각이 들어서 긍정적으로 생각하기로 했습니다. 집에서 공부하니까 집중이 잘 안되는 거

pizzathedeveloper.tistory.com

 

서치를 하다가 비동기, 동기 개념에 대해 더 깊게 파고들었고 결국 해결했습니다. 허탈하긴하지만 @Async 어노테이션 하나로 해결했습니다. 컨벤션 관련 리팩토링 중이여서 푸시를 못하고 있다가 오후에 시도했는데 @Async 하나 붙였더니 connection leak 이 발생하거나 connection pool이 다 차는 에러가 발생하지 않고 정상적으로 잘 작동이 되었습니다. 알림전송도 잘 됩니다.

@Async

비동기처리를 위해서 @Async 를 사용합니다. 사용하기 위해서는 @Enableasync를 함께 추가해줘야합니다. 

 

@Async // 비동기 처리를 위한 어노테이션
@Transactional
public void send(Member receiver, Member sender, NotificationType notificationType, String content, RedirectionType type, Long typeId, Long postId) {
    Notification notification = notificationRepository.save(new Notification(receiver, notificationType, content, type, typeId, postId, sender));
    String memberId = String.valueOf(receiver.getId());

    Map<String, SseEmitter> sseEmitters = emitterRepository.findAllEmitterStartWithByMemberId(memberId);
    sseEmitters.forEach(
            (key, emitter) -> {
                emitterRepository.saveEventCache(key, notification);
                sendToClient(emitter, key, SSE_MAPPER.NotificationtoResponseNotificationDto(notification));
            }
    );
}

 

아래와 같이 프로젝트Application에 @EnableAsync를 달아줍니다.

 

@EnableJpaAuditing
@EnableAsync
@SpringBootApplication
public class HanghaeFinalProjectApplication {
    public static void main(String[] args) {
        SpringApplication.run(HanghaeFinalProjectApplication.class, args);
    }
}

@Async를 사용하기 위해서는 public이어야 하며 self-invocation이면 안됩니다. 

 

우선은 @Async를 사용하면 기본 설정으로 SimpleAsyncTaskExecutor를 사용하게 됩니다. 커스터마이징 하고 싶으면 Async 설정 클래스를 만들어서 설정할 수 있는데 이는 내일 구현할 예정입니다. 일단 급한 불을 끄는데 집중했습니다. 

참조 블로그 (https://steady-coding.tistory.com/611)

 

 

동기 vs. 비동기

  • 동기(synchronous): 요청과 결과가 동시에 일어난다는 약속; 요청한 자리에서 결과가 주어져야 함
    • A작업이 모두 진행 될 때까지 B작업은 대기해야함
  • 비동기(Asynchronous): 요청과 결과가 동시에 일어나지 않을거라는 약속; 
    • A작업이 시작하면 동시에 B작업이 실행되며, A작업은 결과값이 나오는 대로 출력된다

 

SSE 관련 더 공부할 부분

ApplicationEventPublisher를 사용해서 서비스간의 의존성을 낮추는 방법에 대해 알게 되었습니다. EventListner를 사용해서 하는 방법이 있다고 해서 연구해보려고 합니다. 무래도 다른 Service안에 알림 Service를 넣다보니 의존성 문제가 있다고 여겨져서 방법을 찾고 있었기 때문입니다.

 

 

 

추가로 공부한 것

 

[Spring JPA] Entity, DTO

Entity, DTO 개념, DTO 사용법

velog.io

각자 파트를 맡아서 개발을 하다보니 컨벤션 준수가 되지 않거나 일관성이 없는 부분들에 대해서 팀장님이 리팩토링을 진행중입니다. DTO 관련해서 링크를 첨부해주셔서 읽어보려고 합니다. Mapper 사용할때 Dto를 참조하는 안티패턴이 발견되었습니다. 여러사람이 같은 프로젝트를 하니까 좋은점은 check-balance가 된다는 점입니다. 내공있는 조원들의 경험을 레버리지 할 수 있어서 좋습니다.

 

 

 

+ Recent posts