package com.xxx.yyy;

import com.alibaba.appengine.api.cache.CacheService;
import com.alibaba.appengine.api.cache.CacheServiceFactory;
import com.alibaba.appengine.api.ds.DataSourceFactory;
import com.alibaba.appengine.api.fetchurl.FetchUrlService;
import com.alibaba.appengine.api.fetchurl.FetchUrlServiceFactory;
import com.alibaba.appengine.api.message.MessageService;
import com.alibaba.appengine.api.message.MessageServiceFactory;
import com.alibaba.appengine.api.store.StoreService;
import com.alibaba.appengine.api.store.StoreServiceFactory;
import com.alibaba.appengine.api.task.SimpleTask;
import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.sql.DataSource;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by shenling.yyb on 14-5-9
 */
public class MySimpleTask implements SimpleTask {
    private final static Logger logger = LoggerFactory.getLogger(MyShardingTask.class);

    @Override
    public String run(String[] args) throws Exception {
        logger.error("sharding task run method param: {}", JSON.toJSONString(args));
        logger.error("args[0]=" + args[0]);
        Map<String, String> ret = new HashMap<String, String>();
        ret.put("success", "true");
        ret.put("time", String.valueOf(System.currentTimeMillis()));

        try {
            CacheService cacheService = CacheServiceFactory.getCacheService();
            logger.error("cache service:{}", cacheService);
            if (cacheService != null) {
                cacheService.put("abcdef", "123456");
                Serializable abcdef = cacheService.get("abcdef");
                logger.error("cache service:get {}", abcdef);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        try {
            DataSource dataSource = DataSourceFactory.getDataSource();
            logger.error("datasource service:{}", dataSource == null);
        } catch (Exception e) {
            e.printStackTrace();
        }

        try {
            StoreService storeService = StoreServiceFactory.getStoreService();
            logger.error("store service {}", storeService == null);
        } catch (Exception e) {
            logger.error("store service", e);
        }

        try {
            FetchUrlService fetchUrlService = FetchUrlServiceFactory.getFetchUrlService();
            logger.error("fetch url service {}", fetchUrlService == null);
        } catch (Exception e) {
            e.printStackTrace();
        }

        try {
            MessageService messageService = MessageServiceFactory.getMessageService();
            logger.error("message service {}", messageService == null);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return JSON.toJSONString(ret);
    }
}
