从外部 Tomcat 目录读取可重新加载的 xml 文件 [英] Reading reloadable xml file from external Tomcat directory

查看:35
本文介绍了从外部 Tomcat 目录读取可重新加载的 xml 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

先决条件

  • Apache Tomcat 7
  • Spring 3.2.11.RELEASE
  • Apache Camel 2.14.1
  • 我的 Web 应用程序照常部署在 ${catalina.home}/webapps/中

要求

  • 读取位于 war-Archive 之外 的 xml 文件(例如来自 ${catalina.home}/myfolder/)
  • xml 文件应该可重新加载.因此,如果 xml 更改,新的 xml 文件应该在我的 Web 应用程序中可用
  • xml 文件应该映射到 Java 对象
  • Reading xml file placed outside of war-Archive (for example from ${catalina.home}/myfolder/)
  • The xml file should be reloadable. So if the xml changes the new xml file should be available in my Webapplication
  • The xml file should be mapped to Java-Objects

第一次尝试

我已经在 catalina.home/conf/catalina.properties 中通过 tomcat 将文件添加到类路径:

I have added the file to classpath via tomcat in catalina.home/conf/catalina.properties:

common.loader=${catalina.base}/lib,${catalina.base}/lib/*.jar,${catalina.home}/lib,${catalina.home}/lib/*.jar,${catalina.home}/myfolder/

文件放在这里:${catalina.home}/myfolder/myFile.xml

The file is placed here: ${catalina.home}/myfolder/myFile.xml

读取文件并将其映射到 Java 对象是通过 apache camel 中的计时器工作的.

Reading and mapping the file to Java-Objects works via a timer in apache camel.

from("timer://myTimer?fixedRate=true&period=20000")
.setBody(simple("resource:classpath:myFile.xml"))
.unmarshal(myFileJaxbDataFormat)
.process(myFileTimerProcessor);

myFileTimerProcessor 获取映射的对象并将其存储到 Spring-Bean.其他 Camel-Routes 使用此 Bean 来访问 xml 文件中包含的数据.

myFileTimerProcessor takes the mapped Objects and stores it to a Spring-Bean. This Bean is used by other Camel-Routes to access the Data contained in the xml file.

问题

正如 Claus Ibsen 在下面提到的,问题在于 ClassLoader 缓存文件.因此,如果文件中的数据已更改,则不会再次读取该文件.如果文件 ${catalina.home}/myfolder/myFile.xml 发生更改,则应该再次读取它,并且计时器应该可以使用新值,以便它可以读取应用程序的新值.

As Claus Ibsen mentioned below the problem is that the ClassLoader caches the file. So the file is not read again if data in it has changed. If the file ${catalina.home}/myfolder/myFile.xml changes it should be read again and the new values should be available to the timer so it can read the new values for the application.

是否有可能在 war-Archive 之外读取 xml 文件并在其内容发生变化时重新加载它?

Is there a possibility to read a xml file outside of war-Archive and reload it if it's content changes?

是否有更常见的谨慎做法?

Is there a more common wary to do that?

提前致谢.

问候,

最大

编辑 1:我重新组织了问题,不仅询问了具体细节.

EDIT 1: I have restructured the question to ask not only specific details.

推荐答案

该文件由 Java 类加载器缓存,Camel 对此无能为力.您可能需要使用文件资源而不是类路径来加载文件.

The file is cached by the Java classloader, not something Camel can do anything about. You likely need to load the file using file resources instead of classpath.

这篇关于从外部 Tomcat 目录读取可重新加载的 xml 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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