第一步 在windows中创建目录
第二部 写properties 配置信息
log4j.rootLogger=INFO,R
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=${flume.dir}/prolog.log
log4j.appender.R.MaxFileSize=512MB
log4j.appender.R.MaxBackupIndex=40
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss}[%c]-[%p] %m%n
第三步:写pom.xml信息 里面的build能够实现打胖包的功能
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.kgc</groupId>
<artifactId>prolog</artifactId>
<version>1.0</version>
<name>prolog</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.75</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.3</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest><!--执行jar包的启动类,入口-->
<main-class>cn.kgc.App</main-class><!--陪主类-->
</manifest>
</archive>
</configuration>
<executions><!--可执行-->
<execution>
<id>make-assembly</id>
<phase>package</phase><!--包-->
<goals>
<goal>single</goal><!--单例-->
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
第四步 在Edit Configurations 中写args参数
第五步 写windows路径下的logger.properties文件
orderId=0
hour=0
day=1
second=0
year=2018
goon=true
month=1
minute=0
第六步 写 APP主类方法:
package cn.kgc;
import com.alibaba.fastjson.JSON;
import org.apache.log4j.Logger;
import java.io.*;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* 模拟生成订单行为日志
*/
public class App
{
private static Logger logger;
private static Properties logConf = new Properties();
private static long lastModified = 0;
private static boolean goon = true;
private static ExecutorService singlePool;
private static String PATH,CONF_PATH;
private static long orderId = 0;
private static final int USER_ID_UPPER = 1000000;
private static final int CMM_ID_UPPER = 10000000;
private static Calendar c = Calendar.getInstance();
private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
private static Random rand = new Random();
public static class OrderDetail{
private long cmmId;
private int buyNum;
public OrderDetail() {}
public OrderDetail(long cmmId, int buyNum) {
this.cmmId = cmmId;
this.buyNum = buyNum;
}
public long getCmmId() {
return cmmId;
}
public int getBuyNum() {
return buyNum;
}
}
public static class Order{
private long orderId;
private long userId;
private String createDate;
private List<OrderDetail> details;
public Order(long orderId, long userId, String createDate) {
this.orderId = orderId;
this.userId = userId;
this.createDate = createDate;
this.details = new ArrayList<>();
}
public void setDetails(long cmmId,int buyNum){
this.details.add(new OrderDetail(cmmId,buyNum));
}
public long getOrderId() {
return orderId;
}
public long getUserId() {
return userId;
}
public String getCreateDate() {
return createDate;
}
public List<OrderDetail> getDetails() {
return details;
}
}
private static int parse(String key){
return Integer.parseInt(logConf.getProperty(key));
}
private static void readConf(File confFile) throws Exception {
lastModified = confFile.lastModified();
FileInputStream fis = new FileInputStream(confFile);
logConf.load(fis);
c.set(parse("year"),
parse("month"),
parse("day"),
parse("hour"),
parse("minute"),
parse("second"));
orderId = Long.parseLong(logConf.getProperty("orderId"));
goon = Boolean.parseBoolean(logConf.getProperty("goon"));
fis.close();
//单线程池侦听配置信息的修改状况
singlePool = Executors.newSingleThreadExecutor();
singlePool.submit(()->{
while (goon){
File file = new File(CONF_PATH);
//如果文件被修改
if(lastModified != file.lastModified()){
//重置修改时间
lastModified = file.lastModified();
try {
//如果要终止进程,将goon改为false即可
Properties pro = new Properties();
pro.load(new FileInputStream(CONF_PATH));
goon = Boolean.parseBoolean(pro.getProperty("goon"));
pro.clear();
} catch (IOException e) {
e.printStackTrace();
}
}
}
});
}
private static void writeConf() throws Exception {
FileOutputStream fos = new FileOutputStream(CONF_PATH);
logConf.setProperty("orderId",String.valueOf(orderId));
logConf.setProperty("year",String.valueOf(c.get(Calendar.YEAR)));
logConf.setProperty("month",String.valueOf(c.get(Calendar.MONTH)+1));
logConf.setProperty("day",String.valueOf(c.get(Calendar.DATE)));
logConf.setProperty("hour",String.valueOf(c.get(Calendar.HOUR)));
logConf.setProperty("minute",String.valueOf(c.get(Calendar.MINUTE)));
logConf.setProperty("second",String.valueOf(c.get(Calendar.SECOND)));
logConf.store(fos,"# modified at "+sdf.format(new Date()));
fos.close();
}
public static void main( String[] args ) throws Exception {
if(args.length<2){
throw new RuntimeException("未提供日志或配置路径");
}
PATH = args[0];
CONF_PATH = args[1];
//设置jvm运行时环境变量
System.setProperty("flume.dir",PATH);
logger = Logger.getLogger(App.class);
File dir = new File(PATH),confFile = new File(CONF_PATH);
if(!confFile.exists() || !confFile.isFile()){
throw new RuntimeException(CONF_PATH+" 不存在或非文件异常");
}
if(!dir.exists()){
dir.mkdirs();
}
if(!dir.isDirectory()){
throw new RuntimeException("必须提供路径,而你错误的提供了文件");
}
//初始化配置信息和日期
readConf(confFile);
while (goon){
c.add(Calendar.SECOND,1+rand.nextInt(30));
Order order = new Order(++orderId, 1 + rand.nextInt(USER_ID_UPPER), sdf.format(c.getTime()));
for (int i = 0; i < 1+rand.nextInt(5) ; i++) {
order.setDetails(1+rand.nextInt(CMM_ID_UPPER),1+rand.nextInt(3));
}
logger.info(JSON.toJSONString(order));
try {
Thread.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
writeConf();
singlePool.shutdown();
}
}