Java Stack/Spring
[Spring] EventListener
[Spring] EventListener
2022.04.17https://www.baeldung.com/spring-events ``kt ApplicationEventPublisher::publishEvent``로 pub 하고 ``kt @EventListener``로 event 받아 처리하면 된다. 단 여기서 주의해야 할점! listener가 이벤트를 수신하는 것이 왠지 비동기로 이루어질 것 같지만, 기본적으로 동기식이다! 한 스레드가 pub한 다음, 해당 이벤트를 처리해야 하는 EventListener들을 돌면서 동기식으로 직접 리스너를 수행한다. By default, the listener is invoked synchronously. However, we can easily make it asynchronous by adding an @Async annot..
테스트 클래스를 일정 수 이상 묶어서 실행하면, 어느 정도 실행하다가 갑자기 JDCB Connection을 무한히 대기하는 현상
테스트 클래스를 일정 수 이상 묶어서 실행하면, 어느 정도 실행하다가 갑자기 JDCB Connection을 무한히 대기하는 현상
2022.01.12``` oracle.net.ns.NetException: Listener refused the connection with the following error: ORA-12519, TNS:no appropriate service handler found. ``` ```sql select * from v$resource_limit where resource_name = 'processes'; ``` 프로세스 수가 최대치에 육박하여 더 이상 요청을 처리할 수 없어 발생하는 문제. 커넥션 풀 close를 제대로 하지 않는 것으로 보임. HikariDataSource(==ConnectionPool)의 maximumPoolSize 기본값은 10인데, 이를 3으로 변경하고 수행하니 커넥션에 여유가 생겨 pendin..
[Spring] DB 관련 : Mybatis CustomTypeHandler
[Spring] DB 관련 : Mybatis CustomTypeHandler
2021.03.31jdbcType : nullable column에 null이 들어갈 때? MyBatis는 nullable 컬럼의 parameter로 null이 넘어왔을 때, jdbcType이 명시되어 있지 않으면 TypeException을 던진다. setNull로 해당 타입에 맞는 null값(VARCHAR인 경우 "")을 넣어줘야 하는데, 뭘 넣어줄지 모르니까 예외가 발생하는 것 The JDBC Type is required by JDBC for all nullable columns, if null is passed as a value. You can investigate this yourself by reading the JavaDocs for the PreparedStatement.setNull() method. [..
[Spring] WebClient
[Spring] WebClient
2021.03.14왜 WebClient ?: RestTemplate은 deprecated 예정. docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/client/RestTemplate.html NOTE: As of 5.0 this class is in maintenance mode, with only minor requests for changes and bugs to be accepted going forward. Please, consider using the org.springframework.web.reactive.client.WebClient which has a more modern API and supports syn..
Java Servlet 이란
Java Servlet 이란
2020.06.15Servlet이 무엇인가? A servlet is a Java programming language class used to extend the capabilities of servers that host applications accessed by means of a request-response programming model. Although servlets can respond to any type of request, they are commonly used to extend the applications hosted by web servers. For such applications, Java Servlet technology defines HTTP-specific servlet classes..
RestTemplate은 어떻게 response Object를 DataType <T>로 변환하는가
RestTemplate은 어떻게 response Object를 DataType <T>로 변환하는가
2020.03.27원래 그 데이터 타입으로는 캐스팅 가능 ```kt @Test fun `테스트`() { val obj: Object = Coffee("id", "name") as Object val coffee: Coffee = obj as Coffee // 이건 원래 그 데이터가 Coffee라서 가능 val obj2: Object = Object() val coffee2: Coffee = obj2 as Coffee // 이건 불가능. id 속성이랑 name 속성이 없으니까 당연 불가능하다 } ``` RestTemplate의 response Object -> T type casting 과정 RestTemplate도 내부적으로는 `` (T)`` 캐스팅을 이용하게 되는데 ``kt extractData()``에서 비검사 형변환..
[Spring] profile로 beta, real 빌드 구분하기
[Spring] profile로 beta, real 빌드 구분하기
2020.03.03[Spring] 외부 상수 넣어주기 https://docs.spring.io/spring-boot/docs/current/reference/html/features.html#features.external-config Spring profile VS Gradle(Maven) profile Spring profile? :: @Profile() Spring profile은 빌드 결과물 내에서 스프링이 어떤 profile을 사용할지 선택하는 것 런타임 프로파일 설정 Gradle(Maven) profile? 빌드 시 어떤 profile을 리소스에 포함 시킬지 선택하는 것 빌드타임 프로파일 설정 ≒ Maven profile 즉, Gradle profile이 더 큰 파일 개념이고, 선택한 파일(e.g., 리소스 파..
[Spring] Controller에서 사용하는 애너테이션
[Spring] Controller에서 사용하는 애너테이션
2019.09.25애너테이션은 웬만하면 붙여주는 편이 명확하다. @ModelAttribute를 명시하지 않아도 자동으로 URL 파라미터를 객체로 매핑해주고, GET은 관례적으로 URL 파라미터로 데이터를 전달하기 때문에, 굳이 @ModelAttribute를 사용해야 하느냐 라고 생각할 수 있는데 Controller에서 GetMapping 메서드는 사실 (URL 파라미터/HTTP 바디) 양 쪽 모두 데이터를 받을 수 있다. https://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Summary_table 보면, GET의 경우 Request has Body가 No가 아니라 Optional로 되어 있다. POST, PUT도 마찬가지. 관례적으로 Body를 통해서 받지만 URL 파라..
Spring AOP / @annotation resolve
Spring AOP / @annotation resolve
2019.07.30AOP란? Aspect Oriented Programming. 즉 관점 지향 프로그래밍이다. 이게 무슨 뜻인지 체감하려면 실사례를 생각해보는게 가장 좋다. 전문 통신 하는 상황을 생각해 보자. 전문 객체는 전문 필드 형식에 맞게 바이너리로 serialization 되어야 한다. ```java class telegramA { String field1; byte[] serialize() { field1이 길이가 4인지 체크하고 serialize } } class telegramB { String field1; byte[] serialize() { field1이 길이가 8인지 체크하고 serialize } } ``` telegramA의 field1은 length가 4여야 하고, telegramB의 field1..
Hystrix with Spring ( Circuit Breaker ) 를 사용할 때 주의해야 할 점.
Hystrix with Spring ( Circuit Breaker ) 를 사용할 때 주의해야 할 점.
2019.07.30기본 Hystrix 라이브러리는 직접 HystrixCommand를 상속받아 정의해서 써야 하므로 약간 번거롭다.Spring을 쓰고 있다면 @HystrixCommand 애너테이션으로 이런 번거로운 설정을 대신하는 라이브러리를 도입할 수 있다.아래 dependency를 추가하도록 하자. ```xml org.springframework.cloud spring-cloud-starter-netflix-hystrix 2.1.2.RELEASE````` spring-cloud-starter-hystrix``는 deprecated 되었기 때문에 `` spring-cloud-starter-netflix-hystrix``를 사용해야 한다는 점과, SpringBoot 2.x 대를 사용하고 있다면 hystrix 버전도 2.x...
[Spring] context.getBean() 사용하기
[Spring] context.getBean() 사용하기
2019.07.29Service 구현체가 많은 경우, 각각을 다 DI 하면 확장성이 떨어져 이를 Map에 담고 Interface로 꺼내 호출하는 방식을 자주 사용하게 된다. 다음과 같은 방법을 사용하면 Service Bean을 직접 불러올 수 있어 굳이 Map에 담지 않아도 사용 가능하다. 새로운 Service가 추가될 때 컨트롤러 쪽을 전혀 수정하지 않아도 되는 방식. (Map에 담는 방식은 생성자에서 map.put 해서 담아주어야 하니까...) ```java private final ApplicationContext context; // + DI ``` ```java PointChangeService pointChangeService = context.getBean(partnerCode + "PointChangeSer..
[Spring] DB 관련 : H2 설정
[Spring] DB 관련 : H2 설정
2019.07.23build.gradle에 다음을 추가. ``` runtimeOnly 'com.h2database:h2' // runtimeOnly 'com.h2database:h2:1.4.193' ``` 반드시 버전을 명시해줄 것. 버전 명시하지 않을 시 최신 버전이 설치되는데, 콘솔에서 connect해보면 자동으로 db 파일을 생성할 수 없어서 에러가 발생한다. :mem:을 써서 인메모리에 하든, :file:을 써서 파일로 저장하든 관계없이 에러가 발생함. 이는 어떤 취약점 때문에 자동으로 파일 생성하는걸 막아둬서 그렇다고 함. IFEXISTS 플래그를 주면 된다고 나와있는데 줘도 안됨. Database "mem:testdb" not found, and IFEXISTS=true, so we cant auto-creat..