diff --git a/src/main/java/com/yutou/biliapi/databases/BiliLiveDatabase.java b/src/main/java/com/yutou/biliapi/databases/BiliLiveDatabase.java index d563d7b..abc192b 100644 --- a/src/main/java/com/yutou/biliapi/databases/BiliLiveDatabase.java +++ b/src/main/java/com/yutou/biliapi/databases/BiliLiveDatabase.java @@ -10,6 +10,7 @@ import com.yutou.common.databases.AbsDatabasesBean; import com.yutou.common.databases.SQLiteManager; import com.yutou.common.okhttp.HttpDownloadUtils; import com.yutou.common.utils.Log; +import lombok.Getter; import java.io.File; import java.sql.PreparedStatement; @@ -20,6 +21,7 @@ import java.util.Date; import java.util.List; public class BiliLiveDatabase extends SQLiteManager { + @Getter LiveRoomConfig config; String fileName; File rootPath; diff --git a/src/main/java/com/yutou/biliapi/net/WebSocketServer.java b/src/main/java/com/yutou/biliapi/net/WebSocketServer.java index 46cc5aa..af32d2e 100644 --- a/src/main/java/com/yutou/biliapi/net/WebSocketServer.java +++ b/src/main/java/com/yutou/biliapi/net/WebSocketServer.java @@ -163,7 +163,6 @@ public class WebSocketServer { private class WebSocketClientTh extends WebSocketClient { private final LiveRoomConfig roomConfig; private final HeartbeatTask heartbeatTask; - BiliLiveDatabase liveDatabase; private final boolean isUser; public WebSocketClientTh(URI serverUri, LiveRoomConfig roomId, boolean isUser) { @@ -171,7 +170,6 @@ public class WebSocketServer { Log.i("WebSocketClientTh.WebSocketClientTh : " + serverUri); this.isUser = isUser; this.roomConfig = roomId; - liveDatabase = liveDatabasesService.getLiveDatabase(roomConfig.getRoomId()); Brotli4jLoader.ensureAvailability(); heartbeatTask = new HeartbeatTask(); addHeader("User-Agent", ConfigTools.getUserAgent()); @@ -253,7 +251,7 @@ public class WebSocketServer { // Log.i("协议:" + useHeader + " 命令数:" + body.getBodyList().size()); for (JSONObject json : body.getBodyList()) { WSData parse = WSData.parse(json); - liveDatabase.addSource(parse); + liveDatabasesService.getLiveDatabase(roomConfig.getRoomId()).addSource(parse); // Log.i("解压:" + parse); } // Log.i(); diff --git a/src/main/java/com/yutou/bilibili/services/LiveDatabasesService.java b/src/main/java/com/yutou/bilibili/services/LiveDatabasesService.java index 86d2f6c..a097c49 100644 --- a/src/main/java/com/yutou/bilibili/services/LiveDatabasesService.java +++ b/src/main/java/com/yutou/bilibili/services/LiveDatabasesService.java @@ -6,6 +6,7 @@ import com.yutou.biliapi.bean.live.LiveRoomConfig; import com.yutou.biliapi.bean.live.database.LiveConfigDatabaseBean; import com.yutou.biliapi.databases.BiliLiveConfigDatabase; import com.yutou.biliapi.databases.BiliLiveDatabase; +import com.yutou.common.utils.Log; import lombok.Getter; import org.springframework.stereotype.Service; import org.springframework.util.StringUtils; @@ -24,6 +25,7 @@ public class LiveDatabasesService { if (it.wasEvicted()) { if (it.getValue() != null) { ((BiliLiveDatabase) it.getValue()).close(); + Log.i("缓存到期移除", ((BiliLiveDatabase) it.getValue()).getConfig().getRoomId()); } } }) @@ -34,7 +36,7 @@ public class LiveDatabasesService { configDatabase = new BiliLiveConfigDatabase(); } public String getCacheInfo() { - return "总数:"+liveDatabases.size()+","+ Arrays.toString(liveDatabases.asMap().keySet().toArray())+","+ Arrays.toString(liveDatabases.asMap().values().toArray()); + return "总数:" + liveDatabases.size() + "," + Arrays.toString(liveDatabases.asMap().keySet().toArray()) + "," + Arrays.toString(liveDatabases.asMap().values().toArray()); } public BiliLiveDatabase getLiveDatabase(String roomId) { diff --git a/src/main/java/com/yutou/bilibili/services/LiveVideoDownloadService.java b/src/main/java/com/yutou/bilibili/services/LiveVideoDownloadService.java index b35650b..8f04dd4 100644 --- a/src/main/java/com/yutou/bilibili/services/LiveVideoDownloadService.java +++ b/src/main/java/com/yutou/bilibili/services/LiveVideoDownloadService.java @@ -142,7 +142,6 @@ public class LiveVideoDownloadService { String savePath; File rootPath; LiveConfigDatabaseBean config; - BiliLiveDatabase database; LiveVideoDatabaseBean videoDatabaseBean = null; LiveRoomInfo roomInfo; @@ -200,7 +199,6 @@ public class LiveVideoDownloadService { config.setLogin(StringUtils.hasText(bean.getRecordUid())); config.setRoomInfo(roomInfo); config.setRootPath(bean.getRecordPath()); - database = liveDatabasesService.getLiveDatabase(bean.getRoomId()); saveLiveInfo(roomInfo); api.getLiveRoomPlayInfo( bean.getRoomId(), @@ -356,7 +354,7 @@ public class LiveVideoDownloadService { } if (videoDatabaseBean != null) { videoDatabaseBean.setStopTime(new Date()); - database.addLiveInfo(videoDatabaseBean); + liveDatabasesService.getLiveDatabase(bean.getRoomId()).addLiveInfo(videoDatabaseBean); } stopRecordDanmu(); } @@ -373,7 +371,7 @@ public class LiveVideoDownloadService { videoDatabaseBean.setPath(savePath); videoDatabaseBean.setRoomInfoJson(JSONObject.toJSONString(roomInfo)); videoDatabaseBean.setStartTime(new Date()); - database.addLiveInfo(videoDatabaseBean); + liveDatabasesService.getLiveDatabase(bean.getRoomId()).addLiveInfo(videoDatabaseBean); recordDanmu(); // LiveInfoNfoTools.saveLiveInfoNfo(roomInfo, rootPath.getAbsolutePath(), new File(savePath).getName().replace(".flv", ".nfo")); } diff --git a/src/main/java/com/yutou/common/databases/SQLiteManager.java b/src/main/java/com/yutou/common/databases/SQLiteManager.java index fc92a5e..75d81d1 100644 --- a/src/main/java/com/yutou/common/databases/SQLiteManager.java +++ b/src/main/java/com/yutou/common/databases/SQLiteManager.java @@ -88,7 +88,7 @@ public abstract class SQLiteManager { } } - public void close() { + public synchronized void close() { try { conn.close(); } catch (SQLException throwables) {