Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
ipgallery.common.java
/
microservice
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Registry
Issues
0
Merge Requests
0
Pipelines
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
aae6a59b
authored
May 04, 2017
by
amir
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
end of day commits
parent
cbfc8f06
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
253 additions
and
47 deletions
doc/ServicesDesign.md
doc/todo.txt
src/main/java/microservice/MicroserviceApp.java
src/main/java/microservice/defs/Constants.java
src/main/java/microservice/handlers/Reactor.java
src/main/java/microservice/io/iface/CommonServices.java
src/main/java/microservice/io/impl/IPubSubServiceMqttImpl.java
src/test/java/microservice/TestMicroserviceApp.java
doc/ServicesDesign.md
0 → 100644
View file @
aae6a59b
# 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:
doc/todo.txt
0 → 100644
View file @
aae6a59b
# 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)
src/main/java/microservice/MicroserviceApp.java
View file @
aae6a59b
...
@@ -8,10 +8,7 @@ import io.undertow.server.handlers.PathHandler;
...
@@ -8,10 +8,7 @@ import io.undertow.server.handlers.PathHandler;
import
io.undertow.server.handlers.resource.ClassPathResourceManager
;
import
io.undertow.server.handlers.resource.ClassPathResourceManager
;
import
io.undertow.util.MimeMappings
;
import
io.undertow.util.MimeMappings
;
import
microservice.defs.Enums
;
import
microservice.defs.Enums
;
import
microservice.handlers.BaseHandler
;
import
microservice.handlers.*
;
import
microservice.handlers.MBIHandler
;
import
microservice.handlers.MonitorHandler
;
import
microservice.handlers.RestHandler
;
import
microservice.io.iface.*
;
import
microservice.io.iface.*
;
import
microservice.io.impl.*
;
import
microservice.io.impl.*
;
import
microservice.params.RMQClientParams
;
import
microservice.params.RMQClientParams
;
...
@@ -25,6 +22,7 @@ import rabbitmq.server.RMQServer;
...
@@ -25,6 +22,7 @@ import rabbitmq.server.RMQServer;
import
java.util.*
;
import
java.util.*
;
import
java.util.Map.Entry
;
import
java.util.Map.Entry
;
import
java.util.function.BiConsumer
;
import
java.util.function.Consumer
;
import
java.util.function.Consumer
;
import
static
io
.
undertow
.
Handlers
.
resource
;
import
static
io
.
undertow
.
Handlers
.
resource
;
...
@@ -67,11 +65,14 @@ public class MicroserviceApp
...
@@ -67,11 +65,14 @@ public class MicroserviceApp
Map
<
String
,
microservice
.
MicroserviceClient
>
msClientMap
=
null
;
Map
<
String
,
microservice
.
MicroserviceClient
>
msClientMap
=
null
;
List
<
IRestServer
>
serverList
=
null
;
List
<
IRestServer
>
serverList
=
null
;
Optional
<
MonitorHandler
>
optMonitorHandler
=
Optional
.
empty
();
Optional
<
MonitorHandler
>
optMonitorHandler
=
Optional
.
empty
();
/////////////// new design services ///////////////////
Reactor
reactor
;
public
MicroserviceApp
(
String
appName
)
public
MicroserviceApp
(
String
appName
)
{
{
this
.
appName
=
appName
;
this
.
appName
=
appName
;
id
=
String
.
valueOf
(
System
.
currentTimeMillis
()
&
0xffff
);
id
=
String
.
valueOf
(
System
.
currentTimeMillis
()
&
0xffff
);
reactor
=
new
Reactor
();
sInstance
=
this
;
sInstance
=
this
;
}
}
...
@@ -513,39 +514,35 @@ public class MicroserviceApp
...
@@ -513,39 +514,35 @@ public class MicroserviceApp
CommonServices
.
IService
iService
,
CommonServices
.
IService
iService
,
String
serviceKey
)
String
serviceKey
)
{
{
Map
<
String
,
CommonServices
.
IService
>
serviceMap
=
servicesArray
.
get
(
serviceType
.
ordinal
());
reactor
.
addService
(
serviceType
,
iService
,
serviceKey
);
if
(
serviceMap
==
null
){
serviceMap
=
new
HashMap
<
String
,
CommonServices
.
IService
>();
servicesArray
.
set
(
serviceType
.
ordinal
(),
serviceMap
);
}
serviceMap
.
put
(
serviceKey
,
iService
);
return
this
;
return
this
;
}
}
CommonServices
.
IService
getService
(
Enums
.
EnumServiceType
serviceType
,
String
serviceKey
){
CommonServices
.
IService
getService
(
Enums
.
EnumServiceType
serviceType
,
String
serviceKey
){
Map
<
String
,
CommonServices
.
IService
>
serviceMap
=
servicesArray
.
get
(
serviceType
.
ordinal
());
return
reactor
.
getService
(
serviceType
,
serviceKey
);
if
(
serviceMap
!=
null
)
return
serviceMap
.
get
(
serviceKey
);
return
null
;
}
}
CommonServices
.
IService
getService
(
String
serviceKey
){
CommonServices
.
IService
getService
(
String
serviceKey
){
Optional
<
CommonServices
.
IService
>
iServiceOptional
=
servicesArray
.
stream
()
return
reactor
.
getService
(
serviceKey
);
.
filter
(
serviceMap
->
serviceMap
!=
null
&&
serviceMap
.
containsKey
(
serviceKey
))
.
map
(
serviceMap
->
serviceMap
.
get
(
serviceKey
))
.
findFirst
();
return
iServiceOptional
.
get
();
}
}
public
MicroserviceApp
addMethod
(
Enums
.
EnumServiceType
serviceType
,
public
MicroserviceApp
addMethod
(
Enums
.
EnumServiceType
serviceType
,
CommonServices
.
IServiceCommands
serviceCommand
,
CommonServices
.
IServiceCommands
serviceCommand
,
String
resourceUri
,
String
resourceUri
,
Consumer
<
CommonServices
.
IMsgContext
>
co
sumerMethod
)
BiConsumer
<
CommonServices
.
IMsgContext
,
CommonServices
.
IService
>
con
sumerMethod
)
{
{
Map
<
String
,
CommonServices
.
IService
>
serviceMap
=
servicesArray
.
get
(
serviceType
.
ordinal
());
reactor
.
addMethod
(
new
CommonServices
.
MethodParams
(
serviceType
,
serviceCommand
,
resourceUri
,
consumerMethod
));
serviceMap
.
values
().
forEach
(
iService
->
{
return
this
;
iService
.
addMethod
(
serviceCommand
,
resourceUri
,
cosumerMethod
);
}
});
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
;
return
this
;
}
}
}
}
src/main/java/microservice/defs/Constants.java
View file @
aae6a59b
...
@@ -16,4 +16,5 @@ public class Constants
...
@@ -16,4 +16,5 @@ public class Constants
public
static
final
String
INVALID_REQUEST_TOKEN
=
"invalid request/token"
;
public
static
final
String
INVALID_REQUEST_TOKEN
=
"invalid request/token"
;
public
static
final
String
AUTHORIZATION_HEADER
=
"Authorization"
;
public
static
final
String
AUTHORIZATION_HEADER
=
"Authorization"
;
public
static
final
String
TYPE_PREFIX_SEPERATOR
=
":"
;
}
}
src/main/java/microservice/handlers/Reactor.java
0 → 100644
View file @
aae6a59b
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
;
}
}
src/main/java/microservice/io/iface/CommonServices.java
View file @
aae6a59b
package
microservice
.
io
.
iface
;
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
;
import
java.util.function.Consumer
;
/**
/**
...
@@ -20,7 +26,51 @@ public class CommonServices {
...
@@ -20,7 +26,51 @@ public class CommonServices {
void
run
();
void
run
();
void
shutdown
();
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
{
public
interface
IRestService
extends
IService
{
...
@@ -30,6 +80,11 @@ public class CommonServices {
...
@@ -30,6 +80,11 @@ public class CommonServices {
E_UPDATE
,
E_UPDATE
,
E_DELETE
;
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
{
public
interface
IPubSubService
extends
IService
{
...
...
src/main/java/microservice/io/impl/IPubSubServiceMqttImpl.java
View file @
aae6a59b
...
@@ -113,9 +113,10 @@ public class IPubSubServiceMqttImpl implements CommonServices.IPubSubService {
...
@@ -113,9 +113,10 @@ public class IPubSubServiceMqttImpl implements CommonServices.IPubSubService {
}
}
@Override
@Override
public
void
addMethod
(
CommonServices
.
IServiceCommands
serviceCommand
,
String
resourceUri
,
Consumer
<
CommonServices
.
IMsgContext
>
consumerMethod
)
{
public
void
addMethod
(
CommonServices
.
MethodParams
methodParams
)
{
if
(
EnumPubSubCommands
.
E_SUBSCRIBE
.
equals
(
serviceCommand
)
&&
String
resourceUri
=
methodParams
.
getResourceUri
();
!
resourceUri
.
isEmpty
()
&&
consumerMethod
!=
null
){
if
(
EnumPubSubCommands
.
E_SUBSCRIBE
.
equals
(
methodParams
.
getServiceCommand
())
&&
!
resourceUri
.
isEmpty
()
&&
methodParams
.
getConsumerMethod
()
!=
null
){
try
{
try
{
/**
/**
* search for last / or wildcard
* search for last / or wildcard
...
@@ -125,7 +126,7 @@ public class IPubSubServiceMqttImpl implements CommonServices.IPubSubService {
...
@@ -125,7 +126,7 @@ public class IPubSubServiceMqttImpl implements CommonServices.IPubSubService {
resourceUri
=
resourceUri
.
substring
(
0
,
indexOfSlash
);
resourceUri
=
resourceUri
.
substring
(
0
,
indexOfSlash
);
}
}
asyncClient
.
subscribe
(
resourceUri
,
qos
);
asyncClient
.
subscribe
(
resourceUri
,
qos
);
topicCallbackMap
.
put
(
resourceUri
,
consumerMethod
);
topicCallbackMap
.
put
(
resourceUri
,
methodParams
.
getConsumerMethod
()
);
}
catch
(
MqttException
e
)
{
}
catch
(
MqttException
e
)
{
e
.
printStackTrace
();
e
.
printStackTrace
();
}
}
...
...
src/test/java/microservice/TestMicroserviceApp.java
View file @
aae6a59b
package
microservice
;
package
microservice
;
import
microservice.defs.Enums
;
import
microservice.io.iface.CommonServices
;
import
microservice.io.iface.ICommandClient
;
import
microservice.io.iface.ICommandClient
;
import
microservice.io.impl.*
;
import
microservice.io.impl.*
;
import
microservice.params.BaseClientParams
;
import
microservice.params.*
;
import
microservice.params.RMQClientParams
;
import
microservice.params.RestClientParams
;
import
microservice.params.RestServerParams
;
import
microservice.types.BaseRestResponse
;
import
org.eclipse.paho.client.mqttv3.MqttException
;
import
org.eclipse.paho.client.mqttv3.MqttException
;
import
org.junit.Test
;
import
org.junit.Test
;
...
@@ -67,21 +67,32 @@ public class TestMicroserviceApp {
...
@@ -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
@Test
public
void
test
Logger
()
public
void
test
MicroServices
()
{
{
System
.
setProperty
(
"configFile.location"
,
"/opt/mcx/config/config.properties"
);
System
.
out
.
println
(
ANSI_RED
+
"hello"
+
ANSI_RESET
);
String
appName
=
"testApp"
;
System
.
out
.
println
(
ANSI_BLUE
+
"hello"
+
ANSI_RESET
);
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
();
}
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment