Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@PreDestroy does not work #5420

Closed
Tracked by #5362
halibobo1205 opened this issue Aug 14, 2023 · 4 comments · Fixed by #5421
Closed
Tracked by #5362

@PreDestroy does not work #5420

halibobo1205 opened this issue Aug 14, 2023 · 4 comments · Fixed by #5421

Comments

@halibobo1205
Copy link
Contributor

halibobo1205 commented Aug 14, 2023

System information

OS : Mac OS X
JVM : Oracle Corporation 1.8.0_321 x86_64
Git : 9e95599
Version : 4.7.2
Code : 18031

OS : Linux
JVM : Red Hat, Inc. 1.8.0_302 amd64
Git : 9e95599
Version : 4.7.2
Code : 18031

Expected behaviour

The PreDestroy annotation is used on methods as a callback notification to signal that the instance is in the process of being removed by the container. The method annotated with PreDestroy is typically used to release resources that it has been holding. This annotation MUST be supported by all container managed objects that support PostConstruct except the application client container in Java EE 5.

As documented in Bean.destroyMethod:

public abstract String destroyMethod
The optional name of a method to call on the bean instance upon closing the application context, for example a close() method on a JDBC DataSource implementation, or a Hibernate SessionFactory object. The method must have no arguments but may throw any exception.
As a convenience to the user, the container will attempt to infer a destroy method against an object returned from the @bean method. For example, given an @bean method returning an Apache Commons DBCP BasicDataSource, the container will notice the close() method available on that object and automatically register it as the destroyMethod. This 'destroy method inference' is currently limited to detecting only public, no-arg methods named 'close' or 'shutdown'. The method may be declared at any level of the inheritance hierarchy and will be detected regardless of the return type of the @bean method (i.e., detection occurs reflectively against the bean instance itself at creation time).
To disable destroy method inference for a particular @bean, specify an empty string as the value, e.g. @bean(destroyMethod=""). Note that the DisposableBean callback interface will nevertheless get detected and the corresponding destroy method invoked: In other words, destroyMethod="" only affects custom close/shutdown methods and Closeable/AutoCloseable declared close methods.
Note: Only invoked on beans whose lifecycle is under the full control of the factory, which is always the case for singletons but not guaranteed for any other scope.
See Also:
DisposableBean, ConfigurableApplicationContext.close()
Default:
"(inferred)"

According to DisposableBeanAdapter.inferDestroyMethodIfNecessary:

If the current value of the given beanDefinition's "destroyMethodName" property is AbstractBeanDefinition.INFER_METHOD, then attempt to infer a destroy method. Candidate methods are currently limited to public, no-arg methods named "close" or "shutdown" (whether declared locally or inherited). The given BeanDefinition's "destroyMethodName" is updated to be null if no such method is found, otherwise set to the name of the inferred method. This constant serves as the default for the @bean#destroyMethod attribute and the value of the constant may also be used in XML within the or attributes.
Also processes the java.io.Closeable and AutoCloseable interfaces, reflectively calling the "close" method on implementing beans as well.

When the process receives the kill -15 signal, the @PreDestroy qualified method should be executed.

Actual behaviour

@PreDestroy does not work

Steps to reproduce the behaviour

Terminate the process with kill -15.

Backtrace

@halibobo1205 halibobo1205 changed the title Fix @PreDestroy not triggering issue @PreDestroy does not work Aug 14, 2023
@halibobo1205
Copy link
Contributor Author

@PreDestroy only work with singleton scoped bean.

@lxcmyf
Copy link
Contributor

lxcmyf commented Aug 14, 2023

@halibobo1205
@PreDestroy lacks a detailed description, only developers familiar with Java know that this is an annotation.

@halibobo1205
Copy link
Contributor Author

@halibobo1205 @PreDestroy lacks a detailed description, only developers familiar with Java know that this is an annotation.

@lxcmyf @PreDestroy can be find in here , https://docs.oracle.com/javase/8/docs/api/javax/annotation/PreDestroy.html

@halibobo1205
Copy link
Contributor Author

Get inspired from here, https://www.netjstech.com/2018/03/registershutdownhook-method-in-spring.html
http://www.javabyexamples.com/close-spring-application-context

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants