์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | |
7 | 8 | 9 | 10 | 11 | 12 | 13 |
14 | 15 | 16 | 17 | 18 | 19 | 20 |
21 | 22 | 23 | 24 | 25 | 26 | 27 |
28 | 29 | 30 |
- ์๋ฐ
- db์ํคํ ์ณ
- ํฐ์คํ ๋ฆฌ์ฑ๋ฆฐ์ง
- ์ฟ ๋ฒ๋คํฐ์ค
- ์๊ณ ๋ฆฌ์ฆ
- spring scheduler
- ํฌ๋ก์ค์ฌ์ดํธ ์คํฌ๋ฆฝํธ
- Personal Access Token
- Jenkins
- ๋ ผ๋ธ๋กํน
- ์ค๋ธ์
- docker
- MySQL
- GoogleLogin
- CICD
- java์ฑํ
- React
- ์ฝํ
- Java
- db์ ๋ต
- ๋์์ธํจํด
- Node.js
- spring
- @scheduler
- oauth
- ์ฝ๋ฉํ ์คํธ
- SpringBoot
- injection๊ณต๊ฒฉ
- dockerhub
- node.js mysql ์ฐ๋
- Today
- Total
<Hello Hosung๐/>
[SpringBoot]Scheduling ๋ณธ๋ฌธ
[SpringBoot]Scheduling
์ข์ถฉ์ฐ๋ ๋ฐฑ์๋ ๊ฐ๋ฐ์ ์ผ๊ธฐ๐ง 2024. 8. 16. 09:45
Spring Boot ์์ ์ค์ผ์ค ๋ง๋ค๊ธฐ
๋ค์ ์์ ๋ ์
๋นํธ open api ์ Spring Boot์ ์ค์ผ์ฅด๋ฌ ๊ธฐ๋ฅ์ ํตํ์ฌ ๋งค ์๊ฐ ๋ง๋ค ์
๋นํธ์ ๋ค์ํ ์ ๋ณด๋ฅผ Mysql ์ ์ ์ฌํ๋ ๋ฐฉ๋ฒ์ ์์ ํ๋ค.
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import com.example.demo.vo.UserInfo;
@EnableScheduling
@SpringBootApplication
public class StartApplication {
public static void main(String[] args) {
SpringApplication.run(StartApplication.class, args);
}
}
๋จผ์ spring boot ๊ฐ ์์๋๋ ์ง์ ์ "@EnableScheduling" ์ ์์ฑํด์ค๋ค. ๊ทธ ์ดํ ์ ์ ์์ฑํ ์๋ฐ ํ์ผ๊ณผ ๊ฐ์ ํจํค์ง์ ์ค์ผ์ค class๋ฅผ ์๋์ ๊ฐ์ด ์์ฑํด์ค๋ค.
package com.example.demo;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClientBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import com.common.Common;
/**
* ๋ฐฐ์น ๋ชฉ๋ก์
๋๋ค.
* @author kimhosung
*
*/
@Component
public class BatchScheduler {
Logger logger = LoggerFactory.getLogger(this.getClass());
// ์
๋นํธ์ ์๋ ์ข
๋ชฉ์ ๋ณด๋ฅผ ๊ฐ์ ธ์ต๋๋ค.
@Scheduled(fixedRate = 10000)
public void scheduleFixedRateTask() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
Date now = new Date();
String strDate = sdf.format(now);
System.out.println("์์
๋ ์ง ์๊ฐ fixedRate::" + strDate);
Common.openapi("http://localhost:8081/batch/list");
}
}
Common.openapi ๋ฉ์๋๋ ์๋์ ๊ฐ์ด ์์ฑํ์๋ค.
package com.common;
import java.io.IOException;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClientBuilder;
/**
* ๊ณตํต ๋ฉ์๋
* @author kimhosung
*
*/
public class Common {
//๋จ์ url call
public static void openapi(String url) {
try {
HttpClient client = HttpClientBuilder.create().build();
HttpGet request = new HttpGet(url);
request.setHeader("Content-Type", "application/json");
HttpResponse response = client.execute(request);
} catch (IOException e) {
e.printStackTrace();
}
}
}
๊ทธ๋ผ ๊ฒฐ๊ตญ "http://localhost:8081/batch/list" url ์ ํตํด ์๋์ ๊ฐ์ด REST ๋ฐฉ์์ผ๋ก ์ปจํธ๋กค๋ฌ๋ฅผ ํธ์ถํ ๊ฒ์ด๋ค. ์ปจํธ๋กค๋ฌ์ ์์ค๋ ์๋์ ๊ฐ๋ค.
package com.upbit.openapi;
import java.io.IOException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.example.demo.mapper.LoginMapper;
import com.example.demo.mapper.MarketListMapper;
import com.example.demo.vo.MartListVO;
import com.example.demo.vo.UserInfo;
@RestController
@CrossOrigin(origins = "http://localhost:8080") // ์ปจํธ๋กค๋ฌ์์ ์ค์
@Service
public class BatchProgram {
@Autowired
MarketListMapper mrMapper;
/*
* ๋ง์ผ์ ์ข
๋ชฉ์ ๋ช
์นญ์ ๊ฐ์ ธ์ต๋๋ค.
*/
@RequestMapping(value="/batch/list", method=RequestMethod.GET)
public void testpp() {
try {
HttpClient client = HttpClientBuilder.create().build();
HttpGet request = new HttpGet("https://api.upbit.com/v1/market/all");
request.setHeader("Content-Type", "application/json");
HttpResponse response = client.execute(request);
HttpEntity entity = response.getEntity();
String json = EntityUtils.toString(entity, "UTF-8");
JSONParser parser = new JSONParser();
JSONArray jsonarray = (JSONArray) parser.parse(json);
//ํ
์ด๋ธ ์ด๊ธฐ
mrMapper.initMarketList();
for (int i = 0; i < jsonarray.size(); i++) {
JSONObject obj = (JSONObject) jsonarray.get(i);
MartListVO vo = new MartListVO();
vo.setMr_cd(obj.get("market").toString());
vo.setEng_nm(obj.get("english_name").toString());
vo.setKor_nm(obj.get("korean_name").toString());
try {
mrMapper.insertMarketList(vo);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} catch (org.apache.http.ParseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (org.json.simple.parser.ParseException e) {
e.printStackTrace();
}
}
}
์ฌ๊ธฐ์ ์ค์ํ๊ฒ ๋ด์ผ๋ ๋ถ๋ถ์ json ํ์์ ํ์ฑ ๋ฐฉ๋ฒ์ด๋ค.
JSONParser parser = new JSONParser();
JSONArray jsonarray = (JSONArray) parser.parse(json);
์์ ๊ฐ์ด JSONParser๋ฅผ ๋ง๋ค๊ณ JSON์ด [] ๋ก ์์ํ๋ค๋ฉด ๋ฐฐ์ด์ด๊ธฐ ๋๋ฌธ์ ์ฌ์ฉํ์๋ค.
jsonarray์ String์ผ๋ก ๋ json ์ ๋ด์ ํ์ฑํ๋ค.
๊ทธ ๋ค์ ์๋์ ๊ฐ์ ์๋น์ค๋ฅผ ํธ์ถํ๊ณ ์ด ์๋น์ค๋ @Mapper๋ก ์ ์ธ ๋์ด์๊ธฐ ๋๋ฌธ์ mapper ํ์ผ์ ๋ง๋ค์ด ์ค๋ค.
package com.example.demo.mapper;
import org.apache.ibatis.annotations.Mapper;
import com.example.demo.vo.MartListVO;
@Mapper
public interface MarketListMapper {
int insertMarketList(MartListVO vo);
int initMarketList();
}
mapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.demo.mapper.MarketListMapper">
<insert id="insertMarketList" parameterType="MartListVO" >
INSERT INTO `shoppingMall`.`TB_MR_LIST`
(`MR_CD`,
`KOR_NM`,
`ENG_NM`,
`SYS_DT`)
VALUES
(#{mr_cd},
#{kor_nm},
#{eng_nm},
sysdate())
</insert>
<delete id="initMarketList">
TRUNCATE TB_MR_LIST;
</delete>
</mapper>