java - 从 Web 服务器智能地提供 jar 文件

标签 java webserver jetty jnlp

我正在编写一个简单(通用)包装 Java 类,它将在与部署的 Web 服务器分开的各种计算机上执行。我想下载最新版本的 jar 文件,该文件是来自关联 Web 服务器(当前为 Jetty 8)的应用程序。

我有这样的代码:

// Get the jar URL which contains the application
URL jarFileURL = new URL("jar:http://localhost:8081/myapplication.jar!/");
JarURLConnection jcl = (JarURLConnection) jarFileURL.openConnection();

Attributes attr = jcl.getMainAttributes();
String mainClass = (attr != null)
            ? attr.getValue(Attributes.Name.MAIN_CLASS)
            : null;
if (mainClass != null)  // launch the program

这工作得很好,除了 myapplication.jar 是一个很大的 jar 文件(一个 OneJar jar 文件,所以里面有很多东西)。我希望这尽可能高效。 jar 文件不会经常更改。

  1. 能否将 jar 文件保存到磁盘(我看到了如何获取 JarFile 对象,但没有保存它)?
  2. 更重要的是,但与 #1 相关的是,jar 文件可以以某种方式缓存吗?

    2.1 我可以(轻松)在 Web 服务器上请求 jar 文件的 MD5,并仅在发生更改时才下载它吗?
    2.2 如果没有,是否有另一种缓存机制,也许只请求 list ?版本/构建信息可以存储在那里。

如果有人做了类似的事情,你能详细地描述一下要做什么吗?

每个初始响应的更新

建议在请求中使用 If-Modified-Since header 并在 URL 上使用 openStream 方法来获取要保存的 jar 文件。

根据这一反馈,我添加了一条关键信息和一些更有针对性的问题。

我上面描述的 java 程序运行从引用的 jar 文件下载的程序。该程序将运行大约 30 秒到 5 分钟左右。然后就完成并退出了。有些用户可能每天多次运行该程序(甚至最多 100 次),而另一些用户则可能每隔一周运行一次。它应该仍然足够聪明,知道它是否具有最新版本的 jar 文件。

更集中的问题:

If-Modified-Since header 在此用法中仍然有效吗?如果是这样,我是否需要完全不同的代码来添加它?也就是说,您能告诉我如何修改所提供的代码以包含该内容吗?关于保存 jar 文件的同样问题 - 最终我真的很惊讶(沮丧!)我可以获得 JarFile 对象,但无法持久化它 - 我什至需要 JarURLConnection 类吗?

赏金问题

我最初并没有意识到我想问的确切问题。是这样的:

如何在退出的命令行程序中本地保存来自 Web 服务器的 jar 文件,并且仅在服务器上更改该 jar 文件时才更新该文件?

任何通过代码示例展示如何完成此操作的答案都将获得赏金。

最佳答案

  1. 是的,文件可以保存到磁盘,您可以使用URL类中的openStream()方法获取输入流。

  2. 根据@fge提到的评论,有一种方法可以检测文件是否被修改。

示例代码:

private void launch() throws IOException {
    // Get the jar URL which contains the application
    String jarName = "myapplication.jar";
    String strUrl = "jar:http://localhost:8081/" + jarName + "!/";

    Path cacheDir = Paths.get("cache");
    Files.createDirectories(cacheDir);
    Path fetchUrl = fetchUrl(cacheDir, jarName, strUrl);
    JarURLConnection jcl = (JarURLConnection) fetchUrl.toUri().toURL().openConnection();

    Attributes attr = jcl.getMainAttributes();
    String mainClass = (attr != null) ? attr.getValue(Attributes.Name.MAIN_CLASS) : null;
    if (mainClass != null) {
        // launch the program
    }
}

private Path fetchUrl(Path cacheDir, String title, String strUrl) throws IOException {
    Path cacheFile = cacheDir.resolve(title);
    Path cacheFileDate = cacheDir.resolve(title + "_date");
    URL url = new URL(strUrl);
    URLConnection connection = url.openConnection();
    if (Files.exists(cacheFile) && Files.exists(cacheFileDate)) {
        String dateValue = Files.readAllLines(cacheFileDate).get(0);
        connection.addRequestProperty("If-Modified-Since", dateValue);

        String httpStatus = connection.getHeaderField(0);
        if (httpStatus.indexOf(" 304 ") == -1) { // assuming that we get status 200 here instead
            writeFiles(connection, cacheFile, cacheFileDate);
        } else { // else not modified, so do not do anything, we return the cache file
            System.out.println("Using cached file");
        }
    } else {
        writeFiles(connection, cacheFile, cacheFileDate);
    }

    return cacheFile;
}

private void writeFiles(URLConnection connection, Path cacheFile, Path cacheFileDate) throws IOException {
    System.out.println("Creating cache entry");
    try (InputStream inputStream = connection.getInputStream()) {
        Files.copy(inputStream, cacheFile, StandardCopyOption.REPLACE_EXISTING);
    }
    String lastModified = connection.getHeaderField("Last-Modified");
    Files.write(cacheFileDate, lastModified.getBytes());
    System.out.println(connection.getHeaderFields());
}

关于java - 从 Web 服务器智能地提供 jar 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27825115/

相关文章:

java - 获取异常 org.apache.logging.slf4j.SLF4JLoggerContext 无法转换为 org.apache.logging.log4j.core.LoggerContext

JavaFX - 为类提供对 Controller 方法的访问权限以在 Canvas 上绘制

nginx - Nginx-在多个目录中搜索静态内容?

java - 使用 CXF、Jetty 和 Spring 的 Web 服务

java - jetty "javax.net.ssl.SSLHandshakeException: no cipher suites in common"

java - 如何创建注释以格式化金额值

java - 在 Spring Data REST 中发布 @OneToMany 子资源关联

ubuntu - 监控和降低高 CPU 使用率

python - 何时使用 Tornado,何时使用 Twisted/Cyclone/GEvent/other

java - Jetty Tapestry 热部署=服务器重启?