欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

mongodb 写日志。

程序员文章站 2024-03-02 14:41:40
...

1.写日志任务

package com.kenick.util;

import com.mongodb.MongoClient;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;

import java.util.ArrayList;
import java.util.List;

public class MongoWriteLogRunnable implements Runnable {

    @Override
    public void run() {
        // 写日志任务
        MongoDatabase databaseConn = getMongoDatabaseConn();
        writeLog(databaseConn);
    }

    private String username;
    private String pwd;
    private String msg;
    private String dbName;
    private static MongoDatabase mongoDatabase;

    public MongoWriteLogRunnable(String username, String pwd, String dbName, String msg){
        this.username = username;
        this.pwd = pwd;
        this.dbName = dbName;
        this.msg = msg;
    }

    // 获取mongodb数据库连接 注意多线程环境
    private MongoDatabase getMongoDatabaseConn(){
        if(mongoDatabase == null){
            synchronized (MongoWriteLogRunnable.class){
                if(mongoDatabase == null){
                    ServerAddress serverAddress = new ServerAddress("localhost", 27017);
                    List<ServerAddress> serverList = new ArrayList<>();
                    serverList.add(serverAddress);

                    MongoCredential credential = MongoCredential.createCredential(username, "admin", pwd.toCharArray());
                    ArrayList<MongoCredential> mongoCredentialList = new ArrayList<>();
                    mongoCredentialList.add(credential);

                    MongoClient mongoClient = new MongoClient(serverList, mongoCredentialList);
                    mongoDatabase = mongoClient.getDatabase(dbName);
                }
            }
        }
        return mongoDatabase;
    }

    // 往mongodb中写入日志
    private void writeLog(MongoDatabase mongoDatabase){
        try {
            MongoCollection<Document> testLogCollection = mongoDatabase.getCollection("testLog");
            testLogCollection.insertOne(new Document("msg",msg));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

2.写日志工具类

package com.kenick.util;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class MongoLogUtil {
    private static ExecutorService executorService = new ThreadPoolExecutor(0,50,
            60L, TimeUnit.SECONDS,new SynchronousQueue<Runnable>());

    // 使用线程池,避免频繁创建线程,关闭线程,浪费资源
    public static void writeLog(String username,String pwd,String dbName,String msg){
        try {
            MongoWriteLogRunnable mongoWriteLogRunnable = new MongoWriteLogRunnable(username, pwd, dbName, msg);
            executorService.execute(mongoWriteLogRunnable);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // 测试
    public static void main(String[] args) {
        Thread thread1 = new Thread(new Runnable() {
            @Override
            public void run() {
                String threadName = Thread.currentThread().getName()+"_"+System.currentTimeMillis();
                for(int i=0;i<10;i++){
                    MongoLogUtil.writeLog("kenick", "kenick.com","tutorial",threadName);
                }
            }
        },"writeLogThread1");

        Thread thread2 = new Thread(new Runnable() {
            @Override
            public void run() {
                String threadName = Thread.currentThread().getName()+"_"+System.currentTimeMillis();
                for(int i=0;i<10;i++){
                    MongoLogUtil.writeLog("kenick", "kenick.com", "tutorial", threadName);
                }
            }
        },"writeLogThread2");

        thread1.start();
        thread2.start();
    }
}

mongodb 写日志。