因为我们每一个任务到是动态创建的,在spring要实现动态的new一个bean对象,我们会用到FactoryBean接口达到我们的目的。我们需要异步和多线程需要使用Spring的TaskExecutor它其实就是javaSE5之后的java.util.concurrent.Executor接口的一共抽象。
新建两个任务类:
package org.dave.spring.async;
import java.util.Date;
public class TaskA implements Runnable{
private String name;
public TaskA(){
name = this.getClass().getName();
}
@Override
public void run() {
String threadName = Thread.currentThread().getName();
System.out.println("ThreadName:"+threadName+" beginning work on "+new Date());
System.out.println("ThreadName:"+threadName+" taskName:"+name);
System.out.println("ThreadName:"+threadName+" completed work on "+new Date());
}
}
package org.dave.spring.async;
import java.util.Date;
public class TaskB implements Runnable{
private String name;
public TaskB(){
name = this.getClass().getName();
}
@Override
public void run() {
String threadName = Thread.currentThread().getName();
System.out.println("ThreadName:"+threadName+" beginning work on "+new Date());
System.out.println("ThreadName:"+threadName+" taskName:"+name);
System.out.println("ThreadName:"+threadName+" completed work on "+new Date());
}
}
创建一个默认任务:
package org.dave.spring.async;
import java.util.Date;
public class DefaultTask implements Runnable{
private String name;
public DefaultTask(){
name = this.getClass().getName();
}
@Override
public void run() {
String threadName = Thread.currentThread().getName();
System.out.println("ThreadName:"+threadName+" beginning work on "+new Date());
System.out.println("ThreadName:"+threadName+" taskName:"+name);
System.out.println("ThreadName:"+threadName+" completed work on "+new Date());
}
}
创建一个任务工厂:
package org.dave.spring.async.factory;
import org.dave.spring.async.DefaultTask;
import org.dave.spring.async.TaskA;
import org.dave.spring.async.TaskB;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;
@Component("taskFactoryBean")
@SuppressWarnings("rawtypes")
public class TaskFactoryBean implements FactoryBean,InitializingBean{
private String taskType;
@Override
public Object getObject() throws Exception {
if(taskType!=null){
if(taskType.equals("a")){
return new TaskA();
}else if(taskType.equals("b")) {
return new TaskB();
}
}
return new DefaultTask();
}
public void setTaskType(String taskType) {
this.taskType = taskType;
}
@Override
public Class getObjectType() {
if(taskType!=null){
if(taskType.equals("a")){
return TaskA.class;
}else if(taskType.equals("b")) {
return TaskB.class;
}
}
return DefaultTask.class;
}
@Override
public boolean isSingleton() {
return false;
}
@Override
public void afterPropertiesSet() throws Exception {
taskType=null;
}
}
创建一个产生任务组件:
package org.dave.spring.async.service;
import org.dave.spring.async.DefaultTask;
import org.dave.spring.async.TaskA;
import org.dave.spring.async.TaskB;
import org.dave.spring.async.factory.TaskFactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component("genTask")
public class GenTask {
@Autowired
private TaskFactoryBean taskFactoryBean;
public Runnable gen(String taskType) throws Exception
{
taskFactoryBean.setTaskType(taskType);
Object obj = taskFactoryBean.getObject();
if(obj instanceof TaskA )
{
return (TaskA)obj;
}else if(obj instanceof TaskB )
{
return (TaskB)obj;
}else if(obj instanceof DefaultTask )
{
return (DefaultTask)obj;
}
return null;
}
}
创建一个简单测试进程:
package org.dave.spring.async.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.task.TaskExecutor;
import org.springframework.stereotype.Service;
@Service
public class SimpleProcessor {
@Autowired
private TaskExecutor taskExecutor;
@Autowired
private GenTask genTask;
public void process() throws Exception
{
taskExecutor.execute(genTask.gen(null));
taskExecutor.execute(genTask.gen("a"));
taskExecutor.execute(genTask.gen("b"));
}
}
Spring 配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="org.dave.spring.async"/>
<task:executor id="taskExecutor" keep-alive="50000" queue-capacity="50" pool-size="5-10"/>
<task:scheduled-tasks>
<task:scheduled ref="simpleProcessor" method="process" cron="3/10 * * * * ?"/>
</task:scheduled-tasks>
</beans>
<context:component-scan标签是组件注解扫描。task:executor 是任务执行器keep-alive是线程保持活动时间默认为秒,queue-capacity任务队列的容量,pool-size是线程池的大小,5-10表示有5个core线程(活动的线程)10最大的线程数的设置。
<task:scheduled-tasks>任务调度器标签,<task:scheduled 任务调度simpleProcessor组件的process方法,每1分钟调用一次。
分享到:
相关推荐
1. SpringBoot 自定义线程池以及多线程间的异步调用(@Async、@EnableAsync) 2.Java多线程之定时任务 以及 SpringBoot多线程实现定时任务 3.@EnableScheduling 与 @Scheduled
Maven项目,可直接编译...全spring配置,处理服务线程池线程数自定义; 上下文读取器配置添加至readerPool.readers; 上下文处理器配置添加至serviceProcessors.services; 分析链线程数配置serviceProcessors.threadCount;
spring boot 纯注解方法事务控制回滚,注解+简单配置文件使用多线程demo
本篇文章主要介绍了详解Spring-Boot中如何使用多线程处理任务,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。
一个简单的spring的多线程demo,希望能对大家有用
spring异步调用的例子
主要介绍了Spring-Boot中如何使用多线程处理任务方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
Spring3.0 mvc 定时器及多线程任务demo
主要介绍了浅谈spring 线程异步执行,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
spring 多线程队列执行
Spring中的同步执行器 1. SyncTaskExecutor:同步可以用SyncTaskExecutor,但这个可以说不算一个线程池,因为还在原线程执行。这个类没有实现异步调用...前者是同步执行器,执行任务同步,后者是线程池,执行任务异步。
文档讲述了SpringTask动态定时任务的使用以及配置方法。可拔插的使用在相关的项目中去
l Spring封装Job调度:当任务信息都持久化在DB中之后,我们需要将这些信息读取出来执行具体的业务逻辑操作,这里我们通过ScheduledExecutorFactoryBean来实现对任务的循环调度,比如说可采取每隔5min扫描一次待处理...
主要介绍了使用Spring事件机制实现异步的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
2)通过前台动态分配定时任务 可指定多个任务,可同时执行任务,可以精确到时分秒扫描并执行任务 3)可以完成稍微复杂点的任务 比如:2001-10-1 10:10:10 执行任务 执行多少次 好久执行一次 到2001-10-10 10:10:10 ...
springboot多线程范例,Spring Boot中使用多线程的方式有很多种,最简单的方式就是使用@Async注解来实现。
主要给大家介绍了关于spring异步service中处理线程数限制的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用spring具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
主要介绍了详解Spring框架下向异步线程传递HttpServletRequest参数的坑,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
基于SpringBoot和POI实现单线程和多线程导出Excel.zip基于SpringBoot和POI实现单线程和多线程导出Excel.zip基于SpringBoot和POI实现单线程和多线程导出Excel.zip基于SpringBoot和POI实现单线程和多线程导出Excel.zip...