简体   繁体   English

为什么在检票口模型中将完成方法称为2次

[英]Why is finalize method called 2 times in wicket model

In my application I have class which extend wicket model and override finalized method(just delete file which is generated asynchronous). 在我的应用程序中,我有一个类扩展了wicket模型并覆盖了最终方法(只是删除异步生成的文件)。 Problem is that finalized method is called immediately after wicket page is loaded for the first time and then once more 问题是,在首次装入检票页面后立即再次调用finalized方法

public class TournamentFileReadOnlyModel<I> extends AbstractReadOnlyModel<File> {

    private static final long serialVersionUID = 1L;

    private CallableService callableService;
    private String uuid;

    public TournamentFileReadOnlyModel(CallableService callableService, I input,
            Class<? extends AbstractPdfCallable<I>> callableClass) {
        this.uuid = UUID.randomUUID().toString();
        this.callableService = callableService;
        callableService.createFile(uuid, WicketApplication.getFilesPath(), input, callableClass);
    }

    @Override
    public File getObject() {
        return callableService.getFile(uuid);
    }

    @Override
    protected void finalize() throws Throwable {
        super.finalize();
        callableService.finalizeFile(uuid);
    }
}

I created link with this model: 我使用此模型创建了链接:

    private void addPrintGroupButton() {
        add(new DownloadModelLink("printGroup", new TournamentFileReadOnlyModel<GroupPageDto>(callableService,
                groupPageDto, GroupPdfCallable.class)));
    }

log: 日志:

  1. time is called for me with no reason because I just load web page and still have reference for model 没有任何理由为我打电话,因为我只是加载网页,仍然有模型参考

    17:50:45.493 [Finalizer] INFO org.tahom.processor.service.callable.CallableService - Cleaning file from cache with uuid: 61286bf6-da4c-4905-b65d-d6061eb1466f 17:50:45.493 [Finalizer] INFO org.tahom.processor.service.callable.CallableService-使用uuid清除缓存中的文件:61286bf6-da4c-4905-b65d-d6061eb1466f

  2. time is load for me OK because I load another web page and already lost reference for this model 时间对我来说是好的,因为我加载了另一个网页,并且已经失去了该模型的参考

    17:51:10.913 [Finalizer] INFO org.tahom.processor.service.callable.CallableService - Error when cleaning file from cache with uuid: 61286bf6-da4c-4905-b65d-d6061eb1466f java.lang.NullPointerException at org.tahom.processor.service.callable.CallableService.finalizeFile(CallableService.java:65) [tahom-processor-0.2.0-SNAPSHOT.jar:?] at WICKET_org.tahom.processor.service.callable.CallableService$$FastClassByCGLIB$$82eb5c9b.invoke() [cglib-3.1.jar:?] at net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204) [cglib-3.1.jar:?] at org.apache.wicket.proxy.LazyInitProxyFactory$AbstractCGLibInterceptor.intercept(LazyInitProxyFactory.java:350) [wicket-ioc-7.1.0.jar:7.1.0] at WICKET_org.tahom.processor.service.callable.CallableService$$EnhancerByCGLIB$$41212df1.finalizeFile() [cglib-3.1.jar:?] at org.tahom.web.model.TournamentFileReadOnlyModel.finalize(TournamentFileReadOnlyModel.java:33) [classes/:?] at java.lang.ref.Finalizer.invokeFinalizeMethod(Native Method) ~[?:1.7.0_25] at java.lang.re 17:51:10.913 [Finalizer]信息org.tahom.processor.service.callable.CallableService-使用uuid清除缓存中的文件时出错:61286bf6-da4c-4905-b65d-d6061eb1466f java.lang.NullPointerException at org.tahom.processor .service.callable.CallableService.finalizeFile(CallableService.java:65)[tahom-processor-0.2.0-SNAPSHOT.jar :?]在WICKET_org.tahom.processor.service.callable.CallableService $$ FastClassByCGLIB $$ 82eb5c9b.invoke ()在net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)的[cglib-3.1.jar :?]在org.apache.wicket.proxy.LazyInitProxyFactory $的[cglib-3.1.jar :?] WICKET_org.tahom.processor.service.callable.CallableService $$ EnhancerByCGLIB $$ 41212df1.finalizeFile()中的AbstractCGLibInterceptor.intercept(LazyInitProxyFactory.java:350)[wickio-ioc-7.1.0.jar:7.1.0] [cglib- 3.1.jar :?],位于org.tahom.web.model.TournamentFileReadOnlyModel.finalize(TournamentFileReadOnlyModel.java:33)[classes / :?],位于java.lang.ref.Finalizer.invokeFinalizeMethod(本机方法)〜[?:1.7 .0_25]在java.lang.re f.Finalizer.runFinalizer(Finalizer.java:101) [?:1.7.0_25] at java.lang.ref.Finalizer.access$100(Finalizer.java:32) [?:1.7.0_25] at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:190) [?:1.7.0_25] f.Finalizer.runFinalizer(Finalizer.java:101)[?:1.7.0_25]在java.lang.ref.Finalizer.access $ 100(Finalizer.java:32)[?:1.7.0_25]在java.lang.ref .Finalizer $ FinalizerThread.run(Finalizer.java:190)[?:1.7.0_25]

UPDATE UPDATE

hm you are right. 嗯,你是对的。 There is another intance of model. 还有另一种模式。 But then I dont how can be same uuid shared by different model. 但是然后我不知道如何由不同的模型共享相同的uuid。 But this is another question 但这是另一个问题

21:08:55.980 [qtp13530976-43] DEBUG org.tahom.processor.service.callable.CallableService - Creating file with uuidb485a4d1-ef67-4255-af64-4d7df6001b09 21:08:55.980 [qtp13530976-43] DEBUG org.tahom.web.model.TournamentFileReadOnlyModel - UUID b485a4d1-ef67-4255-af64-4d7df6001b09 generated for classorg.tahom.web.model.TournamentFileReadOnlyModel@1ba03be 21:08:55.980 [qtp13530976-43]调试org.tahom.processor.service.callable.CallableService-使用uuidb485a4d1-ef67-4255-af64-4d7df6001b09创建文件21:08:55.980 [qtp13530976-43]调试org.tahom。 web.model.TournamentFileReadOnlyModel-为classorg.tahom.web.model.TournamentFileReadOnlyModel@1ba03be生成的UUID b485a4d1-ef67-4255-af64-4d7df6001b09

21:08:56.794 [Finalizer] DEBUG org.tahom.web.model.TournamentFileReadOnlyModel - Class org.tahom.web.model.TournamentFileReadOnlyModel@14be425 21:08:56.907 [Finalizer] INFO org.tahom.processor.service.callable.CallableService - Cleaning file from cache with uuid: b485a4d1-ef67-4255-af64-4d7df6001b09 21:08:56.794 [Finalizer]调试org.tahom.web.model.TournamentFileReadOnlyModel-类org.tahom.web.model.TournamentFileReadOnlyModel@14be425 21:08:56.907 [Finalizer] INFO org.tahom.processor.service.callable。 CallableService-使用uuid清理缓存中的文件:b485a4d1-ef67-4255-af64-4d7df6001b09

21:09:23.696 [Finalizer] WARN org.tahom.processor.service.callable.CallableService - Error when cleaning file from cache with uuid: b485a4d1-ef67-4255-af64-4d7df6001b09 21:09:23.696 [Finalizer] DEBUG org.tahom.web.model.TournamentFileReadOnlyModel - Class org.tahom.web.model.TournamentFileReadOnlyModel@1d76f52 21:09:23.696 [Finalizer] WARN org.tahom.processor.service.callable.CallableService-使用uuid从缓存中清除文件时出错:b485a4d1-ef67-4255-af64-4d7df6001b09 21:09:23.696 [Finalizer]调试组织。 tahom.web.model.TournamentFileReadOnlyModel-类org.tahom.web.model.TournamentFileReadOnlyModel@1d76f52

Odds are you are dealing with (in other areas of your code) two instances of the TournamentFileReadOnlyModel . 奇怪的是,您正在处理TournamentFileReadOnlyModel两个实例(在代码的其他区域)。

Many frameworks create and destroy instances instead of hold them. 许多框架创建和销毁实例,而不是持有它们。 Some do it to isolate one instance from another (web services isolating connections, for example) others do it to verify classes are of the right inheritance chain (SWT's Service interface, for example), some just do it to validate a detail. 有些人这样做是为了将一个实例与另一个实例隔离(例如,Web服务隔离连接),而其他人则是为了验证类是否具有正确的继承链(例如SWT的Service接口),而有些人只是为了验证详细信息。

In any case, putting required logic in a finalize block is a known "really bad pattern", and while you may be tempted to find the reason why your class is constructed twice, please instead consider not using a finalize block. 无论如何,将所需的逻辑放在finalize块中是一种已知的“非常糟糕的模式”,虽然您可能会想知道为什么要两次构造您的类,但请考虑不要使用finalize块。

This might mean using a different approach for managing the temporary files you talked about; 这可能意味着要使用一种不同的方法来管理您所讨论的临时文件。 but, with a different approach, you can explicitly detail the policy instead hoping that the JVM (which has no guarantee to do so) follow your policy. 但是,通过另一种方法,您可以显式详细说明策略,而不希望JVM(无法保证这样做)遵循您的策略。

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

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