Create a dynamic config engine in 5 minutes, using Java

Author : JONSON
Publish Date : 2021-03-21 18:03:39
 Create a dynamic config engine in 5 minutes, using Java

Create a dynamic config engine in 5 minutes, using Java What is a runtime hierarchical config engine?

 

A hierarchical config engine is a collection of config rules, where the rules get more specific as you travel down your hierarchy. The rules apply to each request in runtime, so its a runtime hierarchical config engine

For example you can have below config rules :-

 

https://belarus.expatreporter.com/advert/watch-free-writers-guild-of-america-awards-2021-live-stream-full-show-free/


https://www.telerealrd.com/advert/abc-free-writers-guild-of-america-awards-2021-live-stream-full-show-free/


https://belarus.expatreporter.com/advert/march-madness-oklahoma-state-vs-oregon-state-live-stream-mens-basketball-free/


https://belarus.expatreporter.com/advert/march-madness-syracuse-vs-west-virginia-live-stream-mens-basketball-free/


https://belarus.expatreporter.com/advert/march-madness-villanova-vs-north-texas-live-stream-mens-basketball-free/

Country Code CityCode AirlineCode Surcharge
us null null 1.00
null null dl 6.00
us null dl 3.00
us nyc null 2.50
us nyc aa 4.50
us nyc dl 5.50

Config rules

These config rules would mean:-

  • All Airlines in US have a surcharge of 1.00, with following exceptions:-
    • Delta (dl) airlines in US has a surcharge of 3.00
    • All airlines in us and nyc has a surcharge of 2.50 with following exceptions:-
      • Delta airlines in us and nyc has a surcharge of 5.50
      • American airlines (aa) in us and nyc has a surcharge of 4.50
  • All Delta flights across the world have a surcharge of 6.00

 

So the idea is that you can define general rules and more specific rules and the most specific rule will apply to your runtime record.

 

What are we building in this example


Let’s build a simple runtime config engine that applies a surcharge to customer selected flight based on origin’s country, city and airline.

Step 1: Get Kiara


<dependency>
	<groupId>com.kapoorlabs</groupId>
	<artifactId>kiara</artifactId>
	<version>2.0.10</version>
</dependency>


Step 2: Create your Config Data POJO


 

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Config {

	private String country;
	
	private String originCity;
	
	private String airline;
	
	private Double surcharge;
	
}

 

Create your config store

 


import com.kapoorlabs.kiara.domain.Store;
import com.kapoorlabs.kiara.exception.LoadDataException;
import com.kapoorlabs.kiara.loader.StoreLoader;

	public void loadConfigStore() throws LoadDataException {
		configStore = new Store<>(Config.class);
		StoreLoader<Config> storeLoader = new StoreLoader<>(configStore);

		storeLoader.loadTable(new Config("in", null, null, 2.00));
		storeLoader.loadTable(new Config("us", null, null, 1.00));
		storeLoader.loadTable(new Config("us", null, "dl", 3.00));
		storeLoader.loadTable(new Config("us", "nyc", null, 2.50));
		storeLoader.loadTable(new Config("us", "nyc", "aa", 4.50));
		storeLoader.loadTable(new Config("us", "nyc", "dl", 5.50));

		storeLoader.prepareForSearch();

	}

In this example we are just loading the config data from within the source code, but the source of the config data can be any data store, such as a file, database, a network stream, etc.

 

Write your search method that applies config rule

 

import com.kapoorlabs.kiara.domain.Store;
import com.kapoorlabs.kiara.exception.LoadDataException;
import com.kapoorlabs.kiara.loader.StoreLoader;

	public Double getSurcharge(Store<Config> configStore, FlightRecord flightRecord) {
		String country = flightRecord.getCountry();
		
		String city = flightRecord.getCity();
		
		String airline = flightRecord.getAirline();

		KeywordSearchResult<Config> result = keywordSearch.getMinimumMatch(country + " " + city + " " + airline, configStore);

                Double surcharge = result.getResult().get(0).getSurCharge()

                return surcharge;

	}

 

And your are Done!

 

Explaining Results


Given you have following rules: –

Country Code CityCode AirlineCode Surcharge
us null null 1.00
null null dl 6.00
us null dl 3.00
us nyc null 2.50
us nyc aa 4.50
us nyc dl 5.50

Config rules

 

The following results should be self explanatory, but feel free to post a question in comments, in case of any doubt.

keywordSearch.getMinimumMatch(” us “, configStore); -> will give you surcharge of 1.00

keywordSearch.getMinimumMatch(” us dl”, configStore); -> will give you surcharge of 3.00

keywordSearch.getMinimumMatch(” nyc us dl”, configStore); -> will give you surcharge of 5.50

keywordSearch.getMinimumMatch(” bos us dl”, configStore); -> will give you surcharge of 3.00

keywordSearch.getMinimumMatch(” us nyc jb”, configStore); -> will give you surcharge of 2.50

 


https://blog.goo.ne.jp/lifetvs/e/3d0c7aa26128aa74163c41d8056d4ec6


https://www.deviantart.com/gdsffds/journal/Create-a-dynamic-config-engine-in-5-minutes-usin-873925623


https://paiza.io/projects/jWCd_FuOMq0EovUlf9XnWA


https://caribbeanfever.com/photo/albums/saasdsasad

https://www.thewyco.com/general/create-a-dynamic-config-engine-in-5-minutes-using-java-21-03-2021



Category : general

E3 2021: Why are the presentations so disappointing this year?

E3 2021: Why are the presentations so disappointing this year?

- As we all know, the highly anticipated E3 2021 event just kicked off on Saturday, and it officially marks the first all-virtual version of this special


What Does It Mean If You Dream About Fire

What Does It Mean If You Dream About Fire

- A dream about fire is common, but what do dreams about fire mean? What causes it? How do you interpret it? There are many


Choosing the right stroller for your baby

Choosing the right stroller for your baby

- This essential purchase when a toddler arrives can turn into a puzzle as the variety of models, options and accessories are so great


Try QuizDumps most updated SAP C_LUMIRA_23 dumps

Try QuizDumps most updated SAP C_LUMIRA_23 dumps

- Try QuizDumps most updated SAP C_LUMIRA_23 dumps and clear your C_LUMIRA_23 exam in the first attempt.You may find many people who want to build their career pr