我有一个系统,要将本系统的操作日志推送给别人的系统,这个对接有什么设计思路吗?我的想法是生成一个文件让对方读取,但是日志推送是用线程定时读的,还有更正的地方或者实现思路吗?请大佬帮我解答一下,十分感激.现在输出是写死的 需要输出到别的系统
public class NoticesThread extends Thread
{
private String sql = " select t.id, "+
" t.content, "+
" t.log_type, "+
" u.name as userName, "+
" d.name as departName "+
" from ry_gis_opr_log t "+
" left join cfg_user u "+
" on t.operator_fk = u.id "+
" left join ry_gis_department d "+
" on t.belongto_fk = d.id "+
" where t.log_type = '上传' "+
" and t.is_fail != 1 " +
" and t.belongto_fk != 0" +
" and t.id > ? " +
" order by t.id desc ";
boolean isStart = true;
Long startLogId = 0l;
public void run() {
Properties prop = new Properties();
try {
InputStream in = new NoticesThread().getClass().getResourceAsStream("/config/jdbc.properties");
prop.load(in);
} catch (IOException e) {
e.printStackTrace();
}
final String driverName = prop.getProperty("jdbc.driverClassName");
final String url = prop.getProperty("jdbc.url");
final String oracleUser = prop.getProperty("jdbc.username");
final String password = prop.getProperty("jdbc.password");
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
Connection conn = null;
PreparedStatement st = null;
try {
Class.forName(driverName);
conn = DriverManager.getConnection(url, oracleUser, password);
st = conn.prepareStatement(sql, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
st.setLong(1, startLogId);
ResultSet rs = st.executeQuery();
if(!isStart){
if(rs.first()) {
startLogId = rs.getLong(1);
String content = rs.getString(2);
String userName = rs.getString(4);
String departName = rs.getString(5);
System.out.println("---logId:" + startLogId + "---content:"+content +"----userName:" + userName +"---departName:" + departName);
}
while(rs.next()){
Long logId = rs.getLong(1);
String content = rs.getString(2);
String userName = rs.getString(4);
String departName = rs.getString(5);
System.out.println("---logId:" + logId + "---content:"+content +"----userName:" + userName +"---departName:" + departName);
}
} else {
isStart = false;
if(rs.first()) {
startLogId = rs.getLong(1);
}
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if(conn != null) {
conn.close();
}
if(st != null) {
st.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}, 0, 5000);
}
}
日志可以邮件发送,可以写入数据库,都有相应实现,不用自己写代码,配置一下就ok,不冲突,而且可以多个日志输出终点
关键是看对面提供什么样的对接方式?如果是接口你可以在直接在使用Listener或者AOP直接在调用你项目相关service层时后台发送到对方接口,如果是数据库你可以考虑下DBlink等。
如果是两家不同的公司,自己写就好了。这个还是很简单的。
1.如果是推送的话,就写个aop 拦截到日志后直接http 调用接口推送给对方。
2.如果是拉取的话,可以自己存到db里面或者其他存储中都可以。每条日志生成一个id,然后对方第一次拉取给id=0,之后给以拉取的最大id来拉取更多的日志即可哈。
如果是一家公司,两个不同的系统可以使用消息中间件发送消息给对方。类似kafka之类的。