1

I have the following problem, I write an Excel file in C:\Tomcat85\webapps\MyWebApp\Excel\myExcel.xls.

As soon as my Java application finishes writing the file, it performs a download for the user to work with it. This gives a nasty 404 error.

If I wait a few seconds and reload the page it downloads all right (or adding a five second sleep in java, it works the same).

So, what I conclude is that Tomcat is taking 5 seconds to recognize that this new excel file exists and just then is able to serve it.

Is there anyway to make Tomcat perform this task faster? Maybe using some configuration in web.xml to treat that "/Excel/" folder differently.

Windows 10 64bits, Tomcat 8.5, Java 7 (could try java8 but I dont think it will make a difference).

Some code:

new ExcelExport(remoteHandle, context).execute( outFileName, outMessage);
// Thread.sleep(5000);
httpContext.wjLoc = formatLink(outFileName);
  • sleep is commented or uncommented depending on the test. With out the sleep, I get 404, with the sleep in 5 seconds it works fine.
  • httpContet.wjLoc just performs the download, as a link to a file.

The wiriting is working fine, as I see it ready and writable in File Explorer, but if I try to open it by URL I get the same 404.

Italo Borssatto
  • 15,044
  • 7
  • 62
  • 88
Federico Alvarez
  • 1,459
  • 1
  • 19
  • 30

1 Answers1

2

Resources are cached by default. The amount of time in milliseconds between the revalidation of cache entries is defined by the cacheTtl parameter, referenced in this documentation. By default its value is 5 seconds.

If you want to disable the cache, just set the cachingAllowed to false.

Italo Borssatto
  • 15,044
  • 7
  • 62
  • 88
  • Thanks Italo, I tryed your idea but it did not work. I added a /META-INF/context.xml, and inside: "". Then deleted work folder and restarted tomcat. The result is the same 404. Also tryed with cachingAllowed="false", but no change. What keeps me thinking, is that I do not want to disable cache o reduce it's TTL, i want it to be available faster. Any other thoughts? Thanks again, Federico. – Federico Alvarez Jun 30 '17 at 18:35
  • 2
    @FedericoAlvarez The `Context` element doesn't have the `cacheTTL` and the `cachingAllowed` parameters. The `Resources` element has. A `Resources` element MAY be nested inside a `Context` component. If you don't have the `Resources` element, add it. Like ``. – Italo Borssatto Jun 30 '17 at 18:39
  • Thanks Italo! It worked fine! With this: ``. Now should I ask: does this have a performance penlty? Beacause resources will not be cached (I set TTL to 1), right? What's your experience? Thanks again, Federico. – Federico Alvarez Jun 30 '17 at 18:47
  • 1
    It'll bring problems if the files are highly downloaded. In this case, I suggest you to use this solution just in emergencial situations. For long term solutions, implement a `Filter` to get your files the way you want. If the files are not accessed frequently, I wouldn't waste time implementing a filter. Choose what is better in your case. I allways prefer the simple one before hit some real problem. – Italo Borssatto Jun 30 '17 at 18:51
  • Good, thanks again. I've just read this [Tomcat Filters](https://tomcat.apache.org/tomcat-7.0-doc/config/filter.html#Introduction), but cant figure out which one to use, should it be something related with ExpiresFilter? Could you point me in the right direction? – Federico Alvarez Jun 30 '17 at 18:59
  • 1
    Do you think your files will be downloaded more than one time between each five seconds? If it's a document like an Excel file, I don't think so. I wouldn't spend time with that, just if it'll really be downloaded like images that are part of your webpage. A file that is downloaded once and a while, don't need a cache on Tomcat. And you can put a cache in front of Tomcat using Apache or Ngnx. – Italo Borssatto Jun 30 '17 at 19:19
  • Sure, I see, using Apache is a possibility, but currently we do not, and for example, JS and CSS files used in the Tomcat App are served through Tomcat. And they would not be cached. Is there a possible solution to this? Where Tomcat caches static content, but is able to serve new content on demand (maybe on a specific folder). – Federico Alvarez Jun 30 '17 at 19:56
  • 1
    I never did that, but you can extend the `org.apache.catalina.WebResourceRoot` class and pass it to the parameter `className` of the `Resources` element. – Italo Borssatto Jun 30 '17 at 20:23
  • Thanks a lot Italo for taking so much time in my issue, but I think this is getting too complex for something that should be really simple: serve a file if it's there. Will keep trying. Thanks again! – Federico Alvarez Jun 30 '17 at 20:54
  • 1
    I ended up adding the following line to /META-INF/context.xml: ''. I still have to look around for the downsides of this. Thanks a lot Italo. – Federico Alvarez Apr 17 '18 at 20:36