0

除了 read()、updated() 和 open() 方法之外,还有什么方法可以在执行上下文中添加条目。

就像在下面的代码中一样,我试图在 close 方法中添加条目。

public class MyFileReader extends FlatFileItemReader<AccountDetails>{
private long currentRowProcessedCount = 0;

    @Autowired
    private ExecutionContext executionContext;

    @Override
    public synchronized AccountDetails read() throws Exception, UnexpectedInputException, ParseException {      
        AccountDetails accDetailsObj = super.read();

        currentRowProcessedCount++;

        return accDetailsObj;
    }   

    @Override
    public void open(ExecutionContext executionContext) throws ItemStreamException {
        super.open(executionContext);

        currentRowProcessedCount = executionContext.getLong(Constants.CONTEXT_COUNT_KEY.getStrValue(),0);
        this.executionContext = executionContext;
    }

    @Override
    public void update(ExecutionContext executionContext) throws ItemStreamException {  


        executionContext.putLong(Constants.CONTEXT_COUNT_KEY.getStrValue(), currentRowProcessedCount);
    }


    @Override
    public void close() throws ItemStreamException {
        System.out.println("close --------------"+currentRowProcessedCount);
        System.out.println(executionContext.getLong(Constants.CONTEXT_COUNT_KEY.getStrValue()));
         this.executionContext.putLong(Constants.CONTEXT_COUNT_KEY.getStrValue(), currentRowProcessedCount);


    }
 }

在上面的示例中,我无法更新新条目。它只能以只读方式工作。我可以读取数据但不能写入。

class abc{
@Autowired
private ExecutionContext executionContext;

    public AccountDetails mapFieldSet(FieldSet fieldSet) throws BindException {

                    executionContext.putLong(Constants.CONTEXT_COUNT_KEY.getStrValue(), 47);

        return accDetailsObj;
        }

}

我还需要在其他类中更新 executionContext。有什么办法吗?

4

1 回答 1

0

您只是put(String key, Object value)用来覆盖已经存在的值。ExecutionContext 得到了支持,ConcurrentHashMap所以如果你真的想要它,你可以通过反射获得对它的引用,然后使用computeIfAbsent等等......

还计算已经实现AbstractItemCountingItemStreamItemReader并且如果您从它继承(并且您是),这应该已经解决了。

于 2017-06-12T11:34:37.357 回答