From f20d9a62aeee672c399fd9c2e6fae29783f184db Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Tue, 2 Apr 2019 11:04:00 +0100 Subject: [PATCH] Polish "Ensure that MongoClient's EventLoopGroup is shut down during context close" See gh-16087 --- .../mongo/MongoReactiveAutoConfiguration.java | 12 ++++++------ .../mongo/MongoReactiveAutoConfigurationTests.java | 12 ++++++------ 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mongo/MongoReactiveAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mongo/MongoReactiveAutoConfiguration.java index 569f816b78..62ce95e47c 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mongo/MongoReactiveAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mongo/MongoReactiveAutoConfiguration.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2018 the original author or authors. + * Copyright 2012-2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -86,19 +86,19 @@ public class MongoReactiveAutoConfiguration { @Bean @Order(Ordered.HIGHEST_PRECEDENCE) - public MongoClientSettingsBuilderCustomizer nettyDriverCustomizer( + public NettyDriverMongoClientSettingsBuilderCustomizer nettyDriverCustomizer( ObjectProvider settings) { - return new EventLoopGroupMongoClientSettingsBuilderCustomizer(settings); + return new NettyDriverMongoClientSettingsBuilderCustomizer(settings); } - private static final class EventLoopGroupMongoClientSettingsBuilderCustomizer + private static final class NettyDriverMongoClientSettingsBuilderCustomizer implements MongoClientSettingsBuilderCustomizer, DisposableBean { private final ObjectProvider settings; - private EventLoopGroup eventLoopGroup; + private volatile EventLoopGroup eventLoopGroup; - private EventLoopGroupMongoClientSettingsBuilderCustomizer( + private NettyDriverMongoClientSettingsBuilderCustomizer( ObjectProvider settings) { this.settings = settings; } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mongo/MongoReactiveAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mongo/MongoReactiveAutoConfigurationTests.java index 3828385523..a0cedbb334 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mongo/MongoReactiveAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mongo/MongoReactiveAutoConfigurationTests.java @@ -91,17 +91,17 @@ public class MongoReactiveAutoConfigurationTests { @Test public void nettyStreamFactoryFactoryIsConfiguredAutomatically() { - AtomicReference capture = new AtomicReference<>(); + AtomicReference eventLoopGroupReference = new AtomicReference<>(); this.contextRunner.run((context) -> { assertThat(context).hasSingleBean(MongoClient.class); StreamFactoryFactory factory = getSettings(context).getStreamFactoryFactory(); assertThat(factory).isInstanceOf(NettyStreamFactoryFactory.class); - capture.set((EventLoopGroup) ReflectionTestUtils.getField(factory, - "eventLoopGroup")); - assertThat(capture.get()).isNotNull(); - assertThat(capture.get().isShutdown()).isFalse(); + EventLoopGroup eventLoopGroup = (EventLoopGroup) ReflectionTestUtils + .getField(factory, "eventLoopGroup"); + assertThat(eventLoopGroup.isShutdown()).isFalse(); + eventLoopGroupReference.set(eventLoopGroup); }); - assertThat(capture.get().isShutdown()).isTrue(); + assertThat(eventLoopGroupReference.get().isShutdown()).isTrue(); } @Test