From dd4685c69d6c33fb7dfa25420d8d5b71755751f4 Mon Sep 17 00:00:00 2001 From: bai <1643359946@qq.com> Date: Thu, 31 Oct 2024 02:25:37 +0800 Subject: [PATCH] =?UTF-8?q?2024=E5=B9=B410=E6=9C=8831=E6=97=A5=20=E4=BF=AE?= =?UTF-8?q?=E6=94=B9=E9=A9=B1=E5=8A=A8=EF=BC=8C=E4=BF=9D=E6=8C=81=E4=B8=80?= =?UTF-8?q?=E8=87=B4=E6=80=A7=EF=BC=8C=E5=A2=9E=E5=8A=A0=E7=AD=89=E5=BE=85?= =?UTF-8?q?=E6=97=B6=E9=97=B4=EF=BC=8C=E5=8D=87=E7=BA=A7jar=E5=8C=85?= =?UTF-8?q?=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/impl/BlStuAnswerServiceImpl.java | 2 +- .../impl/BlTeacherAnswerServiceImpl.java | 14 +- .../jeecg/modules/tools/WebDriverUtil.java | 124 +++++++++++++++--- jeecg-boot/pom.xml | 2 +- 4 files changed, 117 insertions(+), 25 deletions(-) diff --git a/jeecg-boot/jeecg-module-demo/src/main/java/org/jeecg/modules/demo/blStuAnswer/service/impl/BlStuAnswerServiceImpl.java b/jeecg-boot/jeecg-module-demo/src/main/java/org/jeecg/modules/demo/blStuAnswer/service/impl/BlStuAnswerServiceImpl.java index 39b0a78..b8ee63f 100644 --- a/jeecg-boot/jeecg-module-demo/src/main/java/org/jeecg/modules/demo/blStuAnswer/service/impl/BlStuAnswerServiceImpl.java +++ b/jeecg-boot/jeecg-module-demo/src/main/java/org/jeecg/modules/demo/blStuAnswer/service/impl/BlStuAnswerServiceImpl.java @@ -88,7 +88,7 @@ public class BlStuAnswerServiceImpl extends ServiceImpl threadService = ThreadUtil.newCompletionService(Executors.newFixedThreadPool(10)); + CompletionService threadService = ThreadUtil.newCompletionService(Executors.newFixedThreadPool(100)); List> calls = Lists.newArrayList(); List mergeListT1_1 = Lists.newArrayList(); List mergeListT2_10 = Lists.newArrayList(); diff --git a/jeecg-boot/jeecg-module-demo/src/main/java/org/jeecg/modules/demo/blTeacherAnswer/service/impl/BlTeacherAnswerServiceImpl.java b/jeecg-boot/jeecg-module-demo/src/main/java/org/jeecg/modules/demo/blTeacherAnswer/service/impl/BlTeacherAnswerServiceImpl.java index 3b696b6..7d1700c 100644 --- a/jeecg-boot/jeecg-module-demo/src/main/java/org/jeecg/modules/demo/blTeacherAnswer/service/impl/BlTeacherAnswerServiceImpl.java +++ b/jeecg-boot/jeecg-module-demo/src/main/java/org/jeecg/modules/demo/blTeacherAnswer/service/impl/BlTeacherAnswerServiceImpl.java @@ -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 threadPoolTaskExecutor; + @Override public WordOperator createWordTjfx(String id) throws Exception { String templateName = "exp1\\teacher-tjfx.docx"; @@ -70,11 +74,11 @@ public class BlTeacherAnswerServiceImpl extends ServiceImpl threadService = ThreadUtil.newCompletionService(Executors.newFixedThreadPool(10)); - List> calls = com.google.common.collect.Lists.newArrayList(); - List allMergeList1 = org.apache.commons.compress.utils.Lists.newArrayList(); - List allMergeList2 = org.apache.commons.compress.utils.Lists.newArrayList(); - List allMergeList3 = org.apache.commons.compress.utils.Lists.newArrayList(); + CompletionService threadService = ThreadUtil.newCompletionService(Executors.newFixedThreadPool(100)); + List> calls = Lists.newArrayList(); + List allMergeList1 = Lists.newArrayList(); + List allMergeList2 = Lists.newArrayList(); + List allMergeList3 = Lists.newArrayList(); calls.add(() -> { res1(blTeacherMain, result); diff --git a/jeecg-boot/jeecg-module-demo/src/main/java/org/jeecg/modules/tools/WebDriverUtil.java b/jeecg-boot/jeecg-module-demo/src/main/java/org/jeecg/modules/tools/WebDriverUtil.java index 66d7b6e..2645941 100644 --- a/jeecg-boot/jeecg-module-demo/src/main/java/org/jeecg/modules/tools/WebDriverUtil.java +++ b/jeecg-boot/jeecg-module-demo/src/main/java/org/jeecg/modules/tools/WebDriverUtil.java @@ -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,9 +47,61 @@ public class WebDriverUtil { // log.info("================================"); // log.info(imgName); // log.info("================================"); - String currentDir = System.getProperty("user.dir"); - log.info(currentDir); +// 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(); + //js执行超时时间 + Duration duration = Duration.of(5, ChronoUnit.SECONDS); + chromeOptions.setScriptTimeout(duration); + //页面加载超时时间 + 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; } /** @@ -50,7 +109,7 @@ public class WebDriverUtil { * * @return */ - public static WebDriver getWebDriver() { + public static WebDriver getWebDriver() throws IOException { // 设置ChromeDriver的路径加载驱动 //"D:/APPLICATION/chromedriver/chromedriver.exe" System.setProperty(global.getConfig("webDriver.driverName"), getUrlStr(global.getConfig("webDriver.driverPath"))); @@ -58,11 +117,12 @@ public class WebDriverUtil { //设置 chrome 的无头模式 ChromeOptions chromeOptions = new ChromeOptions(); //js执行超时时间 - Duration duration = Duration.of(5, ChronoUnit.SECONDS); + 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:, @@ -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,36 +184,50 @@ public class WebDriverUtil { return getImgByte(json, path, null); } - /** - * 获取图片byte - */ public static String getImgByte(String json, String path, Map 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 runJs) { + log.info("发往浏览器的json: {}",json); + try { //访问网址 //往缓存写入jar包里的资源 //把js拉过来 getUrl(global.getConfig("webDriver.webHTMLPath") + "/echarts.min.js"); - if(StringUtils.isNotBlank(path)){ + if (StringUtils.isNotBlank(path)) { driver.get(getUrl(global.getConfig("webDriver.webHTMLPath") + "/" + path).toString()); } else { driver.get(getUrl(global.getConfig("webDriver.webHTMLPath") + "/echarts.html").toString()); } - log.info("最终浏览器执行的地址: {}",driver.getCurrentUrl()); + log.info("最终浏览器执行的地址: {}", driver.getCurrentUrl()); JavascriptExecutor js = (JavascriptExecutor) driver; //执行绘制图片前 var runJsStr = "showImg(" + json + ")"; //绘制方法是默认的还是自定义的 - if(runJs != null && runJs.containsKey("showImg")){ + if (runJs != null && runJs.containsKey("showImg")) { runJsStr = runJs.get("showImg"); } - if(runJs != null && runJs.containsKey("runBefore")){ + if (runJs != null && runJs.containsKey("runBefore")) { runJsStr = runJs.get("runBefore") + runJsStr; } //执行绘制图片后 - if(runJs != null && runJs.containsKey("runAfter")){ + if (runJs != null && runJs.containsKey("runAfter")) { runJsStr = runJsStr + runJs.get("runAfter"); } // 绘制方法是默认的还是自定义的 @@ -153,8 +241,8 @@ public class WebDriverUtil { Thread.sleep(1000); String imgTxt = js.executeScript("return returnEchartImg()").toString().replace("data:image/png;base64,", ""); String uid = RandomUtil.randomString(6); - String imageName = DateUtils.formatDate(new Date(),"yyyyMMddHHmmss") + uid + ".png"; - return base64StringToImage(imgTxt,imageName); + String imageName = DateUtils.formatDate(new Date(), "yyyyMMddHHmmss") + uid + ".png"; + return base64StringToImage(imgTxt, imageName); } catch (Exception e) { log.error(e.getMessage(), e); } finally { diff --git a/jeecg-boot/pom.xml b/jeecg-boot/pom.xml index 943d5e3..238fcbe 100644 --- a/jeecg-boot/pom.xml +++ b/jeecg-boot/pom.xml @@ -164,7 +164,7 @@ org.seleniumhq.selenium selenium-java - 4.21.0 + 4.25.0