Subversion Repositories SmartDukaan

Rev

Go to most recent revision | Details | 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;
9
import org.springframework.context.event.ContextRefreshedEvent;
10
import org.springframework.stereotype.Component;
11
 
12
/**
13
 * Triggers a full Solr sync on application startup.
14
 * Ensures data consistency after application restarts.
15
 * Only active in production environment.
16
 */
17
@Component
18
public class SolrStartupSync implements ApplicationListener<ContextRefreshedEvent> {
19
 
20
    private static final Logger logger = LoggerFactory.getLogger(SolrStartupSync.class);
21
    private boolean executed = false;
22
 
23
    @Value("${prod:false}")
24
    private boolean isProd;
25
 
26
    @Autowired
27
    private FofoSolr fofoSolr;
28
 
29
    @Override
30
    public void onApplicationEvent(ContextRefreshedEvent event) {
31
        // Only execute once (ContextRefreshedEvent may fire multiple times)
32
        if (executed) {
33
            return;
34
        }
35
        executed = true;
36
 
37
        if (!isProd) {
38
            logger.info("Skipping Solr startup sync in non-prod environment");
39
            return;
40
        }
41
 
42
        logger.info("Application context refreshed - triggering full Solr sync for data consistency");
43
        try {
44
            fofoSolr.pushData();
45
            logger.info("Full Solr sync completed successfully on startup");
46
        } catch (Exception e) {
47
            logger.error("Full Solr sync failed on startup", e);
48
        }
49
    }
50
}