refactor(config): 移除BrowserConfig配置类并引入PlaywrightManager单例管理器
将原先通过Spring管理的Playwright实例改为使用自定义的单例模式进行全局管理, 提升资源控制能力与生命周期管理的灵活性。同时在应用关闭时主动释放Playwright资源,避免内存泄漏。- 删除`BrowserConfig`类,不再通过Spring容器管理Playwright实例 - 新增`PlaywrightManager`单例类,统一管理Playwright的创建、获取和销毁- 在`DesktopApplication`中添加对Playwright实例的关闭调用 - 更新`FtbCrawlNetDy`和`FtbCrawlNetMt`服务类中的Playwright实例获取方式
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
package com.fantaibao;
|
package com.fantaibao;
|
||||||
|
|
||||||
import com.fantaibao.config.GlobalConfig;
|
import com.fantaibao.config.GlobalConfig;
|
||||||
|
import com.fantaibao.config.PlaywrightManager;
|
||||||
import com.fantaibao.page.InitialiNetWork;
|
import com.fantaibao.page.InitialiNetWork;
|
||||||
import com.fantaibao.page.LoginView;
|
import com.fantaibao.page.LoginView;
|
||||||
import javafx.application.Application;
|
import javafx.application.Application;
|
||||||
@@ -41,11 +42,15 @@ public class DesktopApplication extends Application {
|
|||||||
GlobalConfig.addIcon(primaryStage);
|
GlobalConfig.addIcon(primaryStage);
|
||||||
// 直接启动登录界面
|
// 直接启动登录界面
|
||||||
new LoginView(primaryStage);
|
new LoginView(primaryStage);
|
||||||
|
// 异步加载浏览器加快启动时间
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void stop() {
|
public void stop() {
|
||||||
closeApplicationContext();
|
closeApplicationContext();
|
||||||
|
// 关闭Playwright实例
|
||||||
|
PlaywrightManager.getInstance().close();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -60,6 +65,8 @@ public class DesktopApplication extends Application {
|
|||||||
private void handleCloseEvent(WindowEvent event) {
|
private void handleCloseEvent(WindowEvent event) {
|
||||||
// 关闭Spring Boot应用上下文
|
// 关闭Spring Boot应用上下文
|
||||||
closeApplicationContext();
|
closeApplicationContext();
|
||||||
|
// 关闭Playwright实例
|
||||||
|
PlaywrightManager.getInstance().close();
|
||||||
// 退出JVM
|
// 退出JVM
|
||||||
Platform.exit();
|
Platform.exit();
|
||||||
System.exit(0);
|
System.exit(0);
|
||||||
|
|||||||
@@ -1,15 +0,0 @@
|
|||||||
package com.fantaibao.config;
|
|
||||||
|
|
||||||
import com.microsoft.playwright.Playwright;
|
|
||||||
import org.springframework.context.annotation.Bean;
|
|
||||||
import org.springframework.context.annotation.Configuration;
|
|
||||||
|
|
||||||
@Configuration
|
|
||||||
public class BrowserConfig {
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
public Playwright playwright() {
|
|
||||||
return Playwright.create();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
70
src/main/java/com/fantaibao/config/PlaywrightManager.java
Normal file
70
src/main/java/com/fantaibao/config/PlaywrightManager.java
Normal file
@@ -0,0 +1,70 @@
|
|||||||
|
package com.fantaibao.config;
|
||||||
|
|
||||||
|
import com.microsoft.playwright.Playwright;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Playwright单例管理类
|
||||||
|
* 用于全局管理和提供Playwright实例
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
public class PlaywrightManager {
|
||||||
|
|
||||||
|
private static volatile PlaywrightManager instance;
|
||||||
|
private Playwright playwright;
|
||||||
|
|
||||||
|
private PlaywrightManager() {
|
||||||
|
if (instance != null) {
|
||||||
|
throw new RuntimeException("请使用getInstance()方法获取实例");
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
this.playwright = Playwright.create();
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("创建Playwright实例失败", e);
|
||||||
|
throw new RuntimeException("创建Playwright实例失败", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取PlaywrightManager单例实例
|
||||||
|
* @return PlaywrightManager实例
|
||||||
|
*/
|
||||||
|
public static PlaywrightManager getInstance() {
|
||||||
|
if (instance == null) {
|
||||||
|
synchronized (PlaywrightManager.class) {
|
||||||
|
if (instance == null) {
|
||||||
|
instance = new PlaywrightManager();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取Playwright实例
|
||||||
|
* @return Playwright实例
|
||||||
|
*/
|
||||||
|
public Playwright getPlaywright() {
|
||||||
|
if (playwright == null) {
|
||||||
|
throw new RuntimeException("Playwright实例未初始化或已被销毁");
|
||||||
|
}
|
||||||
|
return playwright;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 关闭Playwright实例并释放资源
|
||||||
|
*/
|
||||||
|
public void close() {
|
||||||
|
if (playwright != null) {
|
||||||
|
try {
|
||||||
|
playwright.close();
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("关闭Playwright实例时出错", e);
|
||||||
|
} finally {
|
||||||
|
playwright = null;
|
||||||
|
instance = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -2,6 +2,7 @@ package com.fantaibao.service;
|
|||||||
|
|
||||||
import com.fantaibao.base.FtbCrawlNetBase;
|
import com.fantaibao.base.FtbCrawlNetBase;
|
||||||
import com.fantaibao.config.GlobalConfig;
|
import com.fantaibao.config.GlobalConfig;
|
||||||
|
import com.fantaibao.config.PlaywrightManager;
|
||||||
import com.microsoft.playwright.*;
|
import com.microsoft.playwright.*;
|
||||||
import jakarta.annotation.Resource;
|
import jakarta.annotation.Resource;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
@@ -14,13 +15,11 @@ import java.util.concurrent.atomic.AtomicBoolean;
|
|||||||
@Slf4j
|
@Slf4j
|
||||||
public class FtbCrawlNetDy extends AbstractFtbCrawlNetBase implements FtbCrawlNetBase {
|
public class FtbCrawlNetDy extends AbstractFtbCrawlNetBase implements FtbCrawlNetBase {
|
||||||
|
|
||||||
@Resource
|
|
||||||
private Playwright playwright;
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void executeCookieIntercept() {
|
public void executeCookieIntercept() {
|
||||||
// 启动可见浏览器
|
// 启动可见浏览器
|
||||||
try (Browser browser = playwright.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false))) {
|
try (Browser browser = PlaywrightManager.getInstance().getPlaywright()
|
||||||
|
.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false))) {
|
||||||
// 设置浏览器窗口大小为1920x1080
|
// 设置浏览器窗口大小为1920x1080
|
||||||
Browser.NewContextOptions contextOptions = new Browser.NewContextOptions()
|
Browser.NewContextOptions contextOptions = new Browser.NewContextOptions()
|
||||||
.setViewportSize(1680, 900);
|
.setViewportSize(1680, 900);
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package com.fantaibao.service;
|
|||||||
|
|
||||||
import com.fantaibao.base.FtbCrawlNetBase;
|
import com.fantaibao.base.FtbCrawlNetBase;
|
||||||
import com.fantaibao.config.GlobalConfig;
|
import com.fantaibao.config.GlobalConfig;
|
||||||
|
import com.fantaibao.config.PlaywrightManager;
|
||||||
import com.microsoft.playwright.*;
|
import com.microsoft.playwright.*;
|
||||||
import jakarta.annotation.Resource;
|
import jakarta.annotation.Resource;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
@@ -14,13 +15,11 @@ import java.util.concurrent.atomic.AtomicBoolean;
|
|||||||
@Slf4j
|
@Slf4j
|
||||||
public class FtbCrawlNetMt extends AbstractFtbCrawlNetBase implements FtbCrawlNetBase {
|
public class FtbCrawlNetMt extends AbstractFtbCrawlNetBase implements FtbCrawlNetBase {
|
||||||
|
|
||||||
@Resource
|
|
||||||
private Playwright playwright;
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void executeCookieIntercept() {
|
public void executeCookieIntercept() {
|
||||||
// 启动可见浏览器
|
// 启动可见浏览器
|
||||||
try (Browser browser = playwright.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false))) {
|
try (Browser browser = PlaywrightManager.getInstance().getPlaywright()
|
||||||
|
.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false))) {
|
||||||
// 设置浏览器窗口大小为1920x1080
|
// 设置浏览器窗口大小为1920x1080
|
||||||
Browser.NewContextOptions contextOptions = new Browser.NewContextOptions()
|
Browser.NewContextOptions contextOptions = new Browser.NewContextOptions()
|
||||||
.setViewportSize(1680, 900);
|
.setViewportSize(1680, 900);
|
||||||
|
|||||||
Reference in New Issue
Block a user