com.apus.hades.admin.task.AdSchedulingUpDownTask.java Source code

Java tutorial

Introduction

Here is the source code for com.apus.hades.admin.task.AdSchedulingUpDownTask.java

Source

/*
 * Copyright 2015 ireader.com All right reserved. This software is the
 * confidential and proprietary information of ireader.com ("Confidential
 * Information"). You shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license agreement you entered
 * into with ireader.com.
 */
package com.apus.hades.admin.task;

import java.util.Date;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.apus.hades.admin.cache.ConfigCache;
import com.apus.hades.common.config.Configuration;
import com.apus.hades.common.utils.DateUtil;
import com.apus.hades.common.utils.JsonUtil;
import com.apus.hades.common.utils.http.HttpClientUtil;
import com.apus.hades.core.common.Status;
import com.apus.hades.core.manager.ad.PositionManager;
import com.apus.hades.core.manager.system.ConfigManager;
import com.apus.hades.core.model.ad.RealPlatformInfo;
import com.apus.hades.core.model.system.Config;

/**
 * @Descriptions ?.
 * @date 2015331
 * @author wangliping
 */
@Component(value = "adSchedulingUpDownTask")
public class AdSchedulingUpDownTask {

    private Logger logger = LoggerFactory.getLogger(getClass());
    private final String[] cmds = { "ad.api.syn_ad_time_data.url", "ad.api.syn_ad_data.url" };

    @Autowired
    private PositionManager positionManager;
    @Autowired
    private ConfigManager configManager;

    private int success;

    public void run() {
        success = 0;
        Config config = configManager.find(ConfigCache.REAL_AD_UP_DOWN.getKey());
        if (null == config) {
            return;
        }
        JSONArray array = JSONArray.fromObject(config.getValue());
        if (null == array || array.isEmpty()) {
            return;
        }
        int size = array.size();
        for (int i = 0; i < size; i++) {
            String s = StringUtils.EMPTY;
            try {
                s = array.getString(i);
                JSONObject jsonObject = array.getJSONObject(i);
                this.processConfig(jsonObject);
            } catch (Exception e) {
                logger.error("[]!:" + s, e);
            }
        }
        if (success > 0) {
            config.setValue(array.toString().replaceAll("\"", StringUtils.EMPTY));
            configManager.update(config);
        }
        logger.info("[],?:{}?", success);
    }

    private void processConfig(JSONObject config) throws Exception {
        int did = config.getInt("did");
        Long id = config.getLong("id");
        Status status = Status.getByValue(config.getInt("status"));
        String time = config.getString("time");
        Date date = DateUtil.strToDate(time, "yyyyMMddHHmmss");
        if (did == 1) {
            return;
        }
        if (date.after(new Date())) {
            return;
        }
        RealPlatformInfo real = positionManager.findRealPlatformInfo(id);
        if (null == real) {
            return;
        }
        real.setStatus(status);
        positionManager.updateRealPlatformInfo(real);
        for (String c : cmds) {
            if (!this.synAPI(c)) {
                return;
            }
        }
        config.put("did", 1);
        success++;
    }

    private boolean synAPI(String cmd) throws Exception {
        String rs = HttpClientUtil.sendGet(Configuration.getInstance().get(cmd));
        logger.info("[]?:{},?:{}", cmd, rs);
        if (StringUtils.isNotBlank(rs)) {
            String status = JsonUtil.getStringValueByJsonStr(rs, "status");
            return "0".equals(status);
        }
        return false;
    }
}