Java 执行jar linux 实例

需求:通过执行jar的方式 ,把某个文件路径下的用户数据同步到redis

  1、main 函数

public class Main {

    private static Logger logger = LoggerFactory.getLogger(Main.class);

    private static DisruptorService disruptorService = new DisruptorService();

    private static AppsTaskService appsTaskService =new AppsTaskService();

    /**
     * 调用具体的方法
     * @param args
     */
    public static void main (String[] args) {
        try {
            if(args[0].trim().equals("appsTask")){

                appsTaskService.appsTask(args[1],args[2]);
            }else{
                Method targetMethod = DisruptorService.class.getDeclaredMethod(args[0].trim(), args.getClass());
                targetMethod.invoke(disruptorService, (Object)args);
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage());
        }
    }

}


2 service

package cn.ycmedia.dmp.redisData.service;

import cn.ycmedia.dmp.redisData.common.Consts;
import cn.ycmedia.dmp.redisData.dao.IndexPartionRedisDao;
import cn.ycmedia.dmp.redisData.utils.FileUtil;
import cn.ycmedia.dmp.redisData.utils.StringUtil;
import org.apache.commons.lang3.StringUtils;
import redis.clients.jedis.Jedis;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;

/**
 * Created by ${朱良兴} on 2016/7/20.
 */
public class AppsTaskService {

    private IndexPartionRedisDao redisDao = new IndexPartionRedisDao();
    /**
     *
     * @param arg app分类+app父类ID
     * @param arg1  文件所在路径
     */
    public void appsTask(String arg, String arg1) {
        File root = new File(arg1);
        try {
            showAllFiles(arg,root);
        }catch (Exception e){

        }

    }

    public  void showAllFiles(String arg ,File dir) throws Exception{
        File[] fs = dir.listFiles();
        BufferedReader reader;
        for(int i=0; i<fs.length; i++){
            reader = new BufferedReader(new FileReader(fs[i].getAbsolutePath()));
            String line;
            while ((line = reader.readLine()) != null&&!" ".equals((line = reader.readLine()))) {
                saveRecord(arg,line);
            }
            if(fs[i].isDirectory()){
                try{
                    showAllFiles(arg,fs[i]);
                }catch(Exception e){}
            }
        }
    }


    public void saveRecord (String projectId, String line)
            throws IOException, InvocationTargetException, IllegalAccessException{
        try {
            Jedis jedis = null;
            try {
                String uid = line.trim();
                String key = Consts.Disruptor.keyPrefix + uid;
                jedis = redisDao.getJedis(uid);
                String exist = jedis.hget(key, "apps");

                System.out.println("之前的:"+exist);
                String value;
                if (StringUtils.isBlank(exist)) {
                    value = projectId;
                    jedis.hset(key, "apps", value);
                }
                else {
                    String newValue= StringUtil.adverticalPro(exist,projectId);
                    jedis.hset(key, "apps", newValue);
                }
                System.out.println("之后的:"+jedis.hget(key, "apps"));
                jedis.expire(key, Consts.Redis.REDIS_EXPIRE_MONTH);
            }catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (jedis != null) {
                    jedis.close();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

 ======================打包成jar

=============================

执行: java -jar proBanDirect-jar-with-dependencies.jar  appsTask 5,6\|8,9  /opt2/dmp/redisApps/file

 

posted @ 2016-07-20 15:23  猪哥哥厉害  阅读(500)  评论(0编辑  收藏  举报