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,8 +21,9 @@ import microservice.params.CommandParams; ...@@ -21,8 +21,9 @@ import microservice.params.CommandParams;
public class MicroserviceClient public class MicroserviceClient
{ {
private static final int INITIAL_CAPACITY = 64; private static final int INITIAL_CAPACITY = 64;
private static final int NO_ENTRY = 0 ;
public static enum EnumRestClientType public static enum EnumRestClientType
{ {
E_HTTP, E_HTTP,
E_RABBITMQ E_RABBITMQ
...@@ -94,55 +95,95 @@ public class MicroserviceClient ...@@ -94,55 +95,95 @@ public class MicroserviceClient
return resp; return resp;
} }
/**
* the read/get of CRUD
* @param cmdParams /**
*/ * 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) public BaseRestResponse read(CommandParams cmdParams,boolean cacheCommand)
{ {
BaseRestResponse resp = null; if (cacheCommand)
try return this.read(cmdParams, params.getCacheTimeout());
{ else
if (cacheCommand && cacheClient != null) 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 (this.isValidExpiraion(secondsForExpiration) && cacheClient != null)
{
/* /*
* get from cache if exists * get from cache if exists
*/ */
String cacheKey = buildCacheKey(cmdParams); String cacheKey = buildCacheKey(cmdParams);
String respString = cacheClient.get(cacheKey); String respString = cacheClient.get(cacheKey);
if (respString == null) if (respString == null)
{ {
resp = commandClient.read(cmdParams); resp = commandClient.read(cmdParams);
if (resp.success && resp.objectNode != null) if (resp.success && resp.objectNode != null)
{ {
// saving only success responses // saving only success responses
respString = resp.objectNode.toString(); respString = resp.objectNode.toString();
if (respString != null) if (respString != null)
cacheClient.set(cacheKey, respString, params.getCacheTimeout()); cacheClient.set(cacheKey, respString, secondsForExpiration);
} }
} else } else
{ {
resp = new BaseRestResponse(true,null); resp = new BaseRestResponse(true,null);
resp.objectNode = JsonHandler.getJsonNodeFromString(respString); resp.objectNode = JsonHandler.getJsonNodeFromString(respString);
} }
} }
else else
{ {
resp = commandClient.read(cmdParams); resp = commandClient.read(cmdParams);
} }
} catch (Exception e) } catch (Exception e)
{ {
resp = new BaseRestResponse(false,e.toString()); resp = new BaseRestResponse(false,e.toString());
} }
finally finally
{ {
if (resp == null) if (resp == null)
resp = new BaseRestResponse(false, null); resp = new BaseRestResponse(false, null);
} }
return resp; return resp;
}
}
private String buildCacheKey(CommandParams cmdParams)
private boolean isValidExpiraion(int secExpiration) {
if (secExpiration>0)
return true;
else
return false;
}
private String buildCacheKey(CommandParams cmdParams)
{ {
StringBuilder apiKey = new StringBuilder(INITIAL_CAPACITY); StringBuilder apiKey = new StringBuilder(INITIAL_CAPACITY);
apiKey.append(cmdParams.getEntity()); apiKey.append(cmdParams.getEntity());
...@@ -158,11 +199,6 @@ public class MicroserviceClient ...@@ -158,11 +199,6 @@ public class MicroserviceClient
return apiKey.toString(); return apiKey.toString();
} }
public BaseRestResponse read(CommandParams cmdParams)
{
return read(cmdParams, false);
}
/** /**
* the update/put of CRUD * the update/put of CRUD
* @param cmdParams * @param cmdParams
......
...@@ -19,8 +19,8 @@ public class TestCommandClient { ...@@ -19,8 +19,8 @@ public class TestCommandClient {
public class ASM public class ASM
{ {
String ae; String ae = "";
String provider; String provider = "";
public String getAe() { public String getAe() {
return ae; return ae;
} }
...@@ -57,7 +57,7 @@ public class TestCommandClient { ...@@ -57,7 +57,7 @@ public class TestCommandClient {
@Override @Override
protected BaseRestResponse run() throws Exception { protected BaseRestResponse run() throws Exception {
BaseRestResponse brr = new BaseRestResponse(true, null); BaseRestResponse brr = new BaseRestResponse(true, null);
asm.setAe(reqCtx.getParamsString()); // asm.setAe(reqCtx.getParamsString());
System.out.println("set ae to: " + asm.getAe()); System.out.println("set ae to: " + asm.getAe());
return brr; return brr;
} }
......
...@@ -17,7 +17,7 @@ import microservice.types.BaseRestResponse; ...@@ -17,7 +17,7 @@ import microservice.types.BaseRestResponse;
public class TestMicroClient public class TestMicroClient
{ {
public static int MAX_ITERATION = 10000; public static int MAX_ITERATION = 10;
@Test @Test
public void testMicroClient() public void testMicroClient()
...@@ -26,7 +26,7 @@ public class TestMicroClient ...@@ -26,7 +26,7 @@ public class TestMicroClient
BaseRestResponse brr = null; BaseRestResponse brr = null;
RestClientParams clientParams = new RestClientParams("ds",true,0, "172.16.1.244:8080",null,100); RestClientParams clientParams = new RestClientParams("ds",true,0, "172.16.1.244:8080",null,100);
final IServiceDiscoveryConsulImpl serDisco = new IServiceDiscoveryConsulImpl("localhost", 8500); final IServiceDiscoveryConsulImpl serDisco = new IServiceDiscoveryConsulImpl("localhost", 8500);
...@@ -61,4 +61,139 @@ public class 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