mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-08 09:38:26 +08:00
!353 feat:新增当审计消息达到一定数量时,发送审计消息的收集器。
Merge pull request !353 from 王帅/main
This commit is contained in:
commit
5eadf0e31b
@ -0,0 +1,77 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
|
||||||
|
* <p>
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
* <p>
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* <p>
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.mybatisflex.core.audit;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.concurrent.locks.ReentrantReadWriteLock;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 抽象消息收集器。
|
||||||
|
*
|
||||||
|
* @author 王帅
|
||||||
|
* @since 2023-09-28
|
||||||
|
*/
|
||||||
|
public abstract class AbstractMessageCollector implements MessageCollector {
|
||||||
|
|
||||||
|
private final MessageReporter messageSender;
|
||||||
|
private final List<AuditMessage> messages = Collections.synchronizedList(new ArrayList<>());
|
||||||
|
private final ReentrantReadWriteLock rrwLock = new ReentrantReadWriteLock();
|
||||||
|
|
||||||
|
protected AbstractMessageCollector(MessageReporter messageSender) {
|
||||||
|
this.messageSender = messageSender;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void collect(AuditMessage message) {
|
||||||
|
try {
|
||||||
|
rrwLock.readLock().lock();
|
||||||
|
messages.add(message);
|
||||||
|
} finally {
|
||||||
|
rrwLock.readLock().unlock();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void doSendMessages() {
|
||||||
|
if (messages.isEmpty()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
List<AuditMessage> sendMessages;
|
||||||
|
try {
|
||||||
|
rrwLock.writeLock().lock();
|
||||||
|
sendMessages = new ArrayList<>(messages);
|
||||||
|
messages.clear();
|
||||||
|
} finally {
|
||||||
|
rrwLock.writeLock().unlock();
|
||||||
|
}
|
||||||
|
messageSender.sendMessages(sendMessages);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void release() {
|
||||||
|
doSendMessages();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected List<AuditMessage> getMessages() {
|
||||||
|
return messages;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected MessageReporter getMessageSender() {
|
||||||
|
return messageSender;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,45 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
|
||||||
|
* <p>
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
* <p>
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* <p>
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
package com.mybatisflex.core.audit;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 计数消息收集器,当消息达到指定数量时发送消息。
|
||||||
|
*
|
||||||
|
* @author 王帅
|
||||||
|
* @since 2023-09-28
|
||||||
|
*/
|
||||||
|
public class CountableMessageCollector extends AbstractMessageCollector {
|
||||||
|
|
||||||
|
private final int count;
|
||||||
|
|
||||||
|
public CountableMessageCollector() {
|
||||||
|
this(1000, new ConsoleMessageReporter());
|
||||||
|
}
|
||||||
|
|
||||||
|
public CountableMessageCollector(int count, MessageReporter messageSender) {
|
||||||
|
super(messageSender);
|
||||||
|
this.count = count;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void collect(AuditMessage message) {
|
||||||
|
super.collect(message);
|
||||||
|
if (getMessages().size() >= count) {
|
||||||
|
new Thread(this::doSendMessages).start();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -15,24 +15,16 @@
|
|||||||
*/
|
*/
|
||||||
package com.mybatisflex.core.audit;
|
package com.mybatisflex.core.audit;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.concurrent.Executors;
|
import java.util.concurrent.Executors;
|
||||||
import java.util.concurrent.ScheduledExecutorService;
|
import java.util.concurrent.ScheduledExecutorService;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
import java.util.concurrent.locks.ReentrantReadWriteLock;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 默认的审计消息收集器,其收集消息后,定时通过消息发送器{@link MessageReporter}把消息发送过去
|
* 默认的审计消息收集器,其收集消息后,定时通过消息发送器{@link MessageReporter}把消息发送过去
|
||||||
*/
|
*/
|
||||||
public class ScheduledMessageCollector implements MessageCollector, Runnable {
|
public class ScheduledMessageCollector extends AbstractMessageCollector {
|
||||||
|
|
||||||
private final ScheduledExecutorService scheduler;
|
private final ScheduledExecutorService scheduler;
|
||||||
private final MessageReporter messageSender;
|
|
||||||
|
|
||||||
private final List<AuditMessage> messages = Collections.synchronizedList(new ArrayList<>());
|
|
||||||
private final ReentrantReadWriteLock rrwLock = new ReentrantReadWriteLock();
|
|
||||||
|
|
||||||
public ScheduledMessageCollector() {
|
public ScheduledMessageCollector() {
|
||||||
this(10, new ConsoleMessageReporter());
|
this(10, new ConsoleMessageReporter());
|
||||||
@ -40,45 +32,17 @@ public class ScheduledMessageCollector implements MessageCollector, Runnable {
|
|||||||
|
|
||||||
|
|
||||||
public ScheduledMessageCollector(long period, MessageReporter messageSender) {
|
public ScheduledMessageCollector(long period, MessageReporter messageSender) {
|
||||||
this.messageSender = messageSender;
|
super(messageSender);
|
||||||
this.scheduler = Executors.newSingleThreadScheduledExecutor(runnable -> {
|
this.scheduler = Executors.newSingleThreadScheduledExecutor(runnable -> {
|
||||||
Thread thread = new Thread(runnable, "ScheduledMessageCollector");
|
Thread thread = new Thread(runnable, "ScheduledMessageCollector");
|
||||||
thread.setDaemon(true);
|
thread.setDaemon(true);
|
||||||
return thread;
|
return thread;
|
||||||
});
|
});
|
||||||
this.scheduler.scheduleAtFixedRate(this, period, period, TimeUnit.SECONDS);
|
this.scheduler.scheduleAtFixedRate(this::doSendMessages, period, period, TimeUnit.SECONDS);
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void collect(AuditMessage message) {
|
|
||||||
try {
|
|
||||||
rrwLock.readLock().lock();
|
|
||||||
messages.add(message);
|
|
||||||
} finally {
|
|
||||||
rrwLock.readLock().unlock();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
if (messages.isEmpty()) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
List<AuditMessage> sendMessages;
|
|
||||||
try {
|
|
||||||
rrwLock.writeLock().lock();
|
|
||||||
sendMessages = new ArrayList<>(messages);
|
|
||||||
messages.clear();
|
|
||||||
} finally {
|
|
||||||
rrwLock.writeLock().unlock();
|
|
||||||
}
|
|
||||||
messageSender.sendMessages(sendMessages);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void release() {
|
public void release() {
|
||||||
run(); //clear the messages
|
doSendMessages(); //clear the messages
|
||||||
scheduler.shutdown();
|
scheduler.shutdown();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user