下面列出了怎么用javax.annotation.PreDestroy的API类实例代码及写法,或者点击链接到github查看源代码。
@Test
public void test_is_predestroy_is_executed() throws Exception {
final LifecycleRegistry registry = new LifecycleRegistry();
final CountDownLatch latch = new CountDownLatch(1);
class PredestroyClass {
@PreDestroy
public void preDestroy() {
latch.countDown();
}
}
registry.addPreDestroyMethod(PredestroyClass.class.getMethod("preDestroy"), new PredestroyClass());
registry.executePreDestroy();
assertEquals(true, latch.await(1, TimeUnit.SECONDS));
}
@Override
@PreDestroy
public void close()
{
boolean taskCanceled = false;
for (SqlTask task : tasks.asMap().values()) {
if (task.getTaskStatus().getState().isDone()) {
continue;
}
task.failed(new PrestoException(SERVER_SHUTTING_DOWN, format("Server is shutting down. Task %s has been canceled", task.getTaskId())));
taskCanceled = true;
}
if (taskCanceled) {
try {
TimeUnit.SECONDS.sleep(5);
}
catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
taskNotificationExecutor.shutdownNow();
}
@Override
@PreDestroy
public void close() {
for (PoolDataSource dataSource : dataSources) {
try {
if (LOG.isDebugEnabled()) {
LOG.debug("Closing connection pool named: {}", dataSource.getConnectionPoolName());
}
UniversalConnectionPoolManagerImpl.getUniversalConnectionPoolManager().destroyConnectionPool(dataSource.getConnectionPoolName());
} catch (Exception e) {
if (LOG.isWarnEnabled()) {
LOG.warn("Error closing data source [" + dataSource + "]: " + e.getMessage(), e);
}
}
}
}
/**
* Handles Eureka cleanup, including shutting down all monitors and yielding all EIPs.
*
* @see javax.servlet.ServletContextListener#contextDestroyed(javax.servlet.ServletContextEvent)
*/
@PreDestroy
public void destroy() {
try {
logger.info("{} Shutting down Eureka Server..", new Date().toString());
ServletContext sc = context.getServletContext();
sc.removeAttribute(EurekaServerContext.class.getName());
DiscoveryManager.getInstance().shutdownComponent();
destroyEurekaServerContext();
destroyEurekaEnvironment();
} catch (Throwable e) {
logger.error("Error shutting down eureka", e);
}
logger.info("{} Eureka Service is now shutdown...", new Date().toString());
}
@PreDestroy
void destroy() {
try {
List<CommandSpec> commandSpecs = getCommands();
commandSpecs.forEach(this::unregisterCommand);
} catch (Throwable t) {
log.debug("Failed to unregister commands natively", t);
}
}
@PreDestroy
public void stop() {
offline();
nettyServerForUi.stop();
nettyServerForAgent.stop();
zkClient.close();
}
@PreDestroy
public void tearDown()
{
for (Map.Entry<HostAddress, JedisPool> entry : jedisPoolCache.asMap().entrySet()) {
try {
entry.getValue().destroy();
}
catch (Exception e) {
log.warn(e, "While destroying JedisPool %s:", entry.getKey());
}
}
}
@PreDestroy
public void stop() {
if (this.channelKeeping != null) {
this.channelKeeping.stop();
}
if (this.remotingServer != null) {
this.remotingServer.shutdown();
}
}
@PreDestroy
@Override
public synchronized void close()
throws IOException
{
if (closed) {
return;
}
if (dockerFilesHostPath != null) {
deleteRecursively(dockerFilesHostPath, ALLOW_INSECURE);
dockerFilesHostPath = null;
}
closed = true;
}
/**
* Disconnect the controller from the agent router.
*/
@PreDestroy
public void stop() {
if (port != null) {
router.disconnect(port);
port = null;
}
}
@PreDestroy
private void destroy() {
if (this.destroyCalled) {
throw new IllegalStateException("Already called");
}
this.destroyCalled = true;
}
/**
* Signals the server to shut down and blocks until shutdown is complete.
*
* @throws Exception
*/
@PreDestroy
public void shutdown() throws Exception {
Channel channel = this.channel;
if (channel != null) {
ChannelFuture closeFuture = channel.close();
closeFuture.sync();
this.channel = null;
}
}
@PreDestroy
public void destroy() throws IOException
{
logger.debug("Closing the HttpClient, HttpClientConnectionManager, and all open connections");
httpClient.close();
}
@PreDestroy
public void stop()
{
coreExecutor.shutdownNow();
updateScheduledExecutor.shutdownNow();
errorScheduledExecutor.shutdownNow();
}
@Test
public void testPostConstructAndPreDestroyWithManualConfiguration() {
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
InitDestroyAnnotationBeanPostProcessor bpp = new InitDestroyAnnotationBeanPostProcessor();
bpp.setInitAnnotationType(PostConstruct.class);
bpp.setDestroyAnnotationType(PreDestroy.class);
bf.addBeanPostProcessor(bpp);
bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(AnnotatedInitDestroyBean.class));
AnnotatedInitDestroyBean bean = (AnnotatedInitDestroyBean) bf.getBean("annotatedBean");
assertTrue(bean.initCalled);
bf.destroySingletons();
assertTrue(bean.destroyCalled);
}
@Override
@PreDestroy
public void close() {
if (this.connection != null && this.connection.isConnected()) {
this.connection.disconnect();
}
}
@PreDestroy
public void stop() {
try {
timer.shutdownNow();
timer.awaitTermination(30, TimeUnit.SECONDS);
}
catch(Exception e) {
logger.warn("Failed clean shutdown", e);
}
}
@PreDestroy
private void close() {
try {
executor.shutdown();
isRunning = false;
} catch (Exception e) {
}
}
@PreDestroy
public void shutdown() {
try {
logger.info("Shutting down APNS provider.");
apnsClient.close().await();
} catch (InterruptedException e) {
logger.warn("APNS provider failed to shut down gracefully.", e);
}
}
@PreDestroy
public void preDestroy() {
log.info("try to destroy {} ", ServiceInvocationEventProcessor.class.getSimpleName());
if (masterExecutor != null) {
masterExecutor.shutdown();
}
if (slaveExecutor != null) {
slaveExecutor.shutdown();
}
}
@Override
@PreDestroy
public void close() {
for (org.apache.tomcat.jdbc.pool.DataSource dataSource : dataSources) {
try {
dataSource.close();
} catch (Exception e) {
if (LOG.isWarnEnabled()) {
LOG.warn("Error closing data source [" + dataSource + "]: " + e.getMessage(), e);
}
}
}
}
@Test
public void testPostConstructAndPreDestroyWithManualConfiguration() {
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
InitDestroyAnnotationBeanPostProcessor bpp = new InitDestroyAnnotationBeanPostProcessor();
bpp.setInitAnnotationType(PostConstruct.class);
bpp.setDestroyAnnotationType(PreDestroy.class);
bf.addBeanPostProcessor(bpp);
bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(AnnotatedInitDestroyBean.class));
AnnotatedInitDestroyBean bean = (AnnotatedInitDestroyBean) bf.getBean("annotatedBean");
assertTrue(bean.initCalled);
bf.destroySingletons();
assertTrue(bean.destroyCalled);
}
@PreDestroy
protected void destroy() {
this.number = null;
this.text = null;
}
@PreDestroy
public void destroy() {
log.info("Closing Cache Manager");
Hazelcast.shutdownAll();
}
@Override
@PreDestroy
protected void destroy2() {
super.destroy2();
}
@PreDestroy
public void destroy() {
githubRepositoryApiImpl.destroy();
}
@PreDestroy
public void destory() throws IOException {
client.close();
}
@PreDestroy
public void destroy()
{
shutdownSpringSessionValidationScheduler();
shutdownAsyncManager();
}
@PreDestroy
public void destroy() {
pool.shutdown();
}
@PreDestroy
public void destroy() {
log.info("AnnotationBean 开始销毁");
}