访问其他类中的一个类的实例的问题

时间:2017-08-22 15:27:38

标签: java streamsets

我试图在另一个类中的一个类的方法中访问实例并尝试修改它们。但它正在向我抛出错误" 无法引用封闭范围中定义的非最终局部变量nextSourceOffset。它正在给我一个修复,将变量修改为最终&# 34;但如果我将其改为最终版,我就无法增加它们。

我使用以下代码:

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

1 个答案:

答案 0 :(得分:1)

您的内部类无法从外部作用域访问非final变量。它也不能改变变量的值。

最干净的解决方案可能是将所有这些信息封装在一个新类中,并将其存储为属性。

快速而肮脏的解决方案是将本地长变量转换为long类型的单个元素数组。变量本身是最终的,因为它总是引用相同的数组,但数组的内容仍然可以更改。

示例:

 public String produce(String lastSourceOffset, final int maxBatchSize, BatchMaker batchMaker)
        throws StageException {
    long[] nextSourceOffset = {0};
    if (lastSourceOffset != null) {
        nextSourceOffset[0] = 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[0]);
                Map<String, Field> map = new HashMap<>();
                map.put("fieldName", Field.create(jsonString));
                record.set(Field.create(map));
                batchMaker.addRecord(record);
                ++nextSourceOffset[0];
                ++numRecords;

            }
        }

    };