Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
P
Pangea-Agent
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
2
Merge Requests
2
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Gavin-Group
Pangea-Agent
Commits
42060fe1
Commit
42060fe1
authored
Dec 31, 2025
by
youxiaoji
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
* [playwright 初始化通过配置文件初始化,manager默认使用Bean]
parent
1ff00b03
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
100 additions
and
72 deletions
+100
-72
PlaywrightConfig.java
...n/java/pangea/hiagent/common/config/PlaywrightConfig.java
+39
-0
PlaywrightManager.java
...ava/pangea/hiagent/tool/playwright/PlaywrightManager.java
+6
-2
PlaywrightManagerImpl.java
...pangea/hiagent/tool/playwright/PlaywrightManagerImpl.java
+55
-70
No files found.
backend/src/main/java/pangea/hiagent/common/config/PlaywrightConfig.java
0 → 100644
View file @
42060fe1
package
pangea
.
hiagent
.
common
.
config
;
import
com.microsoft.playwright.Browser
;
import
com.microsoft.playwright.BrowserType
;
import
com.microsoft.playwright.Playwright
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
@Slf4j
@Configuration
public
class
PlaywrightConfig
{
@Bean
public
Playwright
playwright
()
{
log
.
info
(
"playwright init success"
);
return
Playwright
.
create
();
}
@Bean
public
Browser
browser
(
Playwright
playwright
)
{
Browser
browser
=
null
;
browser
=
playwright
.
chromium
().
launch
(
new
BrowserType
.
LaunchOptions
()
.
setHeadless
(
true
)
.
setArgs
(
java
.
util
.
Arrays
.
asList
(
"--no-sandbox"
,
"--disable-dev-shm-usage"
,
"--disable-gpu"
,
"--remote-allow-origins=*"
)));
browser
.
onDisconnected
((
tmp
)
->
{
log
.
info
(
"浏览器实例已断开连接"
);
// 发送监听事件
tmp
.
close
();
});
return
browser
;
}
}
backend/src/main/java/pangea/hiagent/tool/playwright/PlaywrightManager.java
View file @
42060fe1
...
...
@@ -15,14 +15,18 @@ public interface PlaywrightManager {
*
* @return Playwright实例
*/
Playwright
getPlaywright
();
default
Playwright
getPlaywright
(){
return
null
;
}
/**
* 获取共享的浏览器实例
*
* @return Browser实例
*/
Browser
getBrowser
();
default
Browser
getBrowser
(){
return
null
;
}
/**
* 为指定用户获取专用的浏览器上下文
...
...
backend/src/main/java/pangea/hiagent/tool/playwright/PlaywrightManagerImpl.java
View file @
42060fe1
...
...
@@ -2,6 +2,7 @@ package pangea.hiagent.tool.playwright;
import
com.microsoft.playwright.*
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Component
;
import
jakarta.annotation.PreDestroy
;
...
...
@@ -17,10 +18,11 @@ import java.util.concurrent.*;
public
class
PlaywrightManagerImpl
implements
PlaywrightManager
{
// 共享的Playwright实例
private
volatile
Playwright
playwright
;
@Autowired
private
Playwright
playwright
;
@Autowired
// 共享的浏览器实例
private
volatile
Browser
browser
;
private
Browser
browser
;
// 用户浏览器上下文映射表(用户ID -> BrowserContext)
private
final
ConcurrentMap
<
String
,
BrowserContext
>
userContexts
=
new
ConcurrentHashMap
<>();
...
...
@@ -47,54 +49,54 @@ public class PlaywrightManagerImpl implements PlaywrightManager {
/**
* 延迟初始化Playwright和浏览器实例
*/
private
void
lazyInitialize
()
{
if
(!
initialized
)
{
synchronized
(
initLock
)
{
if
(!
initialized
)
{
try
{
log
.
info
(
"正在初始化Playwright管理器..."
);
// 创建Playwright实例
if
(
playwright
==
null
)
{
this
.
playwright
=
Playwright
.
create
();
}
if
(
browser
==
null
)
{
// 启动Chrome浏览器,无头模式
this
.
browser
=
playwright
.
chromium
().
launch
(
new
BrowserType
.
LaunchOptions
()
.
setHeadless
(
true
)
.
setArgs
(
java
.
util
.
Arrays
.
asList
(
"--no-sandbox"
,
"--disable-dev-shm-usage"
,
"--disable-gpu"
,
"--remote-allow-origins=*"
)));
this
.
browser
.
onDisconnected
((
browser
)
->
{
log
.
info
(
"浏览器实例已断开连接"
);
this
.
browser
.
close
();
userContexts
.
clear
();
this
.
browser
=
playwright
.
chromium
().
launch
(
new
BrowserType
.
LaunchOptions
()
.
setHeadless
(
true
)
.
setArgs
(
java
.
util
.
Arrays
.
asList
(
"--no-sandbox"
,
"--disable-dev-shm-usage"
,
"--disable-gpu"
,
"--remote-allow-origins=*"
)));
});
}
// 每5分钟检查一次超时的用户上下文
cleanupScheduler
.
scheduleAtFixedRate
(
this
::
cleanupExpiredContexts
,
5
,
3600
,
TimeUnit
.
MINUTES
);
this
.
initialized
=
true
;
log
.
info
(
"Playwright管理器初始化成功"
);
}
catch
(
Exception
e
)
{
log
.
error
(
"Playwright管理器初始化失败: "
,
e
);
throw
new
RuntimeException
(
"Failed to initialize Playwright manager"
,
e
);
}
}
}
}
}
//
private void lazyInitialize() {
//
if (!initialized) {
//
synchronized (initLock) {
//
if (!initialized) {
//
try {
//
log.info("正在初始化Playwright管理器...");
// Thread.currentThread().setContextClassLoader(Playwright.class.getClassLoader());
//
// 创建Playwright实例
//
if (playwright == null) {
//
this.playwright = Playwright.create();
//
}
//
if (browser == null) {
//
// 启动Chrome浏览器,无头模式
//
this.browser = playwright.chromium().launch(new BrowserType.LaunchOptions()
//
.setHeadless(true)
//
.setArgs(java.util.Arrays.asList(
//
"--no-sandbox",
//
"--disable-dev-shm-usage",
//
"--disable-gpu",
//
"--remote-allow-origins=*")));
//
//
this.browser.onDisconnected((browser) -> {
//
log.info("浏览器实例已断开连接");
//
this.browser.close();
//
userContexts.clear();
//
this.browser = playwright.chromium().launch(new BrowserType.LaunchOptions()
//
.setHeadless(true)
//
.setArgs(java.util.Arrays.asList(
//
"--no-sandbox",
//
"--disable-dev-shm-usage",
//
"--disable-gpu",
//
"--remote-allow-origins=*")));
//
});
//
}
//
// 每5分钟检查一次超时的用户上下文
//
cleanupScheduler.scheduleAtFixedRate(this::cleanupExpiredContexts,
//
5, 3600, TimeUnit.MINUTES);
//
//
this.initialized = true;
//
log.info("Playwright管理器初始化成功");
//
} catch (Exception e) {
//
log.error("Playwright管理器初始化失败: ", e);
//
throw new RuntimeException("Failed to initialize Playwright manager", e);
//
}
//
}
//
}
//
}
//
}
// 移除@PostConstruct注解,避免在Spring初始化时自动调用
/*
...
...
@@ -104,27 +106,10 @@ public class PlaywrightManagerImpl implements PlaywrightManager {
* }
*/
@Override
public
Playwright
getPlaywright
()
{
lazyInitialize
();
if
(
playwright
==
null
)
{
throw
new
IllegalStateException
(
"Playwright instance is not initialized"
);
}
return
playwright
;
}
@Override
public
Browser
getBrowser
()
{
lazyInitialize
();
if
(
browser
==
null
||
!
browser
.
isConnected
())
{
throw
new
IllegalStateException
(
"Browser instance is not available"
);
}
return
browser
;
}
@Override
public
BrowserContext
getUserContext
(
String
userId
)
{
lazyInitialize
();
//
lazyInitialize();
Browser
.
NewContextOptions
options
=
new
Browser
.
NewContextOptions
()
.
setViewportSize
(
1920
,
1080
)
// 设置视口大小为全高清分辨率,适用于Windows 11桌面环境
.
setUserAgent
(
...
...
@@ -136,7 +121,7 @@ public class PlaywrightManagerImpl implements PlaywrightManager {
@Override
public
BrowserContext
getUserContext
(
String
userId
,
Browser
.
NewContextOptions
options
)
{
lazyInitialize
();
//
lazyInitialize();
if
(
userId
==
null
||
userId
.
isEmpty
())
{
throw
new
IllegalArgumentException
(
"User ID cannot be null or empty"
);
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment