Skip to content

Allow custom cron expression for cleanup task #616

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

Closed
ajthom90 opened this issue Sep 7, 2016 · 10 comments
Closed

Allow custom cron expression for cleanup task #616

ajthom90 opened this issue Sep 7, 2016 · 10 comments
Assignees
Labels
Milestone

Comments

@ajthom90
Copy link

ajthom90 commented Sep 7, 2016

Updated Description

Use spring.session.cleanup.cron.expression property to override the cleanup task cron expression.

Original Description

I have a Spring Boot application using Spring Session 1.2.1.RELEASE. I have two servers running the application behind a load balancer, and the session information is stored in MySQL databases that use master-master replication. When the Scheduled task runs to delete sessions (JdbcOperationsSessionRepository.cleanUpExpiredSessions()), it runs on both web servers, which then tries to run on both databases and the replication runs into issues.

It would be nice to be able to set a system property so the scheduled cleanUpExpiredSessions() method will only run on one database, and MySQL replication will take care of the rest behind the scenes.

@ajthom90 ajthom90 changed the title Allow system property to disable Allow system property to disable deleting of JDBC sessions in multi-database environments Sep 7, 2016
@rwinch
Copy link
Member

rwinch commented Sep 7, 2016

Thanks for the report!

I'm not sure it is quite that simple since you need fail-over. What happens when one of the server instances crashes? Nothing is cleaning up expired sessions then.

@ajthom90
Copy link
Author

ajthom90 commented Sep 7, 2016

Ahh, very good point! Hadn't thought about what would happen if the server in charge of cleaning up sessions goes down.

@vpavic
Copy link
Contributor

vpavic commented Sep 7, 2016

@rwinch We could expose the cron expression used by JdbcOperationsSessionRepository#cleanUpExpiredSessions as a configurable parameter. Users could then adjust the expression to ensure the executions on multiple nodes do not overlap.

@ajthom90
Copy link
Author

ajthom90 commented Sep 8, 2016

I created Pull Request #621 with @vpavic's suggestion about exposing the cron expression. Let me know what you think

(sorry it took me so many commits to get a passing build... apparently I decided to ignore Checkstyle today)

@vpavic
Copy link
Contributor

vpavic commented Sep 11, 2016

@ajthom90 Having in mind this situation can be viewed as an edge case, perhaps it's not worth the complexity it would add to the configuration support. IMO it falls in the same category as custom SQL queries which will be introduced as a part of #613. Setting custom SQL will require users to manually declare JdbcOperationsSessionRepository bean.

That being said, you could easily work around your problem with a custom JdbcOperationsSessionRepository like this example:

public class CustomJdbcOperationsSessionRepository
        extends JdbcOperationsSessionRepository {

    public CustomJdbcOperationsSessionRepository(JdbcOperations jdbcOperations,
            PlatformTransactionManager transactionManager) {
        super(jdbcOperations, transactionManager);
    }

    @Override
    @Scheduled(cron = "desired cron expression")
    public void cleanUpExpiredSessions() {
        super.cleanUpExpiredSessions();
    }

}

When registering the custom repository, make sure the bean is named sessionRepository so it overrides the bean defined in JdbcHttpSessionConfiguration. Here's an example of such config:

@EnableJdbcHttpSession
public class HttpSessionConfig {

    @Bean
    public CustomJdbcOperationsSessionRepository sessionRepository(
            @Qualifier("springSessionJdbcOperations") JdbcOperations jdbcOperations,
            PlatformTransactionManager transactionManager) {
        return new CustomJdbcOperationsSessionRepository(
                jdbcOperations, transactionManager);
    }

}

@rwinch rwinch self-assigned this Sep 13, 2016
@rwinch rwinch added this to the 1.3.0 M1 milestone Sep 13, 2016
@rwinch rwinch changed the title Allow system property to disable deleting of JDBC sessions in multi-database environments Allow custom cron expression for cleanup task Sep 13, 2016
@rwinch rwinch closed this as completed in 5ecf390 Sep 13, 2016
@ajthom90
Copy link
Author

Thanks @rwinch!! That was much simpler than I proposed :) Thanks for your help!!!

@rwinch
Copy link
Member

rwinch commented Sep 13, 2016

@ajthom90 Thanks for the report and helping to make Spring Session better :)

@vpavic
Copy link
Contributor

vpavic commented Sep 13, 2016

@rwinch Great solution to this problem, I like it! 👍

@littlehome-eugene
Copy link


APPLICATION FAILED TO START


Description:

Parameter 0 of method sessionRepository in com.example.AportalApplication required a bean of type 'org.springframework.jdbc.core.JdbcOperations' that could not be found.

Action:

Consider defining a bean of type 'org.springframework.jdbc.core.JdbcOperations' in your configuration.

How to solve this?

@vpavic
Copy link
Contributor

vpavic commented Feb 21, 2018

@littlehome-eugene Commeting on closed issue isn't the best way to report problem. Please open a new issue and provide as much information (related configurations, versions of Spring Session, Boot, etc. you use) as possible to help us diagnose the problem.

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

No branches or pull requests

4 participants