Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fb241b0
commit de47781
Showing
9 changed files
with
201 additions
and
43 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.wishuok.config; | ||
|
||
import org.apache.log4j.Logger; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.ComponentScan; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.scheduling.annotation.EnableAsync; | ||
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; | ||
|
||
import java.util.concurrent.Executor; | ||
import java.util.concurrent.ThreadPoolExecutor; | ||
|
||
@EnableAsync | ||
@Configuration | ||
public class ExcutorConfig { | ||
private static Logger logger = Logger.getLogger(ExcutorConfig.class); | ||
|
||
@Bean(name = "asyncServiceExecutor") | ||
public Executor asyncServiceExecutor() { | ||
logger.info("start executor -->"); | ||
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); | ||
//设置核心线程数 | ||
executor.setCorePoolSize(50); | ||
//设置最大线程数 | ||
executor.setMaxPoolSize(300); | ||
//设置队列大小 | ||
executor.setQueueCapacity(300); | ||
//配置线程池的前缀 | ||
executor.setThreadNamePrefix("async-service-"); | ||
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); | ||
//设置空闲时间 | ||
executor.setKeepAliveSeconds(60); | ||
//进行加载 | ||
executor.initialize(); | ||
return executor; | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
src/main/java/com/wishuok/controller/TaskExecuterTestController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package com.wishuok.controller; | ||
|
||
import com.wishuok.pojo.Role; | ||
import com.wishuok.service.TaskExecuterTestService; | ||
import io.swagger.annotations.Api; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Controller; | ||
import org.springframework.util.StopWatch; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestMethod; | ||
import org.springframework.web.bind.annotation.ResponseBody; | ||
|
||
import java.text.SimpleDateFormat; | ||
import java.util.ArrayList; | ||
import java.util.Date; | ||
import java.util.List; | ||
import java.util.UUID; | ||
|
||
@Controller | ||
@RequestMapping("/task") | ||
@Api(value = "/task", tags = {"TaskExecuter测试"}) | ||
public class TaskExecuterTestController { | ||
|
||
@Autowired | ||
private TaskExecuterTestService taskExecuterTestService; | ||
|
||
@RequestMapping(value = "/doTest", method = RequestMethod.GET) | ||
@ResponseBody | ||
public String doTest() { | ||
StopWatch sc = new StopWatch(); | ||
sc.start(); | ||
SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSS"); | ||
List<Role> roles = new ArrayList<Role>(); | ||
int totalCount = 100000; | ||
for (int i = 0; i < totalCount; i++) { | ||
Role role = new Role(); | ||
role.setId(UUID.randomUUID().toString()); | ||
role.setRoleName("testRole_" + i); | ||
role.setNote(ft.format(new Date())); | ||
roles.add(role); | ||
} | ||
taskExecuterTestService.insertRoles(roles); | ||
sc.stop(); | ||
return "测试完成,插入数据 "+totalCount+" 条,总耗时 " + sc.getTotalTimeMillis() + " 毫秒"; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,4 +4,6 @@ | |
|
||
public interface IRoleService { | ||
Role findRoleById(String id); | ||
|
||
int insertRole(Role role); | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/com/wishuok/service/TaskExecuterTestService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.wishuok.service; | ||
|
||
import com.wishuok.pojo.Role; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.List; | ||
|
||
|
||
public interface TaskExecuterTestService { | ||
|
||
void insertRoles(List<Role> roles); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
src/main/java/com/wishuok/service/impl/TaskExecuterTestServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package com.wishuok.service.impl; | ||
|
||
import com.wishuok.pojo.Role; | ||
import com.wishuok.service.TaskExecuterTestService; | ||
import org.apache.log4j.Logger; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.context.ApplicationContext; | ||
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; | ||
import org.springframework.stereotype.Service; | ||
|
||
import javax.annotation.Resource; | ||
import java.util.List; | ||
import java.util.concurrent.CountDownLatch; | ||
|
||
@Service | ||
public class TaskExecuterTestServiceImpl implements TaskExecuterTestService { | ||
|
||
private Logger logger = Logger.getLogger(this.getClass()); | ||
|
||
@Resource(name = "asyncServiceExecutor") | ||
private ThreadPoolTaskExecutor taskExecutor; | ||
|
||
@Autowired | ||
private ApplicationContext applicationContext; | ||
|
||
@Override | ||
public void insertRoles(List<Role> roles) { | ||
CountDownLatch countDownLatch = new CountDownLatch(1000); | ||
for (int i = 0; i < roles.size(); i++) { | ||
Role role = roles.get(i); | ||
taskExecutor.execute(new Runnable() { | ||
@Override | ||
public void run() { | ||
try { | ||
RoleService roleService = applicationContext.getBean(RoleService.class); | ||
int ret = roleService.insertRole(role); | ||
System.out.println("插入Role[" + role.getRoleName()+"]结果: " + ret + ", 当前线程id: " + Thread.currentThread().getId()); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} finally { | ||
countDownLatch.countDown(); //这个不管是否异常都需要数量减,否则会被堵塞无法结束 | ||
} | ||
} | ||
}); | ||
} | ||
try { | ||
countDownLatch.await(); //保证之前的所有的线程都执行完成,才会走下面的; | ||
// 这样就可以在下面拿到所有线程执行完的集合结果 | ||
System.out.println("all roles insert done.........................................................."); | ||
} catch (Exception e) { | ||
logger.error("阻塞异常"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters