1

Now in my application I have button which create file for download. Problem is that creating file takes a long time (3s) and file is created again and again when button is pressed. Is there some way to create file asynchronous once when page is rendered and then use this file in DownloadModelLink ?

    private void addDownloadButton() {
        add(new DownloadModelLink("file", new AbstractReadOnlyModel<File>() {
            private static final long serialVersionUID = 1L;

            @Override
            public File getObject() {
                return service.createFile();
            }

        }));
    }
hudi
  • 15,555
  • 47
  • 142
  • 246
  • Does the file need to be on the filesystem? Else you could create a dynamically-created file and stream it, see here: http://stackoverflow.com/a/16782334/461499 – Rob Audenaerde Mar 08 '16 at 07:56
  • hm nope. Actually it is necessary to delete file after the process. But in this example I just see how to download existing file and I need to create it asynchronous – hudi Mar 08 '16 at 09:28
  • So you want the user to wait for 3 seconds before the page is returned? That's not very user friendly :) You could create a `Thread` that creates the file and put a waiting-`Panel` with `AjaxSelfUpdatingTimigBehaviour` op the page. You can replace the panel with a download link once the file-generation is done. – Rob Audenaerde Mar 08 '16 at 11:44
  • lol no that is the point. I want to return page immediately and in asynchronous way I want to create this file. And when user press button to download file it will wait if file is still not created or it will immediately return already created file – hudi Mar 08 '16 at 12:03
  • Ah I see. You could still create a Thread `myThread` when the page is rendered, and have a `ResourceLink`. The `WriteCallback` of the `ResourceResponse` can call `myThread.join()` to wait. Also there is `wicket-async-task` which might be worth checking out. – Rob Audenaerde Mar 08 '16 at 12:17
  • yea but according this thread http://stackoverflow.com/questions/10195056/wicket-and-multi-threaded-business-object it is bad idea – hudi Mar 08 '16 at 12:44
  • You should put the Thread / Threadpool in the `Application`. See also how the wicket-async-task works (good point btw ;). – Rob Audenaerde Mar 08 '16 at 12:59

0 Answers0