package com.xxx.yyy;

import com.alibaba.appengine.jae.api.task.AbstractShardingTask;
import com.alibaba.appengine.jae.api.task.ShardingSet;
import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by shenling.yyb on 14-5-7
 */
public class MyShardingTask extends AbstractShardingTask {
    private final static Logger logger = LoggerFactory.getLogger(MyShardingTask.class);

    @Override
    public ShardingSet take(String[] args) {
        logger.error("sharding task take method param: {}", JSON.toJSONString(args));
        ShardingSet set = new ShardingSet(true);
        for (int i = 0; i < 10001; i++) {
            set.add("id" + i);
        }
        logger.error("sharding task take method generate 10001 record finished.");
        return set;
    }

    @Override
    public String run(String[] args) {
        logger.error("sharding task run method param: {}", JSON.toJSONString(args));
        logger.error("args[0]=" + args[0]);
        Map<String, String> ret = new HashMap<String, String>(5);
        try {
            ret.put(System.getProperty("host.name") + " " + System.currentTimeMillis(), System.currentTimeMillis() + "");
            if (args.length >= 4) {
                ret.put(args[3], String.valueOf(JSON.parseObject(args[0], List.class).size()));
            } else {
                logger.error("sharding task run method args length error.");
                ret.put("" + System.currentTimeMillis() + " param error", System.getProperty("host.name"));
            }
            logger.error("sharding task run method return value:{}", JSON.toJSONString(ret));

        } catch (Exception e) {
            logger.error("sharding tsk run method error.", e);
        }
        return JSON.toJSONString(ret);
    }

    public static void main(String[] args) {
        MyShardingTask task = new MyShardingTask();
        task.run(null);
    }
}
