I am working with an application that aggregates data from multiple sources. In this case I need to connect to two different Solr services with unrelated data. To this end I set up two different data repositories. I have defined the beans as follows:
@Configuration
@EnableSolrRepositories(basePackages={"foo.utilities.solr.repos.gcr"}, multicoreSupport=true)
public class GcrSolrContext {
@Bean
public SolrClient solrClient() {
return new HttpSolrClient("http://foo:8086/solr/gcr");
}
@Bean
public SolrTemplate solrTemplate(SolrClient client) throws Exception {
return new SolrTemplate(client);
}
}
The problem I have is that I cannot figure out how to have two completely independent Solr clients, with each pointing at different urls. As the beans solrClient() and solrTemplate() are needed, and trying to create a new Context with different URLs simply gets the solrClient and solrTemplate bean overwritten with the beans created first. Each client works fine if they are the only Solr clients defined.
In short, how can I create two (or more) different Spring Solr clients, each connecting to different URLS?
Additional Information is response to comments...
I have attempted to simply recreate the SolrContext class with different configurations.Consider the following:
@Configuration
@EnableSolrRepositories(basePackages={"foo.utilities.solr.repos.different"}, multicoreSupport=true)
public class DifferentSolrContext {
@Bean
public SolrClient solrClient() {
return new HttpSolrClient("http://SomethingDifferent:8086/solr/something");
}
@Bean
public SolrTemplate solrTemplate(SolrClient client) throws Exception {
return new SolrTemplate(client);
}
}
What happens here is that the @Bean's solrClient, and solrTemplate get overridden when the beans are created during startup.
INFO : org.springframework.beans.factory.support.DefaultListableBeanFactory - Overriding bean definition for bean 'solrClient' with a different definition: replacing [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=yyySolrContext; factoryMethodName=solrClient; initMethodName=null; destroyMethodName=(inferred); defined in foo.utilities.solr.GcrSolrContext] with [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=xxxSolrContext; factoryMethodName=solrClient; initMethodName=null; destroyMethodName=(inferred); defined in foo.utilities.solr.XxxSolrContext]
INFO : org.springframework.beans.factory.support.DefaultListableBeanFactory - Overriding bean definition for bean 'solrTemplate' with a different definition: replacing [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=yyySolrContext; factoryMethodName=solrTemplate; initMethodName=null; destroyMethodName=(inferred); defined in foo.utilities.solr.GcrSolrContext] with [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=haystackSolrContext; factoryMethodName=solrTemplate; initMethodName=null; destroyMethodName=(inferred); defined in foo.utilities.solr.XxxSolrContext]
Testing shows that the URL is indeed only the first bean instantiated. I have also tried renaming the second context beans to something else, but then the Solr functionality can't locate/identify the beans.
Here is the error thrown when trying to define more than one SolrClient.
Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'org.apache.solr.client.solrj.SolrClient' available: expected single matching bean but found 2: solrClient2,solrClient
In short it appears that only one URL is possible for the Spring Solr Data library. (note: using version 2.1.4) Does anyone have more than one URL (not just core) working under Spring Solr data?