Commit eeed2859 authored by liujiahua123123's avatar liujiahua123123

task

parent 9e3d1209
...@@ -33,7 +33,6 @@ public final class MiraiMain { ...@@ -33,7 +33,6 @@ public final class MiraiMain {
}) })
.setValidWhile((a) -> true); .setValidWhile((a) -> true);
server.getEventManager() server.getEventManager()
.onEvent(ServerDisableEvent.class) .onEvent(ServerDisableEvent.class)
.setHandler(a -> { .setHandler(a -> {
......
...@@ -2,7 +2,6 @@ package net.mamoe.mirai; ...@@ -2,7 +2,6 @@ package net.mamoe.mirai;
import lombok.Getter; import lombok.Getter;
import net.mamoe.mirai.event.MiraiEventManager; 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.ServerDisableEvent;
import net.mamoe.mirai.event.events.server.ServerEnableEvent; import net.mamoe.mirai.event.events.server.ServerEnableEvent;
import net.mamoe.mirai.network.Network; import net.mamoe.mirai.network.Network;
...@@ -45,7 +44,7 @@ public class MiraiServer { ...@@ -45,7 +44,7 @@ public class MiraiServer {
this.taskManager = MiraiTaskManager.getInstance(); this.taskManager = MiraiTaskManager.getInstance();
try { try {
Network.start(Network.getAvaliablePort()); Network.start(Network.getAvailablePort());
} catch (InterruptedException | IOException e) { } catch (InterruptedException | IOException e) {
e.printStackTrace(); e.printStackTrace();
this.shutdown(); this.shutdown();
......
...@@ -60,7 +60,7 @@ public final class Network { ...@@ -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); //读取空闲的可用端口 ServerSocket serverSocket = new ServerSocket(0); //读取空闲的可用端口
int port = serverSocket.getLocalPort(); int port = serverSocket.getLocalPort();
serverSocket.close(); serverSocket.close();
......
...@@ -17,4 +17,7 @@ public final class PacketUtil { ...@@ -17,4 +17,7 @@ public final class PacketUtil {
} }
return init & 2147483647; return init & 2147483647;
} }
} }
package net.mamoe.mirai.task; 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 { public class MiraiTaskManager {
private static MiraiTaskManager instance; private static MiraiTaskManager instance;
...@@ -12,9 +22,118 @@ public class MiraiTaskManager { ...@@ -12,9 +22,118 @@ public class MiraiTaskManager {
return MiraiTaskManager.instance; return MiraiTaskManager.instance;
} }
private MiraiTaskPool pool; 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());
}
private MiraiTaskManager(){ 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();
}
} }
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);
}
}
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();
}
}
}
...@@ -3,4 +3,8 @@ package net.mamoe.mirai.task; ...@@ -3,4 +3,8 @@ package net.mamoe.mirai.task;
@FunctionalInterface @FunctionalInterface
public interface MiralTaskExceptionHandler { public interface MiralTaskExceptionHandler {
void onHandle(Throwable e); void onHandle(Throwable e);
static MiralTaskExceptionHandler byDefault(){
return Throwable::printStackTrace;
}
} }
...@@ -3,48 +3,27 @@ package net.mamoe.mirai.utils; ...@@ -3,48 +3,27 @@ package net.mamoe.mirai.utils;
public class EventException extends RuntimeException { public class EventException extends RuntimeException {
private final Throwable cause; private final Throwable cause;
/**
* Constructs a new EventException based on the given Exception
*
* @param throwable Exception that triggered this Exception
*/
public EventException(Throwable throwable) { public EventException(Throwable throwable) {
cause = throwable; cause = throwable;
} }
/**
* Constructs a new EventException
*/
public EventException() { public EventException() {
cause = null; 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) { public EventException(Throwable cause, String message) {
super(message); super(message);
this.cause = cause; this.cause = cause;
} }
/**
* Constructs a new EventException with the given message
*
* @param message The message
*/
public EventException(String message) { public EventException(String message) {
super(message); super(message);
cause = null; cause = null;
} }
/**
* If applicable, returns the Exception that triggered this Exception
*
* @return Inner exception, or null if one does not exist
*/
@Override @Override
public Throwable getCause() { public Throwable getCause() {
return cause; return cause;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment