100
100
* This connection factory implements {@link InitializingBean} and {@link SmartLifecycle} for flexible lifecycle
101
101
* control. It must be {@link #afterPropertiesSet() initialized} and {@link #start() started} before you can obtain a
102
102
* connection. {@link #afterPropertiesSet() Initialization} {@link SmartLifecycle#start() starts} this bean
103
- * {@link #isAutoStartup() by default}. You can {@link SmartLifecycle#stop()} and {@link SmartLifecycle#start() restart}
104
- * this connection factory if needed.
103
+ * {@link #isEarlyStartup() early} by default. You can {@link SmartLifecycle#stop()} and {@link SmartLifecycle#start()
104
+ * restart} this connection factory if needed. Disabling {@link #isEarlyStartup() early startup} leaves lifecycle
105
+ * management to the container refresh if {@link #isAutoStartup() auto-startup} is enabled.
105
106
*
106
107
* @author Costin Leau
107
108
* @author Jennifer Hickey
@@ -121,13 +122,14 @@ public class LettuceConnectionFactory implements RedisConnectionFactory, Reactiv
121
122
private static final ExceptionTranslationStrategy EXCEPTION_TRANSLATION = new PassThroughExceptionTranslationStrategy (
122
123
LettuceExceptionConverter .INSTANCE );
123
124
125
+ private int phase = 0 ; // in between min and max values
126
+ private boolean autoStartup = true ;
127
+ private boolean earlyStartup = true ;
124
128
private boolean convertPipelineAndTxResults = true ;
125
129
private boolean eagerInitialization = false ;
130
+
126
131
private boolean shareNativeConnection = true ;
127
132
private boolean validateConnection = false ;
128
-
129
- private int phase = 0 ; // in between min and max values
130
-
131
133
private @ Nullable AbstractRedisClient client ;
132
134
133
135
private final AtomicReference <State > state = new AtomicReference <>(State .CREATED );
@@ -556,12 +558,13 @@ public void setShareNativeConnection(boolean shareNativeConnection) {
556
558
557
559
/**
558
560
* Indicates {@link #setShareNativeConnection(boolean) shared connections} should be eagerly initialized. Eager
559
- * initialization requires a running Redis instance during application startup to allow early validation of connection
560
- * factory configuration. Eager initialization also prevents blocking connect while using reactive API and is
561
- * recommended for reactive API usage.
561
+ * initialization requires a running Redis instance during {@link #start() startup} to allow early validation of
562
+ * connection factory configuration. Eager initialization also prevents blocking connect while using reactive API and
563
+ * is recommended for reactive API usage.
562
564
*
563
- * @return {@link true} if the shared connection is initialized upon {@link #afterPropertiesSet ()}.
565
+ * @return {@link true} if the shared connection is initialized upon {@link #start ()}.
564
566
* @since 2.2
567
+ * @see #start()
565
568
*/
566
569
public boolean getEagerInitialization () {
567
570
return this .eagerInitialization ;
@@ -795,6 +798,70 @@ public RedisClusterConfiguration getClusterConfiguration() {
795
798
return isClusterAware () ? (RedisClusterConfiguration ) this .configuration : null ;
796
799
}
797
800
801
+ @ Override
802
+ public int getPhase () {
803
+ return this .phase ;
804
+ }
805
+
806
+ /**
807
+ * Specify the lifecycle phase for pausing and resuming this executor. The default is {@code 0}.
808
+ *
809
+ * @since 3.2
810
+ * @see SmartLifecycle#getPhase()
811
+ */
812
+ public void setPhase (int phase ) {
813
+ this .phase = phase ;
814
+ }
815
+
816
+ /**
817
+ * @since 3.3
818
+ */
819
+ @ Override
820
+ public boolean isAutoStartup () {
821
+ return this .autoStartup ;
822
+ }
823
+
824
+ /**
825
+ * Configure if this Lifecycle connection factory should get started automatically by the container at the time that
826
+ * the containing ApplicationContext gets refreshed.
827
+ * <p>
828
+ * This connection factory defaults to early auto-startup during {@link #afterPropertiesSet()} and can potentially
829
+ * create Redis connections early on in the lifecycle. See {@link #setEarlyStartup(boolean)} for delaying connection
830
+ * creation to the ApplicationContext refresh if auto-startup is enabled.
831
+ *
832
+ * @param autoStartup {@literal true} to automatically {@link #start()} the connection factory; {@literal false}
833
+ * otherwise.
834
+ * @since 3.3
835
+ * @see #setEarlyStartup(boolean)
836
+ * @see #start()
837
+ */
838
+ public void setAutoStartup (boolean autoStartup ) {
839
+ this .autoStartup = autoStartup ;
840
+ }
841
+
842
+ /**
843
+ * @return whether to {@link #start()} the component during {@link #afterPropertiesSet()}.
844
+ * @since 3.3
845
+ */
846
+ public boolean isEarlyStartup () {
847
+ return this .earlyStartup ;
848
+ }
849
+
850
+ /**
851
+ * Configure if this InitializingBean's component Lifecycle should get started early by {@link #afterPropertiesSet()}
852
+ * at the time that the bean is initialized. The component defaults to auto-startup.
853
+ * <p>
854
+ * This method is related to {@link #setAutoStartup(boolean) auto-startup} and can be used to delay Redis client
855
+ * startup until the ApplicationContext refresh. Disabling early startup does not disable auto-startup.
856
+ *
857
+ * @param earlyStartup {@literal true} to early {@link #start()} the component; {@literal false} otherwise.
858
+ * @since 3.3
859
+ * @see #setAutoStartup(boolean)
860
+ */
861
+ public void setEarlyStartup (boolean earlyStartup ) {
862
+ this .earlyStartup = earlyStartup ;
863
+ }
864
+
798
865
/**
799
866
* Specifies if pipelined results should be converted to the expected data type. If {@code false}, results of
800
867
* {@link LettuceConnection#closePipeline()} and {LettuceConnection#exec()} will be of the type returned by the
@@ -924,21 +991,6 @@ public void stop() {
924
991
state .set (State .STOPPED );
925
992
}
926
993
927
- @ Override
928
- public int getPhase () {
929
- return this .phase ;
930
- }
931
-
932
- /**
933
- * Specify the lifecycle phase for pausing and resuming this executor. The default is {@code 0}.
934
- *
935
- * @since 3.2
936
- * @see SmartLifecycle#getPhase()
937
- */
938
- public void setPhase (int phase ) {
939
- this .phase = phase ;
940
- }
941
-
942
994
@ Override
943
995
public boolean isRunning () {
944
996
return State .STARTED .equals (this .state .get ());
@@ -947,7 +999,7 @@ public boolean isRunning() {
947
999
@ Override
948
1000
public void afterPropertiesSet () {
949
1001
950
- if (isAutoStartup ()) {
1002
+ if (isEarlyStartup ()) {
951
1003
start ();
952
1004
}
953
1005
}
0 commit comments