Skip to content
一、作业执行器配置【Job Executor】
  • 作业执行器的相关配置

    • asyncExecutorActivate:激活作业执行器,默认是false
    • asyncExecutorXXX:异步执行器的属性配置
    • asyncExecutor:异步执行器bean
  • 自定义线程池【ExecutorService】

    • coolPoolSize:核心线程数
    • maxPoolSize:最大线程数
    • queueCapacity:堵塞队列大小
  • 流程定义定时启动流程

    • 定时开始事件【Timer Start Event】
      • timeDate:指定启动时间。
      • timeDuration:指定持续时间间隔后执行。
      • timeCycle:R5/P1DT1H指定事件段后周期执行。
二、具体逻辑代码
  • 测试代码入口:ConfigJobTest

    java
    package com.laogoubi.config;
    
    import org.activiti.engine.runtime.Job;
    import org.activiti.engine.test.ActivitiRule;
    import org.activiti.engine.test.Deployment;
    import org.junit.Rule;
    import org.junit.Test;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
    import java.util.List;
    
    /**
     * @ClassName ConfigJobTest
     * @Description 执行器配置
     * @Author eastern
     * @Date 2020/6/27 下午10:55
     * @Version 1.0
     **/
    public class ConfigJobTest {
    
    	private static final Logger logger = LoggerFactory.getLogger(ConfigJobTest.class);
    
    	@Rule
    	public ActivitiRule activitiRule = new ActivitiRule("activiti_job.cfg.xml");
    
    	@Test
    	@Deployment(resources = {"com/laogoubi/my-process_job.bpmn20.xml"})
    	public void test() throws InterruptedException {
    		logger.info("start");
    		List<Job> jobs = activitiRule.getManagementService().createTimerJobQuery().listPage(0, 100);
    		for (Job job : jobs) {
    			logger.info("定时任务 = {} , 默认重试次数 = {}", job, job.getRetries());
    		}
    		logger.info("jobs.size", jobs.size());
    		Thread.sleep(1000 * 100);
    		logger.info("end");
    	}
    }
  • 配置文件:activiti_job.cfg.xml

    xml
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://www.springframework.org/schema/beans   http://www.springframework.org/schema/beans/spring-beans.xsd">
    
        <bean id="processEngineConfiguration" class="org.activiti.engine.impl.cfg.StandaloneInMemProcessEngineConfiguration">
            <property name="enableDatabaseEventLogging" value="true"/>
            <property name="asyncExecutorActivate" value="true"/>
            <property name="asyncExecutor" ref="asyncExecutor"/>
            <property name="eventListeners">
                <list>
                    <bean class="com.laogoubi.event.JobEventListener"></bean>
                </list>
            </property>
        </bean>
    
        <bean id="asyncExecutor" class="org.activiti.engine.impl.asyncexecutor.DefaultAsyncJobExecutor">
            <property name="executorService" ref="executorService"></property>
        </bean>
        <bean id="executorService" class="org.springframework.scheduling.concurrent.ThreadPoolExecutorFactoryBean">
            <property name="threadNamePrefix" value="activiti-job-"/>
            <property name="corePoolSize" value="5"/>
            <property name="maxPoolSize" value="20"/>
            <property name="queueCapacity" value="100"/>
            <property name="rejectedExecutionHandler">
                <bean class="java.util.concurrent.ThreadPoolExecutor$AbortPolicy"></bean>
            </property>
        </bean>
    </beans>
  • 配置文件:my-process_job.bpmn20.xml

    xml
    <?xml version="1.0" encoding="UTF-8"?>
    
    <definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:activiti="http://activiti.org/bpmn"
    	xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:omgdc="http://www.omg.org/spec/DD/20100524/DC"
    	xmlns:omgdi="http://www.omg.org/spec/DD/20100524/DI" typeLanguage="http://www.w3.org/2001/XMLSchema"
    	expressionLanguage="http://www.w3.org/1999/XPath" targetNamespace="http://www.activiti.org/test">
    
    	<process id="my-process">
    		<startEvent id="start">
    			<timerEventDefinition>
    				<timeCycle>R5/PT10S</timeCycle>
    			</timerEventDefinition>
    		</startEvent>
    		<sequenceFlow id="flow1" sourceRef="start" targetRef="someTask" />
    		<userTask id="someTask" name="Activiti is awesome!" />
    		<sequenceFlow id="flow2" sourceRef="someTask" targetRef="end" />
    		<endEvent id="end" />
    	</process>
    </definitions>

Released under the MIT License.