package com.example.schedule;
import java.io.IOException;
import java.util.Date;
import com.example.entity.ProductCountEntity;
import com.example.repository.ProductCountRepository;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
@Component
public class MySchedule {
// 일정 시간 지나면 읽었다가 데이터를 복제해야 하는 등의 기능을 쓸 때 저장소랑 같이 사용하면 됨
@Autowired ProductCountRepository pcRepository;
// 10초 단위로 출력
// @Scheduled(cron = "*/10 * * * * *")
public void printData() {
Date date = new Date();
System.out.println(date.toString());
}
// 가지고 온 데이터는 스트링밖에 안됨. 제이슨으로 바꿔줘야 함.
// @Scheduled(cron = "*/10 * * * * *")
public void printData1() throws IOException {
final String URL = "http://ihongss.com/json/exam1.json";
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder().url(URL).build();
Response response = client.newCall(request).execute();
String msg = response.body().string();
System.out.println(msg);
// { "ret": "y", "data": "123" }
// [{ "ret": "y", "data": "123" }, { "ret": "t", "data": "1234" }]
// 라이브러리 받은 것 사용
// 한 개 오면 JSONObject, 여러개 오면 JSONArray
JSONObject jobj = new JSONObject(msg);
System.out.println(jobj.getString("ret"));
System.out.println(jobj.getInt("data"));
// ProductCountEntity p = new ProductCountEntity();
// pcRepository.save(p);
}
}