最近遭遇collection reference错误使用的bug,原始代码:
List<CallbackRequestModel> callbackModels = new ArrayList<CallbackRequestModel>(datas.size());
for (TaskData<BatchUserData> taskData : datas) {
CallbackRequestModel request = new CallbackRequestModel();
BatchUserData user = taskData.getData();
request.setData(user.getUserId());
//先解析任务配置时的扩展数据
Map<String, Object> configExtra=dispatch.getSubTask().getTaskConfig().getOutBizMarkMap();
if (configExtra==null){
configExtra=new HashMap<String,Object>();//原始任务数据被替换、当前临时数据写入任务配置中、被其他线程使用
}
//再解析任务数据的扩展信息
Map<String, Object> dataExtra = JSONUtil.parseToMap(taskData.getExtraData());
//任务数据扩展信息不为空时,合并两份扩展
if (!CollectionUtils.isEmpty(dataExtra)) {
configExtra.putAll(dataExtra);
}
request.setExtraData(configExtra);
callbackModels.add(request);
}
修改后:
List<CallbackRequestModel> callbackModels = new ArrayList<CallbackRequestModel>(datas.size());
for (TaskData<BatchUserData> taskData : datas) {
CallbackRequestModel request = new CallbackRequestModel();
BatchUserData user = taskData.getData();
request.setData(user.getUserId());
//单独新增扩展数据map,拷贝元素任务扩展数据
Map<String, Object> configExtra = new HashMap<String, Object>();
//先解析任务配置时的扩展数据
Map<String, Object> outBizMarkMap=dispatch.getSubTask().getTaskConfig().getOutBizMarkMap();
if (!CollectionUtils.isEmpty(outBizMarkMap)){
configExtra.putAll(outBizMarkMap);
}
//再解析任务数据的扩展信息
Map<String, Object> dataExtra = JSONUtil.parseToMap(taskData.getExtraData());
//任务数据扩展信息不为空时,合并两份扩展
if (!CollectionUtils.isEmpty(dataExtra)) {
configExtra.putAll(dataExtra);
}
request.setExtraData(configExtra);
callbackModels.add(request);
}
java collection reference immutable引用不可修改:
List<String> names = new ArrayList<String>();
names.add(“helo”);
names.add(“google”);
final List<String> list = names;
list.add(“baidu”);//集合可修改list = new ArrayList<String>();//引用不可修改
java collection immutable引用所指对象不可修改:
List<String> names = new ArrayList<String>();
names.add(“helo”);
names.add(“google”);
List<String> copyList = Collections.unmodifiableList(names);copyList.add(“world”); 集合不可修改
copyList=new ArrayList<String>();//引用可修改