Commit a5d18faa by Eli Ben Baruch

MicroserviceClient: add new read api to support specified cache expiration time…

MicroserviceClient: add new read api to support specified cache expiration time (other than default).
parent 0be8d431
......@@ -21,6 +21,7 @@ import microservice.params.CommandParams;
public class MicroserviceClient
{
private static final int INITIAL_CAPACITY = 64;
private static final int NO_ENTRY = 0 ;
public static enum EnumRestClientType
{
......@@ -94,16 +95,48 @@ public class MicroserviceClient
return resp;
}
/**
* the read/get of CRUD
* this read method is used for cases where cache is not required
* @param cmdParams
* @return
*/
public BaseRestResponse read(CommandParams cmdParams)
{
return this.read(cmdParams, 0);
}
/**
*
* @param cmdParams
* @param cacheCommand - if true keep the result in cache for default expiration time(set in init),
* else don't use cache
* @return
*/
public BaseRestResponse read(CommandParams cmdParams,boolean cacheCommand)
{
if (cacheCommand)
return this.read(cmdParams, params.getCacheTimeout());
else
return this.read(cmdParams, 0);
}
/**
* use this read method when you want to use cache with expiration time different than the default value
* example: microserviceClientObj.read(cmdParams,60);
* @param cmdParams
* @param secondsForExpiration - given expiration time (seconds) for keeping the result in cache.
* if not valid value take the default value (set at init time).
*
* @return
*/
public BaseRestResponse read(CommandParams cmdParams, int secondsForExpiration){
BaseRestResponse resp = null;
try
{
if (cacheCommand && cacheClient != null)
if (this.isValidExpiraion(secondsForExpiration) && cacheClient != null)
{
/*
* get from cache if exists
......@@ -118,7 +151,7 @@ public class MicroserviceClient
// saving only success responses
respString = resp.objectNode.toString();
if (respString != null)
cacheClient.set(cacheKey, respString, params.getCacheTimeout());
cacheClient.set(cacheKey, respString, secondsForExpiration);
}
} else
{
......@@ -140,6 +173,14 @@ public class MicroserviceClient
resp = new BaseRestResponse(false, null);
}
return resp;
}
private boolean isValidExpiraion(int secExpiration) {
if (secExpiration>0)
return true;
else
return false;
}
private String buildCacheKey(CommandParams cmdParams)
......@@ -158,11 +199,6 @@ public class MicroserviceClient
return apiKey.toString();
}
public BaseRestResponse read(CommandParams cmdParams)
{
return read(cmdParams, false);
}
/**
* the update/put of CRUD
* @param cmdParams
......
......@@ -19,8 +19,8 @@ public class TestCommandClient {
public class ASM
{
String ae;
String provider;
String ae = "";
String provider = "";
public String getAe() {
return ae;
}
......@@ -57,7 +57,7 @@ public class TestCommandClient {
@Override
protected BaseRestResponse run() throws Exception {
BaseRestResponse brr = new BaseRestResponse(true, null);
asm.setAe(reqCtx.getParamsString());
// asm.setAe(reqCtx.getParamsString());
System.out.println("set ae to: " + asm.getAe());
return brr;
}
......
......@@ -17,7 +17,7 @@ import microservice.types.BaseRestResponse;
public class TestMicroClient
{
public static int MAX_ITERATION = 10000;
public static int MAX_ITERATION = 10;
@Test
public void testMicroClient()
......@@ -61,4 +61,139 @@ public class TestMicroClient
}
@Test
public void testMicroClient_mde()
{
MicroserviceClient client = null;
BaseRestResponse brr = null;
///mde/api/v1/chicago/transportation/routes?key=gT2nciTKwRv6Jy5njqm8fe7LW
RestClientParams clientParams = new RestClientParams("mde",true,0, "172.16.1.56:50040",null,100);
// final IServiceDiscoveryConsulImpl serDisco = new IServiceDiscoveryConsulImpl("localhost", 8500);
try
{
// ICommandClient cmdClient = new IRestClientRestImpl(clientParams).withServiceDiscovery(serDisco);
client = new MicroserviceClient(EnumRestClientType.E_HTTP,clientParams);
CommandParams cmdParams = new CommandParams("/api/v1/chicago/transportation", "routes", "key=gT2nciTKwRv6Jy5njqm8fe7LW", null, null);
System.out.println("Start Testing");
for (int i = 0 ; i < MAX_ITERATION; i++)
{
//1
brr = client.read(cmdParams);
if (brr.success)
System.out.println("1. i="+i+ brr.objectNode.toString());
else
System.out.println("1. i="+i+brr.error);
Thread.sleep(500);
JsonNode metrics = client.getMetrics();
if (i % 10 == 0)
System.out.println("metrics: " + metrics.toString());
//2
brr = client.read(cmdParams,true);
if (brr.success)
System.out.println("2. i="+i+brr.objectNode.toString());
else
System.out.println("2. i="+i+brr.error);
Thread.sleep(500);
metrics = client.getMetrics();
if (i % 10 == 0)
System.out.println("2. i="+i+"metrics: " + metrics.toString());
//3
brr = client.read(cmdParams, 10);
if (brr.success)
System.out.println("3. i="+i+brr.objectNode.toString());
else
System.out.println("3. i="+i+brr.error);
Thread.sleep(500);
metrics = client.getMetrics();
if (i % 10 == 0)
System.out.println("3. i="+i+"metrics: " + metrics.toString());
}
System.out.println("End\nMetrics: " + client.getMetrics().toString());
} catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Test
public void testMicroClient_ds()
{
MicroserviceClient client = null;
BaseRestResponse brr = null;
//http://172.16.1.97:8012/ds/deviceInstance/getDevicesOfSubscriber/97297705810
///mde/api/v1/chicago/transportation/routes?key=gT2nciTKwRv6Jy5njqm8fe7LW
RestClientParams clientParams = new RestClientParams("ds",true,0, "172.16.1.97:8012",null,100);
// final IServiceDiscoveryConsulImpl serDisco = new IServiceDiscoveryConsulImpl("localhost", 8500);
try
{
// ICommandClient cmdClient = new IRestClientRestImpl(clientParams).withServiceDiscovery(serDisco);
client = new MicroserviceClient(EnumRestClientType.E_HTTP,clientParams);
CommandParams cmdParams = new CommandParams("/deviceInstance/getDevicesOfSubscriber", "97297705810", null, null, null);
System.out.println("Start Testing");
for (int i = 0 ; i < MAX_ITERATION; i++)
{
//1
brr = client.read(cmdParams);
if (brr.success)
System.out.println("1. i="+i+ brr.objectNode.toString());
else
System.out.println("1. i="+i+brr.error);
Thread.sleep(500);
JsonNode metrics = client.getMetrics();
if (i % 10 == 0)
System.out.println("metrics: " + metrics.toString());
//2
brr = client.read(cmdParams,true);
if (brr.success)
System.out.println("2. i="+i+brr.objectNode.toString());
else
System.out.println("2. i="+i+brr.error);
Thread.sleep(500);
metrics = client.getMetrics();
if (i % 10 == 0)
System.out.println("2. i="+i+"metrics: " + metrics.toString());
//3
brr = client.read(cmdParams, 6);
if (brr.success)
System.out.println("3. i="+i+brr.objectNode.toString());
else
System.out.println("3. i="+i+brr.error);
Thread.sleep(500);
metrics = client.getMetrics();
if (i % 10 == 0)
System.out.println("3. i="+i+"metrics: " + metrics.toString());
}
System.out.println("End\nMetrics: " + client.getMetrics().toString());
} catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment