4

I am using POI's SXSSFWorkbench class to create extremely large workbooks. Multiple processes may be running of my application concurrently, so I thought it prudent to append the processId to the default temporary filename. I don't know how to do that, and could not find any recent coding examples.

Can anyone point me to an example, or outline to me what has to be done? I see there is a static TempFile.createTempFile method. Should I be executing that using a class override before instantiating the SXSSFWorkbook class? Or after?

I also saw there was a DefaultTempFileCreationStrategy class. Could not find examples of how to use this either.

Olaf Kock
  • 46,930
  • 8
  • 59
  • 90
Mike
  • 676
  • 1
  • 8
  • 17

1 Answers1

4

The main class that Apache POI uses for this is TempFile

The method you'll want to call is TempFile.setTempFileCreationStrategy

What you'll need to do is create your own class implementing the interface TempFileCreationStrategy. This is nice and simple, with just two methods, createTempDirectory and createTempFile.

To get an idea of what's involved, you can look at the source code for DefaultTempFileCreationStrategy online here. It's pretty easy, just put in the logic for your own needs in terms of threading and naming.

Mike
  • 676
  • 1
  • 8
  • 17
Gagravarr
  • 47,320
  • 10
  • 111
  • 156
  • Thank you. This has been helpful. I'm, embarrassed I didn't see this earlier. For clarification: calling TempFile.setTempFileCreationStrategy should occur before instantiating the SXSSFWorkbook object? Or right after the workbook object is created? Or anytime after SXSSFWorkbook is created and the first Sheet is created? That part isn't clear to me. – Mike Apr 03 '19 at 18:22
  • 1
    Needs to be set before, opening a workbook can use temp files depending on options chosen – Gagravarr Apr 03 '19 at 20:04
  • Be careful when using in in a multi-threaded environment. – Christoph Oct 02 '20 at 20:34