如何最有效地共享log4j库

时间:2020-12-02 02:05:11

i have a few batch java command-line applications which are planned to be deployed as:

我有一些批量java命令行应用程序,计划部署为:

batch_apps
  app_1
    batch1.jar
    run_batch1.sh
  app_2
    batch2.jar
    run_batch3.sh
{...etc...}

what would be the best practice on organizing a shared library pool - for example log4j:

组织共享库池的最佳做法是什么 - 例如log4j:

batch_apps
  app_1
    batch1.jar
    run_batch1.sh
  app_2
    batch2.jar
    run_batch3.sh
  libs
    log4j.jar
    ojdbc.jar

?

and include individual log4j.xml's in each app's own jar file? i understand i would need to add 'libs' to the classpath either in manifests or in run_batchX.sh (which way is preferable?)

并在每个应用程序自己的jar文件中包含单独的log4j.xml?我明白我需要在清单或run_batchX.sh中添加'libs'到类路径(哪种方式更可取?)

I am mostly wondering what would be the most efficient setup performance-wise.

我主要想知道什么是最有效的设置性能。

thanks

4 个解决方案

#1


2  

Having a shared libs directory at the root of your install dir is definitely the way to go. Since libs will be loaded in memory once, when the JVM launches, there is no impact on performance whatever solution you choose.

在安装目录的根目录下拥有一个共享的libs目录绝对是可行的方法。由于libs将在内存中加载一次,当JVM启动时,无论您选择何种解决方案,都不会对性能产生影响。

I would not put the classpath in the jar files, as this would force you to change your jars if you need to relocate your lib dir. Editing a script is much simpler.

我不会将类路径放在jar文件中,因为如果你需要重新定位你的lib目录,这会迫使你改变你的jar。编辑脚本要简单得多。

I would not include the log4j conf file in your jar files either, for the same reason.

出于同样的原因,我也不会在你的jar文件中包含log4j conf文件。

#2


1  

It appears your applications don't share a single JVM instance. (i.e. They are individually started via 'java -jar batch1.jar' or some such.) Therefore, sharing library .jar files only saves you DISK space not RAM.

您的应用程序似乎不共享单个JVM实例。 (即它们是通过'java -jar batch1.jar'或类似的单独启动的。)因此,共享库.jar文件只能节省DISK空间而不是RAM。

If the apps are not sharing a single JVM then ease-of-deployment should take precedence over disk space. (Your time is worth more than a few "wasted" MB.)

如果应用程序不共享单个JVM,则易于部署应优先于磁盘空间。 (你的时间比一些“浪费”MB更值钱。)

In this instance I would recommend making each application self contained, in either a single .jar file including all its libraries, or a single folder of .jar files. (i.e. Put the libs for each app in a folder for that app.)

在这个例子中,我建议将每个应用程序自包含在单个.jar文件中,包括其所有库,或单个.jar文件夹。 (即将每个应用程序的库放在该应用程序的文件夹中。)

If the apps were sharing a single JVM then I would recommend the shared library folder option.

如果应用程序共享一个JVM,那么我建议使用共享库文件夹选项。

#3


0  

You can use the java extension mechanism. Place them in JAVA_HOME/lib/ext and they will be accessible by all apps. Of course, this may not be the best for all deployments, but its certainly easier.

您可以使用java扩展机制。将它们放在JAVA_HOME / lib / ext中,所有应用程序都可以访问它们。当然,这可能不是所有部署的最佳选择,但它肯定更容易。

#4


0  

This doesn't directly answer your question, but I have already tried the approach that you propose but would now create a single jar per application (see how to do it with Ant). That way, no need to include anything in the classpath:

这并没有直接回答你的问题,但我已经尝试了你提出的方法,但现在每个应用程序创建一个jar(请参阅如何使用Ant)。这样,不需要在类路径中包含任何内容:

java -jar myApp.jar

is all you need. I find it cleaner but everything is debatable.

是你所需要的全部。我发现它更干净但一切都值得商榷。

It doesn't make any difference from a performance point-of-view since each application is run inside its own JVM.

它与性能观点没有任何区别,因为每个应用程序都在自己的JVM中运行。

The only downside is that some libraries will be present in each jar file. It only costs more to store on the HD, but these days, MB are pretty cheap :-) I trade simplicity (no external lib folder) and no jar hell (not placing your jars inside the Java ext folder) over storage price any time. If your application doesn't include terrabyte of libraries, I think it's fine.

唯一的缺点是每个jar文件中都会有一些库。存储在高清上只需要花费更多,但是现在,MB非常便宜:-)我交易简单(没有外部lib文件夹)并且没有jar地狱(不将你的jar放在Java ext文件夹中)随时随地存储价格。如果您的应用程序不包含TB的库,我认为它没问题。

For the Log4j configuration file, I would place one default file inside the jar but provide a sample config file (log4j-custom.xml.sample) that someone can modify and specify in the command line:

对于Log4j配置文件,我会在jar中放置一个默认文件,但提供一个示例配置文件(log4j-custom.xml.sample),有人可以在命令行中修改和指定:

java -Dlog4j.configuration=log4j-custom.xml -jar myApp.jar

#1


2  

Having a shared libs directory at the root of your install dir is definitely the way to go. Since libs will be loaded in memory once, when the JVM launches, there is no impact on performance whatever solution you choose.

在安装目录的根目录下拥有一个共享的libs目录绝对是可行的方法。由于libs将在内存中加载一次,当JVM启动时,无论您选择何种解决方案,都不会对性能产生影响。

I would not put the classpath in the jar files, as this would force you to change your jars if you need to relocate your lib dir. Editing a script is much simpler.

我不会将类路径放在jar文件中,因为如果你需要重新定位你的lib目录,这会迫使你改变你的jar。编辑脚本要简单得多。

I would not include the log4j conf file in your jar files either, for the same reason.

出于同样的原因,我也不会在你的jar文件中包含log4j conf文件。

#2


1  

It appears your applications don't share a single JVM instance. (i.e. They are individually started via 'java -jar batch1.jar' or some such.) Therefore, sharing library .jar files only saves you DISK space not RAM.

您的应用程序似乎不共享单个JVM实例。 (即它们是通过'java -jar batch1.jar'或类似的单独启动的。)因此,共享库.jar文件只能节省DISK空间而不是RAM。

If the apps are not sharing a single JVM then ease-of-deployment should take precedence over disk space. (Your time is worth more than a few "wasted" MB.)

如果应用程序不共享单个JVM,则易于部署应优先于磁盘空间。 (你的时间比一些“浪费”MB更值钱。)

In this instance I would recommend making each application self contained, in either a single .jar file including all its libraries, or a single folder of .jar files. (i.e. Put the libs for each app in a folder for that app.)

在这个例子中,我建议将每个应用程序自包含在单个.jar文件中,包括其所有库,或单个.jar文件夹。 (即将每个应用程序的库放在该应用程序的文件夹中。)

If the apps were sharing a single JVM then I would recommend the shared library folder option.

如果应用程序共享一个JVM,那么我建议使用共享库文件夹选项。

#3


0  

You can use the java extension mechanism. Place them in JAVA_HOME/lib/ext and they will be accessible by all apps. Of course, this may not be the best for all deployments, but its certainly easier.

您可以使用java扩展机制。将它们放在JAVA_HOME / lib / ext中,所有应用程序都可以访问它们。当然,这可能不是所有部署的最佳选择,但它肯定更容易。

#4


0  

This doesn't directly answer your question, but I have already tried the approach that you propose but would now create a single jar per application (see how to do it with Ant). That way, no need to include anything in the classpath:

这并没有直接回答你的问题,但我已经尝试了你提出的方法,但现在每个应用程序创建一个jar(请参阅如何使用Ant)。这样,不需要在类路径中包含任何内容:

java -jar myApp.jar

is all you need. I find it cleaner but everything is debatable.

是你所需要的全部。我发现它更干净但一切都值得商榷。

It doesn't make any difference from a performance point-of-view since each application is run inside its own JVM.

它与性能观点没有任何区别,因为每个应用程序都在自己的JVM中运行。

The only downside is that some libraries will be present in each jar file. It only costs more to store on the HD, but these days, MB are pretty cheap :-) I trade simplicity (no external lib folder) and no jar hell (not placing your jars inside the Java ext folder) over storage price any time. If your application doesn't include terrabyte of libraries, I think it's fine.

唯一的缺点是每个jar文件中都会有一些库。存储在高清上只需要花费更多,但是现在,MB非常便宜:-)我交易简单(没有外部lib文件夹)并且没有jar地狱(不将你的jar放在Java ext文件夹中)随时随地存储价格。如果您的应用程序不包含TB的库,我认为它没问题。

For the Log4j configuration file, I would place one default file inside the jar but provide a sample config file (log4j-custom.xml.sample) that someone can modify and specify in the command line:

对于Log4j配置文件,我会在jar中放置一个默认文件,但提供一个示例配置文件(log4j-custom.xml.sample),有人可以在命令行中修改和指定:

java -Dlog4j.configuration=log4j-custom.xml -jar myApp.jar