2024年10月31日 修改驱动,保持一致性,增加等待时间,升级jar包版本

This commit is contained in:
bai 2024-10-31 02:25:37 +08:00
parent b1ad133dcf
commit dd4685c69d
4 changed files with 117 additions and 25 deletions

View File

@ -88,7 +88,7 @@ public class BlStuAnswerServiceImpl extends ServiceImpl<BlStuAnswerMapper, BlStu
//尝试异步加快导出速度
log.info("多线程主线程开始执行");
//多线程执行
CompletionService<Boolean> threadService = ThreadUtil.newCompletionService(Executors.newFixedThreadPool(10));
CompletionService<Boolean> threadService = ThreadUtil.newCompletionService(Executors.newFixedThreadPool(100));
List<Callable<Boolean>> calls = Lists.newArrayList();
List<TableMerge> mergeListT1_1 = Lists.newArrayList();
List<TableMerge> mergeListT2_10 = Lists.newArrayList();

View File

@ -20,6 +20,7 @@ import org.jeecg.modules.tools.word.WordOperator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
@ -43,6 +44,9 @@ public class BlTeacherAnswerServiceImpl extends ServiceImpl<BlTeacherAnswerMappe
@Autowired
public BlTeacherInfoMapper blTeacherInfoMapper;
//@Resource
//public CompletionService<Boolean> threadPoolTaskExecutor;
@Override
public WordOperator createWordTjfx(String id) throws Exception {
String templateName = "exp1\\teacher-tjfx.docx";
@ -70,11 +74,11 @@ public class BlTeacherAnswerServiceImpl extends ServiceImpl<BlTeacherAnswerMappe
//尝试异步加快导出速度
log.info("多线程主线程开始执行");
//多线程执行
CompletionService<Boolean> threadService = ThreadUtil.newCompletionService(Executors.newFixedThreadPool(10));
List<Callable<Boolean>> calls = com.google.common.collect.Lists.newArrayList();
List<TableMerge> allMergeList1 = org.apache.commons.compress.utils.Lists.newArrayList();
List<TableMerge> allMergeList2 = org.apache.commons.compress.utils.Lists.newArrayList();
List<TableMerge> allMergeList3 = org.apache.commons.compress.utils.Lists.newArrayList();
CompletionService<Boolean> threadService = ThreadUtil.newCompletionService(Executors.newFixedThreadPool(100));
List<Callable<Boolean>> calls = Lists.newArrayList();
List<TableMerge> allMergeList1 = Lists.newArrayList();
List<TableMerge> allMergeList2 = Lists.newArrayList();
List<TableMerge> allMergeList3 = Lists.newArrayList();
calls.add(() -> {
res1(blTeacherMain, result);

View File

@ -11,8 +11,12 @@ import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.util.DateUtils;
import org.jeecg.common.util.SpringContextHolder;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.SessionNotCreatedException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WindowType;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeDriverLogLevel;
import org.openqa.selenium.chrome.ChromeDriverService;
import org.openqa.selenium.chrome.ChromeOptions;
import javax.imageio.ImageIO;
@ -20,8 +24,6 @@ import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.Duration;
import java.time.temporal.ChronoUnit;
import java.util.Date;
@ -33,6 +35,11 @@ public class WebDriverUtil {
// private static final Global global = null;
private static final Global global = SpringContextHolder.getBean(Global.class);
//驱动
// private static WebDriver driver;
private static ChromeDriverService driverService;
//private static BASE64Decoder decoder = new sun.misc.BASE64Decoder();
public static void main(String []s){
@ -40,20 +47,11 @@ public class WebDriverUtil {
// log.info("================================");
// log.info(imgName);
// log.info("================================");
String currentDir = System.getProperty("user.dir");
log.info(currentDir);
}
/**
* 初始化WebDriver
*
* @return
*/
public static WebDriver getWebDriver() {
// 设置ChromeDriver的路径加载驱动
//"D:/APPLICATION/chromedriver/chromedriver.exe"
System.setProperty(global.getConfig("webDriver.driverName"), getUrlStr(global.getConfig("webDriver.driverPath")));
// String currentDir = System.getProperty("user.dir");
// log.info(currentDir);
// if(driver == null) {
WebDriver driver = null;
System.setProperty("webdriver.chrome.driver", getUrlStr("web/webDriver/130.0.6723.70/chromedriver.exe"));
// System.setProperty("webdriver.chrome.driver", "D:/developkit/driver/chromedriver.exe");
//设置 chrome 的无头模式
ChromeOptions chromeOptions = new ChromeOptions();
@ -64,6 +62,68 @@ public class WebDriverUtil {
//页面加载超时时间
chromeOptions.setPageLoadTimeout(duration);
//无头模式
chromeOptions.setHeadless(false);
//地址出现data:,
// chromeOptions.addArguments("--user-data-dir=C:/Users/Administrator/AppData/Local/Google/Chrome/User Data/Default");
//Chrome正在受到自动软件的控制 不显示提示语
chromeOptions.addArguments("disable-infobars");
chromeOptions.addArguments("--remote-allow-origins=*");
//设置浏览器完整路径
chromeOptions.setBinary("D:/Program Files (x86)/Google/Chrome/Application/chrome.exe");
//启动一个 chrome 实例
driver = new ChromeDriver(chromeOptions);
// }
driver.get("https://www.baidu.com/");
WebDriver driver2 = driver.switchTo().newWindow(WindowType.TAB);
driver2.get("https://cn.bing.com/");
log.info("第一个页面:{}", driver.getTitle());
log.info("第一个页面:{}", driver2.getTitle());
driver.close();
driver2.close();
}
public static void createService() throws IOException {
Duration duration = Duration.of(500, ChronoUnit.SECONDS);
ChromeDriverService service = new ChromeDriverService.Builder()
//超时时间
.withTimeout(duration)
//执行驱动
//.usingDriverExecutable(getUrl(global.getConfig("webDriver.driverPath")))
.build();
// service.start();
driverService = service;
}
/**
* 初始化WebDriver
*
* @return
*/
public static WebDriver getWebDriver() throws IOException {
// 设置ChromeDriver的路径加载驱动
//"D:/APPLICATION/chromedriver/chromedriver.exe"
System.setProperty(global.getConfig("webDriver.driverName"), getUrlStr(global.getConfig("webDriver.driverPath")));
// System.setProperty("webdriver.chrome.driver", "D:/developkit/driver/chromedriver.exe");
//设置 chrome 的无头模式
ChromeOptions chromeOptions = new ChromeOptions();
//js执行超时时间
Duration duration = Duration.of(500, ChronoUnit.SECONDS);
chromeOptions.setScriptTimeout(duration);
//页面加载超时时间
chromeOptions.setPageLoadTimeout(duration);
chromeOptions.setImplicitWaitTimeout(duration);
//无头模式
chromeOptions.setHeadless(Boolean.parseBoolean(global.getConfig("webDriver.isShow")));
//地址出现data:,
// chromeOptions.addArguments("--user-data-dir=C:/Users/Administrator/AppData/Local/Google/Chrome/User Data/Default");
@ -72,8 +132,22 @@ public class WebDriverUtil {
chromeOptions.addArguments("--remote-allow-origins=*");
//设置浏览器完整路径
chromeOptions.setBinary(global.getConfig("webDriver.webExamplePath"));
chromeOptions.setLogLevel(ChromeDriverLogLevel.ALL);
//启动一个 chrome 实例
return new ChromeDriver(chromeOptions);
// if(driverService == null) {
// createService();
// }
ChromeDriverService service = new ChromeDriverService.Builder()
//超时时间
.withTimeout(duration)
//执行驱动
//.usingDriverExecutable(getUrl(global.getConfig("webDriver.driverPath")))
.build();
// service.start();
return new ChromeDriver(service, chromeOptions);
}
public static File getResourcesFileToTempFile(String path) {
@ -110,14 +184,28 @@ public class WebDriverUtil {
return getImgByte(json, path, null);
}
/**
* 获取图片byte
*/
public static String getImgByte(String json, String path, Map<String, String> runJs) {
log.info("发往浏览器的json: {}",json);
WebDriver driver = null;
try {
driver = getWebDriver();
} catch (SessionNotCreatedException e) {
log.error(e.getMessage(), e);
log.error("重新整一次????");
//不知为何这里总会报连接不上的错误解决办法就是不停的执行错一次多执行一次但是有问题就是会多出一个无主的chromedriver无法关闭
//ThreadUtil.sleep(1000);//延迟1秒试试
return getImgByte(json, path, runJs);
} catch (Exception e) {
log.error(e.getMessage(), e);
}
return getImgByte(driver, json, path, runJs);
}
/**
* 获取图片byte
*/
public static String getImgByte(WebDriver driver, String json, String path, Map<String, String> runJs) {
log.info("发往浏览器的json: {}",json);
try {
//访问网址
//往缓存写入jar包里的资源
//把js拉过来

View File

@ -164,7 +164,7 @@
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.21.0</version>
<version>4.25.0</version>
</dependency>