[英]How to uniquely identify the instance of a class loader in Java
我有一个问题,看起来它是由于同时使用类加载器的两个实例引起的。 计划任务始终运行两次而不是一次,即使设置为避免并发使用也是如此。
如何在运行时唯一标识给定类实例的类加载器实例? 我知道它的完全限定名称将是classloader,package和class name的组合。 我想知道同一个类加载器的两个单独实例是否同时运行。
我尝试使用以下内容进行日志记录,但它在我实际希望看到的内容方面没有任何用处(相当于一个独特的线程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上的哈希在回顾中也很有用,正如评论中有用地指出的那样。 我对不同的线程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'
看来这有一个已知的问题:
System.identityHashCode(getClass().getClassLoader());
将为不同的classloaders
返回不同的值。 您可以将其用作classloader ID
并将其包含在日志信息中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.