List of usage examples for org.apache.solr.core CloseHook CloseHook
CloseHook
From source file:com.search.MySearchHandler.java
License:Apache License
/** * Initialize the components based on name. Note, if using * <code>INIT_FIRST_COMPONENTS</code> or <code>INIT_LAST_COMPONENTS</code>, * then the {@link DebugComponent} will always occur last. If this is not * desired, then one must explicitly declare all components using the * <code>INIT_COMPONENTS</code> syntax. */// w w w .j av a2s. c o m @Override @SuppressWarnings("unchecked") public void inform(SolrCore core) { Object declaredComponents = initArgs.get(INIT_COMPONENTS); List<String> first = (List<String>) initArgs.get(INIT_FIRST_COMPONENTS); List<String> last = (List<String>) initArgs.get(INIT_LAST_COMPONENTS); List<String> list = null; boolean makeDebugLast = true; if (declaredComponents == null) { // Use the default component list list = getDefaultComponents(); if (first != null) { List<String> clist = first; clist.addAll(list); list = clist; } if (last != null) { list.addAll(last); } } else { list = (List<String>) declaredComponents; if (first != null || last != null) { System.out.println("First/Last components only valid if you do not declare 'components'"); } makeDebugLast = false; } // Build the component list components = new ArrayList<SearchComponent>(list.size()); DebugComponent dbgCmp = null; for (String c : list) { SearchComponent comp = core.getSearchComponent(c); if (comp instanceof DebugComponent && makeDebugLast == true) { dbgCmp = (DebugComponent) comp; } else { components.add(comp); log.debug("Adding component:" + comp); } } if (makeDebugLast == true && dbgCmp != null) { components.add(dbgCmp); log.debug("Adding debug component:" + dbgCmp); } if (shfInfo == null) { shardHandlerFactory = core.getCoreDescriptor().getCoreContainer().getShardHandlerFactory(); } else { shardHandlerFactory = core.createInitInstance(shfInfo, ShardHandlerFactory.class, null, null); core.addCloseHook(new CloseHook() { @Override public void preClose(SolrCore core) { shardHandlerFactory.close(); } @Override public void postClose(SolrCore core) { } }); } }
From source file:com.search.MySearchHandlerTest.java
License:Apache License
/** * Initialize the components based on name. Note, if using <code>INIT_FIRST_COMPONENTS</code> or <code>INIT_LAST_COMPONENTS</code>, * then the {@link DebugComponent} will always occur last. If this is not desired, then one must explicitly declare all components using * the <code>INIT_COMPONENTS</code> syntax. *///ww w . j a v a 2s.co m @Override @SuppressWarnings("unchecked") public void inform(SolrCore core) { Object declaredComponents = initArgs.get(INIT_COMPONENTS); List<String> first = (List<String>) initArgs.get(INIT_FIRST_COMPONENTS); List<String> last = (List<String>) initArgs.get(INIT_LAST_COMPONENTS); List<String> list = null; boolean makeDebugLast = true; if (declaredComponents == null) { // Use the default component list list = getDefaultComponents(); if (first != null) { List<String> clist = first; clist.addAll(list); list = clist; } if (last != null) { list.addAll(last); } } else { list = (List<String>) declaredComponents; if (first != null || last != null) { throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "First/Last components only valid if you do not declare 'components'"); } makeDebugLast = false; } // Build the component list components = new ArrayList<SearchComponent>(list.size()); DebugComponent dbgCmp = null; for (String c : list) { SearchComponent comp = core.getSearchComponent(c); if (comp instanceof DebugComponent && makeDebugLast == true) { dbgCmp = (DebugComponent) comp; } else { components.add(comp); log.debug("Adding component:" + comp); } } if (makeDebugLast == true && dbgCmp != null) { components.add(dbgCmp); log.debug("Adding debug component:" + dbgCmp); } if (shfInfo == null) { shardHandlerFactory = core.getCoreDescriptor().getCoreContainer().getShardHandlerFactory(); } else { shardHandlerFactory = core.createInitInstance(shfInfo, ShardHandlerFactory.class, null, null); core.addCloseHook(new CloseHook() { @Override public void preClose(SolrCore core) { shardHandlerFactory.close(); } @Override public void postClose(SolrCore core) { } }); } }
From source file:com.zemanta.solrcassandrabridge.CassandraBridgeComponent.java
License:Apache License
@Override public void inform(SolrCore core) { log.warn("A2"); log.info("B2"); core.addCloseHook(new CloseHook() { @Override/*ww w. ja v a2s. c om*/ public void preClose(SolrCore core) { cassandraConnector.close(); } @Override public void postClose(SolrCore core) { } }); }
From source file:org.alfresco.solr.lifecycle.SolrCoreLoadRegistration.java
License:Open Source License
/** * Registers with the admin handler the information server and the trackers. *///from w ww.j av a 2 s . c o m public static void registerForCore(AlfrescoCoreAdminHandler adminHandler, CoreContainer coreContainer, SolrCore core, String coreName) { TrackerRegistry trackerRegistry = adminHandler.getTrackerRegistry(); Properties props = new CoreDescriptorDecorator(core.getCoreDescriptor()).getProperties(); //Prepare cores SolrResourceLoader loader = core.getLatestSchema().getResourceLoader(); SolrKeyResourceLoader keyResourceLoader = new SolrKeyResourceLoader(loader); SOLRAPIClientFactory clientFactory = new SOLRAPIClientFactory(); SOLRAPIClient repositoryClient = clientFactory.getSOLRAPIClient(props, keyResourceLoader, AlfrescoSolrDataModel.getInstance().getDictionaryService(CMISStrictDictionaryService.DEFAULT), AlfrescoSolrDataModel.getInstance().getNamespaceDAO()); //Start content store SolrContentStore contentStore = new SolrContentStore(coreContainer.getSolrHome()); SolrInformationServer srv = new SolrInformationServer(adminHandler, core, repositoryClient, contentStore); props.putAll(srv.getProps()); adminHandler.getInformationServers().put(coreName, srv); SolrTrackerScheduler scheduler = adminHandler.getScheduler(); // Prevents other threads from registering the ModelTracker at the same time // Create model tracker and load all the persisted models createModelTracker(coreName, trackerRegistry, props, coreContainer.getSolrHome(), repositoryClient, srv, scheduler); log.info("Starting to track " + coreName); if (Boolean.parseBoolean(props.getProperty("enable.alfresco.tracking", "false"))) { if (trackerRegistry.hasTrackersForCore(coreName)) { log.info("Trackers for " + coreName + " is already registered, shutting them down."); shutdownTrackers(coreName, trackerRegistry.getTrackersForCore(coreName), scheduler); trackerRegistry.removeTrackersForCore(coreName); adminHandler.getInformationServers().remove(coreName); } List<Tracker> trackers = createCoreTrackers(coreName, trackerRegistry, props, scheduler, repositoryClient, srv); CommitTracker commitTracker = new CommitTracker(props, repositoryClient, coreName, srv, trackers); trackerRegistry.register(coreName, commitTracker); scheduler.schedule(commitTracker, coreName, props); log.info("The Trackers are now scheduled to run"); trackers.add(commitTracker); //Add the commitTracker to the list of scheduled trackers that can be shutdown core.addCloseHook(new CloseHook() { @Override public void preClose(SolrCore core) { log.info("Shutting down " + core.getName()); SolrCoreLoadRegistration.shutdownTrackers(core.getName(), trackers, scheduler); } @Override public void postClose(SolrCore core) { // Nothing to be done here } }); } }
From source file:org.springframework.data.rest.webmvc.solr.SolrInfrastructureConfig.java
License:Apache License
/** * {@link SpringJUnit4ClassRunner} executes {@link ClassRule}s before the actual shutdown of the * {@link ApplicationContext}. This causes the {@link TemporaryFolder} to vanish before Solr can gracefully shutdown. * <br />//from w ww . ja v a2s.c om * To prevent error messages popping up we register a {@link CloseHook} re adding the index directory and removing it * after {@link SolrCore#close()}. * * @param factory */ private void attachCloseHook(SolrClientFactory factory) { EmbeddedSolrServer server = (EmbeddedSolrServer) factory.getSolrClient(); for (SolrCore core : server.getCoreContainer().getCores()) { core.addCloseHook(new CloseHook() { private String path; @Override public void preClose(SolrCore core) { CoreDescriptor cd = core.getCoreDescriptor(); if (cd == null) { return; } File tmp = new File(core.getIndexDir()).getParentFile(); if (tmp.exists()) { return; } try { File indexFile = new File(tmp, "index"); indexFile.mkdirs(); this.path = indexFile.getPath(); } catch (Exception e) { e.printStackTrace(); } } @Override public void postClose(SolrCore core) { if (!StringUtils.hasText(this.path)) { return; } File tmp = new File(this.path); if (tmp.exists() && tmp.getPath().startsWith(FileUtils.getTempDirectoryPath())) { try { FileUtils.deleteDirectory(tmp); } catch (IOException e) { e.printStackTrace(); } } } }); } }
From source file:org.springframework.data.solr.AbstractITestWithEmbeddedSolrServer.java
License:Apache License
@BeforeClass public static void initSolrServer() throws IOException, ParserConfigurationException, SAXException, InterruptedException { String solrHome = ResourceUtils.getURL("classpath:org/springframework/data/solr").getPath(); CoreContainer coreContainer = CoreContainer.createAndLoad(solrHome, new File(solrHome + "/solr.xml")); for (SolrCore core : coreContainer.getCores()) { core.addCloseHook(new CloseHook() { @Override/*from w ww . j a va 2 s.co m*/ public void preClose(SolrCore core) { } @Override public void postClose(SolrCore core) { CoreDescriptor cd = core.getCoreDescriptor(); if (cd != null) { File dataDir = new File(cd.getInstanceDir() + File.separator + "data"); try { FileUtils.deleteDirectory(dataDir); } catch (IOException e) { e.printStackTrace(); } } } }); } solrServer = new EmbeddedSolrServer(coreContainer, "collection1"); }
From source file:uk.co.flax.biosolr.solr.update.processor.OntologyUpdateProcessorFactory.java
License:Apache License
private void initialiseOntologyCheckScheduler(SolrCore core) { executor = new ScheduledThreadPoolExecutor(1, new DefaultSolrThreadFactory("ontologyUpdate"), (Runnable r, ThreadPoolExecutor e) -> LOGGER.warn("Skipping execution of '{}' using '{}'", r, e)); executor.setExecuteExistingDelayedTasksAfterShutdownPolicy(false); executor.setContinueExistingPeriodicTasksAfterShutdownPolicy(false); // Add CloseHook to tidy up if core closes core.addCloseHook(new CloseHook() { @Override/* w ww . ja v a 2 s . c o m*/ public void preClose(SolrCore core) { LOGGER.info("Triggering graceful shutdown of OntologyUpdate executor"); disposeHelper(); executor.shutdown(); } @Override public void postClose(SolrCore core) { if (executor.isTerminating()) { LOGGER.info("Forcing shutdown of OntologyUpdate executor"); executor.shutdownNow(); } } }); if (DELETE_CHECK_DELAY_MS > 0) { executor.scheduleAtFixedRate(new OntologyCheckRunnable(this), DELETE_CHECK_DELAY_MS, DELETE_CHECK_DELAY_MS, TimeUnit.MILLISECONDS); } }