feat: 使用 guava 的 EventBus

This commit is contained in:
2023-11-11 18:39:20 +08:00
parent a5c2eec5e5
commit d70841167f
6 changed files with 119 additions and 93 deletions

View File

@@ -77,7 +77,7 @@ public abstract class BallAPI {
protected void enable() throws SQLException, InterruptedException {
BallServerInfo localInfo = getLocalServerInfo();
try (Connection connection = BallAPI.getInstance().getDatasource().getConnection()) {
try (Connection connection = getDatasource().getConnection()) {
try (Statement statement = connection.createStatement()) {
statement.execute("CREATE TABLE IF NOT EXISTS `hamster_ball_player_info`(" +
"`uuid` CHAR(36) PRIMARY KEY," +
@@ -142,13 +142,13 @@ public abstract class BallAPI {
}
RedisClient client = getRedisClient();
subConnection.addListener(BallRedisListener.INSTANCE);
subConnection.async().subscribe(BALL_CHANNEL);
subscribe(BALL_CHANNEL);
}
protected void disable() throws SQLException, InterruptedException {
sendBallMessage(new BallMessage(BALL_CHANNEL, BallActions.ServerOffline.name(), new ServerOfflineEvent(getLocalServerId())), true);
try (Connection connection = BallAPI.getInstance().getDatasource().getConnection()) {
try (Connection connection = getDatasource().getConnection()) {
try (PreparedStatement statement = connection.prepareStatement(
"DELETE FROM `hamster_ball_server_info` WHERE `id`=?;"
)) {
@@ -309,7 +309,7 @@ public abstract class BallAPI {
if (info != null && info.isOnline()) {
continue;
}
try (Connection connection = BallAPI.getInstance().getDatasource().getConnection()) {
try (Connection connection = getDatasource().getConnection()) {
try (PreparedStatement statement = connection.prepareStatement(
"INSERT INTO `hamster_ball_cached_message` VALUES(?, ?);"
)) {
@@ -447,6 +447,22 @@ public abstract class BallAPI {
}
}
public void subscribe(@NotNull String... channel) {
subConnection.sync().subscribe(channel);
}
public void subscribePatterns(@NotNull String patterns) {
subConnection.sync().psubscribe(patterns);
}
public void unsubscribe(@NotNull String... channel) {
subConnection.sync().unsubscribe(channel);
}
public void unsubscribePatterns(@NotNull String patterns) {
subConnection.sync().punsubscribe(patterns);
}
/**
* 获取本地服务器ID
*