博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java代码规范总结,更新持续中
阅读量:4090 次
发布时间:2019-05-25

本文共 5024 字,大约阅读时间需要 16 分钟。

1. 实例的开启与关闭

var zkClient: ZooKeeper = null      try {        zkClient = new ZooKeeper(getZkUrl(), 2000, new Watcher {          override def process(watchedEvent: WatchedEvent): Unit = {}        })        val data = zkClient.getData(s"/hadoop-ha/" + hdfsNameServiceHost + s"/ActiveBreadCrumb", new Watcher {          override def process(watchedEvent: WatchedEvent): Unit = {}        }, new Stat())        val activeNodeInfo = HAZKInfoProtos.ActiveNodeInfo.parseFrom(data)        activeNodeInfo.getHostname      } catch {        case ex: Exception => {          logError(s"ERROR: zookeeper connect failure, can not get the active hdfs namenode according to ${hdfsNameServiceHost}", ex)          ""        }      }finally {        if (zkClient != null){          zkClient.close()        }      }

  如上述,代码 zkClient 在try中开启,不能再在try中关闭,因为发生异常的时候是关闭不掉的,切记切记切记!!!必须在finally中zkClient.close()才能把zkClient关闭,不这样做的后果就是会不断报连接zkserver失败

2. 异常捕获

2.1 每个方法都需要try catch,内或外的区别

  每个方法里面或者外面都需要try catch。不然会导致程序挂死,占用资源。

2.1 异常后的代码不会执行

  注意:try catch里面需要代码异常之后,异常代码剩下的代码将不会执行,如下示例:

try{      assert(argsCheck(args))      if (dbName != null && dbName != "") {        logInfo(s"use $dbName")        sparkSession.sql(s"use $dbName")      }      processSparkPara      run      sparkSession.stop()      result = true      // determine if the taskVars json file exists, if not write the file, if exists overwrite the file      val taskVarsJson = JsonProtocal.taskVarsToJson(TaskVars(systemVars, runtimeVars))      logInfo(s"write taskVarsJsonStr:$taskVarsJson")      //    if (!isLocal){
HDFSUtil.writeHDFSFile(taskVarsJsonPath, taskVarsJson) // }else{ // createLocalFile(taskVarsJsonPath, taskVarsJson) // } }catch { case e: Throwable => { logError(s"== process failed, please check.",e) cause = e.getMessage result = false } }

  示例中的run方法发生异常之后,taskVarsJson文件将不会生成,而这将会导致后续的程序异常。如果taskVarsJson文件是程序所必须的,则不能放在try catch里面

try{      assert(argsCheck(args))      if (dbName != null && dbName != "") {        logInfo(s"use $dbName")        sparkSession.sql(s"use $dbName")      }      processSparkPara      run      sparkSession.stop()      result = true          }catch {      case e: Throwable => {        logError(s"== process failed, please check.",e)        cause = e.getMessage        result = false      }    }    // determine if the taskVars json file exists, if not write the file, if exists overwrite the file    val taskVarsJson = JsonProtocal.taskVarsToJson(TaskVars(systemVars, runtimeVars))    logInfo(s"write taskVarsJsonStr:$taskVarsJson")    //    if (!isLocal){
HDFSUtil.writeHDFSFile(taskVarsJsonPath, taskVarsJson) // }else{ // createLocalFile(taskVarsJsonPath, taskVarsJson) // }

3. 测试规范

  测试成功,测试不成功两种。不要只测试成功的,遗留不成功,程序不成功也会导致程序线程挂死(占用资源)、不断打印连接不上日志等等异常情况。好像给别人造成不少麻烦...

4. scala的 case class 类构造优化

原代码:case class Desc(name:String)val taskDesc = TaskDesc("hello")类扩展:case class Desc(name:String, config: Config)val taskDesc = TaskDesc("hello", null)

  评:上述代码扩展性不好,需要修改原来的老代码。参考如下设计,可以在扩展类的时候,避免修改其他老代码:

原代码:case class Desc(name:String)val taskDesc = TaskDesc("hello")类扩展:case class Desc(name:String, config: Config = null)val taskDesc = TaskDesc("hello)

5. File文件与文件夹

def cleanExpiredLog(pathname: String, task: String): Unit ={    System.out.println("clean expired log start")    try{      var lifeCycle = 3      if (lifeCycle < 1) lifeCycle = 3      val sdfDay = new java.text.SimpleDateFormat("yyyy-MM-dd")      val strDay = sdfDay.format(System.currentTimeMillis() - lifeCycle * 24 * 60 * 60 * 1000)      val tasksFile = new File(pathname  + File.separator + task + File.separator)      val tasksSubFiles = tasksFile.listFiles()      for(taskFile <- tasksSubFiles){          // 筛选task任务目录下过期的文件夹          val filterFiles = filterExpiredLog(taskFile, strDay)          // 删除过期日志          deleteExpiredLog(filterFiles)      }    }catch{      case ex: Exception =>        System.out.println("clean expired log error, " + ex.getMessage)    }    System.out.println("clean expired log end")  }

  评:对文件进行操作时,需判断该文件父类是文件还是文件夹,

def cleanExpiredLog(pathname: String, task: String): Unit ={    System.out.println("clean expired log start")    try{      var lifeCycle = 3      if (lifeCycle < 1) lifeCycle = 3      val sdfDay = new java.text.SimpleDateFormat("yyyy-MM-dd")      val strDay = sdfDay.format(System.currentTimeMillis() - lifeCycle * 24 * 60 * 60 * 1000)      val tasksFile = new File(pathname  + File.separator + task + File.separator)      val tasksSubFiles = tasksFile.listFiles()      for(taskFile <- tasksSubFiles){        //这里需要判断文件还是文件夹,否则文件的listFiles会报错           if (tasksFile.isDirectory){          // 筛选task任务目录下过期的文件夹          val filterFiles = filterExpiredLog(taskFile, strDay)          // 删除过期日志          deleteExpiredLog(filterFiles)        }      }    }catch{      case ex: Exception =>        System.out.println("clean expired log error, " + ex.getMessage)    }    System.out.println("clean expired log end")  }

转载地址:http://kkcii.baihongyu.com/

你可能感兴趣的文章
数据结构-栈(栈基本实现C++)
查看>>
数据结构-队列(队列的基本实现C++)
查看>>
数据结构-链表(链表的基本实现C++)
查看>>
数据结构-树(树基本实现C++)
查看>>
数据结构-图(图的基本实现C++)
查看>>
关于C/C++中const char*、char*、string之间那点事
查看>>
常用Git指令
查看>>
使用git命令行将本地仓库代码上传到远程仓库
查看>>
AI 图像智能修复老照片
查看>>
Linux操作系统概述
查看>>
Linux从入门到放弃之Ubuntu设置默认登录用户为root
查看>>
Linux从入门到放弃之VirtualBox下Ubuntu16.04版本的网络配置
查看>>
Linux从入门到放弃之VirtualBox下Ubuntu16.04版本的ssh配置
查看>>
求在平面直角坐标系中,一个点绕坐标原点旋转一定角度后点的坐标
查看>>
求两圆的公切线计算
查看>>
Boost学习之序列化
查看>>
Windows下LIB和DLL的区别与使用
查看>>
数据结构-红黑树
查看>>
十大排序算法C++实现
查看>>
七大常用查找算法C++实现
查看>>