Merge branch '4.3.3'

This commit is contained in:
tjq 2023-06-11 17:24:05 +08:00
commit df7ceb7ba5
32 changed files with 351 additions and 100 deletions

View File

@ -6,7 +6,7 @@
<groupId>tech.powerjob</groupId> <groupId>tech.powerjob</groupId>
<artifactId>powerjob</artifactId> <artifactId>powerjob</artifactId>
<version>4.3.2</version> <version>4.3.3</version>
<packaging>pom</packaging> <packaging>pom</packaging>
<name>powerjob</name> <name>powerjob</name>
<url>http://www.powerjob.tech</url> <url>http://www.powerjob.tech</url>

View File

@ -5,18 +5,18 @@
<parent> <parent>
<artifactId>powerjob</artifactId> <artifactId>powerjob</artifactId>
<groupId>tech.powerjob</groupId> <groupId>tech.powerjob</groupId>
<version>4.3.2</version> <version>4.3.3</version>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<artifactId>powerjob-client</artifactId> <artifactId>powerjob-client</artifactId>
<version>4.3.2</version> <version>4.3.3</version>
<packaging>jar</packaging> <packaging>jar</packaging>
<properties> <properties>
<junit.version>5.9.1</junit.version> <junit.version>5.9.1</junit.version>
<fastjson.version>1.2.83</fastjson.version> <fastjson.version>1.2.83</fastjson.version>
<powerjob.common.version>4.3.2</powerjob.common.version> <powerjob.common.version>4.3.3</powerjob.common.version>
<mvn.shade.plugin.version>3.2.4</mvn.shade.plugin.version> <mvn.shade.plugin.version>3.2.4</mvn.shade.plugin.version>
</properties> </properties>

View File

@ -5,12 +5,12 @@
<parent> <parent>
<artifactId>powerjob</artifactId> <artifactId>powerjob</artifactId>
<groupId>tech.powerjob</groupId> <groupId>tech.powerjob</groupId>
<version>4.3.2</version> <version>4.3.3</version>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<artifactId>powerjob-common</artifactId> <artifactId>powerjob-common</artifactId>
<version>4.3.2</version> <version>4.3.3</version>
<packaging>jar</packaging> <packaging>jar</packaging>
<properties> <properties>

View File

@ -5,12 +5,12 @@
<parent> <parent>
<artifactId>powerjob</artifactId> <artifactId>powerjob</artifactId>
<groupId>tech.powerjob</groupId> <groupId>tech.powerjob</groupId>
<version>4.3.2</version> <version>4.3.3</version>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<artifactId>powerjob-official-processors</artifactId> <artifactId>powerjob-official-processors</artifactId>
<version>4.3.2</version> <version>4.3.3</version>
<packaging>jar</packaging> <packaging>jar</packaging>
<properties> <properties>
@ -20,7 +20,7 @@
<!-- 不会被打包的部分scope 只能是 test 或 provide --> <!-- 不会被打包的部分scope 只能是 test 或 provide -->
<junit.version>5.9.1</junit.version> <junit.version>5.9.1</junit.version>
<logback.version>1.2.9</logback.version> <logback.version>1.2.9</logback.version>
<powerjob.worker.version>4.3.2</powerjob.worker.version> <powerjob.worker.version>4.3.3</powerjob.worker.version>
<spring.jdbc.version>5.2.9.RELEASE</spring.jdbc.version> <spring.jdbc.version>5.2.9.RELEASE</spring.jdbc.version>
<h2.db.version>2.1.214</h2.db.version> <h2.db.version>2.1.214</h2.db.version>
<mysql.version>8.0.28</mysql.version> <mysql.version>8.0.28</mysql.version>

View File

@ -5,7 +5,7 @@
<parent> <parent>
<artifactId>powerjob</artifactId> <artifactId>powerjob</artifactId>
<groupId>tech.powerjob</groupId> <groupId>tech.powerjob</groupId>
<version>4.3.2</version> <version>4.3.3</version>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<packaging>pom</packaging> <packaging>pom</packaging>

View File

@ -5,7 +5,7 @@
<parent> <parent>
<artifactId>powerjob-remote</artifactId> <artifactId>powerjob-remote</artifactId>
<groupId>tech.powerjob</groupId> <groupId>tech.powerjob</groupId>
<version>4.3.2</version> <version>4.3.3</version>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
@ -21,8 +21,8 @@
<logback.version>1.2.9</logback.version> <logback.version>1.2.9</logback.version>
<springboot.version>2.7.4</springboot.version> <springboot.version>2.7.4</springboot.version>
<powerjob-remote-impl-http.version>4.3.2</powerjob-remote-impl-http.version> <powerjob-remote-impl-http.version>4.3.3</powerjob-remote-impl-http.version>
<powerjob-remote-impl-akka.version>4.3.2</powerjob-remote-impl-akka.version> <powerjob-remote-impl-akka.version>4.3.3</powerjob-remote-impl-akka.version>
<gatling.version>3.9.0</gatling.version> <gatling.version>3.9.0</gatling.version>
<gatling-maven-plugin.version>4.2.9</gatling-maven-plugin.version> <gatling-maven-plugin.version>4.2.9</gatling-maven-plugin.version>

View File

@ -5,11 +5,11 @@
<parent> <parent>
<artifactId>powerjob-remote</artifactId> <artifactId>powerjob-remote</artifactId>
<groupId>tech.powerjob</groupId> <groupId>tech.powerjob</groupId>
<version>4.3.2</version> <version>4.3.3</version>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<version>4.3.2</version> <version>4.3.3</version>
<artifactId>powerjob-remote-framework</artifactId> <artifactId>powerjob-remote-framework</artifactId>
<properties> <properties>
@ -17,7 +17,7 @@
<maven.compiler.target>8</maven.compiler.target> <maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<powerjob-common.version>4.3.2</powerjob-common.version> <powerjob-common.version>4.3.3</powerjob-common.version>
<reflections.version>0.10.2</reflections.version> <reflections.version>0.10.2</reflections.version>

View File

@ -5,19 +5,19 @@
<parent> <parent>
<artifactId>powerjob-remote</artifactId> <artifactId>powerjob-remote</artifactId>
<groupId>tech.powerjob</groupId> <groupId>tech.powerjob</groupId>
<version>4.3.2</version> <version>4.3.3</version>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<artifactId>powerjob-remote-impl-akka</artifactId> <artifactId>powerjob-remote-impl-akka</artifactId>
<version>4.3.2</version> <version>4.3.3</version>
<properties> <properties>
<maven.compiler.source>8</maven.compiler.source> <maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target> <maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<powerjob-remote-framework.version>4.3.2</powerjob-remote-framework.version> <powerjob-remote-framework.version>4.3.3</powerjob-remote-framework.version>
<akka.version>2.6.13</akka.version> <akka.version>2.6.13</akka.version>
</properties> </properties>

View File

@ -5,12 +5,12 @@
<parent> <parent>
<artifactId>powerjob-remote</artifactId> <artifactId>powerjob-remote</artifactId>
<groupId>tech.powerjob</groupId> <groupId>tech.powerjob</groupId>
<version>4.3.2</version> <version>4.3.3</version>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<artifactId>powerjob-remote-impl-http</artifactId> <artifactId>powerjob-remote-impl-http</artifactId>
<version>4.3.2</version> <version>4.3.3</version>
<properties> <properties>
<maven.compiler.source>8</maven.compiler.source> <maven.compiler.source>8</maven.compiler.source>
@ -18,7 +18,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<vertx.version>4.3.7</vertx.version> <vertx.version>4.3.7</vertx.version>
<powerjob-remote-framework.version>4.3.2</powerjob-remote-framework.version> <powerjob-remote-framework.version>4.3.3</powerjob-remote-framework.version>
</properties> </properties>
<dependencies> <dependencies>

View File

@ -5,12 +5,12 @@
<parent> <parent>
<artifactId>powerjob</artifactId> <artifactId>powerjob</artifactId>
<groupId>tech.powerjob</groupId> <groupId>tech.powerjob</groupId>
<version>4.3.2</version> <version>4.3.3</version>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<artifactId>powerjob-server</artifactId> <artifactId>powerjob-server</artifactId>
<version>4.3.2</version> <version>4.3.3</version>
<packaging>pom</packaging> <packaging>pom</packaging>
<modules> <modules>
@ -49,9 +49,9 @@
<groovy.version>3.0.10</groovy.version> <groovy.version>3.0.10</groovy.version>
<cron-utils.version>9.1.6</cron-utils.version> <cron-utils.version>9.1.6</cron-utils.version>
<powerjob-common.version>4.3.2</powerjob-common.version> <powerjob-common.version>4.3.3</powerjob-common.version>
<powerjob-remote-impl-http.version>4.3.2</powerjob-remote-impl-http.version> <powerjob-remote-impl-http.version>4.3.3</powerjob-remote-impl-http.version>
<powerjob-remote-impl-akka.version>4.3.2</powerjob-remote-impl-akka.version> <powerjob-remote-impl-akka.version>4.3.3</powerjob-remote-impl-akka.version>
<springdoc-openapi-ui.version>1.6.14</springdoc-openapi-ui.version> <springdoc-openapi-ui.version>1.6.14</springdoc-openapi-ui.version>
</properties> </properties>

View File

@ -5,7 +5,7 @@
<parent> <parent>
<artifactId>powerjob-server</artifactId> <artifactId>powerjob-server</artifactId>
<groupId>tech.powerjob</groupId> <groupId>tech.powerjob</groupId>
<version>4.3.2</version> <version>4.3.3</version>
<relativePath>../pom.xml</relativePath> <relativePath>../pom.xml</relativePath>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>

View File

@ -5,7 +5,7 @@
<parent> <parent>
<artifactId>powerjob-server</artifactId> <artifactId>powerjob-server</artifactId>
<groupId>tech.powerjob</groupId> <groupId>tech.powerjob</groupId>
<version>4.3.2</version> <version>4.3.3</version>
<relativePath>../pom.xml</relativePath> <relativePath>../pom.xml</relativePath>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>

View File

@ -10,6 +10,7 @@ import tech.powerjob.common.enums.TimeExpressionType;
import tech.powerjob.common.model.LifeCycle; import tech.powerjob.common.model.LifeCycle;
import tech.powerjob.common.request.ServerStopInstanceReq; import tech.powerjob.common.request.ServerStopInstanceReq;
import tech.powerjob.common.request.TaskTrackerReportInstanceStatusReq; import tech.powerjob.common.request.TaskTrackerReportInstanceStatusReq;
import tech.powerjob.common.utils.CommonUtils;
import tech.powerjob.remote.framework.base.URL; import tech.powerjob.remote.framework.base.URL;
import tech.powerjob.server.common.module.WorkerInfo; import tech.powerjob.server.common.module.WorkerInfo;
import tech.powerjob.server.common.timewheel.holder.HashedWheelTimerHolder; import tech.powerjob.server.common.timewheel.holder.HashedWheelTimerHolder;
@ -81,6 +82,14 @@ public class InstanceManager implements TransportServiceAware {
log.warn("[InstanceManager-{}] can't find InstanceInfo from database", instanceId); log.warn("[InstanceManager-{}] can't find InstanceInfo from database", instanceId);
return; return;
} }
// 考虑极端情况Processor 处理耗时小于 server DB 耗时会导致状态上报时无 taskTracker 地址此处等待后重新从DB获取数据 GitHub#620
if (StringUtils.isEmpty(instanceInfo.getTaskTrackerAddress())) {
log.warn("[InstanceManager-{}] TaskTrackerAddress is empty, server will wait then acquire again!", instanceId);
CommonUtils.easySleep(277);
instanceInfo = instanceInfoRepository.findByInstanceId(instanceId);
}
int originStatus = instanceInfo.getStatus(); int originStatus = instanceInfo.getStatus();
// 丢弃过期的上报数据 // 丢弃过期的上报数据
if (req.getReportTime() <= instanceInfo.getLastReportTime()) { if (req.getReportTime() <= instanceInfo.getLastReportTime()) {

View File

@ -5,7 +5,7 @@
<parent> <parent>
<artifactId>powerjob-server</artifactId> <artifactId>powerjob-server</artifactId>
<groupId>tech.powerjob</groupId> <groupId>tech.powerjob</groupId>
<version>4.3.2</version> <version>4.3.3</version>
<relativePath>../pom.xml</relativePath> <relativePath>../pom.xml</relativePath>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>

View File

@ -5,7 +5,7 @@
<parent> <parent>
<artifactId>powerjob-server</artifactId> <artifactId>powerjob-server</artifactId>
<groupId>tech.powerjob</groupId> <groupId>tech.powerjob</groupId>
<version>4.3.2</version> <version>4.3.3</version>
<relativePath>../pom.xml</relativePath> <relativePath>../pom.xml</relativePath>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>

View File

@ -5,7 +5,7 @@
<parent> <parent>
<artifactId>powerjob-server</artifactId> <artifactId>powerjob-server</artifactId>
<groupId>tech.powerjob</groupId> <groupId>tech.powerjob</groupId>
<version>4.3.2</version> <version>4.3.3</version>
<relativePath>../pom.xml</relativePath> <relativePath>../pom.xml</relativePath>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>

View File

@ -5,7 +5,7 @@
<parent> <parent>
<artifactId>powerjob-server</artifactId> <artifactId>powerjob-server</artifactId>
<groupId>tech.powerjob</groupId> <groupId>tech.powerjob</groupId>
<version>4.3.2</version> <version>4.3.3</version>
<relativePath>../pom.xml</relativePath> <relativePath>../pom.xml</relativePath>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>

View File

@ -5,7 +5,7 @@
<parent> <parent>
<artifactId>powerjob-server</artifactId> <artifactId>powerjob-server</artifactId>
<groupId>tech.powerjob</groupId> <groupId>tech.powerjob</groupId>
<version>4.3.2</version> <version>4.3.3</version>
<relativePath>../pom.xml</relativePath> <relativePath>../pom.xml</relativePath>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>

View File

@ -5,7 +5,7 @@
<parent> <parent>
<artifactId>powerjob-server</artifactId> <artifactId>powerjob-server</artifactId>
<groupId>tech.powerjob</groupId> <groupId>tech.powerjob</groupId>
<version>4.3.2</version> <version>4.3.3</version>
<relativePath>../pom.xml</relativePath> <relativePath>../pom.xml</relativePath>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
@ -58,6 +58,8 @@
<version>${springboot.version}</version> <version>${springboot.version}</version>
<configuration> <configuration>
<mainClass>tech.powerjob.server.PowerJobServerApplication</mainClass> <mainClass>tech.powerjob.server.PowerJobServerApplication</mainClass>
<!-- 支持 server 作为 jar 包被外部系统集成 https://gitee.com/KFCFans/PowerJob/issues/I6H8JF -->
<classifier>exec</classifier>
</configuration> </configuration>
<executions> <executions>
<execution> <execution>

View File

@ -5,24 +5,24 @@
<parent> <parent>
<artifactId>powerjob</artifactId> <artifactId>powerjob</artifactId>
<groupId>tech.powerjob</groupId> <groupId>tech.powerjob</groupId>
<version>4.3.2</version> <version>4.3.3</version>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<artifactId>powerjob-worker-agent</artifactId> <artifactId>powerjob-worker-agent</artifactId>
<version>4.3.2</version> <version>4.3.3</version>
<packaging>jar</packaging> <packaging>jar</packaging>
<properties> <properties>
<powerjob.worker.version>4.3.2</powerjob.worker.version> <powerjob.worker.version>4.3.3</powerjob.worker.version>
<logback.version>1.2.9</logback.version> <logback.version>1.2.9</logback.version>
<picocli.version>4.3.2</picocli.version> <picocli.version>4.3.2</picocli.version>
<spring.version>5.3.23</spring.version> <spring.version>5.3.23</spring.version>
<spring.boot.version>2.3.4.RELEASE</spring.boot.version> <spring.boot.version>2.3.4.RELEASE</spring.boot.version>
<powerjob.official.processors.version>4.3.2</powerjob.official.processors.version> <powerjob.official.processors.version>4.3.3</powerjob.official.processors.version>
<!-- dependency for dynamic sql processor --> <!-- dependency for dynamic sql processor -->
<mysql.version>8.0.28</mysql.version> <mysql.version>8.0.28</mysql.version>

View File

@ -5,18 +5,18 @@
<parent> <parent>
<artifactId>powerjob</artifactId> <artifactId>powerjob</artifactId>
<groupId>tech.powerjob</groupId> <groupId>tech.powerjob</groupId>
<version>4.3.2</version> <version>4.3.3</version>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<artifactId>powerjob-worker-samples</artifactId> <artifactId>powerjob-worker-samples</artifactId>
<version>4.3.2</version> <version>4.3.3</version>
<properties> <properties>
<springboot.version>2.7.4</springboot.version> <springboot.version>2.7.4</springboot.version>
<powerjob.worker.starter.version>4.3.2</powerjob.worker.starter.version> <powerjob.worker.starter.version>4.3.3</powerjob.worker.starter.version>
<fastjson.version>1.2.83</fastjson.version> <fastjson.version>1.2.83</fastjson.version>
<powerjob.official.processors.version>4.3.2</powerjob.official.processors.version> <powerjob.official.processors.version>4.3.3</powerjob.official.processors.version>
<!-- 部署时跳过该module --> <!-- 部署时跳过该module -->
<maven.deploy.skip>true</maven.deploy.skip> <maven.deploy.skip>true</maven.deploy.skip>

View File

@ -0,0 +1,18 @@
package tech.powerjob.samples.processors.test;
import tech.powerjob.worker.core.processor.ProcessResult;
import tech.powerjob.worker.core.processor.TaskContext;
import tech.powerjob.worker.core.processor.sdk.BasicProcessor;
/**
* ZeroCostTestProcessor
*
* @author tjq
* @since 2023/5/7
*/
public class ZeroCostTestProcessor implements BasicProcessor {
@Override
public ProcessResult process(TaskContext context) throws Exception {
return new ProcessResult(true, "zero cost");
}
}

View File

@ -0,0 +1,36 @@
package tech.powerjob.samples.tester;
import org.springframework.stereotype.Component;
import tech.powerjob.worker.annotation.PowerJobHandler;
import tech.powerjob.worker.core.processor.TaskContext;
import tech.powerjob.worker.log.OmsLogger;
@Component(value = "springMethodProcessorService")
public class SpringMethodProcessorService {
/**
* 处理器配置方法1 全限定类名#方法名比如 tech.powerjob.samples.tester.SpringMethodProcessorService#testEmptyReturn
* 处理器配置方法2 SpringBean名称#方法名比如 springMethodProcessorService#testEmptyReturn
* @param context 必须要有入参 TaskContext返回值可以是 null也可以是其他任意类型正常返回代表成功抛出异常代表执行失败
*/
@PowerJobHandler(name = "testEmptyReturn")
public void testEmptyReturn(TaskContext context) {
OmsLogger omsLogger = context.getOmsLogger();
omsLogger.warn("测试日志");
}
@PowerJobHandler(name = "testNormalReturn")
public String testNormalReturn(TaskContext context) {
OmsLogger omsLogger = context.getOmsLogger();
omsLogger.warn("测试日志");
return "testNormalReturn";
}
@PowerJobHandler(name = "testThrowException")
public String testThrowException(TaskContext context) {
OmsLogger omsLogger = context.getOmsLogger();
omsLogger.warn("testThrowException");
throw new IllegalArgumentException("test");
}
}

View File

@ -5,16 +5,16 @@
<parent> <parent>
<artifactId>powerjob</artifactId> <artifactId>powerjob</artifactId>
<groupId>tech.powerjob</groupId> <groupId>tech.powerjob</groupId>
<version>4.3.2</version> <version>4.3.3</version>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<artifactId>powerjob-worker-spring-boot-starter</artifactId> <artifactId>powerjob-worker-spring-boot-starter</artifactId>
<version>4.3.2</version> <version>4.3.3</version>
<packaging>jar</packaging> <packaging>jar</packaging>
<properties> <properties>
<powerjob.worker.version>4.3.2</powerjob.worker.version> <powerjob.worker.version>4.3.3</powerjob.worker.version>
<springboot.version>2.7.4</springboot.version> <springboot.version>2.7.4</springboot.version>
</properties> </properties>

View File

@ -5,12 +5,12 @@
<parent> <parent>
<artifactId>powerjob</artifactId> <artifactId>powerjob</artifactId>
<groupId>tech.powerjob</groupId> <groupId>tech.powerjob</groupId>
<version>4.3.2</version> <version>4.3.3</version>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<artifactId>powerjob-worker</artifactId> <artifactId>powerjob-worker</artifactId>
<version>4.3.2</version> <version>4.3.3</version>
<packaging>jar</packaging> <packaging>jar</packaging>
<properties> <properties>
@ -21,10 +21,10 @@
<logback.version>1.2.9</logback.version> <logback.version>1.2.9</logback.version>
<powerjob-common.version>4.3.2</powerjob-common.version> <powerjob-common.version>4.3.3</powerjob-common.version>
<powerjob-remote-framework.version>4.3.2</powerjob-remote-framework.version> <powerjob-remote-framework.version>4.3.3</powerjob-remote-framework.version>
<powerjob-remote-impl-akka.version>4.3.2</powerjob-remote-impl-akka.version> <powerjob-remote-impl-akka.version>4.3.3</powerjob-remote-impl-akka.version>
<powerjob-remote-impl-http.version>4.3.2</powerjob-remote-impl-http.version> <powerjob-remote-impl-http.version>4.3.3</powerjob-remote-impl-http.version>
</properties> </properties>
<dependencies> <dependencies>

View File

@ -8,6 +8,7 @@ import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware; import org.springframework.context.ApplicationContextAware;
import tech.powerjob.worker.common.PowerJobWorkerConfig; import tech.powerjob.worker.common.PowerJobWorkerConfig;
import tech.powerjob.worker.extension.processor.ProcessorFactory; import tech.powerjob.worker.extension.processor.ProcessorFactory;
import tech.powerjob.worker.processor.impl.BuildInSpringMethodProcessorFactory;
import tech.powerjob.worker.processor.impl.BuiltInSpringProcessorFactory; import tech.powerjob.worker.processor.impl.BuiltInSpringProcessorFactory;
import java.util.Collections; import java.util.Collections;
@ -43,12 +44,14 @@ public class PowerJobSpringWorker implements ApplicationContextAware, Initializi
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
BuiltInSpringProcessorFactory springProcessorFactory = new BuiltInSpringProcessorFactory(applicationContext); BuiltInSpringProcessorFactory springProcessorFactory = new BuiltInSpringProcessorFactory(applicationContext);
BuildInSpringMethodProcessorFactory springMethodProcessorFactory = new BuildInSpringMethodProcessorFactory(applicationContext);
// append BuiltInSpringProcessorFactory // append BuiltInSpringProcessorFactory
List<ProcessorFactory> processorFactories = Lists.newArrayList( List<ProcessorFactory> processorFactories = Lists.newArrayList(
Optional.ofNullable(config.getProcessorFactoryList()) Optional.ofNullable(config.getProcessorFactoryList())
.orElse(Collections.emptyList())); .orElse(Collections.emptyList()));
processorFactories.add(springProcessorFactory); processorFactories.add(springProcessorFactory);
processorFactories.add(springMethodProcessorFactory);
config.setProcessorFactoryList(processorFactories); config.setProcessorFactoryList(processorFactories);
} }

View File

@ -0,0 +1,26 @@
package tech.powerjob.worker.annotation;
import java.lang.annotation.*;
/**
* 方法级别的power-job调度
* <a href="https://github.com/PowerJob/PowerJob/pull/610">PR#610</a>
*
* @author <a href="https://github.com/vannewang">vannewang</a>
* @since 2023/4/6
*/
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
public @interface PowerJobHandler {
/**
* handler name
*/
String name();
}

View File

@ -57,7 +57,9 @@ public class ConnectionFactory {
// JVM 关闭时删除数据库文件 // JVM 关闭时删除数据库文件
try { try {
FileUtils.forceDeleteOnExit(new File(H2_PATH)); FileUtils.forceDeleteOnExit(new File(H2_PATH));
}catch (Exception ignore) { log.info("[PowerDatasource] delete worker db file[{}] on JVM exit successfully", H2_PATH);
}catch (Throwable t) {
log.warn("[PowerDatasource] delete file on JVM exit failed: {}", H2_PATH, t);
} }
} }

View File

@ -0,0 +1,65 @@
package tech.powerjob.worker.processor.impl;
import com.google.common.collect.Sets;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationContext;
import tech.powerjob.common.enums.ProcessorType;
import tech.powerjob.worker.extension.processor.ProcessorFactory;
import java.util.Set;
@Slf4j
public abstract class AbstractBuildInSpringProcessorFactory implements ProcessorFactory {
protected final ApplicationContext applicationContext;
protected AbstractBuildInSpringProcessorFactory(ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
}
@Override
public Set<String> supportTypes() {
return Sets.newHashSet(ProcessorType.BUILT_IN.name());
}
protected boolean checkCanLoad() {
try {
ApplicationContext.class.getClassLoader();
return applicationContext != null;
} catch (Throwable ignore) {
}
return false;
}
@SuppressWarnings("unchecked")
protected static <T> T getBean(String className, ApplicationContext ctx) throws Exception {
// 0. 尝试直接用 Bean 名称加载
try {
final Object bean = ctx.getBean(className);
if (bean != null) {
return (T) bean;
}
} catch (Exception ignore) {
}
// 1. ClassLoader 存在则直接使用 clz 加载
ClassLoader classLoader = ctx.getClassLoader();
if (classLoader != null) {
return (T) ctx.getBean(classLoader.loadClass(className));
}
// 2. ClassLoader 不存在(系统类加载器不可见)尝试用类名称小写加载
String[] split = className.split("\\.");
String beanName = split[split.length - 1];
// 小写转大写
char[] cs = beanName.toCharArray();
cs[0] += 32;
String beanName0 = String.valueOf(cs);
log.warn("[SpringUtils] can't get ClassLoader from context[{}], try to load by beanName:{}", ctx, beanName0);
return (T) ctx.getBean(beanName0);
}
}

View File

@ -0,0 +1,95 @@
package tech.powerjob.worker.processor.impl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.context.ApplicationContext;
import tech.powerjob.worker.annotation.PowerJobHandler;
import tech.powerjob.worker.extension.processor.ProcessorBean;
import tech.powerjob.worker.extension.processor.ProcessorDefinition;
import java.lang.reflect.Method;
import java.util.LinkedList;
import java.util.List;
/**
* 内建的 SpringBean 处理器工厂用于加载 Spring 管理Bean下的方法使用PowerJob注解非核心依赖
*
* @author wxp
* @since 2023/4/06
*/
@Slf4j
public class BuildInSpringMethodProcessorFactory extends AbstractBuildInSpringProcessorFactory {
private static final List<String> jobHandlerRepository = new LinkedList<>();
private final static String DELIMITER = "#";
public BuildInSpringMethodProcessorFactory(ApplicationContext applicationContext) {
super(applicationContext);
}
@Override
public ProcessorBean build(ProcessorDefinition processorDefinition) {
try {
boolean canLoad = checkCanLoad();
if (!canLoad) {
log.info("[ProcessorFactory] can't find Spring env, this processor can't load by 'BuildInSpringMethodProcessorFactory'");
return null;
}
String processorInfo = processorDefinition.getProcessorInfo();
if (!processorInfo.contains(DELIMITER)) {
log.info("[ProcessorFactory] can't parse processorDefinition, this processor can't load by 'BuildInSpringMethodProcessorFactory'");
return null;
}
String[] split = processorInfo.split(DELIMITER);
String methodName = split[1];
String className = split[0];
Object bean = getBean(className,applicationContext);
Method[] methods = bean.getClass().getDeclaredMethods();
for (Method method : methods) {
PowerJobHandler powerJob = method.getAnnotation(PowerJobHandler.class);
if (powerJob == null) {
continue;
}
String name = powerJob.name();
//匹配到和页面定义相同的methodName
if (!name.equals(methodName)) {
continue;
}
if (name.trim().length() == 0) {
throw new RuntimeException("method-jobhandler name invalid, for[" + bean.getClass() + "#" + method.getName() + "] .");
}
if (containsJobHandler(name)) {
throw new RuntimeException("jobhandler[" + name + "] naming conflicts.");
}
method.setAccessible(true);
registerJobHandler(methodName);
MethodBasicProcessor processor = new MethodBasicProcessor(bean, method);
return new ProcessorBean()
.setProcessor(processor)
.setClassLoader(processor.getClass().getClassLoader());
}
} catch (NoSuchBeanDefinitionException ignore) {
log.warn("[ProcessorFactory] can't find the processor in SPRING");
} catch (Throwable t) {
log.warn("[ProcessorFactory] load by BuiltInSpringProcessorFactory failed. If you are using Spring, make sure this bean was managed by Spring", t);
}
return null;
}
public static void registerJobHandler(String name) {
jobHandlerRepository.add(name);
}
private boolean containsJobHandler(String name) {
return jobHandlerRepository.contains(name);
}
}

View File

@ -19,17 +19,11 @@ import java.util.Set;
* @since 2023/1/17 * @since 2023/1/17
*/ */
@Slf4j @Slf4j
public class BuiltInSpringProcessorFactory implements ProcessorFactory { public class BuiltInSpringProcessorFactory extends AbstractBuildInSpringProcessorFactory {
private final ApplicationContext applicationContext;
public BuiltInSpringProcessorFactory(ApplicationContext applicationContext) { public BuiltInSpringProcessorFactory(ApplicationContext applicationContext) {
this.applicationContext = applicationContext; super(applicationContext);
}
@Override
public Set<String> supportTypes() {
return Sets.newHashSet(ProcessorType.BUILT_IN.name());
} }
@Override @Override
@ -41,8 +35,12 @@ public class BuiltInSpringProcessorFactory implements ProcessorFactory {
log.info("[ProcessorFactory] can't find Spring env, this processor can't load by 'BuiltInSpringProcessorFactory'"); log.info("[ProcessorFactory] can't find Spring env, this processor can't load by 'BuiltInSpringProcessorFactory'");
return null; return null;
} }
String processorInfo = processorDefinition.getProcessorInfo();
BasicProcessor basicProcessor = getBean(processorDefinition.getProcessorInfo(), applicationContext); //用于区分方法级别的参数
if (processorInfo.contains("#")) {
return null;
}
BasicProcessor basicProcessor = getBean(processorInfo, applicationContext);
return new ProcessorBean() return new ProcessorBean()
.setProcessor(basicProcessor) .setProcessor(basicProcessor)
.setClassLoader(basicProcessor.getClass().getClassLoader()); .setClassLoader(basicProcessor.getClass().getClassLoader());
@ -55,42 +53,5 @@ public class BuiltInSpringProcessorFactory implements ProcessorFactory {
return null; return null;
} }
private boolean checkCanLoad() {
try {
ApplicationContext.class.getClassLoader();
return applicationContext != null;
} catch (Throwable ignore) {
}
return false;
}
@SuppressWarnings("unchecked")
private static <T> T getBean(String className, ApplicationContext ctx) throws Exception {
// 0. 尝试直接用 Bean 名称加载
try {
final Object bean = ctx.getBean(className);
if (bean != null) {
return (T) bean;
}
} catch (Exception ignore) {
}
// 1. ClassLoader 存在则直接使用 clz 加载
ClassLoader classLoader = ctx.getClassLoader();
if (classLoader != null) {
return (T) ctx.getBean(classLoader.loadClass(className));
}
// 2. ClassLoader 不存在(系统类加载器不可见)尝试用类名称小写加载
String[] split = className.split("\\.");
String beanName = split[split.length - 1];
// 小写转大写
char[] cs = beanName.toCharArray();
cs[0] += 32;
String beanName0 = String.valueOf(cs);
log.warn("[SpringUtils] can't get ClassLoader from context[{}], try to load by beanName:{}", ctx, beanName0);
return (T) ctx.getBean(beanName0);
}
} }

View File

@ -0,0 +1,34 @@
package tech.powerjob.worker.processor.impl;
import org.apache.commons.lang3.exception.ExceptionUtils;
import tech.powerjob.common.serialize.JsonUtils;
import tech.powerjob.worker.core.processor.ProcessResult;
import tech.powerjob.worker.core.processor.TaskContext;
import tech.powerjob.worker.core.processor.sdk.BasicProcessor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
class MethodBasicProcessor implements BasicProcessor {
private final Object bean;
private final Method method;
public MethodBasicProcessor(Object bean, Method method) {
this.bean = bean;
this.method = method;
}
@Override
public ProcessResult process(TaskContext context) throws Exception {
try {
Object result = method.invoke(bean, context);
return new ProcessResult(true, JsonUtils.toJSONString(result));
} catch (InvocationTargetException ite) {
ExceptionUtils.rethrow(ite.getTargetException());
}
return new ProcessResult(false, "IMPOSSIBLE");
}
}