【问题标题】:How to uniquely identify the instance of a class loader in Java如何在 Java 中唯一标识类加载器的实例
【发布时间】:2013-07-29 13:13:55
【问题描述】:

我有一个问题,看起来是因为同时使用了一个类加载器的两个实例。计划任务总是运行两次而不是一次,即使设置为避免并发使用也是如此。

如何在运行时唯一标识给定类实例的类加载器实例?我知道它的完全限定名称是类加载器、包和类名的组合。我想知道同一个类加载器的两个单独的实例是否同时运行。

我尝试使用以下内容进行日志记录,但就我实际希望看到的内容而言,它没有给我任何有用的信息(相当于唯一的线程 ID 等)。是的,它确实给出了类加载器的类型及其父名称,但这不足以帮助我解决这个问题。

final String classLoaderInfoCurrentThread = Thread.currentThread().getContextClassLoader().toString();
final String classLoaderInfoClass = getClass().getClassLoader().toString();
logger.debug("Class loader info current thread: " + classLoaderInfoClass);
logger.debug("Class loader info class: " + classLoaderInfoCurrentThread);

以上结果:

类加载器信息类:WebappClassLoader

上下文:

委托:假

存储库: /WEB-INF/classes/

---------> 父类加载器: org.apache.catalina.loader.StandardClassLoader@68de123

非常感谢任何想法。

更新:

感谢您的快速反馈。使用系统哈希码足以确认相同的类加载器。回想起来,StandardClassLoader 上的哈希也很有用,正如 cmets 中有用地指出的那样。我检查了不同的线程ID。

将 Spring 与 Tomcat 和 Quartz 1.8 调度框架一起使用。

通过在 Spring/Quartz 上提高日志记录,我能够确定两个不同的调度程序实例正在启动:

org.quartz.core.QuartzScheduler - Scheduler meta-data: Quartz Scheduler (v1.8.6) 'scheduler' with instanceId '<compname>.local1375104695468'
org.quartz.core.QuartzScheduler - Scheduler meta-data: Quartz Scheduler (v1.8.6) 'scheduler' with instanceId '<compname>.local1375104696411'

这似乎存在一个已知问题:

Quartz job runs twice when deployed on tomcat 6/Ubuntu 10.04LTS

【问题讨论】:

  • 68de123 应该足以用于唯一的运行时标识。

标签: java classloader


【解决方案1】:

System.identityHashCode(getClass().getClassLoader());

将为不同的classloaders 返回不同的值。您可以将其用作classloader ID 并将其包含在日志信息中。

【讨论】:

  • hash 只得到一个保证:对于 equals 对象也是如此。这并不意味着不同的对象会有所不同。
猜你喜欢
  • 2021-12-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多