|
| 1 | +/* |
| 2 | + * Copyright 2014 - 2015 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.data.solr.server.support; |
| 17 | + |
| 18 | +import org.apache.solr.client.solrj.SolrClient; |
| 19 | +import org.apache.solr.client.solrj.impl.CloudSolrClient; |
| 20 | +import org.apache.solr.client.solrj.impl.LBHttpSolrClient; |
| 21 | +import org.springframework.beans.factory.DisposableBean; |
| 22 | +import org.springframework.beans.factory.FactoryBean; |
| 23 | +import org.springframework.beans.factory.InitializingBean; |
| 24 | +import org.springframework.util.Assert; |
| 25 | + |
| 26 | +/** |
| 27 | + * |
| 28 | + * @author Christos Manios |
| 29 | + * |
| 30 | + */ |
| 31 | +public class CloudSolrClientFactoryBean extends CloudSolrClientFactory implements FactoryBean<SolrClient>, |
| 32 | + InitializingBean, DisposableBean { |
| 33 | + |
| 34 | + private String zkHost; |
| 35 | + private String collection; |
| 36 | + private Integer zkClientTimeout; |
| 37 | + private Integer zkConnectTimeout; |
| 38 | + private Integer httpConnectTimeout; |
| 39 | + private Integer httpSoTimeout; |
| 40 | + |
| 41 | + @Override |
| 42 | + public void afterPropertiesSet() throws Exception { |
| 43 | + Assert.hasText(zkHost); |
| 44 | + Assert.hasText(collection); |
| 45 | + |
| 46 | + initSolrClient(); |
| 47 | + } |
| 48 | + |
| 49 | + private void initSolrClient() { |
| 50 | + |
| 51 | + // create a new CloudSolrClient with a specified comma delimited string |
| 52 | + // format which contains IP1:port,IP2:port of Zookeeper ensemble |
| 53 | + CloudSolrClient solrClient = new CloudSolrClient(zkHost); |
| 54 | + |
| 55 | + // set collection name |
| 56 | + solrClient.setDefaultCollection(collection); |
| 57 | + |
| 58 | + // set Zookeeper ensemble connection timeout |
| 59 | + if (zkConnectTimeout != null) { |
| 60 | + solrClient.setZkConnectTimeout(zkConnectTimeout); |
| 61 | + } |
| 62 | + |
| 63 | + // set Zookeeper ensemble client timeout |
| 64 | + if (zkClientTimeout != null) { |
| 65 | + solrClient.setZkClientTimeout(zkClientTimeout); |
| 66 | + } |
| 67 | + |
| 68 | + // set http connection timeout |
| 69 | + if (httpConnectTimeout != null) { |
| 70 | + solrClient.getLbClient().setConnectionTimeout(httpConnectTimeout); |
| 71 | + } |
| 72 | + |
| 73 | + // set http read timeout |
| 74 | + if (httpSoTimeout != null) { |
| 75 | + solrClient.getLbClient().setSoTimeout(httpSoTimeout); |
| 76 | + } |
| 77 | + |
| 78 | + this.setSolrClient(solrClient); |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public SolrClient getObject() throws Exception { |
| 83 | + return getSolrClient(); |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public Class<?> getObjectType() { |
| 88 | + if (getSolrClient() == null) { |
| 89 | + return CloudSolrClient.class; |
| 90 | + } |
| 91 | + return getSolrClient().getClass(); |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + public boolean isSingleton() { |
| 96 | + return true; |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Returns a pair of IP and its respective port of the Zookeeper server which belongs to the Zookeeper ensemble. This |
| 101 | + * string can contain multiple IP:port definitions separated by comma. |
| 102 | + * <p> |
| 103 | + * Example: <code>192.168.1.1:2181,192.168.1.2:2181,192.168.1.3:2181</code> |
| 104 | + * </p> |
| 105 | + * |
| 106 | + */ |
| 107 | + public String getZkHost() { |
| 108 | + return zkHost; |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Sets the IPs and their respective ports of the Zookeeper servers which belong to the Zookeeper ensemble. This |
| 113 | + * string can contain multiple IP:port definitions separated by comma. |
| 114 | + * <p> |
| 115 | + * Example: <code>192.168.1.1:2181,192.168.1.2:2181,192.168.1.3:2181</code> |
| 116 | + * </p> |
| 117 | + * |
| 118 | + */ |
| 119 | + public void setZkHost(String zkHost) { |
| 120 | + this.zkHost = zkHost; |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Returns the SolrCloud collection name on which this {@link CloudSolrClient} will be connected to. |
| 125 | + */ |
| 126 | + public String getCollection() { |
| 127 | + return collection; |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * Set the SolrCloud collection name on which this {@link CloudSolrClient} will be connected to. |
| 132 | + */ |
| 133 | + public void setCollection(String collectionName) { |
| 134 | + this.collection = collectionName; |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * Returns the HTTP connection timeout of underlying {@link LBHttpSolrClient} used for queries, in milliseconds. |
| 139 | + * <p> |
| 140 | + * Default is 0 (infinite timeout) |
| 141 | + * </p> |
| 142 | + */ |
| 143 | + public Integer getHttpConnectTimeout() { |
| 144 | + return httpConnectTimeout; |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * Sets the HTTP connection timeout of underlying {@link LBHttpSolrClient} in milliseconds. |
| 149 | + * |
| 150 | + * @param httpConnectTimeout HTTP connect timeout in milliseconds . Default is 0 (infinite timeout) |
| 151 | + * |
| 152 | + */ |
| 153 | + public void setHttpConnectTimeout(Integer httpConnectTimeout) { |
| 154 | + this.httpConnectTimeout = httpConnectTimeout; |
| 155 | + } |
| 156 | + |
| 157 | + /** |
| 158 | + * Returns the HTTP soTimeout (read timeout) of underlying {@link LBHttpSolrClient} used for queries, in milliseconds. |
| 159 | + * <p> |
| 160 | + * Default is 0 (infinite timeout) |
| 161 | + * </p> |
| 162 | + */ |
| 163 | + public Integer getHttpSoTimeout() { |
| 164 | + return httpSoTimeout; |
| 165 | + } |
| 166 | + |
| 167 | + /** |
| 168 | + * Sets the HTTP soTimeout (read timeout) of underlying {@link LBHttpSolrClient} in milliseconds. |
| 169 | + * |
| 170 | + * @param httpReadTimeout HTTP read timeout in milliseconds. Default is 0 (infinite timeout) |
| 171 | + */ |
| 172 | + public void setHttpSoTimeout(Integer httpSoTimeout) { |
| 173 | + this.httpSoTimeout = httpSoTimeout; |
| 174 | + } |
| 175 | + |
| 176 | + /** |
| 177 | + * Returns the client timeout to the zookeeper ensemble in milliseconds |
| 178 | + */ |
| 179 | + public Integer getZkClientTimeout() { |
| 180 | + return zkClientTimeout; |
| 181 | + } |
| 182 | + |
| 183 | + /** |
| 184 | + * Sets the client timeout to the zookeeper ensemble in milliseconds. Default value: 10000ms |
| 185 | + * |
| 186 | + * @param zkClientTimeout client timeout to zookeeper ensemble in milliseconds. |
| 187 | + */ |
| 188 | + public void setZkClientTimeout(Integer zkClientTimeout) { |
| 189 | + this.zkClientTimeout = zkClientTimeout; |
| 190 | + } |
| 191 | + |
| 192 | + /** |
| 193 | + * Returns the connection timeout to the zookeeper ensemble in milliseconds. Default value: 10000ms |
| 194 | + * |
| 195 | + * @param zkConnectTimeout connection timeout to zookeeper ensemble in milliseconds. |
| 196 | + */ |
| 197 | + public Integer getZkConnectTimeout() { |
| 198 | + return zkConnectTimeout; |
| 199 | + } |
| 200 | + |
| 201 | + /** |
| 202 | + * Sets the connection timeout to the zookeeper ensemble in milliseconds. Default value: 10000ms |
| 203 | + * |
| 204 | + * @param zkConnectTimeout connection timeout to zookeeper ensemble in milliseconds. |
| 205 | + */ |
| 206 | + public void setZkConnectTimeout(Integer zkConnectTimeout) { |
| 207 | + this.zkConnectTimeout = zkConnectTimeout; |
| 208 | + } |
| 209 | + |
| 210 | +} |
0 commit comments