mirror of
https://github.com/PowerJob/PowerJob.git
synced 2025-07-17 00:00:04 +08:00
feat: support powerjob method handler
This commit is contained in:
parent
8ecc5768c7
commit
8953ecc74f
@ -1,25 +1,31 @@
|
||||
package tech.powerjob.samples.tester;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
import tech.powerjob.worker.annotation.PowerJob;
|
||||
import tech.powerjob.worker.annotation.PowerJobHandler;
|
||||
import tech.powerjob.worker.core.processor.TaskContext;
|
||||
import tech.powerjob.worker.log.OmsLogger;
|
||||
|
||||
@Component
|
||||
public class SpringMethodProcessorService {
|
||||
|
||||
@PowerJob("test")
|
||||
public String test(TaskContext context) {
|
||||
@PowerJobHandler(name = "testEmptyReturn")
|
||||
public void test(TaskContext context) {
|
||||
OmsLogger omsLogger = context.getOmsLogger();
|
||||
omsLogger.warn("测试日志");
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
@PowerJob("test1")
|
||||
@PowerJobHandler(name = "testNormalReturn")
|
||||
public String test1(TaskContext context) {
|
||||
OmsLogger omsLogger = context.getOmsLogger();
|
||||
omsLogger.warn("测试日志");
|
||||
return "测试日志";
|
||||
return "testNormalReturn";
|
||||
}
|
||||
|
||||
@PowerJobHandler(name = "testThrowException")
|
||||
public String testThrowException(TaskContext context) {
|
||||
OmsLogger omsLogger = context.getOmsLogger();
|
||||
omsLogger.warn("testThrowException");
|
||||
throw new IllegalArgumentException("test");
|
||||
}
|
||||
}
|
||||
|
@ -5,17 +5,21 @@ 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 PowerJob {
|
||||
public @interface PowerJobHandler {
|
||||
|
||||
|
||||
/**
|
||||
* handler name
|
||||
*/
|
||||
String value();
|
||||
String name();
|
||||
|
||||
|
||||
|
@ -3,7 +3,7 @@ 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.PowerJob;
|
||||
import tech.powerjob.worker.annotation.PowerJobHandler;
|
||||
import tech.powerjob.worker.extension.processor.ProcessorBean;
|
||||
import tech.powerjob.worker.extension.processor.ProcessorDefinition;
|
||||
import tech.powerjob.worker.processor.MethodBasicProcessor;
|
||||
@ -45,17 +45,17 @@ public class BuildInSpringMethodProcessorFactory extends AbstractBuildInSpringPr
|
||||
log.info("[ProcessorFactory] can't parse processorDefinition, this processor can't load by 'BuildInSpringMethodProcessorFactory'");
|
||||
return null;
|
||||
}
|
||||
String[] split = processorInfo.split("#");
|
||||
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) {
|
||||
PowerJob powerJob = method.getAnnotation(PowerJob.class);
|
||||
PowerJobHandler powerJob = method.getAnnotation(PowerJobHandler.class);
|
||||
if (powerJob == null) {
|
||||
continue;
|
||||
}
|
||||
String name = powerJob.value();
|
||||
String name = powerJob.name();
|
||||
//匹配到和页面定义相同的methodName
|
||||
if (!name.equals(methodName)) {
|
||||
continue;
|
||||
|
Loading…
x
Reference in New Issue
Block a user