一个Log生成工具小项目的实现
这两天的主要工作是用java写一个log生成工具,用于大数据量的测试。写这个工具,使用了:
Spring RMI:用于启动、关闭工具 Commons-logging + log4j,用于写log,其中重新实现了RollingFileAppender用于自定义log文件名 Math.random以及Ramdon,用于生成随机的数据 ScheduledThreadPoolExecutor用于多线程定时执行一些job,每个job负责生成相应的log文件 泛型,每种log的生成器在运行时决定是哪一种log Maven assembly plugin,用于打包,打成一个.zip包,解开后,运行里面的shell(linux下)或者bat(windows下)即可以运行 Windows bat的写法,用于写在windows下可执行bat文件
1. Spring RMI应用,在我的另外一篇文章中有介绍,而且工具也采用那个后台server框架。一个简单的JAVA后台程序框架
2. Commons-logging + log4j,这块没什么好讲的,自定义log文件名这一块详见自定义log4j生成的log文件名
3. 随机数的生成
3.1 在一个指定范围内生成随机整数
public static final long getRandomNumber(long begin, long end) { return (long) (begin + (end - begin) * Math.random()); } public static final String randomString(int length) { if (length < 1) { return null; } if (randGen == null) { synchronized (initLock) { if (randGen == null) { randGen = new Random(); numbersAndLetters = ("0123456789abcdefghijklmnopqrstuvwxyz" + "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ") .toCharArray(); } } } char[] randBuffer = new char[length]; for (int i = 0; i < randBuffer.length; i++) { randBuffer[i] = numbersAndLetters[randGen.nextInt(71)]; } return new String(randBuffer); }<plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-assembly-plugin</artifactId><configuration><archive><manifest> <!-- 这里指定主程序入口 --><mainClass>cn.lettoo.test.Launcher</mainClass></manifest></archive><descriptors> <!-- 这里指定装配文件 --><descriptor>src/main/assembly/assembly.xml</descriptor></descriptors></configuration><executions><execution><id>make-assembly</id><phase>package</phase><goals><goal>single</goal></goals></execution></executions></plugin>
<assembly> <id>bin</id> <formats> <!-- 指定打zip包 --> <format>zip</format> </formats> <dependencySets> <dependencySet> <!-- copy jar文件到lib目录 --> <useProjectArtifact>true</useProjectArtifact> <outputDirectory>lib</outputDirectory> </dependencySet> </dependencySets> <!-- 下面主要是copy README.txt,以及配置文件到conf目录,还有运行脚本 --> <fileSets> <fileSet> <outputDirectory>/</outputDirectory> <includes> <include>README.txt</include> </includes> </fileSet> <fileSet> <directory>src/main/scripts</directory> <outputDirectory>/</outputDirectory> </fileSet> <fileSet> <directory>src/main/resources</directory> <outputDirectory>/conf</outputDirectory> <includes> <include>*.xml</include> <include>*.properties</include> </includes> </fileSet> </fileSets></assembly>
set CLASSPATH=.;confsetlocal enabledelayedexpansion for %%j in (lib/*.jar) do ( set CLASSPATH=!CLASSPATH!;lib\%%j ) endlocal
CLASSPATH=.:conffor file in lib/*.jar;do CLASSPATH=${CLASSPATH}:$file;done