简体   繁体   English

Glassfish环境用于具有共同依赖关系的多个应用程序

[英]Glassfish environment for multiple applications with common dependency

I have a question which I am struggling with. 我有一个正在苦苦挣扎的问题。 The problem is that I do not even know what exactly I should search for on google, so that's why I am asking for a short explanation here on SO. 问题是我什至不知道我应该在Google上确切搜索什么,所以这就是为什么我要求在SO上做一个简短的解释。

Let's say that I have three projects: A, B and C. A is a dependency for both B and C. B and C are web-services for example, which can be deployed in Glassfish. 假设我有三个项目:A,B和C。A是B和C的依存关系。例如,B和C是Web服务,可以在Glassfish中部署。 In A, there is a class with a static field, let's say an int . 在A中,有一个带有静态字段的类,比如说一个int When building project B, the static int has value 3. Then, we build project C but we change the value of the static int to 4. 构建项目B时, static int值为3。然后,构建项目C,但将static int的值更改为4。

The question is: when we deploy both B and C to Glassfish, do these applications run in separate "environments" where the the static int has different values in each environment (3 in env of B, 4 in env of C) or is there just one static int ? 问题是:当我们将B和C都部署到Glassfish上时,这些应用程序是否在单独的“环境”中运行,其中static int在每个环境中具有不同的值(B的环境中为3,C的环境中为4)还是存在?只是一个static int

LE: B and C are both deployed in the same Glassfish domain. LE:B和C都部署在同一个Glassfish域中。 Glassfish version is 3.1.1. Glassfish版本为3.1.1。

If "A for B" and "A for C" are different, you need to create a deployment for both B and C each enclosing its own copy of A. 如果“ A代表B”和“ A代表C”不同,则需要为B和C创建一个部署,每个部署都包含自己的A副本。

The simplest approach is most likely a separate WAR for B enclosing A_for_B.jar and a separate WAR for C enclosing A_for_C.jar. 最简单的方法很可能是将B包围A_for_B.jar的单独WAR和将C包围A_for_C.jar的C单独的WAR。

The problem is that I do not even know what exactly I should search for on google, so that's why I am asking for a short explanation here on SO.

What you want to search for is "Classloader". 您要搜索的是“ Classloader”。 Objects of this type are responsible for loading Java classes. 这种类型的对象负责加载Java类。 Each class loaded by JVM is identified by both its fully qualified name AND the instance of classloader that loaded it. JVM加载的每个类都由其完全限定名称加载它的类加载器实例标识。 This means that in a single JVM you could have a couple of classes named exactly "com.example.Test", each loaded by a different classloader - and each of them would have its own static fields. 这意味着在一个JVM中,您可以有几个名为“ com.example.Test”的类,每个类都由不同的类加载器加载-每个类都有自己的静态字段。 What's more, the classes would be considered different at runtime: casting "com.example.Test" from one classloader to "com.example.Test" from some other classloader would end in a ClassCastException. 而且,这些类在运行时会被认为是不同的:将一个类加载器中的“ com.example.Test”转换为其他类加载器中的“ com.example.Test”将以ClassCastException结尾。

To avoid situation where you have multitude of common classes loaded multiple times (like java.util.List), classloaders are usually kept in a clean hierarchy. 为了避免多次加载多个公共类(例如java.util.List)的情况,通常将类加载器保持在干净的层次结构中。 Each classloader knows its "parent", that is classloaders responsible for loading "more general classes". 每个类加载器都知道其“父级”,即负责加载“更多通用类”的类加载器。 When we try to load a class (which usually happens "by name"), like "com.example.OtherTest", the current classloader tries to ask its parent if it is able to load it (and the parent in turn asks its parent, and so on). 当我们尝试加载一个类(通常是“按名称”发生)时,例如“ com.example.OtherTest”,当前的类加载器会尝试询问其父级是否能够加载它(而父级又会询问其父级) , 等等)。 This way each class is loaded by the most general classloader that is able to load it (java.lang.String will be always loaded by the standard bootstrap classloader even if some other classloader has some different version available). 这样,每个类都由能够加载它的最通用的类​​加载器加载(即使其他类加载器具有某些可用的版本,标准的引导类加载器也会始终加载java.lang.String)。

When you deploy a war, glassfish creates a separate classloader for it. 部署战争时,glassfish会为其创建一个单独的类加载器。 The classloader knows how to load classes from WEB-INF/lib and WEB-INF/classes. 类加载器知道如何从WEB-INF / lib和WEB-INF / classs加载类。 Its parent is (or at least grandfather) the domain classloader, common for all applications. 它的父级是(或至少是祖父级的)域类加载器,对于所有应用程序都是通用的。 So before application classloader loads any class - it will ask Glassfish to do it. 因此,在应用程序类加载器加载任何类之前,它将要求Glassfish进行操作。

The whole process is quite configurable and enables you to have just one copy of common libraries loaded into JVM (eg. if you put your libraries into - AFAIR - domain1/lib/ext, they will be available to the domain classloader, an ancestor of each application's classloader; or you could even put it in your JRE's lib/ext folder and have it available to the main classloader, that will load the classes for any application you run). 整个过程是可配置的,使您仅可以将一个普通库的一个副本加载到JVM中(例如,如果将库放入-AFAIR-domain1 / lib / ext,它们将可供域类加载器使用,它们是每个应用程序的类加载器;或者甚至可以将其放在JRE的lib / ext文件夹中,并提供给主类加载器使用,这将为您运行的任何应用程序加载类。 In such case they will share static fields. 在这种情况下,它们将共享静态字段。 Or you could have your libraries loaded by war classloaders, and have all the static fields "private" to your applications. 或者,您可以通过war类加载器加载您的库,并使所有静态字段“专用”于您的应用程序。

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

相关问题 pom对3个应用程序使用通用依赖项Spring-mvc - pom use a common dependency for 3 applications Spring-mvc 多个应用程序共享TomEE中的通用EJB JAR - Multiple applications sharing common EJB JAR in TomEE 修复Windows 7中的环境变量中的“PATH”以用于多个应用程序 - Fixing 'PATH' in Environment Variables in Windows 7 for multiple applications Gradle:多个java项目的公共资源依赖 - Gradle: common resource dependency for multiple java projects Log 4j常用日志文件用于多个Web应用程序? - Log 4j common log file for multiple web applications? 部署Java应用程序(Tomcat / Glassfish) - Deploying java applications (Tomcat/Glassfish) 自定义清单在-post-jar中以通用依赖项多次添加 - Custom manifest added multiple times in -post-jar in a common dependency Glassfish LOG4J loggin不同的应用程序 - Glassfish LOG4J loggin diferent applications org.glassfish.deployment.common.DeploymentException:LLog; - org.glassfish.deployment.common.DeploymentException: LLog; 未创建表,在 Spring Boot 中的多个应用程序中使用来自 maven jar 依赖项的实体 - Tables not created, Using Entities from maven jar dependency in multiple applications in Spring Boot
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM