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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
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 @   猪哥哥厉害  阅读(501)  评论(0编辑  收藏  举报
(评论功能已被禁用)
点击右上角即可分享
微信分享提示