Commit a3667c41 by amir

after testing client side read async

parent 31ad7e90
package microservice;
import microservice.defs.Constants;
import microservice.types.BaseRestResponse;
import common.CacheClient;
import common.JsonHandler;
......@@ -20,7 +21,6 @@ import microservice.params.CommandParams;
*/
public class MicroserviceClient
{
private static final int INITIAL_CAPACITY = 64;
public static enum EnumRestClientType
{
......@@ -184,7 +184,7 @@ public class MicroserviceClient
private String buildCacheKey(CommandParams cmdParams)
{
StringBuilder apiKey = new StringBuilder(INITIAL_CAPACITY);
StringBuilder apiKey = new StringBuilder(Constants.STRING_INITIAL_CAPACITY);
apiKey.append(cmdParams.getEntity());
if (cmdParams.getParams() != null)
{
......
......@@ -19,4 +19,5 @@ public class Constants
public static final String TYPE_PREFIX_SEPERATOR = ":";
public static final String EXIT_MSG = "exit";
public static final int EXIT_MSG_LEN = EXIT_MSG.length();
public static final int STRING_INITIAL_CAPACITY = 64;
}
......@@ -10,6 +10,7 @@ public class CommandParams
String requestParams;
String content;
Map<String,String> headersMap = null;
int cmndId = 0;
public CommandParams() {
}
......@@ -96,4 +97,13 @@ public class CommandParams
this.headersMap = headersMap;
return this;
}
public CommandParams setCmndId(int cmndId) {
this.cmndId = cmndId;
return this;
}
public int getCmndId() {
return cmndId;
}
}
......@@ -53,6 +53,7 @@ public class RestImpl {
@Override
public boolean startAsync(Runnable asyncFunc) {
asyncFunc.run();
return true;
}
}
......
package microservice.types;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.JsonNode;
public class BaseRestResponse
{
public static final String SUCCESS = "success";
public static final String ERROR = "error";
public static final String OBJECT_NODE = "objectNode";
// public static final String SUCCESS = "success";
// public static final String ERROR = "error";
// public static final String OBJECT_NODE = "objectNode";
@JsonIgnore
public BaseRestResponse(boolean success, String error)
{
super();
this.success = success;
this.error = error;
}
@JsonCreator
public BaseRestResponse(@JsonProperty("success") boolean success,
@JsonProperty("error") String error,
@JsonProperty("objectNode") JsonNode objectNode) {
this.success = success;
this.error = error;
this.objectNode = objectNode;
}
public boolean success = true;
public String error = null;
public JsonNode objectNode = null;
// @JsonIgnore
public void setError(String error)
{
this.success = false;
this.error = error;
}
public void setSuccess(boolean success) {
this.success = success;
}
public void setObjectNode(JsonNode objectNode) {
this.objectNode = objectNode;
}
}
package microservice.utils;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import microservice.services.CommonServices;
import java.util.concurrent.TimeUnit;
/**
* Created by amir on 25/05/17.
*/
public class ICacheClientGuavaImpl<K,V> implements CommonServices.ICacheClient<K,V> {
private Cache<K, V> cache = null;
public ICacheClientGuavaImpl(int expiresMilliSeconds) {
cache = CacheBuilder.newBuilder()
//.maximumSize(10000)
.expireAfterWrite(expiresMilliSeconds, TimeUnit.MILLISECONDS)
//.removalListener(MY_LISTENER)
.build();
}
@Override
public void set(K key, V val) {
cache.put(key,val);
}
@Override
public void set(K key, V val, int expiration) {
set(key,val);
}
@Override
public void setExpiration(K key, int expiration) {
}
@Override
public V get(K key) {
return cache.getIfPresent(key);
}
@Override
public void delete(K key) {
cache.invalidate(key);
}
@Override
public void deleteByPattern(K pattern) {
}
@Override
public V[] getByPattern(K pattern) {
return null;
}
}
......@@ -63,14 +63,19 @@ public class RestHttpClient extends SimpleRestClient {
if(params == null) {
return this._get(entity, (String)null, requestParams);
} else {
for(int i = 0; i < params.length; ++i) {
paramPath = paramPath + "/" + params[i];
}
paramPath = getUrlPath(params, paramPath);
return this._get(entity, paramPath, requestParams);
}
}
private String getUrlPath(String[] params, String paramPath) {
for(int i = 0; i < params.length; ++i) {
paramPath = paramPath + "/" + params[i];
}
return paramPath;
}
public BaseRestResponse _get(String entity, String params, String requestParams) {
StringResponse srr;
if (domain == null) {
......@@ -90,9 +95,7 @@ public class RestHttpClient extends SimpleRestClient {
if(params == null) {
return this._post(entity, (String[])null, requestParams, content);
} else {
for(int i = 0; i < params.length; ++i) {
paramPath = paramPath + "/" + params[i];
}
paramPath = getUrlPath(params, paramPath);
return this._post(entity, paramPath, requestParams, content);
}
}
......@@ -114,9 +117,7 @@ public class RestHttpClient extends SimpleRestClient {
if(params == null) {
return this._put(entity, (String[])null, requestParams, content);
} else {
for(int i = 0; i < params.length; ++i) {
paramPath = paramPath + "/" + params[i];
}
paramPath = getUrlPath(params, paramPath);
return this._put(entity, paramPath, requestParams, content);
}
}
......@@ -151,9 +152,7 @@ public class RestHttpClient extends SimpleRestClient {
if(params == null) {
return this._delete(entity, (String)null, requestParams);
} else {
for(int i = 0; i < params.length; ++i) {
paramPath = paramPath + "/" + params[i];
}
paramPath = getUrlPath(params, paramPath);
return this._delete(entity, paramPath, requestParams);
}
......
......@@ -133,11 +133,12 @@ public class ServiceBuilderFactory {
this.serverParams = serverParams;
return this;
}
public RestServiceZmqBuilder setClientReceiveParams(ZMQParams.ServerParams clientReceiveParams) {
public RestServiceZmqBuilder setClientParams(ZMQParams.ServerParams clientReceiveParams, ZMQParams.ServerParams clientSendParams) {
this.clientReceiveParams = clientReceiveParams;
this.clientSendParams = clientSendParams;
return this;
}
public void setClientSendParams(ZMQParams.ServerParams clientSendParams) { this.clientSendParams = clientSendParams; }
public RestServiceZmqBuilder setNumOfClientWorkers(int num) {
numOfClientWorkers = num;
......
......@@ -12,6 +12,7 @@ import microservice.io.impl.*;
import microservice.services.IRestServiceHttpImpl;
import microservice.params.*;
import microservice.services.IRestServiceZmqImpl;
import microservice.types.BaseRestResponse;
import microservice.utils.ServiceBuilderFactory;
import org.eclipse.paho.client.mqttv3.MqttException;
......@@ -85,8 +86,10 @@ public class TestMicroserviceApp {
.hasRestServerParams(new RestServerParams(32000, "localhost", 2))
.hasRestClientParams(new RestClientParams(null,false,0,null,null))
.build();
CommonServices.IService zmqRestService = ServiceBuilderFactory.createRestServiceZmqBuilder(CommonServices.EnumRestServiceMode.E_SERVER)
CommonServices.IService zmqRestService = ServiceBuilderFactory.createRestServiceZmqBuilder(CommonServices.EnumRestServiceMode.E_CLIENT_SERVER)
.setServerParams(new ZMQParams.ServerParams(ZMQParams.ServerParams.EnumProtocol.eTcp,32010,"localhost"))
.setClientParams(new ZMQParams.ServerParams(ZMQParams.ServerParams.EnumProtocol.eTcp,32011,"localhost"),
new ZMQParams.ServerParams(ZMQParams.ServerParams.EnumProtocol.eTcp,32010,"localhost")) // sending to myself
.build();
microservice.MicroserviceApp msApp = new microservice.MicroserviceApp(appName);
msApp.withMetrics()
......@@ -132,6 +135,24 @@ public class TestMicroserviceApp {
(msgCtx,orgService) -> {
queryRegistry((RestContext)msgCtx);
}));
methodParamsList.add(new CommonServices.MethodParams(Enums.EnumServiceType.E_REST,
CommonServices.EnumRestCommands.E_READ,
"/test/zmq/{query}",
(msgCtx,orgService) -> {
testZmqRead((RestContext)msgCtx);
}));
}
private void testZmqRead(RestContext msgCtx) {
RestContext restContext = (RestContext)msgCtx;
CommonServices.IRestService inRestService = (CommonServices.IRestService)MicroserviceApp.getsInstance().getService(Enums.EnumServiceType.E_REST,"undertowRestService");
CommonServices.IRestService outRestService = (CommonServices.IRestService)MicroserviceApp.getsInstance().getService(Enums.EnumServiceType.E_REST,"zmqRestService");
String query = restContext.getPathParameter("query");
CommandParams cmdParams = new CommandParams().setEntity("resource").setParamsString("rid").setRequestParams("q=" + query);
((IRestServiceZmqImpl)outRestService).startAsync(restContext.request,() -> {
boolean retstat = outRestService.asyncRead(cmdParams, brr -> inRestService.writeObjectToResponse(restContext.response,brr));
});
}
private void resourceRidCreate(RestContext msgCtx) {
......
package microservice;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectReader;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import common.JsonHandler;
import io.undertow.predicate.Predicate;
import io.undertow.util.PathTemplateMatcher;
import microservice.services.CommonServices;
import microservice.services.IRestServiceZmqImpl;
import microservice.types.BaseRestResponse;
import org.apache.commons.lang.SerializationUtils;
import org.junit.Assert;
import org.junit.Test;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
......@@ -184,4 +196,58 @@ public class TestServicesAndMethods {
}
public static class _BaseRestResponse
{
public boolean success = true;
public String error = null;
public JsonNode objectNode = null;
@JsonCreator
public _BaseRestResponse(@JsonProperty("success") boolean success,
@JsonProperty("error") String error,
@JsonProperty("objectNode") JsonNode objectNode) {
this.success = success;
this.error = error;
this.objectNode = objectNode;
}
@JsonIgnore
public _BaseRestResponse(boolean success, String error)
{
super();
this.success = success;
this.error = error;
}
public void setError(String error)
{
this.success = false;
this.error = error;
}
public void setSuccess(boolean success) {
this.success = success;
}
public void setObjectNode(JsonNode objectNode) {
this.objectNode = objectNode;
}
}
@Test
public void testJson() throws JsonProcessingException {
ObjectMapper objMapper = new ObjectMapper();
_BaseRestResponse brr = new _BaseRestResponse(true,null,null);
brr.objectNode = JsonNodeFactory.instance.objectNode().put("rid", "rid");
String content = objMapper.writeValueAsString(brr);
final JsonNode jsonNode = JsonHandler.getJsonNodeFromString(content);
if (jsonNode != null) {
ObjectReader objectReader = objMapper.reader(_BaseRestResponse.class);
try {
brr = (_BaseRestResponse)objectReader.readValue(jsonNode);
} catch (IOException e) {
e.printStackTrace();
}
Assert.assertNotNull(brr);
}
}
}
package microservice;
import com.fasterxml.jackson.databind.JsonNode;
import com.google.flatbuffers.FlatBufferBuilder;
import itc.ItcMessage;
import itc.ItcMessageQueue;
......
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