Skip to content

Commit e6aafd8

Browse files
committed
添加多线程
把截图功能设置为线程任务提高响应效率
1 parent acfb0ff commit e6aafd8

File tree

7 files changed

+69
-66
lines changed

7 files changed

+69
-66
lines changed

springboot-dubbo-api/src/main/java/com/lzq/api/pojo/Example.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public class Example implements Serializable {
2727
/**
2828
* 主键
2929
*/
30-
@TableId(value = "example_id")
30+
@TableId(value = "example_id",type =IdType.ID_WORKER_STR )
3131
@JsonProperty("exampleId")
3232
private String exampleId;
3333
/**

springboot-dubbo-api/src/main/java/com/lzq/api/service/ExampleService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public interface ExampleService {
2121
* @param example 实例对象
2222
* @return 实例对象
2323
*/
24-
Boolean insert(Example example);
24+
Example insert(Example example);
2525

2626
/**
2727
* 修改数据

springboot-dubbo-service/src/main/java/com/lzq/dubboservice/service/ExampleServiceImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
public class ExampleServiceImpl extends ServiceImpl<ExampleMapper, Example> implements ExampleService {
2323

2424
@Override
25-
public Boolean insert(Example example) {
26-
return baseMapper.insert(example) > 0 ? true : false;
25+
public Example insert(Example example) {
26+
return baseMapper.insert(example) > 0 ? example : null;
2727
}
2828

2929
@Override

springboot-dubbo-service/src/main/resources/application.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
spring:
22
profiles:
3-
active: prod
3+
active: dev
44
application:
55
#项目名称
66
name: service
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.lzq.web.config;
2+
3+
import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
4+
import org.springframework.context.annotation.Configuration;
5+
import org.springframework.scheduling.annotation.AsyncConfigurer;
6+
import org.springframework.scheduling.annotation.EnableAsync;
7+
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
8+
9+
import java.util.concurrent.Executor;
10+
11+
/**
12+
* @author :LZQ
13+
* @description:TODO
14+
* @date :2021/9/24 9:17
15+
*/
16+
@Configuration
17+
@EnableAsync
18+
public class CustomMultiThreadingConfig implements AsyncConfigurer {
19+
@Override
20+
public Executor getAsyncExecutor() {
21+
ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
22+
taskExecutor.setCorePoolSize(10);
23+
taskExecutor.setMaxPoolSize(20);
24+
taskExecutor.setQueueCapacity(30);
25+
taskExecutor.initialize();
26+
return taskExecutor;
27+
}
28+
29+
@Override
30+
public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
31+
return null;
32+
}
33+
}

springboot-dubbo-web/src/main/java/com/lzq/web/controller/ExampleController.java

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ public class ExampleController {
6262
@Autowired
6363
private RabbitTemplate rabbitTemplate;
6464

65+
@Autowired
66+
ExampleUtils exampleUtils;
67+
6568
@Value("${resources.route}")
6669
public String fileLocation;
6770

@@ -81,7 +84,7 @@ public Map<String, Object> CreateFile(HttpServletRequest request,Example example
8184
example.setUsername(username);
8285
log.info("开始"+new Date(System.currentTimeMillis()).toString());
8386
//生成22位uuid
84-
String uuid = ExampleUtils.getUUid();
87+
// String uuid = ExampleUtils.getUUid();
8588
Boolean bol =false;
8689
//随机生成uuid
8790
//判断用户是保存实例还是第一次创建实例
@@ -90,25 +93,25 @@ public Map<String, Object> CreateFile(HttpServletRequest request,Example example
9093
//通过id查询实例信息
9194
Example query = exampleService.queryById(example.getExampleId());
9295
//获取实例的文件名和图片key
93-
example.setFileName(query.getFileName());
9496
example.setImg(query.getImg());
97+
//修改实例内容
98+
bol = contentService.updateContent(exampleContent);
99+
String file = ExampleUtils.FILE_LOCATION + example.getUsername() + "/" + query.getFileName() + ".html";
95100
//用户保存实例
96-
bol = ExampleUtils.SaveExampleContent(example, exampleContent, content, exampleService, contentService);
97-
log.info("结束"+new Date(System.currentTimeMillis()).toString());
101+
exampleUtils.SaveExampleContent(example,content, exampleService,file);
98102
return ResultMapUtils.ResultMap(bol,0,"保存实例内容成功");
99103
} catch (IOException e) {
100104
e.printStackTrace();
101105
return ResultMapUtils.ResultMap(false,0,"修改文件失败");
102106
}
103107
}else {
104-
example.setExampleId(uuid);
105108
//用户第一次创建实例
106109
//获取当前时间毫秒
107110
long time = System.currentTimeMillis();
108111
//创建编译后的html文件
109112
String file = ExampleUtils.FILE_LOCATION + example.getUsername() + "/" + time + ".html";
110113
//初始化模板
111-
File initfile = new File(ExampleUtils.INIT_HTML);
114+
// File initfile = new File(ExampleUtils.INIT_HTML);
112115
File existfile = new File(file);
113116
try {
114117
//创建文件
@@ -117,18 +120,23 @@ public Map<String, Object> CreateFile(HttpServletRequest request,Example example
117120
}
118121
existfile.createNewFile();
119122
//把初始化模板拷贝到新建的html中
120-
FileUtils.copyFile(initfile, existfile);
123+
// FileUtils.copyFile(initfile, existfile);
121124
//插入编译后的文件名称time
122125
example.setFileName(Long.toString(time));
123126
//把文件信息插入到数据库中
124-
bol = exampleService.insert(example);
125-
if (bol){
127+
example = exampleService.insert(example);
128+
System.out.println(example);
129+
if (example!=null){
126130
//更新用户的个人作品数
127131
accountService.addWorks(example.getUsername());
128132
//保存实例内容
129-
bol = ExampleUtils.SaveExampleContent(example, exampleContent, content, exampleService, contentService);
130-
log.info("结束"+new Date(System.currentTimeMillis()).toString());
133+
exampleContent.setExampleId(example.getExampleId());
134+
//第一次保存时在表中添加实例内容
135+
bol = contentService.addContent(exampleContent);
136+
//用户保存实例
137+
exampleUtils.SaveExampleContent(example,content, exampleService,file);
131138
}
139+
log.info(example.getExampleId());
132140
return ResultMapUtils.ResultMap(bol, 0, example.getExampleId());
133141
} catch (Exception e) {
134142
e.printStackTrace();

springboot-dubbo-web/src/main/java/com/lzq/web/utils/ExampleUtils.java

Lines changed: 12 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22

33

44
import com.luciad.imageio.webp.WebPWriteParam;
5-
import com.lzq.api.pojo.Content;
65
import com.lzq.api.pojo.Example;
7-
import com.lzq.api.service.ContentService;
86
import com.lzq.api.service.ExampleService;
97
import com.qiniu.util.StringUtils;
108
import lombok.extern.slf4j.Slf4j;
@@ -14,6 +12,7 @@
1412
import org.openqa.selenium.chrome.ChromeDriver;
1513
import org.openqa.selenium.chrome.ChromeOptions;
1614
import org.springframework.beans.factory.annotation.Value;
15+
import org.springframework.scheduling.annotation.Async;
1716
import org.springframework.stereotype.Component;
1817

1918
import javax.imageio.IIOImage;
@@ -24,7 +23,7 @@
2423
import java.io.File;
2524
import java.io.FileOutputStream;
2625
import java.io.IOException;
27-
import java.util.UUID;
26+
import java.util.Date;
2827
import java.util.concurrent.TimeUnit;
2928

3029
@Slf4j
@@ -39,8 +38,6 @@ public class ExampleUtils {
3938

4039
public static String BUCKET;
4140

42-
private static final char[] _UU64 = "-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz".toCharArray();
43-
4441
@Value("${resources.InitHtml}")
4542
public void setInitHtml(String initHtml) {
4643
INIT_HTML = initHtml;
@@ -86,7 +83,7 @@ public static String screenshot(String username, String filename) throws IOExcep
8683
Dimension dim = new Dimension(winWidth, winHeight);
8784
broswer.manage().window().setSize(dim);
8885
//等待1秒,
89-
broswer.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);
86+
broswer.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
9087
//打开url
9188
broswer.get("http://localhost:8090/" + username + "/" + filename + ".html");
9289
//截图
@@ -110,26 +107,22 @@ public static String screenshot(String username, String filename) throws IOExcep
110107
* 保存实例
111108
*
112109
* @param example
113-
* @param exampleContent
114110
* @param content 编译后的内容
115111
* @param exampleService
116-
* @param contentService
112+
* @param file
117113
* @return
118114
* @throws IOException
119115
*/
120-
public static Boolean SaveExampleContent(Example example, Content exampleContent, String content, ExampleService exampleService, ContentService contentService) throws IOException {
121-
//获取html文件路劲
122-
//实例内容和实例进行绑定
123-
exampleContent.setExampleId(example.getExampleId());
124-
String file = FILE_LOCATION + example.getUsername() + "/" + example.getFileName() + ".html";
125-
log.info(file);
116+
@Async
117+
public void SaveExampleContent(Example example, String content, ExampleService exampleService,String file) throws IOException {
118+
log.info("开始截图-------"+new Date(System.currentTimeMillis()));
119+
//获取文件输出流
126120
FileOutputStream fos = new FileOutputStream(new File(file));
127121
String screenshot = null;
128122
Boolean bol = false;
129123
try {
130124
//包编译后的html内容覆盖原来的内容
131125
fos.write(content.getBytes("UTF-8"));
132-
// fos.write(content.getBytes("GBK")) ;
133126
//第一次保存时生成图片
134127
if (StringUtils.isNullOrEmpty(example.getImg())) {
135128
//截图后进行保存
@@ -145,20 +138,15 @@ public static Boolean SaveExampleContent(Example example, Content exampleContent
145138
}
146139
//更新实例
147140
bol = exampleService.update(example);
148-
//修改实例内容
149-
bol = contentService.updateContent(exampleContent);
150-
//当表无该数据时插入数据
151-
if (!bol) {
152-
//第一次保存时在表中添加实例内容
153-
bol = contentService.addContent(exampleContent);
154-
}
155-
return bol;
141+
log.info("截图成功");
156142
} catch (IOException e) {
157143
e.printStackTrace();
158-
return bol;
144+
log.info("截图失败");
145+
// return bol;
159146
} finally {
160147
log.info("上传关闭流-----------------------");
161148
fos.close();
149+
log.info("结束截图-------"+new Date(System.currentTimeMillis()));
162150
}
163151
}
164152

@@ -190,30 +178,4 @@ public static void convertWebp(File oldfile, File newfile) throws IOException {
190178
}
191179

192180

193-
//生成22为uuid
194-
public static String getUUid() {
195-
UUID uuid = UUID.randomUUID();
196-
int index = 0;
197-
char[] cs = new char[22];
198-
long L = uuid.getMostSignificantBits();
199-
long R = uuid.getLeastSignificantBits();
200-
long mask = 63;
201-
// 从L64位取10次,每次取6位
202-
for (int off = 58; off >= 4; off -= 6) {
203-
long hex = (L & (mask << off)) >>> off;
204-
cs[index++] = _UU64[(int) hex];
205-
}
206-
// 从L64位取最后的4位 + R64位头2位拼上
207-
int l = (int) (((L & 0xF) << 2) | ((R & (3 << 62)) >>> 62));
208-
cs[index++] = _UU64[l];
209-
// 从R64位取10次,每次取6位
210-
for (int off = 56; off >= 2; off -= 6) {
211-
long hex = (R & (mask << off)) >>> off;
212-
cs[index++] = _UU64[(int) hex];
213-
}
214-
// 剩下的两位最后取
215-
cs[index++] = _UU64[(int) (R & 3)];
216-
// 返回字符串
217-
return new String(cs);
218-
}
219181
}

0 commit comments

Comments
 (0)