java动态调用service

场景:针对业务的需求,需要获取不同数据,并生成指定文件、上传到指定服务器,由于表的数目较大,但是业务逻辑比较一致,故提取出来公共方法,根据传参的不同,实现不同表的逻辑

代码示例:

//工具类 获取到对应的service层的实现类
private static Map<String,Object> config=new HashMap<String,Object>();
    static {
        config.put("projBasicInfoMServiceImpl" , new ProjBasicInfoMServiceImpl().getClass().getClass());
    }
public static HashMap getServiceImpl(String name, String type) {
        String[] split = name.split("_");
        int length = split.length;
        String beanName = "";
        for (int i=0; i<length; i++){
            if (i==0){
                String first = split[i].substring(0, 1).toLowerCase();
                String second = split[i].substring(1, split[i].length() ).toLowerCase();
                beanName += first+second;
            }else if (i<length){
                String first = split[i].substring(0, 1);
                String second = split[i].substring(1, split[i].length() ).toLowerCase();
                beanName += first+second;
            }
        }
        if (StringUtils.equals(type,"month")){
            beanName += "MServiceImpl";
        }
        if (StringUtils.equals(type,"day")){
            beanName += "DServiceImpl";
        }
        HashMap hashMap = new HashMap();
        String s = config.get(beanName).toString();
        //value值的格式 class xx.xx.xxx.xxxServiceImpl 格式需要调整
        String classForName = s.substring(6, s.length());
        hashMap.put(beanName,classForName);

        return  hashMap;
}
//具体业务代码 通过反射调用对应的方法
public R genMonthFile(String tableName){ //tableName="PROJ_BASIC_INFO"

        String key = "";
        String value = "";

        //获取项目基本信息-月批表所有数据
        HashMap map = ServiceFactory.getServiceImpl(tableName, "month");
        try {

            //不可直接反射创建serviceimpi对象,因为反射创建出来的对象无法实例化dao接口
            //从ApplicationContext中取出已创建好的的对象
            //该方法实际是对获取application和getBean的封装
            //注意!!!   getBean时 bean的首字母是小写 不能大写 格式:abcServiceImpl否则会报错
            Object bean = SpringBeanUtil.getBean(key);
            //反射调用方法
            Class<?> classType = Class.forName(value);
            //为保持方法名一致,不同业务层调用的方法名 取一个相同的
            Method m = classType.getDeclaredMethod("linkedHashMapList",null);
            //第一个对象为 调用方法的对象 第二个为方法的参数
            List<LinkedHashMap<String, Object>> list = (List<LinkedHashMap<String, Object>>) m.invoke(bean,null);
            //该方法分两步 一、生成报文文件 二、上传至指定服务器
            boolean flag = genReportFileService.genMonthFile(list, tableName);

            return R.status(flag);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }  catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        return null;
}
//生成报文并上传
public boolean genMonthFile(List<LinkedHashMap<String, Object>> dataList, String dataType) {

        //获取文件日期
        EcifSubUtil util = new EcifSubUtil();
        //获取上月的最后一天
        String fileDate = util.lastDays();
        //调用报文生成方法
        boolean flag = this.prodFile(dataList, dataType,"月批", fileDate);
        if (flag) {
            sftpUtil.jsch(fileDate,dataType);
        }

        return flag;
}
//使用jsch 跨服务器上传文件
public  boolean jsch(String monName, String tableName)  {

        SftpUtil sftpUtil = new SftpUtil();
        EcifSubUtil util = new EcifSubUtil();

        Map<String, String> sftpDetails = new HashMap<String, String>();
        // 设置主机ip,端口,用户名,密码
        sftpDetails.put("host", host);
        sftpDetails.put("username", username);
        sftpDetails.put("password", password);
        sftpDetails.put("port", port);


        //.dat文件
        String datFileName = util.genMonGroupDataFileName(monName, tableName);
        String srcDat = src + datFileName;
        String targetDat = target + datFileName;

        //.verf文件
        String verfFileName = util.genMonGroupVerfFileName(monName, tableName);
        String srcVerf = src +  verfFileName;
        String targetVerf = target + verfFileName;

        SFTPChannel channel = sftpUtil.getSFTPChannel();
        ChannelSftp chSftp = null;
        try {
            chSftp = channel.getChannel(sftpDetails, 60000);
            chSftp.put(srcDat, targetDat, ChannelSftp.OVERWRITE);
            chSftp.put(srcVerf, targetVerf, ChannelSftp.OVERWRITE);
            chSftp.quit();
            channel.closeChannel();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return true;

 }
//jsch相关代码
public class SFTPChannel {
    Session session = null;
    Channel channel = null;


    public ChannelSftp getChannel(Map<String, String> sftpDetails, int timeout) throws JSchException {

        String ftpHost = sftpDetails.get("host");
        String port = sftpDetails.get("port");
        String ftpUserName = sftpDetails.get("username");
        String ftpPassword = sftpDetails.get("password");

        int ftpPort = 22;
        if (port != null && !port.equals("")) {
            ftpPort = Integer.valueOf(port);
        }

        JSch jsch = new JSch(); // 创建JSch对象
        session = jsch.getSession(ftpUserName, ftpHost, ftpPort); // 根据用户名,主机ip,端口获取一个Session对象

        if (ftpPassword != null) {
            session.setPassword(ftpPassword); // 设置密码
        }
        Properties config = new Properties();
        config.put("StrictHostKeyChecking", "no");
        session.setConfig(config); // 为Session对象设置properties
        session.setTimeout(timeout); // 设置timeout时间
        session.connect(); // 通过Session建立链接


        channel = session.openChannel("sftp"); // 打开SFTP通道
        channel.connect(); // 建立SFTP通道的连接

        return (ChannelSftp) channel;
    }

    public void closeChannel() throws Exception {
        if (channel != null) {
            channel.disconnect();
        }
        if (session != null) {
            session.disconnect();
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值