31
31
import org .apache .commons .logging .LogFactory ;
32
32
33
33
import org .springframework .beans .factory .DisposableBean ;
34
+ import org .springframework .beans .factory .InitializingBean ;
34
35
import org .springframework .jdbc .core .ArgumentPreparedStatementSetter ;
35
36
import org .springframework .jdbc .core .JdbcOperations ;
36
37
import org .springframework .jdbc .core .PreparedStatementSetter ;
40
41
import org .springframework .scheduling .support .CronTrigger ;
41
42
import org .springframework .util .Assert ;
42
43
import org .springframework .util .CollectionUtils ;
43
- import org .springframework .util .StringUtils ;
44
44
45
45
/**
46
46
*
56
56
* @author Max Batischev
57
57
* @since 6.4
58
58
*/
59
- public final class JdbcOneTimeTokenService implements OneTimeTokenService , DisposableBean {
59
+ public final class JdbcOneTimeTokenService implements OneTimeTokenService , DisposableBean , InitializingBean {
60
60
61
61
private final Log logger = LogFactory .getLog (getClass ());
62
62
@@ -70,7 +70,7 @@ public final class JdbcOneTimeTokenService implements OneTimeTokenService, Dispo
70
70
71
71
private ThreadPoolTaskScheduler taskScheduler ;
72
72
73
- private static final String DEFAULT_CLEANUP_CRON = "0 * * * * * " ;
73
+ private static final String DEFAULT_CLEANUP_CRON = "@hourly " ;
74
74
75
75
private static final String TABLE_NAME = "one_time_tokens" ;
76
76
@@ -104,23 +104,27 @@ public final class JdbcOneTimeTokenService implements OneTimeTokenService, Dispo
104
104
/**
105
105
* Constructs a {@code JdbcOneTimeTokenService} using the provide parameters.
106
106
* @param jdbcOperations the JDBC operations
107
- * @param cleanupCron cleanup cron expression
108
107
*/
109
- public JdbcOneTimeTokenService (JdbcOperations jdbcOperations , String cleanupCron ) {
110
- Assert .isTrue (StringUtils .hasText (cleanupCron ), "cleanupCron cannot be null orr empty" );
108
+ public JdbcOneTimeTokenService (JdbcOperations jdbcOperations ) {
111
109
Assert .notNull (jdbcOperations , "jdbcOperations cannot be null" );
112
110
this .jdbcOperations = jdbcOperations ;
113
- this .taskScheduler = createTaskScheduler (cleanupCron );
111
+ this .taskScheduler = createTaskScheduler (DEFAULT_CLEANUP_CRON );
114
112
}
115
113
116
114
/**
117
- * Constructs a {@code JdbcOneTimeTokenService} using the provide parameters.
118
- * @param jdbcOperations the JDBC operations
115
+ * Sets the chron expression used for cleaning up expired sessions. The default is to
116
+ * run hourly.
117
+ *
118
+ * For more advanced use cases the cleanupCron may be set to null which will disable
119
+ * the built-in cleanup. Users can then invoke {@link #cleanupExpiredTokens()} using
120
+ * custom logic.
121
+ * @param cleanupCron the chron expression passed to {@link CronTrigger} used for
122
+ * determining how frequent to perform cleanup. The default is "@hourly".
123
+ * @see CronTrigger
124
+ * @see #cleanupExpiredTokens()
119
125
*/
120
- public JdbcOneTimeTokenService (JdbcOperations jdbcOperations ) {
121
- Assert .notNull (jdbcOperations , "jdbcOperations cannot be null" );
122
- this .jdbcOperations = jdbcOperations ;
123
- this .taskScheduler = createTaskScheduler (DEFAULT_CLEANUP_CRON );
126
+ public void setCleanupCron (String cleanupCron ) {
127
+ this .taskScheduler = createTaskScheduler (cleanupCron );
124
128
}
125
129
126
130
@ Override
@@ -174,6 +178,9 @@ private void deleteOneTimeToken(OneTimeToken oneTimeToken) {
174
178
}
175
179
176
180
private ThreadPoolTaskScheduler createTaskScheduler (String cleanupCron ) {
181
+ if (cleanupCron == null ) {
182
+ return null ;
183
+ }
177
184
ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler ();
178
185
taskScheduler .setThreadNamePrefix ("spring-one-time-tokens-" );
179
186
taskScheduler .initialize ();
@@ -188,6 +195,11 @@ public void cleanupExpiredTokens() {
188
195
this .logger .debug ("Cleaned up " + deletedCount + " expired tokens" );
189
196
}
190
197
198
+ @ Override
199
+ public void afterPropertiesSet () throws Exception {
200
+ this .taskScheduler .afterPropertiesSet ();
201
+ }
202
+
191
203
@ Override
192
204
public void destroy () throws Exception {
193
205
if (this .taskScheduler != null ) {
0 commit comments