Subversion Repositories SmartDukaan

Rev

Rev 35547 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
35547 amit 1
package com.spice.profitmandi.dao.event;
2
 
3
import com.spice.profitmandi.dao.service.solr.FofoSolr;
4
import org.slf4j.Logger;
5
import org.slf4j.LoggerFactory;
6
import org.springframework.beans.factory.annotation.Autowired;
7
import org.springframework.beans.factory.annotation.Value;
8
import org.springframework.context.ApplicationListener;
35800 amit 9
import org.springframework.context.annotation.Profile;
35547 amit 10
import org.springframework.context.event.ContextRefreshedEvent;
11
import org.springframework.stereotype.Component;
12
 
13
/**
14
 * Triggers a full Solr sync on application startup.
15
 * Ensures data consistency after application restarts.
16
 * Only active in production environment.
17
 */
18
@Component
35800 amit 19
@Profile("scheduled")
35547 amit 20
public class SolrStartupSync implements ApplicationListener<ContextRefreshedEvent> {
21
 
22
    private static final Logger logger = LoggerFactory.getLogger(SolrStartupSync.class);
23
    private boolean executed = false;
24
 
25
    @Value("${prod:false}")
26
    private boolean isProd;
27
 
28
    @Autowired
29
    private FofoSolr fofoSolr;
30
 
31
    @Override
32
    public void onApplicationEvent(ContextRefreshedEvent event) {
33
        // Only execute once (ContextRefreshedEvent may fire multiple times)
34
        if (executed) {
35
            return;
36
        }
37
        executed = true;
38
 
39
        if (!isProd) {
40
            logger.info("Skipping Solr startup sync in non-prod environment");
41
            return;
42
        }
43
 
44
        logger.info("Application context refreshed - triggering full Solr sync for data consistency");
45
        try {
46
            fofoSolr.pushData();
47
            logger.info("Full Solr sync completed successfully on startup");
48
        } catch (Exception e) {
49
            logger.error("Full Solr sync failed on startup", e);
50
        }
51
    }
52
}