1
2
3
4
5
6 package org.tailormap.api.solr;
7
8 import io.micrometer.core.instrument.Metrics;
9 import java.io.IOException;
10 import java.util.concurrent.TimeUnit;
11 import org.apache.solr.client.solrj.SolrClient;
12 import org.apache.solr.client.solrj.SolrServerException;
13 import org.apache.solr.client.solrj.impl.Http2SolrClient;
14 import org.apache.solr.client.solrj.response.SolrPingResponse;
15 import org.springframework.beans.factory.annotation.Value;
16 import org.springframework.stereotype.Service;
17
18 @Service
19 public class SolrService {
20 @Value("${tailormap-api.solr-url}")
21 private String solrUrl;
22
23 @Value("${tailormap-api.solr-core-name:tailormap}")
24 private String solrCoreName;
25
26 @Value("${tailormap-api.solr-connection-timeout-seconds:60}")
27 private int solrConnectionTimeout;
28
29 @Value("${tailormap-api.solr-request-timeout-seconds:240}")
30 private int solrRequestTimeout;
31
32 @Value("${tailormap-api.solr-idle-timeout-seconds:10}")
33 private int solrIdleTimeout;
34
35
36
37
38
39
40 public SolrClient getSolrClientForIndexing() {
41 return new Http2SolrClient.Builder(this.solrUrl + this.solrCoreName)
42 .withFollowRedirects(true)
43 .withConnectionTimeout(solrConnectionTimeout, TimeUnit.SECONDS)
44 .withRequestTimeout(solrRequestTimeout, TimeUnit.SECONDS)
45
46
47
48 .withIdleTimeout(solrIdleTimeout, TimeUnit.SECONDS)
49 .build();
50 }
51
52
53
54
55
56
57 public SolrClient getSolrClientForSearching() {
58 return new Http2SolrClient.Builder(this.solrUrl + this.solrCoreName)
59 .withConnectionTimeout(solrConnectionTimeout, TimeUnit.SECONDS)
60 .withFollowRedirects(true)
61 .build();
62 }
63
64 public void setSolrUrl(String solrUrl) {
65 this.solrUrl = solrUrl;
66 }
67
68 public boolean isSolrServiceAvailable() {
69 try (SolrClient client = getSolrClientForIndexing()) {
70 SolrPingResponse response = client.ping();
71 Metrics.timer("tailormap_solr_ping").record(response.getElapsedTime(), TimeUnit.MILLISECONDS);
72 return true;
73 } catch (SolrServerException | IOException e) {
74 return false;
75 }
76 }
77 }