關於線程執行方法:
在方法裏執行threadPool.submit(new TellBreaksSuccessTask(rep)); 其中threadPool是注射進來的或自己設置,
TellBreakSuccessTask是自己定義的方法,代碼如下:
public class TellBreaksSuccessTask implements Callable {
protected final Logger logger = LoggerFactory.getLogger(getClass());
private RepayExemptionPre repayExemptionPre;
private RepayExemptionService repayExemptionService;
public TellBreaksSuccessTask(RepayExemptionPre repayExemptionPre){
this.repayExemptionPre = repayExemptionPre;
this.repayExemptionService = (RepayExemptionService) ContextUtils.getBean(RepayExemptionService.class);
}
@Override
public Object call() throws Exception {
logger.debug("do preBreaks success:{}",repayExemptionPre);
repayExemptionService.preBreaksSuccess(repayExemptionPre);
logger.debug("=======do preBreaks end ==============");
return null;
}
注:threadPool.submit(new TellBreaksSuccessTask(rep));方法是有返回值的。Future
package com.mobanker.framework.context;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
@Component
public class ContextUtils implements ApplicationContextAware{
private static ApplicationContext applicationContext;
private static final Logger logger = LoggerFactory.getLogger(ContextUtils.class);
public static ApplicationContext getApplicationContext() {
synchronized (ContextUtils.class) {
while (applicationContext == null) {
try {
ContextUtils.class.wait(60000);
if (applicationContext == null) {
logger.warn("Have been waiting for ApplicationContext to be set for 1 minute", new Exception());
}
} catch (InterruptedException ex) {
logger.debug("getApplicationContext, wait interrupted");
}
}
return applicationContext;
}
}
public static Object getBean(Class<?> beanType) {
return getApplicationContext().getBean(beanType);
}
public static Object getBean(String name) {
return getApplicationContext().getBean(name);
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException{
synchronized (ContextUtils.class) {
ContextUtils.applicationContext = applicationContext;
ContextUtils.class.notifyAll();
}
}
}