This commit is contained in:
liujiahua123123 2019-08-09 14:13:35 +08:00
parent 9e3d1209cf
commit eeed28598e
9 changed files with 163 additions and 59 deletions

View File

@ -33,7 +33,6 @@ public final class MiraiMain {
})
.setValidWhile((a) -> true);
server.getEventManager()
.onEvent(ServerDisableEvent.class)
.setHandler(a -> {

View File

@ -2,7 +2,6 @@ package net.mamoe.mirai;
import lombok.Getter;
import net.mamoe.mirai.event.MiraiEventManager;
import net.mamoe.mirai.event.events.MiraiEvent;
import net.mamoe.mirai.event.events.server.ServerDisableEvent;
import net.mamoe.mirai.event.events.server.ServerEnableEvent;
import net.mamoe.mirai.network.Network;
@ -45,7 +44,7 @@ public class MiraiServer {
this.taskManager = MiraiTaskManager.getInstance();
try {
Network.start(Network.getAvaliablePort());
Network.start(Network.getAvailablePort());
} catch (InterruptedException | IOException e) {
e.printStackTrace();
this.shutdown();

View File

@ -60,7 +60,7 @@ public final class Network {
}
public static int getAvaliablePort() throws IOException {
public static int getAvailablePort() throws IOException {
ServerSocket serverSocket = new ServerSocket(0); //读取空闲的可用端口
int port = serverSocket.getLocalPort();
serverSocket.close();

View File

@ -17,4 +17,7 @@ public final class PacketUtil {
}
return init & 2147483647;
}
}

View File

@ -1,6 +1,16 @@
package net.mamoe.mirai.task;
import lombok.Getter;
import net.mamoe.mirai.MiraiServer;
import net.mamoe.mirai.event.events.server.ServerDisableEvent;
import java.util.concurrent.Callable;
import java.util.concurrent.Future;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Consumer;
import java.util.function.Predicate;
public class MiraiTaskManager {
private static MiraiTaskManager instance;
@ -12,9 +22,118 @@ public class MiraiTaskManager {
return MiraiTaskManager.instance;
}
private MiraiTaskPool pool;
private MiraiTaskManager(){
private MiraiThreadPool pool;
private MiraiTaskManager() {
this.pool = new MiraiThreadPool();
MiraiServer.getInstance().getEventManager()
.onEvent(ServerDisableEvent.class)
.setHandler(a -> this.pool.close());
}
/**
基础Future处理
*/
public void execute(Runnable runnable){
this.execute(runnable,MiralTaskExceptionHandler.byDefault());
}
public void execute(Runnable runnable, MiralTaskExceptionHandler handler){
this.pool.execute(() ->
{
try{
runnable.run();
}catch (Exception e){
handler.onHandle(e);
}
});
}
public <D> Future<D> submit(Callable<D> callable) {
return this.submit(callable, MiralTaskExceptionHandler.byDefault());
}
public <D> Future<D> submit(Callable<D> callable, MiralTaskExceptionHandler handler) {
return this.pool.submit(() -> {
try {
return callable.call();
} catch (Throwable e) {
handler.onHandle(e);
return null;
}
});
}
/**
异步任务
*/
public <D> void ansycTask(Callable<D> callable, Consumer<D> callback){
this.ansycTask(callable,callback,MiralTaskExceptionHandler.byDefault());
}
public <D> void ansycTask(Callable<D> callable, Consumer<D> callback, MiralTaskExceptionHandler handler){
this.pool.execute(() -> {
try {
callback.accept(callable.call());
} catch (Throwable e) {
handler.onHandle(e);
}
});
}
/**
定时任务
*/
public void repeatingTask(Runnable runnable, long interval){
this.repeatingTask(runnable,interval, MiralTaskExceptionHandler.byDefault());
}
public void repeatingTask(Runnable runnable, long interval, MiralTaskExceptionHandler handler){
this.repeatingTask(runnable,interval,a -> true,handler);
}
public void repeatingTask(Runnable runnable, long interval, int times){
this.repeatingTask(runnable,interval,times,MiralTaskExceptionHandler.byDefault());
}
public void repeatingTask(Runnable runnable, long interval, int times, MiralTaskExceptionHandler handler){
AtomicInteger integer = new AtomicInteger(times);
this.repeatingTask(
runnable,interval, a -> integer.getAndDecrement() > 0, handler
);
}
public <D extends Runnable> void repeatingTask(D runnable, long interval, Predicate<D> shouldContinue, MiralTaskExceptionHandler handler){
new Thread(() -> {
do {
this.pool.execute(() -> {
try {
runnable.run();
} catch (Exception e) {
handler.onHandle(e);
}
});
try {
Thread.sleep(interval);
} catch (InterruptedException e) {
e.printStackTrace();
}
} while (shouldContinue.test(runnable));
}).start();
}
public void deleteTask(Runnable runnable, long interval){
new Thread(() -> {
try{
Thread.sleep(interval);
} catch (InterruptedException e) {
e.printStackTrace();
}
this.pool.execute(runnable);
}).start();
}
}

View File

@ -1,28 +0,0 @@
package net.mamoe.mirai.task;
import java.util.concurrent.*;
public class MiraiTaskPool {
ExecutorService service;
protected MiraiTaskPool(){
this.service = Executors.newCachedThreadPool();
}
public <D> Future<D> submit(Callable<D> callable, MiralTaskExceptionHandler handler) {
return this.service.submit(() -> {
try {
return callable.call();
} catch (Throwable e) {
handler.onHandle(e);
return null;
}
});
}
public <D> Future<D> submit(Callable<D> callable) {
return this.submit(callable, Throwable::printStackTrace);
}
}

View File

@ -0,0 +1,29 @@
package net.mamoe.mirai.task;
import java.io.Closeable;
import java.io.IOException;
import java.util.concurrent.*;
import java.util.function.Consumer;
public class MiraiThreadPool extends ThreadPoolExecutor implements Closeable {
protected MiraiThreadPool(){
super(0,
Integer.MAX_VALUE,
60L,
TimeUnit.SECONDS,
new SynchronousQueue<>()
);
}
@Override
public void close(){
this.shutdown();
if(!this.isShutdown()){
this.shutdownNow();
}
}
}

View File

@ -3,4 +3,8 @@ package net.mamoe.mirai.task;
@FunctionalInterface
public interface MiralTaskExceptionHandler {
void onHandle(Throwable e);
static MiralTaskExceptionHandler byDefault(){
return Throwable::printStackTrace;
}
}

View File

@ -3,48 +3,27 @@ package net.mamoe.mirai.utils;
public class EventException extends RuntimeException {
private final Throwable cause;
/**
* Constructs a new EventException based on the given Exception
*
* @param throwable Exception that triggered this Exception
*/
public EventException(Throwable throwable) {
cause = throwable;
}
/**
* Constructs a new EventException
*/
public EventException() {
cause = null;
}
/**
* Constructs a new EventException with the given message
*
* @param cause The exception that caused this
* @param message The message
*/
public EventException(Throwable cause, String message) {
super(message);
this.cause = cause;
}
/**
* Constructs a new EventException with the given message
*
* @param message The message
*/
public EventException(String message) {
super(message);
cause = null;
}
/**
* If applicable, returns the Exception that triggered this Exception
*
* @return Inner exception, or null if one does not exist
*/
@Override
public Throwable getCause() {
return cause;