Developing Your Own ExchangeRateProvider for CurrencyField Type

In the previous entry showing how to develop custom Solr functionalities we are showing how to implement your own Solr filter. Today I’ll show you how to implement your own exchange rate provider, so we will implement the  ExchangeRateProvider for the CurrencyField type introduced in Solr 3.6.

Assumptions

In order to show how to implement your own provider for the CurrencyField type, I assume that our application which uses Solr is able to provide exchange rates in the proper form. So, I assume we have a library which provided implementation for the following interface:

And the Currency class looks like this:

Similar to the previous development entry I’ll omit the details about IDE and jar building. We will focus on the most interesting stuff – the implementation of SolrPLCurrencyExchangeProvider.

SolrPLExchangeRateProvider Implementation

Some Explanations

Some explanations regarding the above code:

  • Linia 11 – we create a class that implements ExchangeRateProvider interface from org.apache.solr.schema package.
  • Linia 12 – our service, which provides exchange rates.
  • Linia 15getExchangeRate method implementation which gets the data from the provided service and returns double value.
  • Linia 19 – get the data regarding the exchange rates. In the production environment we would like to cache this data from some period of time. We don’t do it in the example to simplify the code.
  • Linie 24 – 29 – loop in which we choose the correct Currency object.
  • Linie 30 – 32 – we throw an exception, when proper Currency object wasn’t found.
  • Linie 37 – 41 – we create an instance of our service in the inform method and we initialize it.
  • Linie 48 – 50 – method returning all the available exchange rates.
  • Linie 53 – 56 – method refreshing the provider.

Configuration

After compilation and jar file preparation, we copy the jar file to a directory where Solr will see it. For example, we can create a lib directory in Solr home directory and then add the following entry to solrconfig.xml file:

We also need to change the schema.xml file and add a type based on solr.CurrencyField, for example:

Summary

The main problem with implementing your own ExchangeRateProvider is the data, not the actual implementation as you can see in the above example. I hope that this entry will help some of you and let you save some precious time 😉 Of course, you should remember that this is only an simplified example and we omit the exchange rates cache for example, which should be present in a code you want to use in the production system.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.