简体   繁体   English

同一Java JVM上每个Java程序的单独JVM属性

[英]Separate JVM properties for each java program on same JVM

I'm running two web applications in tomat host process, which means In my opinion should have two separate programs, one for each application. 我正在tomat主机进程中运行两个Web应用程序,这意味着我认为应该有两个单独的程序,每个应用程序一个。

Another thing is both apps have a JVM property used which I want to be app specific. 另一件事是两个应用程序都使用了我想成为特定于应用程序的JVM属性。

//common-service library used in both web-apps
public class CommonService {

    private static Logger logger = LogManager.getLogger(CommonService.class);

    static {
        String uuid = UUID.randomUUID().toString();
        logger.debug("CommonService initialization for {}" , uuid);
        System.setProperty("key1", "value1-"+ uuid);
    }
}

When I deploy the wars for each app and see the value for key1 property it is overriden by the second application loaded. 当我为每个应用程序部署Wars并看到key1属性的值时,第二个加载的应用程序将覆盖它。

As I'm showing here, when app1 is loaded, the value for property key1 如我在此处所示,加载app1时,属性key1的值

值1

when app2 is loaded, the value for property key1 加载app2时,属性key1的值 值2

But after app2 is loaded, it overrides key1 for app1. 但是在加载app2之后,它将覆盖app1的key1

valu1_for_app1

The code is pretty simple for above, 上面的代码很简单,

public class Service1Servlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        CommonService commonService = new CommonService();
        System.out.println("Service1 key1= " + System.getProperty("key1"));
        PrintWriter out = resp.getWriter();
        out.println("<html>");
        out.println("<body>");
        out.println("<h1>" + System.getProperty("key1")+ "</h1>");
        out.println("</body>");
        out.println("</html>");
    }
}

So, it seems I can have JVM parameters global only in single JVM, but I have multiple services running on my server where each service needs to have its own set of JVM parameters, based on which there is another api which actually uses that JVM property. 因此,似乎我只能在单个JVM中拥有全局的JVM参数,但是我在服务器上运行了多个服务,其中每个服务都需要具有自己的JVM参数集,基于该参数,还有一个实际使用该JVM属性的API。 。

For application properties, you don't want to use VM parameters. 对于应用程序属性,您不想使用VM参数。 As you have discovered, these are global to the entire container. 如您所知,这些对于整个容器都是全局的。 Instead, you should use a properties file which is loaded when the application loads. 相反,您应该使用在应用程序加载时加载的属性文件。 One approach I like to use is loading a resource bundle into a static Map. 我喜欢使用的一种方法是将资源包加载到静态Map中。 Let's say you had an "application.properties" file in the WEB-INF/classes directory. 假设您在WEB-INF / classes目录中有一个“ application.properties”文件。 Like this: 像这样:

//common-service library used in both web-apps
public class CommonService {

    public static Map< String, String > APPLICATION_PROPERTIES = new HashMap<>();

    static {
        ResourceBundle bundle = ResourceBundle.getBundle( "application" );
        for( String key : bundle.keySet() ) {
          APPLICATION_PROPERTIES.put( key, ( String )bundle.getObject( key ) );
        }
    }
}

Then, when you want to access an application property, you do it in your code like this: 然后,当您要访问应用程序属性时,可以在代码中执行以下操作:

String key1Value = CommonService.APPLICATION_PROPERTIES.get( "key1" );

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

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