简体   繁体   English

如何从Java EE Server(JBoss)上同一个耳朵中的另一场战争中访问文件?

[英]How to access a file from a different war which is in the same ear on a Java EE Server (JBoss)?

So following problem: We have a load balancer with n servers behind it. 所以下面的问题:我们有一个负载均衡器,后面有n个服务器。 On each server there is a PDF project (war) installed in ear together with a pdf-builder project in the same earfile (different war). 在每个服务器上都安装了一个PDF项目(war)以及同一个earfile中的pdf-builder项目(不同的战争)。 I now want to read a pdf from the other war. 我现在想从另一场战争中读取一个pdf。

The problem is: I don't know the IP address of the server I'm running on and using the dns lookup I might end on the different server through the loadbalancer. 问题是:我不知道我正在运行的服务器的IP地址并使用dns查找我可能通过loadbalancer在不同的服务器上结束。 Furthermore there might be problems with the access / security constraints since the server itself is not a valid user who has rights to make accesses to the pdf file locations of the pdf war project. 此外,访问/安全性约束可能存在问题,因为服务器本身不是有权访问pdf war项目的pdf文件位置的有效用户。

Is there a way to obtain something like getResourceAsStream() which works across different modules / war files within an ear? 有没有办法获得类似getResourceAsStream()的东西,它可以在耳内的不同模块/ war文件中运行?

You can move the .PDFs to a separate jar (ie pdf-builder.jar) and put it into the .EAR In the manifest of your .WAR ( META-INF/MANIFEST.MF ) put this line: 您可以将.PDF移动到一个单独的jar(即pdf-builder.jar)并将其放入.EAR中.WAR( META-INF/MANIFEST.MF )的META-INF/MANIFEST.MF放入以下行:

Class-Path: pdf-builder.jar

Now you can load the .PDFs using your class loader. 现在,您可以使用类加载器加载.PDF。 This is J2EE compliant. 这符合J2EE标准。

You need to provide absolute path to the file in order to access a resource which is not in current class path. 您需要提供文件的绝对路径才能访问不在当前类路径中的资源。

you can call 你可以打电话

serverletContext.getRealpath("/");
will give the the path till the server node (context). 将给出直到服务器节点(上下文)的路径。 From there you can navigate to the file. 从那里你可以导航到该文件。

One more way is, Jboss uses (or set) several environment variables while it bootstraps. 还有一种方法是,Jboss在引导时使用(或设置)几个环境变量。 You can use those properties in order to get the current node (like 'default'). 您可以使用这些属性来获取当前节点(如“default”)。 Following two properties will give you the absolute path to current server node. 以下两个属性将为您提供当前服务器节点的绝对路径。

 System.getProperty("jboss.server.base.dir") + File.separator + System.getProperty("jboss.server.name") ; 
(Please use System.getenv('varname') if getProperty don't work). (如果getProperty不起作用,请使用System.getenv('varname') )。 From there you can navigate. 从那里你可以导航。 Here is the complete list of system varibles that jboss uses. 以下是jboss使用的系统变量的完整列表。

WARs are intended to be isolated from each other. WAR旨在彼此隔离。 They can see "up" into the application class loader (EAR level) but cannot access a peer's class loader. 他们可以在应用程序类加载器(EAR级别)中看到“up”,但无法访问对等的类加载器。 So while there may be a way to get it to work on your particular application server, doing so isn't advisable. 因此,虽然可能有一种方法可以让它在您的特定应用程序服务器上运行,但这样做是不可取的。 If someone on the operations team changes deployment options down the line, they may not be aware that you're relying on that mechanism and would break the application in a (possibly) cryptic kind of manner. 如果运营团队中的某个人更改了部署选项,他们可能不会意识到您依赖于该机制并且会以(可能)神秘的方式破坏应用程序。

Attempting to access a peer class loader like that violates Java EE (if not the letter of the spec, at least the spirit of it). 试图访问这样的对等类加载器违反了Java EE(如果不是规范的字母,至少是它的精神)。

If you can't put static content on a separate server for access, at least put the static PDFs into a JAR (as been suggested by others) and deploy into the EAR. 如果您无法将静态内容放在单独的服务器上进行访问,请至少将静态PDF放入JAR(如其他人建议的那样)并部署到EAR中。 When do you that, don't forget to add the JAR entry to the manifest file of the declaring WAR (else it won't load the JAR into the application class loader). 你是什​​么时候做的,不要忘记将JAR条目添加到声明WAR的清单文件中(否则它不会将JAR加载到应用程序类加载器中)。

  1. Change the .war to .jar ( .ear ?) if deployment is not too often. 如果部署不太频繁,请将.war更改为.jar.ear ?)。 For instance in the server lib directory if you do a daily restart. 例如,如果每天重新启动,则在服务器lib目录中。

  2. Do not deploy a war, but upload the files to a fixed directory ie with rsync . 不要部署战争,而是将文件上传到固定目录,即使用rsync This has my preference. 这是我的偏好。

  3. Do as @Rp- said, possibly unzipping. 做@ Rp-说,可能解压缩。

I know I have not enough experience to answer such a question like this , so , all I can think of in a very , very rough sketch is setting each .war as a diferent Spring Context and then load it dynamically by setting the aprropiate mapping inside Springs MVC Framework. 我知道我没有足够的经验来回答这样的问题,因此,我在一个非常非常粗略的草图中可以想到的是将每个.war设置为不同的Spring Context,然后通过设置内部的aprropiate映射动态加载它Springs MVC框架。

Dynamically Loading Spring Contexts at Runtime 在运行时动态加载Spring上下文

Loading Spring Resources as Dependent Jars 将Spring Resources作为依赖Jars加载

Web resources - Chapter 9 - Spring Reference Web资源 - 第9章 - Spring参考

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM