- 任务RollingLog展示逻辑优化,修复超时任务无法查看的问题;master
parent
af6c46743f
commit
df2b9f7e0c
11 changed files with 214 additions and 137 deletions
@ -0,0 +1,67 @@ |
||||
package com.xxl.job.core.executor.impl; |
||||
|
||||
import com.xxl.job.core.executor.XxlJobExecutor; |
||||
import com.xxl.job.core.glue.GlueFactory; |
||||
import com.xxl.job.core.handler.IJobHandler; |
||||
import com.xxl.job.core.handler.annotation.JobHandler; |
||||
import org.springframework.beans.BeansException; |
||||
import org.springframework.context.ApplicationContext; |
||||
import org.springframework.context.ApplicationContextAware; |
||||
|
||||
import java.util.Map; |
||||
|
||||
/** |
||||
* xxl-job executor (for spring) |
||||
* |
||||
* @author xuxueli 2018-11-01 09:24:52 |
||||
*/ |
||||
public class XxlJobSpringExecutor extends XxlJobExecutor implements ApplicationContextAware { |
||||
|
||||
|
||||
@Override |
||||
public void start() throws Exception { |
||||
|
||||
// init JobHandler Repository
|
||||
initJobHandlerRepository(applicationContext); |
||||
|
||||
// refresh GlueFactory
|
||||
GlueFactory.refreshInstance(1); |
||||
|
||||
|
||||
// super start
|
||||
super.start(); |
||||
} |
||||
|
||||
private void initJobHandlerRepository(ApplicationContext applicationContext){ |
||||
if (applicationContext == null) { |
||||
return; |
||||
} |
||||
|
||||
// init job handler action
|
||||
Map<String, Object> serviceBeanMap = applicationContext.getBeansWithAnnotation(JobHandler.class); |
||||
|
||||
if (serviceBeanMap!=null && serviceBeanMap.size()>0) { |
||||
for (Object serviceBean : serviceBeanMap.values()) { |
||||
if (serviceBean instanceof IJobHandler){ |
||||
String name = serviceBean.getClass().getAnnotation(JobHandler.class).value(); |
||||
IJobHandler handler = (IJobHandler) serviceBean; |
||||
if (loadJobHandler(name) != null) { |
||||
throw new RuntimeException("xxl-job jobhandler naming conflicts."); |
||||
} |
||||
registJobHandler(name, handler); |
||||
} |
||||
} |
||||
} |
||||
} |
||||
|
||||
// ---------------------- applicationContext ----------------------
|
||||
private static ApplicationContext applicationContext; |
||||
@Override |
||||
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { |
||||
this.applicationContext = applicationContext; |
||||
} |
||||
public static ApplicationContext getApplicationContext() { |
||||
return applicationContext; |
||||
} |
||||
|
||||
} |
@ -0,0 +1,80 @@ |
||||
package com.xxl.job.core.glue.impl; |
||||
|
||||
import com.xxl.job.core.executor.impl.XxlJobSpringExecutor; |
||||
import com.xxl.job.core.glue.GlueFactory; |
||||
import org.slf4j.Logger; |
||||
import org.slf4j.LoggerFactory; |
||||
import org.springframework.beans.factory.annotation.Autowired; |
||||
import org.springframework.beans.factory.annotation.Qualifier; |
||||
import org.springframework.core.annotation.AnnotationUtils; |
||||
|
||||
import javax.annotation.Resource; |
||||
import java.lang.reflect.Field; |
||||
import java.lang.reflect.Modifier; |
||||
|
||||
/** |
||||
* @author xuxueli 2018-11-01 |
||||
*/ |
||||
public class SpringGlueFactory extends GlueFactory { |
||||
private static Logger logger = LoggerFactory.getLogger(SpringGlueFactory.class); |
||||
|
||||
|
||||
/** |
||||
* inject action of spring |
||||
* @param instance |
||||
*/ |
||||
@Override |
||||
public void injectService(Object instance){ |
||||
if (instance==null) { |
||||
return; |
||||
} |
||||
|
||||
if (XxlJobSpringExecutor.getApplicationContext() == null) { |
||||
return; |
||||
} |
||||
|
||||
Field[] fields = instance.getClass().getDeclaredFields(); |
||||
for (Field field : fields) { |
||||
if (Modifier.isStatic(field.getModifiers())) { |
||||
continue; |
||||
} |
||||
|
||||
Object fieldBean = null; |
||||
// with bean-id, bean could be found by both @Resource and @Autowired, or bean could only be found by @Autowired
|
||||
|
||||
if (AnnotationUtils.getAnnotation(field, Resource.class) != null) { |
||||
try { |
||||
Resource resource = AnnotationUtils.getAnnotation(field, Resource.class); |
||||
if (resource.name()!=null && resource.name().length()>0){ |
||||
fieldBean = XxlJobSpringExecutor.getApplicationContext().getBean(resource.name()); |
||||
} else { |
||||
fieldBean = XxlJobSpringExecutor.getApplicationContext().getBean(field.getName()); |
||||
} |
||||
} catch (Exception e) { |
||||
} |
||||
if (fieldBean==null ) { |
||||
fieldBean = XxlJobSpringExecutor.getApplicationContext().getBean(field.getType()); |
||||
} |
||||
} else if (AnnotationUtils.getAnnotation(field, Autowired.class) != null) { |
||||
Qualifier qualifier = AnnotationUtils.getAnnotation(field, Qualifier.class); |
||||
if (qualifier!=null && qualifier.value()!=null && qualifier.value().length()>0) { |
||||
fieldBean = XxlJobSpringExecutor.getApplicationContext().getBean(qualifier.value()); |
||||
} else { |
||||
fieldBean = XxlJobSpringExecutor.getApplicationContext().getBean(field.getType()); |
||||
} |
||||
} |
||||
|
||||
if (fieldBean!=null) { |
||||
field.setAccessible(true); |
||||
try { |
||||
field.set(instance, fieldBean); |
||||
} catch (IllegalArgumentException e) { |
||||
logger.error(e.getMessage(), e); |
||||
} catch (IllegalAccessException e) { |
||||
logger.error(e.getMessage(), e); |
||||
} |
||||
} |
||||
} |
||||
} |
||||
|
||||
} |
Loading…
Reference in new issue