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
fffb5b8a
authored
Feb 21, 2019
by
Amir Aharon
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
first working test of pulsar pub sub implementation
parent
b09167c5
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
11 changed files
with
109 additions
and
31 deletions
README.md
build.gradle
src/common/java/common/microservice/context/PubSubMsg.java
src/common/java/common/microservice/services/CommonServices.java
src/main/java/microservice/types/BaseRestResponse.java → src/common/java/common/microservice/types/BaseRestResponse.java
src/main/java/microservice/types/UserProfile.java → src/common/java/common/microservice/types/UserProfile.java
src/main/java/microservice/services/IPubSubServicePulsarImpl.java
src/main/java/microservice/utils/ServiceBuilderFactory.java
src/test/java/microservice/TestMicroserviceApp.java
src/test/java/microservice/TestPulsar.java
src/test/java/microservice/TestServicesAndMethods.java
README.md
View file @
fffb5b8a
### Microservice Framework in JAVA
## 2.2.0
-
Add Pulsar PubSub Implementation
-
Add Either as default for optional
-
Divide src to common and main so that app's can use only common where needed
## 2.1.1
-
downgrade metrics influxdb to fit the reporter on iot jar
...
...
build.gradle
View file @
fffb5b8a
group
'com.ipgallery.common'
version
'2.
1.1
'
version
'2.
2.0
'
apply
plugin:
'java'
apply
plugin:
'maven-publish'
...
...
src/common/java/common/microservice/context/PubSubMsg.java
View file @
fffb5b8a
...
...
@@ -15,6 +15,12 @@ public class PubSubMsg {
this
.
mcid
=
mcid
;
}
public
PubSubMsg
(
String
content
,
String
mcid
,
Map
<
String
,
String
>
parameters
)
{
this
.
content
=
content
;
this
.
mcid
=
mcid
;
this
.
parameters
=
parameters
;
}
PubSubMsg
addParameter
(
String
param
,
String
value
){
if
(
parameters
==
null
)
parameters
=
new
HashMap
<>();
...
...
@@ -33,4 +39,9 @@ public class PubSubMsg {
public
String
getMcid
()
{
return
mcid
;
}
@Override
public
String
toString
()
{
return
"mcid: "
+
mcid
+
", content: "
+
content
;
}
}
src/common/java/common/microservice/services/CommonServices.java
View file @
fffb5b8a
...
...
@@ -181,7 +181,7 @@ public class CommonServices {
}
public
static
abstract
class
IPubSubService
extends
IService
{
public
class
PubSubMsgContext
implements
IMsgContext
{
public
static
class
PubSubMsgContext
implements
IMsgContext
{
private
String
topic
;
private
String
msg
;
private
String
mcid
=
null
;
...
...
@@ -227,6 +227,10 @@ public class CommonServices {
return
this
;
}
public
Map
<
String
,
String
>
getParameters
()
{
return
parameters
;
}
@Override
public
void
setParameters
(
Map
<
String
,
String
>
parameters
)
{
this
.
parameters
=
parameters
;
...
...
src/
main/java
/microservice/types/BaseRestResponse.java
→
src/
common/java/common
/microservice/types/BaseRestResponse.java
View file @
fffb5b8a
package
microservice
.
types
;
package
common
.
microservice
.
types
;
import
com.fasterxml.jackson.annotation.JsonCreator
;
import
com.fasterxml.jackson.annotation.JsonIgnore
;
...
...
src/
main/java
/microservice/types/UserProfile.java
→
src/
common/java/common
/microservice/types/UserProfile.java
View file @
fffb5b8a
package
microservice
.
types
;
package
common
.
microservice
.
types
;
import
io.jsonwebtoken.Claims
;
import
io.jsonwebtoken.impl.DefaultClaims
;
import
microservice.services.CommonServices
;
import
common.
microservice.services.CommonServices
;
import
java.util.HashMap
;
import
java.util.Map
;
...
...
src/main/java/microservice/services/IPubSubServicePulsarImpl.java
View file @
fffb5b8a
This diff is collapsed.
Click to expand it.
src/main/java/microservice/utils/ServiceBuilderFactory.java
View file @
fffb5b8a
...
...
@@ -330,6 +330,8 @@ public class ServiceBuilderFactory {
CommonServices
.
EnumPubSubServiceMode
serviceMode
=
CommonServices
.
EnumPubSubServiceMode
.
E_BOTH
;
int
consumerPoolSize
=
0
;
String
serviceUrl
=
null
;
String
adminUrl
=
null
;
String
clusters
=
"standalone"
;
public
PubSubServicePulsarBuilder
(
CommonServices
.
EnumPubSubServiceMode
serviceMode
)
{
this
.
serviceMode
=
serviceMode
;
...
...
@@ -345,6 +347,16 @@ public class ServiceBuilderFactory {
return
this
;
}
public
PubSubServicePulsarBuilder
setAdminUrl
(
String
adminUrl
)
{
this
.
adminUrl
=
adminUrl
;
return
this
;
}
public
PubSubServicePulsarBuilder
setClusters
(
String
clusters
)
{
this
.
clusters
=
clusters
;
return
this
;
}
private
boolean
validateParams
()
{
/**
* defaulting to number of processors
...
...
@@ -352,7 +364,7 @@ public class ServiceBuilderFactory {
if
(
consumerPoolSize
==
0
){
consumerPoolSize
=
Runtime
.
getRuntime
().
availableProcessors
();
}
if
(
serviceUrl
==
null
)
if
(
serviceUrl
==
null
&&
adminUrl
!=
null
)
return
false
;
return
true
;
...
...
@@ -362,7 +374,7 @@ public class ServiceBuilderFactory {
public
CommonServices
.
IService
build
()
{
if
(
validateParams
())
{
try
{
pubSubServicePulsar
=
new
IPubSubServicePulsarImpl
(
serviceUrl
,
consumerPoolSize
);
pubSubServicePulsar
=
new
IPubSubServicePulsarImpl
(
serviceUrl
,
adminUrl
,
clusters
,
consumerPoolSize
);
pubSubServicePulsar
.
setServiceMode
(
serviceMode
);
}
catch
(
Exception
exp
){
...
...
src/test/java/microservice/TestMicroserviceApp.java
View file @
fffb5b8a
...
...
@@ -103,6 +103,8 @@ public class TestMicroserviceApp {
.
build
();
CommonServices
.
IService
pulsarPubSub
=
ServiceBuilderFactory
.
createPubSubServicePulsarBuilder
(
CommonServices
.
EnumPubSubServiceMode
.
E_BOTH
)
.
setServiceUrl
(
"localhost:6650"
)
.
setAdminUrl
(
"localhost:8080"
)
.
setClusters
(
"standalone"
)
.
build
();
microservice
.
MicroserviceApp
msApp
=
new
microservice
.
MicroserviceApp
(
appName
);
...
...
@@ -169,12 +171,28 @@ public class TestMicroserviceApp {
(
msgCtx
,
orgService
)
->
{
testZmqMsgQueue
((
CommonServices
.
IMsgQService
.
MsgQContext
)
msgCtx
);
}));
methodParamsList
.
add
(
new
CommonServices
.
MethodParams
(
Enums
.
EnumServiceType
.
E_PUBSUB
,
CommonServices
.
EnumPubSubCommands
.
E_NOTIFY
,
"/activity"
,
(
msgCtx
,
orgService
)
->
{
testPubSubMsg
((
CommonServices
.
IPubSubService
.
PubSubMsgContext
)
msgCtx
);
}));
}
long
startTime
=
0
;
ObjectMapper
objectMapper
=
new
ObjectMapper
();
private
void
testPubSubMsg
(
CommonServices
.
IPubSubService
.
PubSubMsgContext
msgCtx
)
{
// System.out.println("Recieved mcid: " + msgCtx.getMcid());
try
{
JsonNode
jsonNode
=
objectMapper
.
readValue
(
msgCtx
.
getMsg
(),
JsonNode
.
class
);
handleJsonTestStartStop
(
jsonNode
);
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
/**
* around 300k/s
* @param msgCtx
...
...
@@ -182,22 +200,26 @@ public class TestMicroserviceApp {
private
void
testZmqMsgQueue
(
CommonServices
.
IMsgQService
.
MsgQContext
msgCtx
)
{
try
{
JsonNode
jsonNode
=
objectMapper
.
readValue
(
msgCtx
.
msg
,
JsonNode
.
class
);
String
state
=
jsonNode
.
path
(
"state"
).
asText
();
switch
(
state
){
case
"start"
:
startTime
=
System
.
currentTimeMillis
();
break
;
case
"end"
:
System
.
out
.
println
(
"Test took: "
+
String
.
valueOf
(
System
.
currentTimeMillis
()
-
startTime
)
+
"ms"
);
break
;
case
"msg"
:
break
;
}
handleJsonTestStartStop
(
jsonNode
);
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
private
void
handleJsonTestStartStop
(
JsonNode
jsonNode
)
{
String
state
=
jsonNode
.
path
(
"state"
).
asText
();
switch
(
state
){
case
"start"
:
startTime
=
System
.
currentTimeMillis
();
break
;
case
"end"
:
System
.
out
.
println
(
"Test took: "
+
String
.
valueOf
(
System
.
currentTimeMillis
()
-
startTime
)
+
"ms"
);
break
;
case
"msg"
:
break
;
}
}
private
void
testZmqRead
(
RestContext
msgCtx
)
{
RestContext
restContext
=
(
RestContext
)
msgCtx
;
CommonServices
.
IRestService
inRestService
=
(
CommonServices
.
IRestService
)
MicroserviceApp
.
getsInstance
().
getService
(
Enums
.
EnumServiceType
.
E_REST
,
"undertowRestService"
);
...
...
src/test/java/microservice/TestPulsar.java
View file @
fffb5b8a
package
microservice
;
import
common.microservice.context.PubSubMsg
;
import
common.microservice.utils.IDGenerator
;
import
org.apache.pulsar.client.admin.PulsarAdmin
;
import
org.apache.pulsar.client.admin.PulsarAdminBuilder
;
...
...
@@ -9,6 +10,7 @@ import org.apache.pulsar.client.admin.internal.PulsarAdminBuilderImpl;
import
org.apache.pulsar.client.api.*
;
import
org.apache.pulsar.client.api.url.URL
;
import
org.apache.pulsar.client.impl.conf.ClientConfigurationData
;
import
org.apache.pulsar.client.impl.schema.JSONSchema
;
import
org.apache.pulsar.common.policies.data.Policies
;
import
org.apache.pulsar.common.policies.data.TenantInfo
;
import
org.junit.Test
;
...
...
@@ -28,18 +30,20 @@ public class TestPulsar {
public
void
testTopicProducer
()
throws
PulsarClientException
{
String
localClusterUrl
=
"pulsar://localhost:6650"
;
String
namespace
=
"mcx/testApp"
;
// This namespace is created automatically
String
topic
=
String
.
format
(
"persistent://%s/
my-topic1
"
,
namespace
);
String
topic
=
String
.
format
(
"persistent://%s/
activity
"
,
namespace
);
PulsarClient
client
=
PulsarClient
.
builder
()
.
serviceUrl
(
localClusterUrl
)
.
build
();
Producer
producer
=
client
.
newProducer
().
topic
(
topic
).
create
();
Producer
<
PubSubMsg
>
producer
=
client
.
newProducer
(
JSONSchema
.
of
(
PubSubMsg
.
class
))
.
topic
(
topic
)
.
create
();
// Publish 10 messages to the topic
for
(
int
i
=
0
;
i
<
10
;
i
++)
{
producer
.
send
(
String
.
format
(
"Message number %d"
,
i
).
getBytes
());
for
(
int
i
=
0
;
i
<
100
;
i
++)
{
PubSubMsg
pubSubMsg
=
new
PubSubMsg
(
"hello - "
+
String
.
valueOf
(
System
.
currentTimeMillis
()),
String
.
valueOf
(
i
));
producer
.
send
(
pubSubMsg
);
System
.
out
.
println
(
"Sending message"
);
// final Message message = MessageBuilder.create()
// .setContent(String.format("Message number %d", i).getBytes())
...
...
@@ -65,17 +69,24 @@ public class TestPulsar {
//final Consumer consumer = client.newConsumer().subscriptionName("my-sub-1").topic(topic).subscribe();
Pattern
allTopicsInNamespace
=
Pattern
.
compile
(
"persistent://mcx/testApp/.*"
);
final
Consumer
consumer
=
client
.
newConsumer
(
)
final
Consumer
<
PubSubMsg
>
consumer
=
client
.
newConsumer
(
JSONSchema
.
of
(
PubSubMsg
.
class
)
)
.
subscriptionName
(
IDGenerator
.
createUUID
())
//
.subscriptionType(SubscriptionType.Shared) // enable for multi-instance
.
subscriptionType
(
SubscriptionType
.
Shared
)
// enable for multi-instance
.
topicsPattern
(
allTopicsInNamespace
)
.
subscribe
();
// final Consumer consumer = client.newConsumer()
// .subscriptionName(IDGenerator.createUUID())
// .subscriptionType(SubscriptionType.Shared) // enable for multi-instance
// .topicsPattern(allTopicsInNamespace)
// .subscribe();
for
(
int
i
=
0
;
i
<
10
;
i
++)
{
// Wait for a message
Message
msg
=
consumer
.
receive
();
Message
<
PubSubMsg
>
msg
=
consumer
.
receive
();
System
.
out
.
printf
(
"Message received: %s\n"
,
new
String
(
msg
.
getData
()
));
System
.
out
.
printf
(
"Message received: %s\n"
,
msg
.
getValue
().
toString
(
));
// Acknowledge the message so that it can be deleted by the message broker
consumer
.
acknowledge
(
msg
);
...
...
src/test/java/microservice/TestServicesAndMethods.java
View file @
fffb5b8a
...
...
@@ -36,7 +36,7 @@ import java.util.stream.Collectors;
*/
public
class
TestServicesAndMethods
{
public
static
final
int
ITERATIONS
=
10000
000
;
public
static
final
int
ITERATIONS
=
10000
;
private
static
class
RoutingMatch
{
...
...
@@ -278,12 +278,25 @@ public class TestServicesAndMethods {
@Test
public
void
testPubSubPulsar
()
throws
InterruptedException
{
CommonServices
.
IService
iService
=
ServiceBuilderFactory
.
createPubSubServicePulsarBuilder
(
CommonServices
.
EnumPubSubServiceMode
.
E_
BOTH
)
CommonServices
.
IService
iService
=
ServiceBuilderFactory
.
createPubSubServicePulsarBuilder
(
CommonServices
.
EnumPubSubServiceMode
.
E_
PUBLISHER
)
.
setServiceUrl
(
"localhost:6650"
)
.
setAdminUrl
(
"localhost:8080"
)
.
build
();
CommonServices
.
IPubSubService
pubSubService
=
(
CommonServices
.
IPubSubService
)
iService
;
// pubSubService.init();
// pubSubService.run();
pubSubService
.
init
();
pubSubService
.
run
();
ObjectNode
objectNode
=
JsonNodeFactory
.
instance
.
objectNode
().
put
(
"state"
,
"start"
).
put
(
"iterations"
,
ITERATIONS
);
System
.
out
.
println
(
"Testing "
+
String
.
valueOf
(
ITERATIONS
)
+
" iterations"
);
long
start
=
System
.
currentTimeMillis
();
pubSubService
.
publish
(
new
CommonServices
.
IPubSubService
.
PubSubMsgContext
(
"/activity"
,
objectNode
.
toString
()));
objectNode
.
put
(
"state"
,
"msg"
);
for
(
int
i
=
0
;
i
<
TestServicesAndMethods
.
ITERATIONS
;
i
++)
{
objectNode
.
put
(
"msg"
,
"hello"
+
String
.
valueOf
(
i
));
pubSubService
.
publish
(
new
CommonServices
.
IPubSubService
.
PubSubMsgContext
(
"/activity"
,
objectNode
.
toString
()));
}
objectNode
.
put
(
"state"
,
"end"
);
pubSubService
.
publish
(
new
CommonServices
.
IPubSubService
.
PubSubMsgContext
(
"/activity"
,
objectNode
.
toString
()));
System
.
out
.
println
(
"Async publish Test of: "
+
String
.
valueOf
(
ITERATIONS
)
+
" took (msec): "
+
String
.
valueOf
(
System
.
currentTimeMillis
()
-
start
));
Thread
.
sleep
(
1000
);
pubSubService
.
shutdown
();
}
...
...
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