记一次xxl_job中HttpJobHandler中的数据请求与存储

@JobHandler(value = "httpJobHandler")
@Component
public class HttpJobHandler extends IJobHandler {

    @Resource
    private BankSynchronizationService bankSynchronizationService;

    @Override
    public ReturnT<String> execute(String param) throws Exception {
        XxlJobLogger.log("XXL-JOB, HTTP请求.");

        // request
        HttpURLConnection connection = null;
        BufferedReader bufferedReader = null;
        try {
            // connection
            URL realUrl = new URL(param);
            connection = (HttpURLConnection) realUrl.openConnection();
            System.out.println(param + "------param");
            // connection setting
            connection.setRequestMethod("POST");
            //connection.setur
            connection.setDoOutput(true);
            connection.setDoInput(true);
            connection.setUseCaches(false);
            connection.setReadTimeout(5 * 1000);
            connection.setConnectTimeout(3 * 1000);

            connection.setRequestProperty("connection", "Keep-Alive");
            // 设置文件类型:传输类型
            connection.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
            // 设置接收类型否则返回415错误
            connection.setRequestProperty("Accept-Charset", "application/json;charset=UTF-8");

            // do connection
            connection.connect();

            //Map> map = connection.getHeaderFields();
            String bankDate = bankSynchronizationService.findDate();
            DateFormat bf = new SimpleDateFormat("yyyy.MM.dd");
            String date = bf.format(new Date());
            System.out.println(date + "----date");

            String date1 = date.substring(date.length() - 2);
            String bank = bankDate.substring(bankDate.length() - 2);

            int i1 = Integer.parseInt(date1) - Integer.parseInt(bank);

            JSONObject json = new JSONObject();
            json.put("dataTable", "insurance_bp_bank_i");
            json.put("limitLower", 0);
            json.put("limitUpper", 99);
            JSONArray array = new JSONArray();
            array.add("vertrag");
            array.add("gpart");
            array.add("bankno");
            json.put("returnFields", array);
            json.put("queryType", "query");
            JSONArray ordering = new JSONArray();
            ordering.add("vertrag");
            json.put("ordering", ordering);
            JSONObject innerJson = new JSONObject();
            innerJson.put("type", "in");
            innerJson.put("field", "chdat");
            JSONArray fieldsArray = new JSONArray();
            fieldsArray.add(bankDate);
            innerJson.put("values", fieldsArray);
            json.put("filter", innerJson);

            System.out.println(json.toString() + "----json");

            // valid StatusCode
            DataOutputStream dos = new DataOutputStream(connection.getOutputStream());
            dos.write((json.toString()).getBytes());
            dos.flush();
            dos.close();

            int statusCode = connection.getResponseCode();
            System.out.println(statusCode + "-----statusCode");
            if (statusCode != 200) {
                throw new RuntimeException("Http Request StatusCode(" + statusCode + ") Invalid.");
            }

            // result
            bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
            StringBuilder result = new StringBuilder();
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                result.append(line);

            String responseMsg = result.toString();
            System.out.println(responseMsg + "-----responseMsg");
            //
            List<InsuranceBpBank> insuranceBpBanks = JSON.parseArray(responseMsg, InsuranceBpBank.class);
            for (InsuranceBpBank insuranceBpBank : insuranceBpBanks) {
                System.out.println("------------------------");
                System.out.println(insuranceBpBank.getVertrag());
                bankSynchronizationService.insertBank(insuranceBpBank);
            }
            XxlJobLogger.log(responseMsg);
            return SUCCESS;
        } catch (Exception e) {
            XxlJobLogger.log(e);
            return FAIL;
        } finally {
            try {
                if (bufferedReader != null) {
                    bufferedReader.close();
                }
                if (connection != null) {
                    connection.disconnect();
                }
            } catch (Exception e2) {
                XxlJobLogger.log(e2);
            }
        }

    }

}

HttpURLConnection 可以封装成工具类,只需要传入param参数即可。

你可能感兴趣的:(JAVA)