-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from qq254963746/develop
添加 mysql JobLogger 实现
- Loading branch information
Showing
17 changed files
with
243 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
package com.lts.job.core.file; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.io.*; | ||
|
||
/** | ||
* @author Robert HG ([email protected]) on 3/6/15. | ||
|
@@ -32,5 +31,13 @@ public static File createDirIfNotExist(String path) { | |
return file; | ||
} | ||
|
||
|
||
public static String read(InputStream is) throws IOException { | ||
BufferedReader br = new BufferedReader(new InputStreamReader(is)); | ||
StringBuilder createTableSql = new StringBuilder(); | ||
String data = null; | ||
while ((data = br.readLine()) != null) { | ||
createTableSql.append(data); | ||
} | ||
return createTableSql.toString(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
job-logger/job-logger-api/src/main/java/com/lts/job/biz/logger/JobLogException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.lts.job.biz.logger; | ||
|
||
/** | ||
* @author Robert HG ([email protected]) on 5/21/15. | ||
*/ | ||
public class JobLogException extends RuntimeException { | ||
|
||
public JobLogException() { | ||
super(); | ||
} | ||
|
||
public JobLogException(String message) { | ||
super(message); | ||
} | ||
|
||
public JobLogException(String message, Throwable cause) { | ||
super(message, cause); | ||
} | ||
|
||
public JobLogException(Throwable cause) { | ||
super(cause); | ||
} | ||
|
||
protected JobLogException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { | ||
super(message, cause, enableSuppression, writableStackTrace); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
job-logger/job-logger-api/src/main/java/com/lts/job/biz/logger/JobLogUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.lts.job.biz.logger; | ||
|
||
import com.lts.job.biz.logger.domain.BizLogPo; | ||
import com.lts.job.biz.logger.domain.JobLogPo; | ||
import com.lts.job.biz.logger.domain.LogType; | ||
|
||
/** | ||
* @author Robert HG ([email protected]) on 5/21/15. | ||
*/ | ||
public class JobLogUtils { | ||
|
||
public static JobLogPo bizConvert(BizLogPo bizLogPo) { | ||
if (bizLogPo == null) { | ||
return null; | ||
} | ||
JobLogPo jobLogPo = new JobLogPo(); | ||
jobLogPo.setTimestamp(bizLogPo.getTimestamp()); | ||
jobLogPo.setTaskTrackerNodeGroup(bizLogPo.getTaskTrackerNodeGroup()); | ||
jobLogPo.setTaskTrackerIdentity(bizLogPo.getTaskTrackerIdentity()); | ||
jobLogPo.setJobId(bizLogPo.getJobId()); | ||
jobLogPo.setMsg(bizLogPo.getMsg()); | ||
jobLogPo.setLevel(bizLogPo.getLevel()); | ||
jobLogPo.setLogType(LogType.BIZ); | ||
return jobLogPo; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?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"> | ||
<parent> | ||
<artifactId>job-logger</artifactId> | ||
<groupId>com.lts</groupId> | ||
<version>1.4.0-SNAPSHOT</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>job-logger-mysql</artifactId> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>com.lts</groupId> | ||
<artifactId>job-logger-api</artifactId> | ||
<version>${project.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>commons-dbutils</groupId> | ||
<artifactId>commons-dbutils</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>mysql</groupId> | ||
<artifactId>mysql-connector-java</artifactId> | ||
</dependency> | ||
</dependencies> | ||
|
||
</project> |
78 changes: 78 additions & 0 deletions
78
job-logger/job-logger-mysql/src/main/java/com/lts/job/biz/logger/mysql/MysqlJobLogger.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package com.lts.job.biz.logger.mysql; | ||
|
||
import com.lts.job.biz.logger.JobLogException; | ||
import com.lts.job.biz.logger.JobLogUtils; | ||
import com.lts.job.biz.logger.JobLogger; | ||
import com.lts.job.biz.logger.domain.BizLogPo; | ||
import com.lts.job.biz.logger.domain.JobLogPo; | ||
import com.lts.job.core.cluster.Config; | ||
import com.lts.job.core.file.FileUtils; | ||
import com.lts.job.core.util.JSONUtils; | ||
import com.lts.job.store.jdbc.JdbcRepository; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.sql.SQLException; | ||
|
||
/** | ||
* @author Robert HG ([email protected]) on 5/21/15. | ||
*/ | ||
public class MysqlJobLogger extends JdbcRepository implements JobLogger { | ||
|
||
public MysqlJobLogger(Config config) { | ||
super(config); | ||
doCreateTable(); | ||
} | ||
|
||
@Override | ||
public void log(JobLogPo jobLogPo) { | ||
if(jobLogPo == null){ | ||
return ; | ||
} | ||
String sql = "INSERT INTO `lts_job_log_po` (`timestamp`, `log_type`, `success`, `msg`" + | ||
", `code`, `task_tracker_identity`, `level`, `task_id`, `job_id`" + | ||
", `priority`, `submit_node_group`, `task_tracker_node_group`, `ext_params`, `needFeedback`" + | ||
", `cron_expression`, `trigger_time`)" + | ||
"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; | ||
|
||
try { | ||
getSqlTemplate().update(sql, | ||
jobLogPo.getTimestamp(), | ||
jobLogPo.getLogType().name(), | ||
jobLogPo.isSuccess(), | ||
jobLogPo.getMsg(), | ||
jobLogPo.getCode(), | ||
jobLogPo.getTaskTrackerIdentity(), | ||
jobLogPo.getLevel().name(), | ||
jobLogPo.getTaskId(), | ||
jobLogPo.getJobId(), | ||
jobLogPo.getPriority(), | ||
jobLogPo.getSubmitNodeGroup(), | ||
jobLogPo.getTaskTrackerNodeGroup(), | ||
JSONUtils.toJSONString(jobLogPo.getExtParams()), | ||
jobLogPo.isNeedFeedback(), | ||
jobLogPo.getCronExpression(), | ||
jobLogPo.getTriggerTime() | ||
); | ||
} catch (SQLException e) { | ||
throw new JobLogException(e.getMessage(), e); | ||
} | ||
} | ||
|
||
@Override | ||
public void log(BizLogPo bizLogPo) { | ||
log(JobLogUtils.bizConvert(bizLogPo)); | ||
} | ||
|
||
private void doCreateTable() { | ||
// 创建表 | ||
try { | ||
InputStream is = this.getClass().getClassLoader().getResourceAsStream("sql/lts_job_log_po.sql"); | ||
getSqlTemplate().update(FileUtils.read(is)); | ||
} catch (SQLException e) { | ||
throw new RuntimeException("create table error!", e); | ||
} catch (IOException e) { | ||
throw new RuntimeException("create table error!", e); | ||
} | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
...er/job-logger-mysql/src/main/java/com/lts/job/biz/logger/mysql/MysqlJobLoggerFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.lts.job.biz.logger.mysql; | ||
|
||
import com.lts.job.biz.logger.JobLogger; | ||
import com.lts.job.biz.logger.JobLoggerFactory; | ||
import com.lts.job.core.cluster.Config; | ||
|
||
/** | ||
* @author Robert HG ([email protected]) on 5/21/15. | ||
*/ | ||
public class MysqlJobLoggerFactory implements JobLoggerFactory { | ||
@Override | ||
public JobLogger getJobLogger(Config config) { | ||
return new MysqlJobLogger(config); | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
...er-mysql/src/main/resources/META-INF/lts/internal/com.lts.job.biz.logger.JobLoggerFactory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
mysql=com.lts.job.biz.logger.mysql.MysqlJobLoggerFactory |
21 changes: 21 additions & 0 deletions
21
job-logger/job-logger-mysql/src/main/resources/sql/lts_job_log_po.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
CREATE TABLE IF NOT EXISTS `lts_job_log_po` ( | ||
`id` int(11) unsigned NOT NULL AUTO_INCREMENT, | ||
`timestamp` bigint(20) DEFAULT NULL COMMENT '日志记录时间', | ||
`log_type` varchar(32) DEFAULT NULL COMMENT '日志类型', | ||
`success` tinyint(11) DEFAULT NULL COMMENT '成功与否', | ||
`msg` text COMMENT '消息', | ||
`code` varchar(32) DEFAULT NULL COMMENT '消息编码', | ||
`task_tracker_identity` varchar(64) DEFAULT NULL COMMENT '执行节点唯一标识', | ||
`level` varchar(32) DEFAULT NULL COMMENT '日志记录级别', | ||
`task_id` varchar(64) DEFAULT NULL COMMENT '客户端ID', | ||
`job_id` varchar(64) DEFAULT '' COMMENT '服务端生成ID', | ||
`priority` int(11) DEFAULT NULL COMMENT '优先级', | ||
`submit_node_group` varchar(64) DEFAULT NULL COMMENT '提交节点group', | ||
`task_tracker_node_group` varchar(64) DEFAULT NULL COMMENT '执行节点group', | ||
`ext_params` text COMMENT '额外参数', | ||
`needFeedback` tinyint(4) DEFAULT NULL COMMENT '是否需要反馈', | ||
`cron_expression` varchar(32) DEFAULT NULL COMMENT 'cron表达式', | ||
`trigger_time` bigint(20) DEFAULT NULL COMMENT '触发时间', | ||
PRIMARY KEY (`id`), | ||
KEY `timestamp` (`timestamp`) | ||
) ENGINE=InnoDB DEFAULT CHARSET=utf8; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.