Commit 77b83f2d by amir

end of day commits

parent f79ae468
# New Services Design
## Example of use:
System.setProperty("configFile.location","/opt/mcx/config/config.properties");
String appName = "testApp";
microservice.MicroserviceApp msApp = new microservice.MicroserviceApp(appName);
msApp.withMetrics()
.withMonitoring()
//.withDefaultServiceAuthorization()
.addService(Enums.EnumServiceType.E_REST,new IRestServiceUndertowImpl(new RestServerParams(32000, "localhost", 2),"undertowRestService")
.addMethod(Enums.EnumServiceType.E_REST,CommonServices.IRestService.E_READ,"/resource/r1",(msgCtx) -> {
BaseRestResponse brr;
brr.setObject({ "a":"b" })
msgCtx.writeResponse(brr);
})
.addMethod(Enums.EnumServiceType.E_REST,CommonServices.IRestService.E_READ,"/resource/r1",(msgCtx) -> {
BaseRestResponse brr;
msgCtx.writeResponse(brr);
})
.build()
.run();
### Flow for Service:
1 Receive Msg
2 Parse
3 Prepare MsgContext
4 Build ServiceKey
5 call reactor.delegate(serviceKey,msgContext)
6 Mitfanen :sleeping:
### Flow for Reactor:
1 find callback method from serviceKey
2 activate method
3 add for metrics,stats etc'
4 Mitfanen :sleeping:
# new services design
- counters and metrics in the handler/reactor
- validateRequest in RestService , the jwt issues
- All the validation ,pre/post handling that was were done by the handler will be done by the base service
- add Runtime Test:
addTest(const char *testName, nsMicroservice_Iface::TestFunction testFunction);
addTest(nsMicroservice_Iface::ITest *p_testClass)
......@@ -8,10 +8,7 @@ import io.undertow.server.handlers.PathHandler;
import io.undertow.server.handlers.resource.ClassPathResourceManager;
import io.undertow.util.MimeMappings;
import microservice.defs.Enums;
import microservice.handlers.BaseHandler;
import microservice.handlers.MBIHandler;
import microservice.handlers.MonitorHandler;
import microservice.handlers.RestHandler;
import microservice.handlers.*;
import microservice.io.iface.*;
import microservice.io.impl.*;
import microservice.params.RMQClientParams;
......@@ -25,6 +22,7 @@ import rabbitmq.server.RMQServer;
import java.util.*;
import java.util.Map.Entry;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import static io.undertow.Handlers.resource;
......@@ -67,11 +65,14 @@ public class MicroserviceApp
Map<String, microservice.MicroserviceClient> msClientMap = null;
List<IRestServer> serverList = null;
Optional<MonitorHandler> optMonitorHandler = Optional.empty();
/////////////// new design services ///////////////////
Reactor reactor;
public MicroserviceApp(String appName)
{
this.appName = appName;
id = String.valueOf(System.currentTimeMillis() & 0xffff);
reactor = new Reactor();
sInstance = this;
}
......@@ -513,39 +514,35 @@ public class MicroserviceApp
CommonServices.IService iService,
String serviceKey)
{
Map<String, CommonServices.IService> serviceMap = servicesArray.get(serviceType.ordinal());
if (serviceMap == null){
serviceMap = new HashMap<String, CommonServices.IService>();
servicesArray.set(serviceType.ordinal(),serviceMap);
}
serviceMap.put(serviceKey,iService);
reactor.addService(serviceType,iService,serviceKey);
return this;
}
CommonServices.IService getService(Enums.EnumServiceType serviceType, String serviceKey){
Map<String, CommonServices.IService> serviceMap = servicesArray.get(serviceType.ordinal());
if (serviceMap != null)
return serviceMap.get(serviceKey);
return null;
return reactor.getService(serviceType,serviceKey);
}
CommonServices.IService getService(String serviceKey){
Optional<CommonServices.IService> iServiceOptional = servicesArray.stream()
.filter(serviceMap -> serviceMap != null && serviceMap.containsKey(serviceKey))
.map(serviceMap -> serviceMap.get(serviceKey))
.findFirst();
return iServiceOptional.get();
return reactor.getService(serviceKey);
}
public MicroserviceApp addMethod(Enums.EnumServiceType serviceType,
CommonServices.IServiceCommands serviceCommand,
String resourceUri,
Consumer<CommonServices.IMsgContext> cosumerMethod)
BiConsumer<CommonServices.IMsgContext,CommonServices.IService> consumerMethod)
{
Map<String, CommonServices.IService> serviceMap = servicesArray.get(serviceType.ordinal());
serviceMap.values().forEach(iService -> {
iService.addMethod(serviceCommand,resourceUri,cosumerMethod);
});
reactor.addMethod(new CommonServices.MethodParams(serviceType,serviceCommand,resourceUri,consumerMethod));
return this;
}
public MicroserviceApp addMethodClass(CommonServices.IMethodClass methodClass)
{
List<CommonServices.MethodParams> methodParamsList = new ArrayList<>();
methodClass.getMethods(methodParamsList);
for (CommonServices.MethodParams methodParams: methodParamsList) {
reactor.addMethod(methodParams);
}
return this;
}
}
......@@ -16,4 +16,5 @@ public class Constants
public static final String INVALID_REQUEST_TOKEN = "invalid request/token";
public static final String AUTHORIZATION_HEADER = "Authorization";
public static final String TYPE_PREFIX_SEPERATOR = ":";
}
package microservice.handlers;
import microservice.defs.Constants;
import microservice.defs.Enums;
import microservice.io.iface.CommonServices;
import microservice.io.iface.IMetricsFactory;
import java.util.*;
import java.util.function.BiConsumer;
/**
* Created by amir on 04/05/17.
*/
public class Reactor {
private Map<String,BiConsumer<CommonServices.IMsgContext,CommonServices.IService>> methodsMap = new HashMap<>();
private IMetricsFactory metricsFactory = null;
List<Map<String,CommonServices.IService>> servicesArray = new ArrayList<>(Enums.EnumServiceType.values().length);
public Reactor() {
}
public Reactor addService(Enums.EnumServiceType serviceType,
CommonServices.IService iService,
String serviceKey)
{
Map<String, CommonServices.IService> serviceMap = servicesArray.get(serviceType.ordinal());
if (serviceMap == null){
serviceMap = new HashMap<>();
servicesArray.set(serviceType.ordinal(),serviceMap);
}
serviceMap.put(serviceKey,iService);
return this;
}
public CommonServices.IService getService(Enums.EnumServiceType serviceType, String serviceKey){
Map<String, CommonServices.IService> serviceMap = servicesArray.get(serviceType.ordinal());
if (serviceMap != null)
return serviceMap.get(serviceKey);
return null;
}
public CommonServices.IService getService(String serviceKey){
Optional<CommonServices.IService> iServiceOptional = servicesArray.stream()
.filter(serviceMap -> serviceMap != null && serviceMap.containsKey(serviceKey))
.map(serviceMap -> serviceMap.get(serviceKey))
.findFirst();
return iServiceOptional.get();
}
public void addMethod(CommonServices.MethodParams methodParams){
/**
* build the key
*/
String key = buildServiceKey(methodParams);
methodsMap.put(key,methodParams.getConsumerMethod());
}
public String buildServiceKey(CommonServices.MethodParams methodParams) {
return methodParams.getServiceType().name() + Constants.TYPE_PREFIX_SEPERATOR +
methodParams.getServiceCommand().toString() + Constants.TYPE_PREFIX_SEPERATOR +
methodParams.getResourceUri();
}
/**
* delegating the msg/request from the service to the method if any
* @param orgService
* @param key
* @param msgContext
* @return
*/
boolean delegate(CommonServices.IService orgService,String key,CommonServices.IMsgContext msgContext){
final BiConsumer<CommonServices.IMsgContext, CommonServices.IService> methodCallback = methodsMap.get(key);
if (methodCallback != null) {
methodCallback.accept(msgContext, orgService);
} else {
orgService.handleNotImplmented(msgContext);
}
return true;
}
public final Map<String, BiConsumer<CommonServices.IMsgContext, CommonServices.IService>> getMethodsMap() {
return methodsMap;
}
public Reactor withMetricsFactory(IMetricsFactory metricsFactory) {
this.metricsFactory = metricsFactory;
return this;
}
}
package microservice.io.iface;
import microservice.defs.Enums;
import microservice.params.CommandParams;
import microservice.types.BaseRestResponse;
import java.util.List;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
/**
......@@ -20,7 +26,51 @@ public class CommonServices {
void run();
void shutdown();
void addMethod(IServiceCommands serviceCommand, String resourceUri, Consumer<IMsgContext> cosumerMethod);
void addMethod(MethodParams methodParams);
void handleNotImplmented(IMsgContext msgContext);
}
// @FunctionalInterface
// public interface IMethodCallback<M extends IMsgContext, S extends IService> {
// void apply(M m, S s);
// }
public static class MethodParams {
Enums.EnumServiceType serviceType;
IServiceCommands serviceCommand;
String resourceUri;
BiConsumer<IMsgContext,IService> consumerMethod;
public MethodParams(Enums.EnumServiceType serviceType,IServiceCommands serviceCommand, String resourceUri, BiConsumer<IMsgContext,IService> consumerMethod) {
this.serviceType = serviceType;
this.serviceCommand = serviceCommand;
this.resourceUri = resourceUri;
this.consumerMethod = consumerMethod;
}
public Enums.EnumServiceType getServiceType() { return serviceType;
}
public IServiceCommands getServiceCommand() {
return serviceCommand;
}
public String getResourceUri() {
return resourceUri;
}
public BiConsumer<IMsgContext,IService> getConsumerMethod() {
return consumerMethod;
}
}
/**
* interface for handler class that contains service methods
*/
public interface IMethodClass {
void getMethods(List<MethodParams> methodParamsList);
}
public interface IRestService extends IService {
......@@ -30,6 +80,11 @@ public class CommonServices {
E_UPDATE,
E_DELETE;
}
public BaseRestResponse create(CommandParams cmdParams);
public BaseRestResponse read(CommandParams cmdParams);
public BaseRestResponse update(CommandParams cmdParams);
public BaseRestResponse delete(CommandParams cmdParams);
}
public interface IPubSubService extends IService {
......
......@@ -113,9 +113,10 @@ public class IPubSubServiceMqttImpl implements CommonServices.IPubSubService {
}
@Override
public void addMethod(CommonServices.IServiceCommands serviceCommand, String resourceUri, Consumer<CommonServices.IMsgContext> consumerMethod) {
if (EnumPubSubCommands.E_SUBSCRIBE.equals(serviceCommand) &&
!resourceUri.isEmpty() && consumerMethod != null){
public void addMethod(CommonServices.MethodParams methodParams) {
String resourceUri = methodParams.getResourceUri();
if (EnumPubSubCommands.E_SUBSCRIBE.equals(methodParams.getServiceCommand()) &&
! resourceUri.isEmpty() && methodParams.getConsumerMethod() != null){
try {
/**
* search for last / or wildcard
......@@ -125,7 +126,7 @@ public class IPubSubServiceMqttImpl implements CommonServices.IPubSubService {
resourceUri = resourceUri.substring(0,indexOfSlash);
}
asyncClient.subscribe(resourceUri,qos);
topicCallbackMap.put(resourceUri,consumerMethod);
topicCallbackMap.put(resourceUri,methodParams.getConsumerMethod());
} catch (MqttException e) {
e.printStackTrace();
}
......
package microservice;
import microservice.defs.Enums;
import microservice.io.iface.CommonServices;
import microservice.io.iface.ICommandClient;
import microservice.io.impl.*;
import microservice.params.BaseClientParams;
import microservice.params.RMQClientParams;
import microservice.params.RestClientParams;
import microservice.params.RestServerParams;
import microservice.params.*;
import microservice.types.BaseRestResponse;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.junit.Test;
......@@ -67,21 +67,32 @@ public class TestMicroserviceApp {
}
public static final String ANSI_RESET = "\u001B[0m";
public static final String ANSI_BLACK = "\u001B[30m";
public static final String ANSI_RED = "\u001B[31m";
public static final String ANSI_GREEN = "\u001B[32m";
public static final String ANSI_YELLOW = "\u001B[33m";
public static final String ANSI_BLUE = "\u001B[34m";
public static final String ANSI_PURPLE = "\u001B[35m";
public static final String ANSI_CYAN = "\u001B[36m";
public static final String ANSI_WHITE = "\u001B[37m";
@Test
public void testLogger()
public void testMicroServices()
{
System.out.println(ANSI_RED + "hello" + ANSI_RESET);
System.out.println(ANSI_BLUE + "hello" + ANSI_RESET);
System.setProperty("configFile.location","/opt/mcx/config/config.properties");
String appName = "testApp";
microservice.MicroserviceApp msApp = new microservice.MicroserviceApp(appName);
msApp.withMetrics()
.withMonitoring()
//.withDefaultServiceAuthorization()
//.addService(Enums.EnumServiceType.E_REST,new IRestServiceUndertowImpl(new RestServerParams(32000, "localhost", 2),"undertowRestService")
.addMethod(Enums.EnumServiceType.E_REST, CommonServices.IRestService.EnumRestCommands.E_READ,"/resource/r1",(msgCtx,orgService) -> {
BaseRestResponse brr;
msApp.getLogger();
CommonServices.IRestService restService = (CommonServices.IRestService)orgService;
//brr.setObject({ "a":"b" })
restService.writeResponse(brr);
})
.addMethod(Enums.EnumServiceType.E_REST,CommonServices.IRestService.EnumRestCommands.E_READ,"/resource/r1",(msgCtx,orgService) -> {
CommonServices.IRestService outRestService = (CommonServices.IRestService)MicroserviceApp.getsInstance().getService(Enums.EnumServiceType.E_REST,"undertowRestService");
CommonServices.IRestService restService = (CommonServices.IRestService)orgService;
CommandParams cmdParams = new CommandParams();
BaseRestResponse brr = restService.read(cmdParams);
restService.writeResponse(brr);
})
.build()
.run();
}
}
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