引入mongodb,使用队列+单独线程记录访问日志
This commit is contained in:
@@ -20,6 +20,7 @@ import org.redkale.util.AnyValue;
|
||||
import javax.annotation.Resource;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
|
||||
import static com.lxyer.bbs.base.kit.RetCodes.RET_USER_UNLOGIN;
|
||||
|
||||
@@ -53,6 +54,9 @@ public class BaseServlet extends HttpServlet {
|
||||
@Resource
|
||||
protected CommentService commentService;
|
||||
|
||||
@Resource
|
||||
protected TaskQueue<Map> logQueue;
|
||||
|
||||
|
||||
@Override
|
||||
public void init(HttpContext context, AnyValue config) {
|
||||
@@ -87,6 +91,24 @@ public class BaseServlet extends HttpServlet {
|
||||
return;
|
||||
}
|
||||
|
||||
Kv visLog = new Kv();//ip、time、userid、uri、para
|
||||
visLog.set("ip", request.getRemoteAddr());
|
||||
request.getHost();
|
||||
visLog.set("time", System.currentTimeMillis());
|
||||
visLog.set("userid", currentid);
|
||||
visLog.set("uri", request.getRequestURI());
|
||||
|
||||
Kv para = Kv.create();
|
||||
for (String key : request.getParameterNames()){
|
||||
para.set(key, request.getParameter(key));
|
||||
}
|
||||
visLog.set("para", para);
|
||||
try {
|
||||
logQueue.put(visLog);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
|
||||
/*if ("/".equals(uri)){
|
||||
finish("/front/index.html", Kv.by("",""));
|
||||
|
69
src/com/lxyer/bbs/base/TaskQueue.java
Normal file
69
src/com/lxyer/bbs/base/TaskQueue.java
Normal file
@@ -0,0 +1,69 @@
|
||||
package com.lxyer.bbs.base;
|
||||
|
||||
import com.mongodb.MongoClient;
|
||||
import com.mongodb.client.MongoCollection;
|
||||
import com.mongodb.client.MongoDatabase;
|
||||
import org.bson.Document;
|
||||
import org.redkale.util.AnyValue;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.LinkedBlockingQueue;
|
||||
|
||||
/**
|
||||
* Created by liangxianyou at 2018/6/20 22:54.
|
||||
*/
|
||||
public class TaskQueue<T extends Object> extends BaseService implements Runnable {
|
||||
|
||||
@Resource(name = "property.mongo.host")
|
||||
private String mongoHost;
|
||||
|
||||
protected static LinkedBlockingQueue queue = new LinkedBlockingQueue();
|
||||
|
||||
private static MongoClient mongoClient;
|
||||
private static MongoDatabase redbbs;
|
||||
private static MongoCollection<Document> visLog;
|
||||
|
||||
public TaskQueue() {
|
||||
new Thread(this).start();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void init(AnyValue config) {
|
||||
mongoClient = new MongoClient(mongoHost, 27017);
|
||||
redbbs = mongoClient.getDatabase(winos ? "redbbs_dev": "redbbs");
|
||||
visLog = redbbs.getCollection("vis_log");
|
||||
}
|
||||
|
||||
public T take() throws InterruptedException {
|
||||
return (T) queue.take();
|
||||
}
|
||||
|
||||
public void put(T t) throws InterruptedException {
|
||||
queue.put(t);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
while (true){
|
||||
Map take = (Map) take();
|
||||
|
||||
take.put("ftime", String.format("%1$tY%1$tm%1$td%1$tH%1$tM", take.get("time")));
|
||||
visLog.insertOne(new Document(take));
|
||||
|
||||
//在这里处理日志数据[访问量]
|
||||
}
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/*public static void main(String[] args) {
|
||||
//测试mongodb 连通性
|
||||
FindIterable<Document> documents = visLog.find().limit(10);
|
||||
documents.forEach((Block<? super Document>) x->{
|
||||
System.out.println(x);
|
||||
});
|
||||
}*/
|
||||
}
|
Reference in New Issue
Block a user