我正在尝试使用另一个类中的一个类的方法访问实例并尝试修改它们。但它给我的错误是“不能引用封闭范围内定义的非最终局部变量 nextSourceOffset。它给了我一个修复,将变量修改为 final ”。但如果我将它更改为 final,我不能增加它们。
我正在使用以下代码:
public String produce(String lastSourceOffset, final int maxBatchSize, BatchMaker batchMaker)
throws StageException {
long nextSourceOffset = 0;
if (lastSourceOffset != null) {
nextSourceOffset = Long.parseLong(lastSourceOffset);
}
final RtmClient client = new RtmClientBuilder(getEndPoint(), getAppKey()).setListener(new RtmClientAdapter() {
@Override
public void onEnterConnected(RtmClient client) {
}
}).build();
SubscriptionAdapter listener = new SubscriptionAdapter() {
@Override
public void onSubscriptionData(SubscriptionData data) {
int numRecords = 0;
for (AnyJson json : data.getMessages()) {
jsonString = json.toString();
Record record = getContext().createRecord("some-id::" + nextSourceOffset);
Map<String, Field> map = new HashMap<>();
map.put("fieldName", Field.create(jsonString));
record.set(Field.create(map));
batchMaker.addRecord(record);
++nextSourceOffset;
++numRecords;
}
}
};
它在以下位置引发错误:
Record record = getContext().createRecord("some-id::" + nextSourceOffset);//error
Map<String, Field> map = new HashMap<>();
map.put("fieldName", Field.create(jsonString));
record.set(Field.create(map));
batchMaker.addRecord(record);//error
++nextSourceOffset;//error
++numRecords;//error