java 异步执行代码(先返回结果,后执行代码
业务场景:
在用户那,角色那变更后,要更新数据,因为更新要比较长时间,需要先返回结果(2:已接收待执行)。更新结束后,再返回值结果。
(执行结果. 0:执行失败 ; 1:执行成功; 2:已接收待执行)
处理1: 简单异步
使用 ExecutorService 异步
-
public void onCallback(JSONObject param) {
-
ExecutorService executor = Executors.newCachedThreadPool();
-
executor.execute(() -> {
-
try {
-
Thread.sleep(1000 * 10);
-
} catch (InterruptedException e) {
-
e.printStackTrace();
-
}
-
// 这边执行具体的方法
-
this.syncDealResult(param);
-
-
});
-
executor.shutdown();
-
}
-
-
public JSONObject dealResult(JSONObject params) {
-
// 先返回结果,然后异步执行
-
this.onCallback(params);
-
JSONObject result = new JSONObject();
-
result.put("excRs", "2");
-
return result;
-
}
-
-
-
public void syncDealResult(JSONObject params) {
-
logger.info("deal abRole param {}", JSON.toJSONString(params));
-
String logId = MapUtils.getString(params, "logId");
-
String excRs = "1";
-
try {
-
// 具体操作
-
} catch (Exception e) {
-
e.printStackTrace();
-
excRs = "-1";
-
}
-
logger.info("update abRole finish callRecordId {}, excRs {}", logId, excRs);
-
// 处理完后推送结果
-
JSONObject param = new JSONObject();
-
param.put("logId", logId);
-
param.put("excRs", excRs);
-
// 推送结果
-
}
加 Thread.sleep(1000 * 10); 就明显看得出差别了。
如果是有多种异步执行,比如:A执行完后,B要做通知;C要入库;D要做统计,这时候要怎么处理呢?
处理2:多个异步执行
IRoleCallback
-
import com.alibaba.fastjson.JSONObject;
-
import org.apache.commons.lang3.StringUtils;
-
-
/**
-
* AB角色异步调用接口
-
*
-
*/
-
public interface IRoleCallback {
-
-
/**
-
* Computes a result, or throws an exception if unable to do so.
-
*
-
* @param param 结果
-
* @return computed result
-
* @throws Exception if unable to compute a result
-
*/
-
Object call(JSONObject param) throws Exception;
-
-
/**
-
* unique name of callback
-
*
-
* @return callback name
-
*/
-
default String name() {
-
return StringUtils.uncapitalize(getClass().getSimpleName());
-
}
-
-
/**
-
* prior to callback 用于排序
-
*
-
* @return order
-
*/
-
default double order() {
-
return 1.0d;
-
}
-
-
}
RoleCallbackRegister
-
import java.util.*;
-
-
public class RoleCallbackRegister {
-
private static final Map<String, IRoleCallback> CALLBACKS = new HashMap<>();
-
-
public static boolean register(IRoleCallback callback) {
-
if (CALLBACKS.containsKey(callback.name())) {
-
return false;
-
}
-
CALLBACKS.put(callback.name(), callback);
-
return true;
-
}
-
-
public static List<IRoleCallback> getCallbacks() {
-
List<IRoleCallback> roleCallbacks = new ArrayList<>(CALLBACKS.values());
-
roleCallbacks.sort(Comparator.comparingDouble(IRoleCallback::order));
-
return roleCallbacks;
-
}
-
}
SpringUtils
-
-
public class SpringUtils implements ApplicationContextAware {
-
-
private static ApplicationContext applicationContext;
-
-
-
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
-
SpringUtils.applicationContext = applicationContext;
-
}
-
-
public static Object getBean(String name) {
-
return applicationContext.getBean(name);
-
}
-
-
public static <T> T getBean(Class<T> clazz) {
-
return applicationContext.getBean(clazz);
-
}
-
}
AbstractRoleCallbackImpl
-
import com.web.work.common.support.SpringUtils;
-
import com.alibaba.fastjson.JSONObject;
-
import org.apache.commons.lang3.StringUtils;
-
import org.slf4j.Logger;
-
import org.slf4j.LoggerFactory;
-
import org.springframework.beans.factory.InitializingBean;
-
-
public abstract class AbstractRoleCallbackImpl implements IRoleCallback, InitializingBean {
-
protected final Logger logger = LoggerFactory.getLogger(getClass());
-
-
-
public Object call(JSONObject param) throws Exception {
-
return doCall(param);
-
}
-
-
protected abstract Object doCall(JSONObject param) throws Exception;
-
-
-
public String name() {
-
return StringUtils.uncapitalize(getClass().getSimpleName());
-
}
-
-
-
public void afterPropertiesSet() {
-
boolean register = RoleCallbackRegister.register(SpringUtils.getBean(this.getClass()));
-
if (!register) {
-
logger.error("register role callback name:{} failed.", name());
-
} else {
-
logger.info("register role callback name:{} succeed.", name());
-
}
-
}
-
}
RoleCallBackService
-
-
public class RoleCallBackService implements InitializingBean, DisposableBean {
-
-
private final static Logger logger = LoggerFactory.getLogger(RoleCallBackService.class);
-
-
private ThreadPoolExecutor pool;
-
-
public void onCallback(JSONObject param) {
-
pool.execute(() -> {
-
RoleCallbackRegister.getCallbacks().forEach(x -> {
-
try {
-
logger.info("call {}", x.name());
-
x.call(param);
-
} catch (Exception e) {
-
logger.error("回调{}接口失败:", x.name(), e);
-
}
-
});
-
});
-
}
-
-
-
public void afterPropertiesSet() {
-
int size = Runtime.getRuntime().availableProcessors() 1;
-
pool = new ThreadPoolExecutor(size, size * 2, 300L, TimeUnit.SECONDS, new LinkedBlockingQueue<>(1000),
-
Executors.defaultThreadFactory(), new ThreadPoolExecutor.CallerRunsPolicy());
-
}
-
-
-
public void destroy() throws Exception {
-
pool.shutdown();
-
}
-
}
RoleUpdateService
-
-
public class RoleUpdateService extends AbstractRoleCallbackImpl {
-
-
private final static Logger logger = LoggerFactory.getLogger(RoleUpdateService.class);
-
-
-
protected Object doCall(JSONObject params) throws Exception {
-
Thread.sleep(1000 * 10);
-
logger.info("deal abRole param {}", JSON.toJSONString(params));
-
String logId = MapUtils.getString(params, "logId");
-
String excRs = "1";
-
try {
-
// 执行更新操作
-
} catch (Exception e) {
-
e.printStackTrace();
-
excRs = "-1";
-
}
-
logger.info("update abRole finish callRecordId {}, excRs {}", logId, excRs);
-
// 处理完后推送结果
-
JSONObject param = new JSONObject();
-
param.put("logId", logId);
-
param.put("excRs", excRs);
-
logger.info("update role record {}", JSON.toJSONString(param));
-
// 推送结果
-
return "";
-
}
-
}
先返回结果后执行
-
-
private RoleCallBackService roleCallBackService;
-
-
public JSONObject dealResult(JSONObject params) {
-
// 先返回结果,然后异步执行
-
try {
-
roleCallBackService.onCallback(params);
-
} catch (Exception e) {
-
e.printStackTrace();
-
}
-
JSONObject result = new JSONObject();
-
result.put("excRs", "2");
-
return result;
-
}
总结:
要先返回结果,后执行内容,需要使用异步的方式,用ExecutorService进行处理。如果是单个的,就直接调用比较简单。如果是多个的,就先要注册下,然后遍历去调用。
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /boutique/detail/tanheckihk
系列文章
更多
同类精品
更多
-
微信小程序没声音怎么办
PHP中文网 06-15 -
excel图片置于文字下方的方法
PHP中文网 06-27 -
怎样阻止微信小程序自动打开
PHP中文网 06-13 -
微信人名旁边有个图标有什么用
PHP中文网 03-11 -
微信提示登录环境异常是什么意思原因
PHP中文网 04-09 -
微信获取用户openid失败怎么办
PHP中文网 03-26 -
photoshop怎么把印章抠出并放在另一张图上
PHP中文网 06-15 -
Excel筛选和排序是灰色的怎么办
PHP中文网 06-22 -
EhViewer(E绅士)最新版_ehviewer白色版彩色版_Ehviewer显示网络错误怎么办?e站进不去了怎么办
Evanpatchouli 09-19 -
photoshop蒙版画笔没反应怎么办
PHP中文网 06-24