전체 글156 CompletableFuture 비동기처리 CompletableFuture로 간단하게 해결*java 8버전부터 가능 ▷ CompletableFuture 란? - 비동기 처리 : CompletableFuture는 작업을 백그라운드 스레드에서 실행하고, 결과를 비동기적으로 반환하는데 사용 - 동기화: 작업이 완료될 때까지 기다린후, 그 결과를 처리 ▷ 주요 메소드 메소드설명runAsync()Runnable을 실행하는 비동기 작업을 생성thenApply()이전 작업의 결과를 입력받아 후속 작업을 실행allOf()여러 CompletableFuture가 모두 완료될 때까지 기다리는 메소드anyOf()여러 CompletableFuture 중 하나라도 완료되면 처리할 메소드 ▷ Example public void asyncExample() .. 2021. 6. 23. @EventListener Example ▷ Example- Application Context 초기화 및 수정 시 파일을 읽어 오는 예제 @Componentpublic class TestGetName { @Getter @Setter private String USER_NAME = ""; // 애플리케이션 시작을 감지 // @EventListener : 이벤트가 발생했을때 이벤트를 수신해서 처리 // ContextRefreshedEvent : Application Context를 초기화 했거나 수정했을때 발생 @EventListener public void onApplicationEvent(ContextRefreshedEvent event) { try{ // 파일 .. 2021. 5. 18. 파일 생성하기 파일의 내용을 채워 원하는 경로에 파일 생성*원하는 경로에 폴더가 존재해야 한다.*폴더 유/무에 따라 폴더를 생성하는 로직은 반영 안함 ▷ Example- 파일의 내용은 json 형태이고, 한 줄만 있다고 가정 public void testMakeFile(){ String filePath = "C:/testFolder/"; // 파일 경로 String fileName = "TestTxt.txt"; // 파일이름 // 파일 내용 String contents = "{\"no\":\"01\", \"name\":\"홍길동\", \"age\":\"19\"}"; try { // 파일 생성 File file = new File(filePath + fileName); // 파일안에 문자열 쓰기 File.. 2021. 5. 13. 파일 검색 후 파일 읽기 원하는 경로의 파일명을 검색하여 값을 추출*미리 파일이 생성돼 있어야 한다. ▷ Example - 파일의 내용은 json 형태이고, 한 줄만 있다고 가정 *개선 2024-11-26 public void testReadFile() { try { // 파일 찾기 (파일명 "TestTxt.txt"로 시작하는 파일) File[] files = new File("C:/testFolder/").listFiles((file, name) -> name.startsWith("TestTxt.txt")); if (files != null && files.length > 0) { // 파일 읽기 (한 줄만 읽음) BufferedReader br = new BufferedReader(new FileReader(.. 2021. 5. 13. 날짜 비교 ▷ Example public void testDateCompare() { // 테스트 데이터 생성 String strTestDate1 = "20210504153300"; String strTestDate2 = "20210505153300"; String strTestDate3 = "20210505153300"; // 날짜 포맷터 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss"); // 형 변환 (String -> LocalDateTime) LocalDateTime testDate1 = LocalDateTime.parse(strTestDate1, formatter); LocalDateTime testDate2 = Loc.. 2021. 5. 4. JSON 응답 필드 Null 값 제외(@JsonInclude) JSON 응답 필드가 Null 값인 경우 응답 필드에서 제외 시키는 방법 ▷ 해결방법 @JsonInclude(JsonInclude.Include.NON_NULL) 어노테이션 사용(필드 별 설정 가능) ▷ Example public class Test { public static void main(String[] args) { try { // 테스트 데이터 String testData = "{\"STUDENTNO\": \"00001\", \"NAME\": \"홍길동\", \"HOBBY\": \"\"}"; // JSON을 객체로 변환 ObjectMapper mapper = new ObjectMappe.. 2021. 4. 30. 해당 월 말일 산출 간단한 함수를 통해 말일을 산출 ▷ Example *개선 2024-11-26 import java.time.LocalDate;import java.time.YearMonth;public class DateUtils { /** 해당 월 말일 산출 */ public static int getLastDay(int year, int month) { return YearMonth.of(year, month).lengthOfMonth(); } public static void main(String[] args) { // 오늘 날짜 LocalDate today = LocalDate.now(); int year = today.getYea.. 2021. 4. 23. 문자열 중간 마스킹 처리 ▷ Example- 영어 이름, 이름 길이 상관 없이 사용자 이름 중간 마스킹 처리 *개선 2024-11-26public String midMasking(String userName) { // 사용자 이름 길이가 2자 이하인 경우 마스킹을 하지 않음 if (userName == null || userName.length() *개선 전/** 사용자 이름 마스킹 처리 */public String midMasking(String userName) { // 사용자 이름 첫글자 String frsName = userName.substring(0,1); // 사용자 이름 중간글자 String midName = userName.substring(1, userName.length()-1); // 사.. 2021. 4. 22. 문자열 n번째 이후 마스킹 처리 ▷ Example- 사용자 아이디 앞 2글자 뒤로 마스킹 처리 public String idMasking(String userId) { // 아이디가 2자 이상일 경우만 마스킹 처리 if (userId == null || userId.length() 2021. 4. 22. 이전 1 ··· 13 14 15 16 17 18 다음 728x90 728x90