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
9bcadcb5
authored
May 25, 2017
by
amir
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
after testing client side read async
parent
d6c86bdc
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
12 changed files
with
202 additions
and
21 deletions
src/main/java/microservice/MicroserviceClient.java
src/main/java/microservice/defs/Constants.java
src/main/java/microservice/params/CommandParams.java
src/main/java/microservice/services/IRestServiceZmqImpl.java
src/main/java/microservice/services/protocol/zmq/RestImpl.java
src/main/java/microservice/types/BaseRestResponse.java
src/main/java/microservice/utils/ICacheClientGuavaImpl.java
src/main/java/microservice/utils/RestHttpClient.java
src/main/java/microservice/utils/ServiceBuilderFactory.java
src/test/java/microservice/TestMicroserviceApp.java
src/test/java/microservice/TestServicesAndMethods.java
src/test/java/microservice/TestZMQ.java
src/main/java/microservice/MicroserviceClient.java
View file @
9bcadcb5
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
)
{
...
...
src/main/java/microservice/defs/Constants.java
View file @
9bcadcb5
...
...
@@ -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
;
}
src/main/java/microservice/params/CommandParams.java
View file @
9bcadcb5
...
...
@@ -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
;
}
}
src/main/java/microservice/services/IRestServiceZmqImpl.java
View file @
9bcadcb5
This diff is collapsed.
Click to expand it.
src/main/java/microservice/services/protocol/zmq/RestImpl.java
View file @
9bcadcb5
...
...
@@ -53,6 +53,7 @@ public class RestImpl {
@Override
public
boolean
startAsync
(
Runnable
asyncFunc
)
{
asyncFunc
.
run
();
return
true
;
}
}
...
...
src/main/java/microservice/types/BaseRestResponse.java
View file @
9bcadcb5
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
;
}
}
src/main/java/microservice/utils/ICacheClientGuavaImpl.java
0 → 100644
View file @
9bcadcb5
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
;
}
}
src/main/java/microservice/utils/RestHttpClient.java
View file @
9bcadcb5
...
...
@@ -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
);
}
...
...
src/main/java/microservice/utils/ServiceBuilderFactory.java
View file @
9bcadcb5
...
...
@@ -133,11 +133,12 @@ public class ServiceBuilderFactory {
this
.
serverParams
=
serverParams
;
return
this
;
}
public
RestServiceZmqBuilder
setClient
ReceiveParams
(
ZMQParams
.
ServerParams
clientReceive
Params
)
{
public
RestServiceZmqBuilder
setClient
Params
(
ZMQParams
.
ServerParams
clientReceiveParams
,
ZMQParams
.
ServerParams
clientSend
Params
)
{
this
.
clientReceiveParams
=
clientReceiveParams
;
this
.
clientSendParams
=
clientSendParams
;
return
this
;
}
public
void
setClientSendParams
(
ZMQParams
.
ServerParams
clientSendParams
)
{
this
.
clientSendParams
=
clientSendParams
;
}
public
RestServiceZmqBuilder
setNumOfClientWorkers
(
int
num
)
{
numOfClientWorkers
=
num
;
...
...
src/test/java/microservice/TestMicroserviceApp.java
View file @
9bcadcb5
...
...
@@ -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
)
{
...
...
src/test/java/microservice/TestServicesAndMethods.java
View file @
9bcadcb5
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
);
}
}
}
src/test/java/microservice/TestZMQ.java
View file @
9bcadcb5
package
microservice
;
import
com.fasterxml.jackson.databind.JsonNode
;
import
com.google.flatbuffers.FlatBufferBuilder
;
import
itc.ItcMessage
;
import
itc.ItcMessageQueue
;
...
...
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