Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse 4 » @PreDestroy for singletons
@PreDestroy for singletons [message #1405509] Thu, 07 August 2014 14:07 Go to next message
Uwe San is currently offline Uwe SanFriend
Messages: 119
Registered: January 2012
Senior Member
Hi,

I have an E4 application with two parts. Only one part is shown at a time, such that part one will be destroyed when part two is shown, and vice versa.

Part one has a dependency to a Foo class which is injected into a @PostConstruct-annotated method in part one. The Foo class is annotated with @Creatable and @Singleton (as I want it to be a singleton).

I tracked the lifecycle of the Foo instance by checking when its @PostConstruct- and @PreDestroy-annotated methods are called. The findings were surprising to me. Switching between parts one and two gave me the following results:

Open part one:

Foo - PostConstruct

Open part two:

Foo - PreDestroy

Open part one again:

<nothing>

Open part two again:

<nothing>

So the Foo singleton instance is destroyed when part one is closed for the first time, but it is not constructed again when part one is opened for the second time. It must still exist since it is injected successfully into part one every time the part is constructed. But when the Foo instance still exists, how come the @PreDestroy method was called?

Could it be that an object is not destroyed after the @PreDestroy method was called? My understanding is that "destroy" means the instance does not exist any more, but I may be wrong. According to the E4 documentation, the object "is to be uninjected and released".

I'd appreciate your feedback.

Thanks,
Uwe
Re: @PreDestroy for singletons [message #1405530 is a reply to message #1405509] Thu, 07 August 2014 15:00 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas SchindlFriend
Messages: 6599
Registered: July 2009
Senior Member
@PreDestroy means that the CONTEXT to which the object was attached to
was disposed.

I think the main problem is that @Createable creates an object on the
requesting context which is wrong for Singleton instances who should
most likely get created on the Root-Context and hence only haveing
access to the information in this root-context (==OSGi-ServiceRegistry)

I'm not a friend of this @Creatable all together and would advice to not
make use of it but better using IContextFunction and decide on what
level of the IEclipseContext the object is a singleton.

Tom

On 07.08.14 16:07, Uwe San wrote:
> Hi,
>
> I have an E4 application with two parts. Only one part is shown at a
> time, such that part one will be destroyed when part two is shown, and
> vice versa.
>
> Part one has a dependency to a Foo class which is injected into a
> @PostConstruct-annotated method in part one. The Foo class is annotated
> with @Creatable and @Singleton (as I want it to be a singleton).
>
> I tracked the lifecycle of the Foo instance by checking when its
> @PostConstruct- and @PreDestroy-annotated methods are called. The
> findings were surprising to me. Switching between parts one and two gave
> me the following results:
>
> Open part one:
>
> Foo - PostConstruct
>
> Open part two:
>
> Foo - PreDestroy
>
> Open part one again:
>
> <nothing>
>
> Open part two again:
>
> <nothing>
>
> So the Foo singleton instance is destroyed when part one is closed for
> the first time, but it is not constructed again when part one is opened
> for the second time. It must still exist since it is injected
> successfully into part one every time the part is constructed. But when
> the Foo instance still exists, how come the @PreDestroy method was called?
>
> Could it be that an object is not destroyed after the @PreDestroy method
> was called? My understanding is that "destroy" means the instance does
> not exist any more, but I may be wrong. According to the
> https://wiki.eclipse.org/Eclipse4/RCP/Dependency_Injection#.40PostConstruct.2C_.40PreDestroy_.28javax.annotation.29,
> the object "is to be uninjected and released".
>
> I'd appreciate your feedback.
>
> Thanks,
> Uwe
Re: @PreDestroy for singletons [message #1405541 is a reply to message #1405530] Thu, 07 August 2014 15:22 Go to previous message
Uwe San is currently offline Uwe SanFriend
Messages: 119
Registered: January 2012
Senior Member
Thanks, Tom. That makes a lot of sense (and should be documented more explicitly somewhere, I think).

Using @Creatable in this case was meant to be a quick solution since, as you said, it is not the best approach for handling the objects in a part's context. I was then struck by the weird behaviour when combining @Creatable and @Singleton, but I suppose everything works as specified.

Uwe
Previous Topic:[E4AP] Error on loading workbench.xmi
Next Topic:How to correctly setup run config or plugin.xml or ... to make use of persistedState in (custom) Men
Goto Forum:
  


Current Time: Fri Jun 18 03:59:49 GMT 2021

Powered by FUDForum. Page generated in 0.01920 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top