Paul-Blanchaert commented on a change in pull request #5613: URL: https://github.com/apache/camel/pull/5613#discussion_r645463514
########## File path: components/camel-solr/src/main/java/org/apache/camel/component/solr/SolrComponent.java ########## @@ -86,38 +113,105 @@ public SolrComponent() { @Override protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception { - Endpoint endpoint = new SolrEndpoint(uri, this, remaining); + SolrConfiguration configuration = new SolrConfiguration(uri, remaining); + Endpoint endpoint = new SolrEndpoint(uri, this, configuration); setProperties(endpoint, parameters); return endpoint; } + public SolrClient getSolrClient(SolrProducer solrProducer, SolrConfiguration solrConfiguration) throws Exception { + String signature = solrConfiguration.getSignature(); + SolrClientReference solrClientReference; + if (!solrClientMap.containsKey(signature)) { + solrClientReference = new SolrClientReference(solrConfiguration.initSolrClient()); + solrClientMap.put(signature, solrClientReference); + // backward compatibility + addSolrClientToSolrServerReference(solrConfiguration, solrClientReference.getSolrClient()); + } else { + solrClientReference = solrClientMap.get(signature); + } + // register producer against solrClient (for later close of client) + solrClientReference.registerSolrProducer(solrProducer); + return solrClientReference.getSolrClient(); + } + + private void addSolrClientToSolrServerReference(SolrConfiguration solrConfiguration, SolrClient solrClient) { + SolrEndpoint solrEndpoint = solrConfiguration.getSolrEndpoint(); + if (solrEndpoint != null) { + SolrServerReference solrServerReference = servers.get(solrEndpoint); + if (solrServerReference == null) { + solrServerReference = new SolrServerReference(); + servers.put(solrEndpoint, solrServerReference); + } + if (solrClient instanceof CloudSolrClient) { + solrServerReference.setCloudSolrServer((CloudSolrClient) solrClient); + } + if (solrClient instanceof ConcurrentUpdateSolrClient) { + solrServerReference.setUpdateSolrServer((ConcurrentUpdateSolrClient) solrClient); + } + if (solrClient instanceof HttpSolrClient) { + solrServerReference.setSolrServer((HttpSolrClient) solrClient); + } + } + } + + public void closeSolrClient(SolrProducer solrProducer) { + // close when generated for endpoint + List<String> signatureToRemoveList = new ArrayList<>(); + for (Map.Entry<String, SolrClientReference> entry : solrClientMap.entrySet()) { + SolrClientReference solrClientReference = entry.getValue(); + if (solrClientReference.unRegisterSolrProducer(solrProducer) == 0) { + signatureToRemoveList.add(entry.getKey()); + } + } + removeFromSolrClientMap(signatureToRemoveList); + } + + private void removeFromSolrClientMap(Collection<String> signatureToRemoveList) { + for (String signature : signatureToRemoveList) { + SolrClientReference solrClientReference = solrClientMap.get(signature); + solrClientMap.remove(signature); + try { + solrClientReference.getSolrClient().close(); + } catch (IOException e) { + LOG.warn("Error shutting down solr client. This exception is ignored.", e); + } + } + } + + @Deprecated public SolrServerReference getSolrServers(SolrEndpoint endpoint) { return servers.get(endpoint); } + @Deprecated public void addSolrServers(SolrEndpoint endpoint, SolrServerReference servers) { this.servers.put(endpoint, servers); } @Override protected void doShutdown() throws Exception { + removeFromSolrClientMap(solrClientMap.keySet()); for (SolrServerReference server : servers.values()) { shutdownServers(server); } servers.clear(); } + @Deprecated Review comment: ok, thanks -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org