This is a follow up post from dynamic proxy to share about more realistic examples of dynamic proxy.
Today's software development is heavily reliant on system observability. Observability helps us to understand when the system degrades or misbehaves so that we can take proactive measures to fix it.
Toy service for this example will be FXService
public interface FXService {
double convert(String from, String to, int amount);
}
This service will use https://api.exchangerate.host API for FX conversion.
Lets look at what types of dynamic proxy we can create on top of this.
Method Timing
It will keep track of method execution time and make it available later for analysis or other purposes. Using this proxy, X slow-running methods will be provided.
In FXService, there is only one method, so this proxy will create a method key with the method name and parameters.
In FXService, there is only one method, so this proxy will create a method key with the method name and parameters.
Method convert( SGD,IDR,1 ) took 1041 ms
Method convert( SGD,GBP,1 ) took 994 ms
Method convert( SGD,USD,1 ) took 983 ms
Method convert( SGD,IDR,1 ) took 672 ms
Method convert( SGD,INR,1 ) took 650 ms
Method convert( SGD,USD,1 ) took 593 ms
Method convert( SGD,JPY,1 ) took 593 ms
Method convert( SGD,GBP,1 ) took 582 ms
Method convert( SGD,USD,1 ) took 580 ms
Method convert( SGD,INR,1 ) took 566 ms
Such type of proxy is very helpful in identifying outage or degradation in API.
Stand In Processing
Proxy services such as this can be used to provide stand-in processing when the underlying service is down. For example, when a real FX service is down, this proxy can answer queries from the last successful call.
It is useful for not only enhancing availability but also improving latency, since such a service can answer queries from the local cache right away. Additionally, it may be possible to save some costs if the underlying API is charged by usage.
Chain of proxy
Nice thing about proxies is that multiple proxies can be composed together to create a complex chain of proxy. For example, we can chain Stand In & Method timing together to get features of both.
Below code snippet is creating chain of proxy
FXService core = new FXServiceAPI("https://api.exchangerate.host", 1);
FXService timeRecorderProxy = create(FXService.class, new TimeRecorderProxy(core, tracker));
FXService standInProxy = create(FXService.class, new StandInProcessingProxy(timeRecorderProxy, cache));
FXService fx = standInProxy;
Full code using all the proxy
List<String> currency = new ArrayList<String>() {{
add("USD");
add("INR");
add("GBP");
add("IDR");
add("JPY");
add("CAD");
}};
IntStream.range(0, 100).forEach($ -> {
Collections.shuffle(currency);
currency.parallelStream().forEach(code -> {
try {
Double d = fx.convert("SGD", code, 1);
System.out.println(d);
} catch (Exception e) {
System.out.println("Failed for " + code);
}
});
});
tracker.dumpSlowRequests(10);
cache.prettyPrint();
Conculsion
A dynamic proxy is a powerful tool that is part of Java's ecosystem. It can be a very useful tool for writers of libraries or frameworks, since a proxy's primary purpose is to extend the functionality of an underlying service/api. Therefore, special precautions must be taken to ensure that it does not negatively impact the underlying service.
"jaifal price
ReplyDeletebuy badam online
goond
best quality badam
best quality walnuts
chuara benefits
khumani fruit
best badam quality
gooseberry online shopping
sugar badam tree
kishmish online
gurbandi badam price per kg
sama ke chawal price
kashmiri mirch sabut
best quality anjeer
best quality almonds
khaand
best quality kaju in india
kali kishmish price
kala chana price per kg
almond lowest price
moong sabut price
saunf rate per kg
fresh amla online
buy supari online
sauf price
bura price
kandhari kishmish
kale chane protein
kali masoor ki daal
gurbandi badam price
khumani dry fruit
white mirch
black kishmish
star fool
singhara atta price of 1kg
alachandalu
"
"jaifal price
ReplyDeletebuy badam online
goond
best quality badam
best quality walnuts
chuara benefits
khumani fruit
best badam quality
gooseberry online shopping
sugar badam tree
kishmish online
gurbandi badam price per kg
sama ke chawal price
kashmiri mirch sabut
best quality anjeer
best quality almonds
khaand
best quality kaju in india
kali kishmish price
kala chana price per kg
almond lowest price
moong sabut price
saunf rate per kg
fresh amla online
buy supari online
sauf price
bura pricekandhari kishmish
kale chane protein
kali masoor ki daal
gurbandi badam price
khumani dry fruit
white mirch
black kishmishstar fool
singhara atta price of 1kg
alachandalu
"
"We at Farmonics provide premium quality products to our customers as we are on n mission to provide good quality products at the doorsteps of every household we deal with in Kirana, seeds nuts,dry fruits, pulses, spices, etc.
ReplyDeleteKirana include badi elaichi, elaichi,dal chinni, sauf, oregano , chilli flakes,etc
Seeds include pumpkin seeds, muskmelon seeds, tarbuja seeds , mix seeds, etc.
Dry fruits include almonds, Kaju, Pista, akroat, anjeer, kishmish, etc.
Pulses include Rajma, ahar, moong, urad, choole, etc.
Spices include mirch, haldi, jeera, daniya, amchoor, etc.
You can explore our products by visiting our website farmonics
"
"We at Farmonics provide premium quality products to our customers as we are on n mission to provide good quality products at the doorsteps of every household we deal with in Kirana, seeds nuts,dry fruits, pulses, spices, etc.
ReplyDeleteKirana include badi elaichi, elaichi,dal chinni, sauf, oregano , chilli flakes,etc
Seeds include pumpkin seeds, muskmelon seeds, tarbuja seeds , mix seeds, etc.
Dry fruits include almonds, Kaju, Pista, akroat, anjeer, kishmish, etc.
Pulses include Rajma, ahar, moong, urad, choole, etc.
Spices include mirch, haldi, jeera, daniya, amchoor, etc.
You can explore our products by visiting our website farmonics
"