-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
63 changed files
with
755 additions
and
230 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
powerjob-common/src/main/java/tech/powerjob/common/enhance/SafeRunnable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package tech.powerjob.common.enhance; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
|
||
import java.util.concurrent.ScheduledExecutorService; | ||
|
||
/** | ||
* 安全的 runnable,可防止因抛出异常导致周期性任务终止 | ||
* 使用 {@link ScheduledExecutorService} 执行任务时,推荐继承此类捕获并打印异常,避免因为抛出异常导致周期性任务终止 | ||
* | ||
* @author songyinyin | ||
* @since 2023/9/20 15:52 | ||
*/ | ||
@Slf4j | ||
public abstract class SafeRunnable implements Runnable{ | ||
@Override | ||
public void run() { | ||
try { | ||
run0(); | ||
} catch (Exception e) { | ||
log.error("[SafeRunnable] run failed", e); | ||
} | ||
} | ||
|
||
protected abstract void run0(); | ||
} |
30 changes: 30 additions & 0 deletions
30
powerjob-common/src/main/java/tech/powerjob/common/enhance/SafeRunnableWrapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package tech.powerjob.common.enhance; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
|
||
import java.util.concurrent.ScheduledExecutorService; | ||
|
||
/** | ||
* 使用 {@link ScheduledExecutorService} 执行任务时,推荐使用此对象包装一层,避免因为抛出异常导致周期性任务终止 | ||
* | ||
* @author songyinyin | ||
* @since 2023/9/20 16:04 | ||
*/ | ||
@Slf4j | ||
public class SafeRunnableWrapper implements Runnable { | ||
|
||
private final Runnable runnable; | ||
|
||
public SafeRunnableWrapper(Runnable runnable) { | ||
this.runnable = runnable; | ||
} | ||
|
||
@Override | ||
public void run() { | ||
try { | ||
runnable.run(); | ||
} catch (Exception e) { | ||
log.error("[SafeRunnableWrapper] run failed", e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
powerjob-common/src/main/java/tech/powerjob/common/utils/net/PingPongServer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package tech.powerjob.common.utils.net; | ||
|
||
import java.io.Closeable; | ||
|
||
/** | ||
* socket 服务器,用于进行连通性测试 | ||
* | ||
* @author tjq | ||
* @since 2024/2/8 | ||
*/ | ||
public interface PingPongServer extends Closeable { | ||
|
||
void initialize(int port) throws Exception; | ||
} |
57 changes: 57 additions & 0 deletions
57
powerjob-common/src/main/java/tech/powerjob/common/utils/net/PingPongSocketServer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package tech.powerjob.common.utils.net; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
import tech.powerjob.common.utils.CommonUtils; | ||
|
||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
import java.net.ServerSocket; | ||
import java.net.Socket; | ||
import java.nio.charset.StandardCharsets; | ||
|
||
/** | ||
* 简易服务器 | ||
* | ||
* @author tjq | ||
* @since 2024/2/8 | ||
*/ | ||
@Slf4j | ||
public class PingPongSocketServer implements PingPongServer { | ||
|
||
private Thread thread; | ||
|
||
private ServerSocket serverSocket; | ||
|
||
private volatile boolean terminated = false; | ||
|
||
@Override | ||
public void initialize(int port) throws Exception{ | ||
serverSocket = new ServerSocket(port); | ||
|
||
thread = new Thread(() -> { | ||
while (true) { | ||
if (terminated) { | ||
return; | ||
} | ||
// 接收连接,如果没有连接,accept() 方法会阻塞 | ||
try (Socket socket = serverSocket.accept();OutputStream outputStream = socket.getOutputStream();) { | ||
outputStream.write(PingPongUtils.PONG.getBytes(StandardCharsets.UTF_8)); | ||
outputStream.flush(); | ||
} catch (Exception e) { | ||
if (!terminated) { | ||
log.warn("[PingPongSocketServer] process accepted socket failed!", e); | ||
} | ||
} | ||
} | ||
}, "PingPongSocketServer-Thread"); | ||
|
||
thread.start(); | ||
} | ||
|
||
@Override | ||
public void close() throws IOException { | ||
terminated = true; | ||
CommonUtils.executeIgnoreException(() -> serverSocket.close()); | ||
thread.interrupt(); | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
powerjob-common/src/main/java/tech/powerjob/common/utils/net/PingPongUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package tech.powerjob.common.utils.net; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
import org.apache.commons.lang3.exception.ExceptionUtils; | ||
|
||
import java.io.*; | ||
import java.net.Socket; | ||
import java.net.UnknownHostException; | ||
import java.nio.charset.StandardCharsets; | ||
|
||
/** | ||
* socket 连通性助手 | ||
* | ||
* @author tjq | ||
* @since 2024/2/8 | ||
*/ | ||
@Slf4j | ||
public class PingPongUtils { | ||
|
||
static final String PING = "ping"; | ||
static final String PONG = "pong"; | ||
|
||
/** | ||
* 验证目标 IP 和 端口的连通性 | ||
* @param targetIp 目标 IP | ||
* @param targetPort 目标端口 | ||
* @return true or false | ||
*/ | ||
public static boolean checkConnectivity(String targetIp, int targetPort) { | ||
|
||
try (Socket s = new Socket(targetIp, targetPort);InputStream is = s.getInputStream();OutputStream os = s.getOutputStream();BufferedReader br = new BufferedReader(new InputStreamReader(is))) { | ||
|
||
// 发送 PING 请求 | ||
os.write(PING.getBytes(StandardCharsets.UTF_8)); | ||
os.flush(); | ||
|
||
//读取服务器返回的消息 | ||
String content = br.readLine(); | ||
|
||
if (PONG.equalsIgnoreCase(content)) { | ||
return true; | ||
} | ||
} catch (UnknownHostException e) { | ||
log.warn("[SocketConnectivityUtils] unknown host: {}:{}", targetIp, targetPort); | ||
} catch (IOException e) { | ||
log.warn("[SocketConnectivityUtils] IOException: {}:{}, msg: {}", targetIp, targetPort, ExceptionUtils.getMessage(e)); | ||
} catch (Exception e) { | ||
log.error("[SocketConnectivityUtils] unknown exception for check ip: {}:{}", targetIp, targetPort, e); | ||
} | ||
|
||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.