Akka延迟10秒后,如何从文件中读取每一行?

时间:2019-01-02 09:10:52

标签: java akka akka-stream

我试图逐行读取文件,但有些延迟。

例如:

  • 读取并打印文件的第一行
  • 等待10秒
  • 读取并打印文件的第二行

到目前为止,我已经尝试过类似的事情:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        view.setOnTouchListener(new OnSwipeTouchListener(MainActivity.this) {
            public void onSwipeRight() {
                choseold = chosenow;
                chosenow = chosenow - 1;
                if (chosenow <= 0) {
                    chosenow = 5;
                    choseold = 6;
                }
                swipe();
            }
            public void onSwipeLeft() {
                choseold = chosenow;
                chosenow = chosenow + 1;
                if (chosenow >= 6) {
                    chosenow = 1;
                    choseold = 0;
                }
                swipe();
            }

        });
        view.setFocusableInTouchMode(true);


        MyAdapterlang = new MyAdapterLang(MainActivity.this, yaziki1, yaziki2, flagi);
        RVlang.setAdapter(MyAdapterlang);
        mLayoutManager = new LinearLayoutManager(this);
        RV1.setLayoutManager(mLayoutManager);
        MyAdapter = new MyAdapterApps(MainActivity.this, childs, childs2);
        RV1.setAdapter(MyAdapter);
    }
}

    class OnSwipeTouchListener implements View.OnTouchListener {

        private final GestureDetector gestureDetector;

        public OnSwipeTouchListener (Context ctx){
            gestureDetector = new GestureDetector(ctx, new GestureListener());
        }

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            return gestureDetector.onTouchEvent(event);
            //return false;
        }
        /*public boolean onTouch(final View v, MotionEvent event) {

            switch (event.getAction()) {

                case MotionEvent.ACTION_DOWN:

                    y1 = event.getY();

                    break;

                case MotionEvent.ACTION_MOVE:

                    y2 = event.getY();

                    float deltaY = y2 - y1;

                    if (Math.abs(deltaY) > MIN_DISTANCE) {
                        return false;
                    } else {
                        return gestureDetector.onTouchEvent(event);
                    }
            }

            return gestureDetector.onTouchEvent(event);
        }
        private float y1, y2;
        private static final int MIN_DISTANCE = 50;*/

        private final class GestureListener extends GestureDetector.SimpleOnGestureListener {

            private static final int SWIPE_THRESHOLD = 100;
            private static final int SWIPE_VELOCITY_THRESHOLD = 100;

            @Override
            public boolean onDown(MotionEvent e) {
                return true;
            }

            @Override
            public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
                try {
                    float diffY = e2.getY() - e1.getY();
                    float diffX = e2.getX() - e1.getX();
                    if (Math.abs(diffX) > Math.abs(diffY)) {
                        if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
                            if (diffX > 0) {
                                onSwipeRight();
                            } else {
                                onSwipeLeft();
                            }
                            return true;
                        }
                    }
                } catch (Exception exception) {
                    exception.printStackTrace();
                }
                return false;
            }
        }

        public void onSwipeRight() {
        }

        public void onSwipeLeft() {
        }
    }

但是这一次爆发过多,而不是一次爆发。

任何想法如何解决?

1 个答案:

答案 0 :(得分:2)

final CompletionStage<IOResult> result = 
 .via(Framing.delimiter(ByteString.fromString(System.lineSeparator()),10000,FramingTruncation.ALLOW))
  FileIO.fromPath(filePath)
 .throttle(1, Duration.create(10, TimeUnit.SECONDS), 1, ThrottleMode.shaping())
 .to(printlnSink)
 .run(materializer);

看看是否适合您,framing Class个文档应澄清