如何防止 Java Web Start 应用程序缓存额外的下载资源 [英] How to prevent Java Web Start application caching additional download resources

查看:35
本文介绍了如何防止 Java Web Start 应用程序缓存额外的下载资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 JWS 应用程序使用一个库(也是我的代码),它在后台从服务器检索各种 XML 文档:

My JWS application uses a library (also my code) that under the hood retrieves various XML documents from server:

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder(); 
Document doc = db.parse(xmlFileUrl);

URL 的内容会不时发生变化,应用程序需要在启动时和运行时重新获取内容.

The contents at the URLs change from time to time, and application needs to re-fetch the content at start up and later while running.

问题在于 JWS 缓存内容并为后续请求返回缓存的内容.

告诉 JWS 删除 URL 的缓存内容确实有效:

Telling JWS to remove cached content for the URL does work:

DownloadService ds = (DownloadService)ServiceManager.lookup("javax.jnlp.DownloadService");
ds.removeResource(xmlFileUrl, null);

但是库被其他前端使用,我想避免让库依赖于javaws.jar.我当前的解决方案在库中定义了一个接口,允许库请求清除缓存.JWS 启动器将接口的实现传递给库.有多个组件可以获取各种资源,虽然我可以让它工作,但整个过程很笨拙.

But the library is used by other front ends, and I want to avoid making the library dependent on javaws.jar. My current solution defines an interface in the library that allows library to request cache clearing. JWS launcher passes an implementation of the interface to the library. There are multiple components that fetch various resources and while I can make it work, the whole thing is clumsy.

另一种解决方法是为每个请求附加一个唯一的查询,例如

Another workaround is to append a unique query for every request, e.g.

url += "?ignored="+System.currentTimeMillis()+Math.random();

但这会污染 JWS 缓存,我将其视为混乱、wtf 和膨胀的根源.

but that will pollute JWS cache and I see it as source of confusion, wtf and bloat.

XML 文档在服务器上生成.设置标题:

The XML documents are generated on the server. Setting header:

Cache-Control: no-cache'

没有帮助.

我对这个问题是否有更清洁的解决方案很感兴趣.如果我可以在服务器上设置一些 HTTP 标头,那将是理想的.列出不缓存在 .jnlp 中的资源是可以接受的,但并不理想,因为 URL 是在库中构造的,我必须显着更改初始化代码.欢迎使用其他方法和想法.

I am interested if there is a cleaner solution to this problem. If I could set some HTTP headers on server that would be ideal. Listing resources not to be cached in .jnlp would be acceptable but not ideal as URLs are constructed in the library and I'd have to significantly change the init code. Other methods and ideas are welcome.

推荐答案

我很惊讶之前没有看到 void java.net.URLConnection.setUseCaches(boolean usecaches) 方法.

I'm surprised I did not see void java.net.URLConnection.setUseCaches(boolean usecaches) method before.

此解决方案适用于 JWS.它不依赖于干净简单的 javaws.jar.

This solution works including in JWS. It does not make dependency on javaws.jar which is clean and simple.

URL url = new URL(xmlFileLocation);
URLConnection connection = url.openConnection();
// Prevent JavaWebStart from returning cached copy.
connection.setUseCaches(false);

// Now fetch the content, e.g.
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder(); 
Document doc = db.parse(connection.getInputStream());

这仍然是落后的,因为使用服务器响应的应用程序决定不使用缓存,而不是框架 (JWS) 支持服务器端缓存控制 HTTP 标头.

This is still backward in that the application that consumes the server response decides to not use the cache, rather than the framework(JWS) honoring server side cache control HTTP headers.

这篇关于如何防止 Java Web Start 应用程序缓存额外的下载资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆