Commit 706a3f14 by amira

Merge branch 'develop' of…

Merge branch 'develop' of https://municipalitybank.com/ipgallery.common.java/microservice into develop
parents 4821064f 99f80084
Showing with 2053 additions and 981 deletions
### Microservice Framework in JAVA
## 2.3.1
- add retention policy and ttl for pulsar, move PulsarParams from common
## 2.3.0
- Seperate Services from main lib
- Split to common,clients,servicePubSub and app jars
-
## 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
- Note on Pulsar topic: can't be nested so after the domain prefix change the / with @
e.g: rest path like /domain/apiFamiliy/api1 will be in pubsub /domain/apiFamily@api1
## 2.1.4
- connect with undertow 2.0.28
## 2.1.3
- add date time to ILoggerConsoleImpl
## 2.1.2
- add config to okhttp client read/connect timeouts
"microservice.http.client.read.timeout.seconds" (30)
"microservice.http.client.connect.timeout.seconds" (10)
## 2.1.1
- downgrade metrics influxdb to fit the reporter on iot jar
......@@ -27,13 +43,13 @@
- add ZeroMQ support for Rest
## 1.3.8: Add colorful logging
## 1.3.7: Add Pagination - PageBuilder
## 1.3.6: upgrade common-utils 1.2.0
## 1.3.6: upgrade common-common.microservice.utils 1.2.0
## 1.3.2:
- add getMD5Hash, catch unhandled exceptions, add command-params builder
## 1.3.1:
- add rabbotmq command client + change version to 1.3.1
## 1.3.0:
- add service authorization with jwt
- add microservice.service authorization with jwt
- env param override config file
## 1.2.5:
- support rabbit on server side
......@@ -58,10 +74,10 @@
- Add '/_stat' in monitoring to returns stats of metrics, more to come
- Change '/reload' to '/_reload'
## Version '1.1.0':
- Add resolveService in service discovery, used by http command-client to resolve the address
of the dest service
# Env for service discovery:
service.resolver.polling.interval (milli) default is 5000
- Add resolveService in microservice.service discovery, used by http command-client to resolve the address
of the dest microservice.service
# Env for microservice.service discovery:
microservice.service.resolver.polling.interval (milli) default is 5000
# Env for jwt:
jwt.token.in.authorization : true
jwt.salt : "12345678901234567890123456789012"
plugins {
id "org.sonarqube" version "2.7"
}
group 'com.ipgallery.common'
version '2.1.3'
version '2.3.1'
apply plugin: 'java'
apply plugin: 'maven-publish'
//for mavenLocal
//apply plugin: 'maven'
sourceCompatibility = 1.8
repositories {
......@@ -17,43 +20,171 @@ repositories {
}
sourceSets {
main {
java {
srcDir 'src/main/java'
exclude '**'
}
}
common {
java.srcDir 'src/main/java/microservice/common'
resources.srcDir 'src/main/java/microservice/common/resources'
}
clients {
java.srcDir'src/main/java/microservice/clients'
resources.srcDir 'src/main/java/microservice/clients/resources'
}
servicePubsub {
java.srcDir 'src/main/java/microservice/service/pubsub'
resources.srcDir 'src/main/java/microservice/service/resources'
}
app {
java.srcDir 'src/main/java/microservice/app'
resources.srcDir 'src/main/java/microservice/resources'
compileClasspath += sourceSets.servicePubsub.runtimeClasspath
compileClasspath += sourceSets.clients.runtimeClasspath
}
test {
compileClasspath += sourceSets.common.runtimeClasspath
compileClasspath += sourceSets.servicePubsub.runtimeClasspath
compileClasspath += sourceSets.clients.runtimeClasspath
compileClasspath += sourceSets.app.runtimeClasspath
}
}
dependencies {
compile 'io.undertow:undertow-core:1.0.1.Final'
compile 'com.fasterxml.jackson.core:jackson-databind:2.2.3'
compile 'com.github.stephenc.eaio-uuid:uuid:3.4.0'
compile 'io.dropwizard.metrics:metrics-core:3.1.0'
compile 'com.netflix.hystrix:hystrix-codahale-metrics-publisher:1.4.14'
compile 'com.netflix.hystrix:hystrix-metrics-event-stream:1.4.12'
compile 'redis.clients:jedis:2.4.2'
compile 'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.0.2'
compile 'com.ipgallery.common:utils:1.2.5'
compile ('com.ipgallery.common:rabbitmq:1.0.3')
compile 'com.ecwid.consul:consul-api:1.1.9'
//compile 'com.github.davidb:metrics-influxdb:0.9.3'
compile 'com.github.davidb:metrics-influxdb:0.8.2'
compile 'io.dropwizard.metrics:metrics-graphite:3.2.5'
compile 'io.jsonwebtoken:jjwt:0.6.0'
compile group: 'org.zeromq', name: 'jeromq', version: '0.4.0'
compile 'org.elasticsearch.client:rest:5.4.1'
compile 'com.netflix.rxjava:rxjava-apache-http:0.20.7'
compile 'com.squareup.okhttp3:okhttp:3.8.0'
commonCompile (
'com.fasterxml.jackson.core:jackson-databind:2.2.3',
'io.jsonwebtoken:jjwt:0.6.0',
'com.ipgallery.common:utils:1.2.5',
'com.github.stephenc.eaio-uuid:uuid:3.4.0',
'com.netflix.hystrix:hystrix-core:1.4.14'
)
clientsCompile (
files('build/common/microservice-common.jar'),
'com.ipgallery.common:utils:1.2.5',
'com.netflix.rxjava:rxjava-apache-http:0.20.7',
'com.netflix.hystrix:hystrix-metrics-event-stream:1.4.12',
'com.squareup.okhttp3:okhttp:3.8.0'
)
servicePubsubCompile (
files('build/common/microservice-common.jar'),
'org.apache.pulsar:pulsar-client:2.4.2',
'org.apache.pulsar:pulsar-client-admin:2.4.2'
)
appCompile (
files('build/common/microservice-common.jar'),
files('build/clients/microservice-clients.jar'),
'io.jsonwebtoken:jjwt:0.6.0',
'io.undertow:undertow-core:2.0.28.Final',
'com.fasterxml.jackson.core:jackson-databind:2.2.3',
'io.dropwizard.metrics:metrics-core:3.1.0',
'redis.clients:jedis:2.4.2',
'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.0.2',
'com.ipgallery.common:utils:1.2.5',
'com.ipgallery.common:rabbitmq:1.0.3',
'com.ecwid.consul:consul-api:1.1.9',
'com.github.davidb:metrics-influxdb:0.8.2',
'io.dropwizard.metrics:metrics-graphite:3.2.5',
'org.zeromq:jeromq:0.4.0',
'org.elasticsearch.client:rest:5.4.1',
'com.netflix.rxjava:rxjava-apache-http:0.20.7',
'com.netflix.hystrix:hystrix-metrics-event-stream:1.4.12',
'com.squareup.okhttp3:okhttp:3.8.0'
)
// compile group: 'org.apache.httpcomponents', name: 'httpasyncclient', version: '4.1.2'
testCompile group: 'junit', name: 'junit', version: '4.11'
testCompile group: 'org.zeromq', name: 'jeromq', version: '0.4.0'
testCompile (
files('build/common/microservice-common.jar'),
files('build/clients/microservice-clients.jar'),
'junit:junit:4.11',
'org.zeromq:jeromq:0.4.0',
'com.github.stephenc.eaio-uuid:uuid:3.4.0'
)
}
//1. use this install task (under "other" section) to create this jar localy on your machine.
// version will be extended with the Postfix "-LOCAL"
//2. in order to use this jar in a project using this jar:
// in the build.gradle add "mavenLocal()" as first repository
//and the dependency with correct version number ended with "-LOCAL"
//install {
// repositories.mavenInstaller {
// pom.version = version + '-LOCAL'
// }
task commonJar(type: Jar) {
from configurations.commonCompile.collect { zipTree it }
from sourceSets.common.output
version = '1.0.2'
archiveName = "microservice-common.jar"
destinationDir = file("build/common")
}
// pubsub
task servicePubsubJar(type: Jar) {
dependsOn commonJar
mustRunAfter commonJar
//from configurations.servicePubsubCompile.collect { zipTree it }
from sourceSets.servicePubsub.output
version = '1.0.1'
archiveName = "microservice-service-pubsub.jar"
destinationDir = file("build/servicePubsub")
}
// client
task clientsJar(type: Jar) {
dependsOn commonJar
mustRunAfter commonJar
//from configurations.clientsCompile.collect { zipTree it }
from sourceSets.clients.output
version = '1.0.0'
archiveName = "microservice-clients.jar"
destinationDir = file("build/clients")
}
// app
task appJar(type: Jar) {
dependsOn commonJar
mustRunAfter commonJar
from configurations.appCompile.collect { zipTree it }
from sourceSets.app.output
version = version // '2.3.0'
archiveName = "microservice-app.jar"
destinationDir = file("build/app")
}
assemble {
dependsOn commonJar
dependsOn clientsJar
dependsOn servicePubsubJar
dependsOn appJar
}
//jar {
// dependsOn servicePubsubJar
// mustRunAfter servicePubsubJar
//}
// from configurations.compile.collect { zipTree it }
// from sourceSets.main.output
// //version = '2.2.0'
// archiveName = "microservice-${version}.jar"
// destinationDir = file("build/main")
//}
sonarqube {
properties {
property "sonar.projectKey", "microservices"
property "sonar.host.url", "http://localhost:9010"
property "sonar.login", "61d37da4d14128f0912d456c7efaacc06cd43ca3"
property "sonar.java.binaries", "build/app"
}
}
publishing {
publications {
......@@ -66,16 +197,101 @@ publishing {
}
}
// repositories.maven {
// url 'http://172.16.1.132:8081/repository/internal'
// credentials {
// username "admin"
// password "giptmgr1"
// mavenJava(MavenPublication) {
// artifactId 'microservice'
// from components.java
// }
// client(MavenPublication) {
// artifactId 'microservice-client'
// version clientJar.version
// // adding dependencies
// pom.withXml {
// def dependenciesNode = asNode().appendNode('dependencies')
// //Iterate over the compile dependencies (we don't want the test ones), adding a <dependency> node for each
// configurations.clientCompile.dependencies.each {
// if (it.group != null && it.name != null){
// def dependencyNode = dependenciesNode.appendNode('dependency')
// dependencyNode.appendNode('groupId', it.group)
// dependencyNode.appendNode('artifactId', it.name)
// dependencyNode.appendNode('version', it.version)
// }
// }
// }
// //version - taking prom the project version
// //from components.java
// artifact clientJar
// }
common(MavenPublication) {
artifactId 'microservice-common'
version commonJar.version
// adding dependencies
pom.withXml {
def dependenciesNode = asNode().appendNode('dependencies')
//Iterate over the compile dependencies (we don't want the test ones), adding a <dependency> node for each
// configurations.clientCompile.dependencies.each {
// if (it.group != null && it.name != null){
// def dependencyNode = dependenciesNode.appendNode('dependency')
// dependencyNode.appendNode('groupId', it.group)
// dependencyNode.appendNode('artifactId', it.name)
// dependencyNode.appendNode('version', it.version)
// }
// }
mavenJava(MavenPublication) {
//artifactId 'group-service'
from components.java
}
//version - taking prom the project version
//from components.java
artifact commonJar
}
clients(MavenPublication) {
artifactId 'microservice-clients'
version clientsJar.version
// adding dependencies
pom.withXml {
def dependenciesNode = asNode().appendNode('dependencies')
configurations.clientsCompile.dependencies.each {
if (it.group != null && it.name != null){
def dependencyNode = dependenciesNode.appendNode('dependency')
dependencyNode.appendNode('groupId', it.group)
dependencyNode.appendNode('artifactId', it.name)
dependencyNode.appendNode('version', it.version)
}
}
}
artifact clientsJar
}
servicePubsub(MavenPublication) {
artifactId 'microservice-service-pubsub'
version servicePubsubJar.version
// adding dependencies
pom.withXml {
def dependenciesNode = asNode().appendNode('dependencies')
configurations.servicePubsubCompile.dependencies.each {
if (it.group != null && it.name != null){
def dependencyNode = dependenciesNode.appendNode('dependency')
dependencyNode.appendNode('groupId', it.group)
dependencyNode.appendNode('artifactId', it.name)
dependencyNode.appendNode('version', it.version)
}
}
}
artifact servicePubsubJar
}
app(MavenPublication) {
artifactId 'microservice-app'
version appJar.version
// adding dependencies
pom.withXml {
def dependenciesNode = asNode().appendNode('dependencies')
}
artifact appJar
}
}
}
Running standalone
docker run -it -p 80:80 -p 8080:8080 -p 6650:6650 apachepulsar/pulsar-standalone
[curl]
- show tenant
curl -s http://localhost:8080/admin/v2/tenants/mcz
- show namespaces
curl -s http://localhost:8080/admin/v2/namespaces/mcx/publish
[pulsar-admin]
run "docker exec -it [pulsar-standalone-instance] bash"
and then execute admin commands:
https://pulsar.apache.org/docs/latest/admin-api/overview/
- create tenant: pulsar-admin tenants create mcx
- create namespace in tenant: pulsar-admin namespaces create mcx/namespace1
- set replication clusters (needed to have policies, otherwise we can't publish):
bin/pulsar-admin namespaces set-clusters mcx/namespace1 --clusters standalone
......@@ -3,13 +3,13 @@
+- counters and metrics in the reactor
+- validateRequest in RestService , the jwt issues
+- Add monitoring apis
+- All the validation ,pre/post handling that was were done by the handler will be done by the base service
+- All the validation ,pre/post handling that was were done by the handler will be done by the base microservice.service
- add Runtime Test:
addTest(const char *testName, nsMicroservice_Iface::TestFunction testFunction);
addTest(nsMicroservice_Iface::ITest *p_testClass)
+- Add true async in http rest service, that we can send response after handleRequest ends.
+- Add true async in http rest microservice.service, that we can send response after handleRequest ends.
// exchange.dispatch(() -> {
// new Timer().schedule(new TimerTask() {
// public void run() {
......
#!/usr/bin/env bash
#!/usr/bin/env sh
##############################################################################
##
......@@ -6,12 +6,30 @@
##
##############################################################################
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
DEFAULT_JVM_OPTS=""
# Attempt to set APP_HOME
# Resolve links: $0 may be a link
PRG="$0"
# Need this for relative symlinks.
while [ -h "$PRG" ] ; do
ls=`ls -ld "$PRG"`
link=`expr "$ls" : '.*-> \(.*\)$'`
if expr "$link" : '/.*' > /dev/null; then
PRG="$link"
else
PRG=`dirname "$PRG"`"/$link"
fi
done
SAVED="`pwd`"
cd "`dirname \"$PRG\"`/" >/dev/null
APP_HOME="`pwd -P`"
cd "$SAVED" >/dev/null
APP_NAME="Gradle"
APP_BASE_NAME=`basename "$0"`
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
DEFAULT_JVM_OPTS=""
# Use the maximum available, or set MAX_FD != -1 to use that value.
MAX_FD="maximum"
......@@ -30,6 +48,7 @@ die ( ) {
cygwin=false
msys=false
darwin=false
nonstop=false
case "`uname`" in
CYGWIN* )
cygwin=true
......@@ -40,26 +59,11 @@ case "`uname`" in
MINGW* )
msys=true
;;
NONSTOP* )
nonstop=true
;;
esac
# Attempt to set APP_HOME
# Resolve links: $0 may be a link
PRG="$0"
# Need this for relative symlinks.
while [ -h "$PRG" ] ; do
ls=`ls -ld "$PRG"`
link=`expr "$ls" : '.*-> \(.*\)$'`
if expr "$link" : '/.*' > /dev/null; then
PRG="$link"
else
PRG=`dirname "$PRG"`"/$link"
fi
done
SAVED="`pwd`"
cd "`dirname \"$PRG\"`/" >/dev/null
APP_HOME="`pwd -P`"
cd "$SAVED" >/dev/null
CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
# Determine the Java command to use to start the JVM.
......@@ -85,7 +89,7 @@ location of your Java installation."
fi
# Increase the maximum file descriptors if we can.
if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
MAX_FD_LIMIT=`ulimit -H -n`
if [ $? -eq 0 ] ; then
if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
......@@ -150,11 +154,19 @@ if $cygwin ; then
esac
fi
# Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
function splitJvmOpts() {
JVM_OPTS=("$@")
# Escape application args
save ( ) {
for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
echo " "
}
eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
APP_ARGS=$(save "$@")
# Collect all arguments for the java command, following the shell quoting and substitution rules
eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
# by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong
if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then
cd "$(dirname "$0")"
fi
exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
exec "$JAVACMD" "$@"
......@@ -8,14 +8,14 @@
@rem Set local scope for the variables with windows NT shell
if "%OS%"=="Windows_NT" setlocal
@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
set DEFAULT_JVM_OPTS=
set DIRNAME=%~dp0
if "%DIRNAME%" == "" set DIRNAME=.
set APP_BASE_NAME=%~n0
set APP_HOME=%DIRNAME%
@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
set DEFAULT_JVM_OPTS=
@rem Find java.exe
if defined JAVA_HOME goto findJavaFromJavaHome
......@@ -46,10 +46,9 @@ echo location of your Java installation.
goto fail
:init
@rem Get command-line arguments, handling Windowz variants
@rem Get command-line arguments, handling Windows variants
if not "%OS%" == "Windows_NT" goto win9xME_args
if "%@eval[2+2]" == "4" goto 4NT_args
:win9xME_args
@rem Slurp the command line arguments.
......@@ -60,11 +59,6 @@ set _SKIP=2
if "x%~1" == "x" goto execute
set CMD_LINE_ARGS=%*
goto execute
:4NT_args
@rem Get arguments from the 4NT Shell from JP Software
set CMD_LINE_ARGS=%$
:execute
@rem Setup the command line
......
package microservice;
import common.microservice.io.iface.*;
import common.microservice.io.impl.ILoggerConsoleImpl;
import io.undertow.Handlers;
import io.undertow.Undertow;
import io.undertow.Undertow.Builder;
......@@ -7,14 +9,14 @@ import io.undertow.server.HttpHandler;
import io.undertow.server.handlers.PathHandler;
import io.undertow.server.handlers.resource.ClassPathResourceManager;
import io.undertow.util.MimeMappings;
import microservice.defs.Enums;
import common.microservice.defs.Enums;
import common.microservice.params.RMQClientParams;
import common.microservice.params.RestServerParams;
import microservice.handlers.*;
import microservice.io.iface.*;
import microservice.io.iface.IRestServer;
import microservice.io.impl.*;
import microservice.params.RMQClientParams;
import microservice.params.RestServerParams;
import microservice.services.CommonServices;
import common.microservice.services.CommonServices;
import org.eclipse.paho.client.mqttv3.MqttException;
import rabbitmq.common.RMQId;
......@@ -41,7 +43,7 @@ import static io.undertow.Handlers.resource;
* @author amir
*
*/
public class MicroserviceApp
public class MicroserviceApp implements IApp
{
private static MicroserviceApp sInstance = null;
......@@ -629,7 +631,7 @@ public class MicroserviceApp
.filter(p -> p != null)
.forEach(servicesMap ->
servicesMap.forEach((serviceKey, service) -> {
service.init();
service.init(this);
if (enableMetrics)
service.withMetricsFactory(IMetricsFactoryImpl.getInstance());
service.register(serviceDiscovery, id);
......@@ -643,6 +645,7 @@ public class MicroserviceApp
/**
* מי מתחכם זה מיקו!!!
* running only if there are any valid services
*/
if (Arrays.stream(servicesArray)
.filter(p -> p != null)
......@@ -689,10 +692,14 @@ public class MicroserviceApp
}
/**
* logging utils
*/
public void logRcid(String from, String rcid){
getLogger().info(from + " RCID: " + rcid);
}
// /**
// * logging utils
// */
// public void logRcid(String from, String rcid){
// getLogger().info(from + " RCID: " + rcid);
// }
//
// public void logMcid(String from, String mcid){
// getLogger().info(from + " MCID: " + mcid);
// }
}
package microservice;
import microservice.defs.Constants;
import microservice.types.BaseRestResponse;
import clients.microservice.impl.IRestClientHttpImpl;
import common.microservice.defs.Constants;
import common.CacheClient;
import common.JsonHandler;
import common.RedisCacheClient;
import com.fasterxml.jackson.databind.JsonNode;
import microservice.io.iface.IRestClient;
import microservice.io.impl.IRestClientHttpImpl;
import microservice.params.BaseClientParams;
import microservice.params.CommandParams;
import common.microservice.io.iface.IRestClient;
import common.microservice.types.BaseRestResponse;
import common.microservice.params.BaseClientParams;
import common.microservice.params.CommandParams;
/**
*
......
......@@ -6,11 +6,11 @@ import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import microservice.MicroserviceApp;
import microservice.common.context.RestContext;
import microservice.io.iface.IConfiguration;
import microservice.io.iface.IContainer;
import microservice.io.iface.ILogger;
import microservice.io.iface.INotifyCallback;
import common.microservice.context.RestContext;
import common.microservice.io.iface.IConfiguration;
import common.microservice.io.iface.IContainer;
import common.microservice.io.iface.ILogger;
import common.microservice.io.iface.INotifyCallback;
public abstract class BaseHandler
{
......
......@@ -5,16 +5,18 @@ import java.util.Deque;
import java.util.HashMap;
import java.util.Map;
import microservice.common.context.RestContext;
import microservice.defs.Constants;
import microservice.defs.Enums.EnumHttpMethod;
import microservice.io.iface.*;
import common.microservice.context.RestContext;
import common.microservice.defs.Constants;
import common.microservice.defs.Enums.EnumHttpMethod;
import common.microservice.io.iface.IContainer;
import common.microservice.io.iface.ILogger;
import common.microservice.io.iface.IRequest;
import common.microservice.io.iface.IResponse;
import common.microservice.types.BaseRestResponse;
import microservice.io.impl.IRequestMBIImpl;
import microservice.io.impl.IResponseMBIImpl;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import microservice.types.BaseRestResponse;
import rabbitmq.common.RMQId;
import rabbitmq.common.RMQMessage;
......
package microservice.handlers;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.SortedMap;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import com.codahale.metrics.Counter;
import com.codahale.metrics.Gauge;
......@@ -16,15 +14,15 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;
import common.microservice.types.BaseRestResponse;
import microservice.MicroserviceApp;
import microservice.defs.Constants;
import microservice.defs.Enums;
import microservice.io.iface.ILogger;
import microservice.services.CommonServices;
import microservice.io.iface.IConfiguration;
import common.microservice.defs.Constants;
import common.microservice.defs.Enums;
import common.microservice.io.iface.ILogger;
import common.microservice.services.CommonServices;
import common.microservice.io.iface.IConfiguration;
import microservice.io.impl.IMetricsFactoryImpl;
import microservice.types.BaseRestResponse;
import microservice.common.context.RestContext;
import common.microservice.context.RestContext;
/**
* this class is for monitoring the microservice
......
......@@ -2,15 +2,17 @@ package microservice.handlers;
import io.undertow.util.PathMatcher;
import io.undertow.util.PathTemplateMatcher;
import microservice.defs.Constants;
import microservice.defs.Enums;
import microservice.services.CommonServices;
import microservice.io.iface.ILogger;
import microservice.io.iface.IMetricsFactory;
import common.microservice.defs.Constants;
import common.microservice.defs.Enums;
import common.microservice.services.CommonServices;
import common.microservice.io.iface.ILogger;
import common.microservice.io.iface.IMetricsFactory;
import java.util.*;
import java.util.function.BiConsumer;
import static common.microservice.services.CommonServices.buildServiceKey;
/**
* ------------
* Services -> | | -> Methods
......@@ -86,20 +88,20 @@ public class Reactor implements CommonServices.IServiceReactor {
methodKeyList.add(key);
}
public static String buildServiceKey(CommonServices.MethodParams methodParams) {
return Constants.SLASH_SEPERATOR +
methodParams.getServiceType().name() + Constants.TYPE_PREFIX_SEPERATOR +
methodParams.getServiceCommand().toString() + Constants.TYPE_PREFIX_SEPERATOR +
methodParams.getResourceUri();
}
public static String buildServiceKey(Enums.EnumServiceType enumServiceType,
CommonServices.IServiceCommands serviceCommands,
String resourceUri) {
return new StringBuilder(32).append(Constants.SLASH_SEPERATOR).append(enumServiceType.name()).append(Constants.TYPE_PREFIX_SEPERATOR)
.append(serviceCommands.toString()).append(Constants.TYPE_PREFIX_SEPERATOR)
.append(resourceUri).toString();
}
// public static String buildServiceKey(CommonServices.MethodParams methodParams) {
// return Constants.SLASH_SEPERATOR +
// methodParams.getServiceType().name() + Constants.TYPE_PREFIX_SEPERATOR +
// methodParams.getServiceCommand().toString() + Constants.TYPE_PREFIX_SEPERATOR +
// methodParams.getResourceUri();
// }
// public static String buildServiceKey(Enums.EnumServiceType enumServiceType,
// CommonServices.IServiceCommands serviceCommands,
// String resourceUri) {
// return new StringBuilder(32).append(Constants.SLASH_SEPERATOR).append(enumServiceType.name()).append(Constants.TYPE_PREFIX_SEPERATOR)
// .append(serviceCommands.toString()).append(Constants.TYPE_PREFIX_SEPERATOR)
// .append(resourceUri).toString();
// }
/**
* delegating the msg/request from the service to the method if any
......
package microservice.handlers;
import com.eaio.uuid.UUID;
import com.fasterxml.jackson.databind.ObjectMapper;
import common.microservice.io.iface.IContainer;
import common.microservice.io.iface.IPubSub;
import common.microservice.io.iface.IRequest;
import common.microservice.io.iface.IResponse;
import common.microservice.types.BaseRestResponse;
import common.microservice.types.UserProfile;
import common.microservice.utils.IDGenerator;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.ExpiredJwtException;
import io.undertow.server.HttpHandler;
......@@ -10,21 +17,18 @@ import io.undertow.util.HeaderMap;
import io.undertow.util.Headers;
import io.undertow.util.HttpString;
import microservice.MicroserviceApp;
import microservice.common.context.RestContext;
import microservice.common.EncryptionUtils;
import microservice.defs.Constants;
import microservice.defs.Enums.EnumHttpMethod;
import microservice.io.iface.*;
import microservice.io.iface.IMetricsFactory.IMeter;
import microservice.io.iface.IMetricsFactory.ITimer;
import common.microservice.context.RestContext;
import common.microservice.security.EncryptionUtils;
import common.microservice.defs.Constants;
import common.microservice.defs.Enums.EnumHttpMethod;
import common.microservice.io.iface.IMetricsFactory.IMeter;
import common.microservice.io.iface.IMetricsFactory.ITimer;
import microservice.io.impl.IMetricsFactoryImpl;
import microservice.io.impl.IRequestRestImpl;
import microservice.io.impl.IResponseRestImpl;
import microservice.services.CommonServices;
import microservice.types.BaseRestResponse;
import microservice.types.UserProfile;
import common.microservice.services.CommonServices;
import static microservice.defs.Constants.*;
import static common.microservice.defs.Constants.*;
public class RestHandler implements HttpHandler , IContainer
{
......@@ -283,7 +287,7 @@ public class RestHandler implements HttpHandler , IContainer
HeaderMap requestHeaders = exchange.getRequestHeaders();
reqCtx.rcid = requestHeaders.getFirst(Constants.RCID_HEADER);
if (reqCtx.rcid == null) // create a new one
reqCtx.rcid = new UUID().toString();
reqCtx.rcid = IDGenerator.createUUID();
return reqCtx;
}
......
......@@ -2,6 +2,9 @@ package microservice.io.iface;
import java.util.Map;
import common.microservice.io.iface.IPubSub;
import common.microservice.io.iface.IServiceDiscovery;
import microservice.handlers.BaseHandler;
/**
......
......@@ -3,7 +3,7 @@ package microservice.io.impl;
import common.configuration.ConfigProperties;
import common.configuration.EnumPropertyType;
import common.configuration.IConfigurationProvider;
import microservice.io.iface.IConfiguration;
import common.microservice.io.iface.IConfiguration;
public class IConfigurationConfigPropImpl implements IConfiguration {
ConfigProperties instance = null;
......
package microservice.io.impl;
import common.Log4jHandler;
import microservice.io.iface.ILogger;
import common.microservice.io.iface.ILogger;
public class ILogger4jImpl implements ILogger
{
......
......@@ -10,7 +10,7 @@ import metrics_influxdb.InfluxdbReporter;
import metrics_influxdb.api.measurements.CategoriesMetricMeasurementTransformer;
import metrics_influxdb.api.protocols.HttpInfluxdbProtocol;
import microservice.MicroserviceApp;
import microservice.io.iface.IMetricsFactory;
import common.microservice.io.iface.IMetricsFactory;
import java.net.InetSocketAddress;
import java.util.concurrent.TimeUnit;
......@@ -167,7 +167,7 @@ public class IMetricsFactoryImpl implements IMetricsFactory
.convertDurationsTo(TimeUnit.MILLISECONDS)
.filter(MetricFilter.ALL)
.skipIdleMetrics(true)
.tag("service", MicroserviceApp.getsInstance().getAppName())
.tag("microservice/service/microservice/microservice", MicroserviceApp.getsInstance().getAppName())
.tag("instance", MicroserviceApp.getsInstance().getId())
.tag("server", MicroserviceApp.getsInstance().getServerName())
.transformer(new CategoriesMetricMeasurementTransformer("module", "artifact"))
......
......@@ -9,8 +9,8 @@ import org.eclipse.paho.client.mqttv3.MqttPersistenceException;
import com.fasterxml.jackson.databind.JsonNode;
import microservice.io.iface.INotifyCallback;
import microservice.io.iface.IPubSub;
import common.microservice.io.iface.INotifyCallback;
import common.microservice.io.iface.IPubSub;
import common.JsonHandler;
public class IPubSubMQTTImpl implements IPubSub{
......
......@@ -10,8 +10,8 @@ import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisPubSub;
import common.JsonHandler;
import microservice.io.iface.INotifyCallback;
import microservice.io.iface.IPubSub;
import common.microservice.io.iface.INotifyCallback;
import common.microservice.io.iface.IPubSub;
public class IPubSubRedisImpl implements IPubSub
{
......
......@@ -4,14 +4,15 @@ import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsPoller;
import common.microservice.io.iface.ILogger;
import http.simpleRestClient.SimpleRestResponse;
import microservice.defs.Enums;
import microservice.io.iface.IRestClient;
import microservice.io.iface.IServiceDiscovery;
import microservice.params.BaseClientParams;
import microservice.params.CommandParams;
import microservice.params.RMQClientParams;
import microservice.types.BaseRestResponse;
import common.microservice.defs.Enums;
import common.microservice.io.iface.IRestClient;
import common.microservice.io.iface.IServiceDiscovery;
import common.microservice.params.BaseClientParams;
import common.microservice.params.CommandParams;
import common.microservice.params.RMQClientParams;
import common.microservice.types.BaseRestResponse;
import rabbitmq.client.RMQRestClient;
import rabbitmq.common.RMQId;
import rabbitmq.common.RMQMessage;
......@@ -28,6 +29,7 @@ import java.util.function.Consumer;
public class IRMQClientRestImpl implements IRestClient
{
private static int REQUEST_TIMEOUT = 30000;
private ILogger logger;
/*********************************************************************************************/
/* JSON LISTENER
......@@ -112,6 +114,11 @@ public class IRMQClientRestImpl implements IRestClient
}
@Override
public void init(ILogger logger) {
this.logger = logger;
}
@Override
public BaseRestResponse create(CommandParams reqCtx)
{
BaseRestResponse brr = null;
......
......@@ -4,14 +4,13 @@ import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
//import com.sun.xml.internal.ws.api.message.stream.InputStreamMessage;
import rabbitmq.common.RMQMessage;
import rabbitmq.common.RMQRestRequest;
//import io.undertow.server.HttpServerExchange;
import microservice.io.iface.IRequest;
import common.microservice.io.iface.IRequest;
public class IRequestMBIImpl implements IRequest
{
......
......@@ -2,15 +2,13 @@ package microservice.io.impl;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Deque;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import io.undertow.server.HttpServerExchange;
import io.undertow.util.HeaderValues;
import microservice.io.iface.IRequest;
import common.microservice.io.iface.IRequest;
public class IRequestRestImpl implements IRequest
{
......
package microservice.io.impl;
import java.io.IOException;
import java.nio.ByteBuffer;
import microservice.io.iface.ILogger;
import common.microservice.io.iface.ILogger;
import rabbitmq.common.RMQSender;
import rabbitmq.common.RMQId;
import rabbitmq.common.RMQMessage;
import rabbitmq.common.RMQRestResponse;
import microservice.io.iface.IResponse;
import common.microservice.io.iface.IResponse;
public class IResponseMBIImpl implements IResponse
{
......
......@@ -4,7 +4,7 @@ import io.undertow.server.HttpServerExchange;
import java.nio.ByteBuffer;
import microservice.io.iface.IResponse;
import common.microservice.io.iface.IResponse;
public class IResponseRestImpl implements IResponse
{
......
......@@ -10,10 +10,10 @@ import rabbitmq.server.RMQHandler;
import rabbitmq.server.RMQServer;
import microservice.handlers.BaseHandler;
import microservice.handlers.MBIHandler;
import microservice.io.iface.IPubSub;
import common.microservice.io.iface.IPubSub;
import microservice.io.iface.IRestServer;
import microservice.io.iface.IServiceDiscovery;
import microservice.params.RMQClientParams;
import common.microservice.io.iface.IServiceDiscovery;
import common.microservice.params.RMQClientParams;
public class IRestServerRMQImpl implements IRestServer {
......
......@@ -16,10 +16,10 @@ import common.Network;
import microservice.MicroserviceApp;
import microservice.handlers.BaseHandler;
import microservice.handlers.RestHandler;
import microservice.io.iface.IPubSub;
import common.microservice.io.iface.IPubSub;
import microservice.io.iface.IRestServer;
import microservice.io.iface.IServiceDiscovery;
import microservice.params.RestServerParams;
import common.microservice.io.iface.IServiceDiscovery;
import common.microservice.params.RestServerParams;
import static io.undertow.Handlers.resource;
public class IRestServerUndertowImpl implements IRestServer {
......
......@@ -10,8 +10,8 @@ import com.google.common.collect.Lists;
import common.Network;
import common.configuration.IConfigurationProvider;
import microservice.MicroserviceApp;
import microservice.defs.Enums;
import microservice.io.iface.IServiceDiscovery;
import common.microservice.defs.Enums;
import common.microservice.io.iface.IServiceDiscovery;
import java.util.*;
import java.util.Base64.Decoder;
......
package microservice.services;
package microservice.service.pubsub.impl;
import microservice.services.CommonServices;
import microservice.io.iface.IServiceDiscovery;
import common.microservice.io.iface.IApp;
import common.microservice.services.CommonServices;
import common.microservice.io.iface.IServiceDiscovery;
import org.eclipse.paho.client.mqttv3.*;
import java.util.HashMap;
......@@ -46,7 +47,7 @@ public class IPubSubServiceMqttImpl extends CommonServices.IPubSubService {
}
@Override
public boolean init() {
public boolean init(IApp app) {
try {
......
package microservice.services;
package microservice.service.pubsub.impl;
import com.eaio.uuid.UUID;
import com.fasterxml.jackson.databind.ObjectMapper;
import common.Network;
import common.microservice.io.iface.*;
import common.microservice.services.CommonServices;
import common.microservice.types.BaseRestResponse;
import common.microservice.types.UserProfile;
import common.microservice.utils.IDGenerator;
import common.microservice.utils.Logging;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.ExpiredJwtException;
import io.undertow.Handlers;
......@@ -16,37 +22,33 @@ import io.undertow.util.Headers;
import io.undertow.util.HttpString;
import io.undertow.util.MimeMappings;
import microservice.MicroserviceApp;
import microservice.common.context.RestContext;
import microservice.common.EncryptionUtils;
import microservice.defs.Constants;
import microservice.defs.Enums;
import common.microservice.context.RestContext;
import common.microservice.security.EncryptionUtils;
import common.microservice.defs.Constants;
import common.microservice.defs.Enums;
import microservice.handlers.BaseHandler;
import microservice.handlers.Reactor;
import microservice.io.iface.*;
import microservice.io.impl.IRequestRestImpl;
import microservice.io.impl.IResponseRestImpl;
import microservice.params.CommandParams;
import microservice.params.RestServerParams;
import microservice.types.BaseRestResponse;
import microservice.types.UserProfile;
import common.microservice.params.CommandParams;
import common.microservice.params.RestServerParams;
import java.util.function.BooleanSupplier;
import java.util.function.Consumer;
import java.util.function.Supplier;
import static common.microservice.services.CommonServices.buildServiceKey;
import static io.undertow.Handlers.resource;
import static microservice.defs.Constants.*;
import static common.microservice.defs.Constants.*;
/**
* Created by amir on 08/05/17.
*/
public class IRestServiceHttpImpl extends CommonServices.IRestService implements HttpHandler , IContainer{
public class IRestServiceHttpImpl extends CommonServices.IRestService implements HttpHandler , IContainer {
ILogger logger = null;
RestServerParams restServerParams = null;
Undertow restServer = null;
Thread restThread = null;
MicroserviceApp msAppInstance = null;
private String appName;
public ObjectMapper objMapper = null;
protected Enums.EnumAuthenticationType authType = Enums.EnumAuthenticationType.DEFAULT;
......@@ -78,7 +80,7 @@ public class IRestServiceHttpImpl extends CommonServices.IRestService implements
try {
if (restClient != null) {
if (cmdParams.getRcid() != null) // log rcid
msAppInstance.logRcid("handleSyncRespCommand." + name,cmdParams.getRcid());
Logging.logRcid(logger,"handleSyncRespCommand." + name,cmdParams.getRcid());
resp = command.get();
};
} catch (Exception e) {
......@@ -94,7 +96,7 @@ public class IRestServiceHttpImpl extends CommonServices.IRestService implements
boolean retstat;
try {
if (cmdParams.getRcid() != null) // log rcid
msAppInstance.logRcid("handleAsyncRespCommand." + name,cmdParams.getRcid());
Logging.logRcid(logger,"handleAsyncRespCommand." + name,cmdParams.getRcid());
retstat = command.getAsBoolean();
} catch (Exception e) {
retstat = false;
......@@ -143,19 +145,21 @@ public class IRestServiceHttpImpl extends CommonServices.IRestService implements
}
@Override
public boolean init() {
public boolean init(IApp app) {
boolean stat = true;
if (MicroserviceApp.getsInstance() != null) {
msAppInstance = MicroserviceApp.getsInstance();
logger = msAppInstance.getLogger();
this.appName = msAppInstance.getAppName();
this.app = app;
if (app != null) {
logger = app.getLogger();
this.appName = app.getAppName();
}
switch (getServiceMode()){
case E_CLIENT:
initRestClient();
break;
case E_CLIENT_SERVER:
case E_SERVER:
initRestClient();
stat = buildRestServer();
break;
default:
......@@ -166,6 +170,12 @@ public class IRestServiceHttpImpl extends CommonServices.IRestService implements
return stat;
}
private void initRestClient() {
if (restClient != null) {
restClient.init(logger);
}
}
private boolean buildRestServer() {
String host = this.restServerParams.getHost();
if (host == null || Network.LOCALHOST.equals(host))
......@@ -233,7 +243,7 @@ public class IRestServiceHttpImpl extends CommonServices.IRestService implements
/**
* finally we can delegate
*/
String key = Reactor.buildServiceKey(Enums.EnumServiceType.E_REST,reqContext.enumRestCommands,reqContext.request.getRelativePath());
String key = buildServiceKey(Enums.EnumServiceType.E_REST,reqContext.enumRestCommands,reqContext.request.getRelativePath());
reactor.delegate(this, key ,reqContext);
}
} else {
......@@ -285,9 +295,9 @@ public class IRestServiceHttpImpl extends CommonServices.IRestService implements
HeaderMap requestHeaders = exchange.getRequestHeaders();
reqCtx.rcid = requestHeaders.getFirst(Constants.RCID_HEADER);
if (reqCtx.rcid == null) // create a new one
reqCtx.rcid = new UUID().toString();
reqCtx.rcid = IDGenerator.createUUID();
else // log it
msAppInstance.logRcid("getRequestContext",reqCtx.rcid);
Logging.logRcid(logger,"getRequestContext",reqCtx.rcid);
return reqCtx;
}
......@@ -343,7 +353,7 @@ public class IRestServiceHttpImpl extends CommonServices.IRestService implements
apiDocMimeMappingsBuilder.addMapping("yaml", "text/x-yaml");
apiDocMimeMappingsBuilder.addMapping("html", "text/html");
pathHandler.addPrefixPath("/static", resource(new ClassPathResourceManager(getClass().getClassLoader(),getClass().getPackage()))//"static"))
.addWelcomeFiles("microservice/services/metrics.html")
.addWelcomeFiles("microservice/service/pubsub/metrics.html")
.setMimeMappings(apiDocMimeMappingsBuilder.build())
.setDirectoryListingEnabled(false));
}
......@@ -427,7 +437,7 @@ public class IRestServiceHttpImpl extends CommonServices.IRestService implements
}
else
{
msAppInstance.getLogger().error(NO_TOKEN_FOR_REQUEST);
logger.error(NO_TOKEN_FOR_REQUEST);
sendErrorResp(restContext.response,NO_TOKEN_FOR_REQUEST);
valid = false;
}
......
package microservice.services;
package microservice.service.pubsub.impl;
import com.eaio.uuid.UUID;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.flatbuffers.FlatBufferBuilder;
import common.JsonHandler;
import microservice.MicroserviceApp;
import microservice.common.context.CrudMethod;
import microservice.common.context.RestContext;
import microservice.common.context.RestMsg;
import microservice.common.context.RestResponse;
import microservice.defs.Constants;
import microservice.defs.Enums;
import microservice.handlers.Reactor;
import microservice.io.iface.*;
import microservice.io.impl.IMetricsFactoryImpl;
import microservice.params.CommandParams;
import microservice.params.ZMQParams;
import microservice.services.protocol.zmq.RestImpl;
import microservice.types.BaseRestResponse;
import microservice.utils.ICacheClientGuavaImpl;
import common.microservice.io.iface.*;
import common.microservice.services.CommonServices;
import common.microservice.types.BaseRestResponse;
import common.microservice.utils.IDGenerator;
import common.microservice.context.CrudMethod;
import common.microservice.context.RestContext;
import common.microservice.context.RestMsg;
import common.microservice.context.RestResponse;
import common.microservice.defs.Constants;
import common.microservice.defs.Enums;
import common.microservice.params.CommandParams;
import common.microservice.params.ZMQParams;
import microservice.service.pubsub.impl.protocol.zmq.RestImpl;
import common.microservice.utils.ICacheClientGuavaImpl;
import microservice.utils.ZSocketPool;
import org.apache.commons.lang.SerializationUtils;
import org.zeromq.ZMQ;
......@@ -28,15 +26,14 @@ import org.zeromq.ZSocket;
import java.io.Serializable;
import java.nio.ByteBuffer;
import java.util.*;
import java.util.function.BiConsumer;
import java.util.function.BooleanSupplier;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;
import static microservice.defs.Constants.EXIT_MSG;
import static microservice.defs.Constants.EXIT_MSG_LEN;
import static common.microservice.defs.Constants.EXIT_MSG;
import static common.microservice.defs.Constants.EXIT_MSG_LEN;
import static common.microservice.services.CommonServices.buildServiceKey;
/**
* Created by amir on 14/05/17.
......@@ -156,15 +153,15 @@ public class IRestServiceZmqImpl extends CommonServices.IRestService implements
private ILogger logger= null;
private CommonServices.ICacheClient<Long,CacheEntry> responseCacheClient = null;
public ClientWorker(int workerNumber, CommonServices.ICacheClient<Long, CacheEntry> responseCacheClient) {
public ClientWorker(ILogger logger,int workerNumber, CommonServices.ICacheClient<Long, CacheEntry> responseCacheClient) {
this.workerNumber = workerNumber;
this.responseCacheClient = responseCacheClient;
bindAddress = ADDRESS + String.valueOf(workerNumber);
this.logger = logger;
}
@Override
public boolean init() {
logger = MicroserviceApp.getsInstance().getLogger();
pull = new ZSocket(ZMQ.PULL);
return pull.bind(bindAddress);
}
......@@ -289,10 +286,12 @@ public class IRestServiceZmqImpl extends CommonServices.IRestService implements
private ZMQParams.ServerParams zmqParams;
private ZSocket pull = null;
private ZSocket push = null;
private ILogger logger;
private int numOfServerWorkers;
public ServerReceive(ZMQParams.ServerParams zmqParams, int numOfServerWorkers) {
public ServerReceive(ILogger logger,ZMQParams.ServerParams zmqParams, int numOfServerWorkers) {
this.zmqParams = zmqParams;
this.numOfServerWorkers = numOfServerWorkers;
this.logger = logger;
}
......@@ -331,7 +330,7 @@ public class IRestServiceZmqImpl extends CommonServices.IRestService implements
}
});
runThread.start();
MicroserviceApp.getsInstance().getLogger().info("ZMQ server started successfully on host: " + zmqParams.getHost() + ", and port: " + String.valueOf(zmqParams.getPort()));
logger.info("ZMQ server started successfully on host: " + zmqParams.getHost() + ", and port: " + String.valueOf(zmqParams.getPort()));
}
@Override
......@@ -354,15 +353,16 @@ public class IRestServiceZmqImpl extends CommonServices.IRestService implements
private CommonServices.IServiceReactor reactor = null;
private CommonServices.IRestService parentService = null;
public ObjectMapper objMapper = null;
private ILogger logger= null;
private ILogger logger = null;
private ZSocketPool serverSendPool = null;
private int workerNumber = 0;
private String bindAddress = null;
public ServerWorker(CommonServices.IServiceReactor reactor,
public ServerWorker(ILogger logger, CommonServices.IServiceReactor reactor,
CommonServices.IRestService parentService,
ZSocketPool serverSendPool,
int workerNumber) {
this.logger = logger;
this.reactor = reactor;
this.parentService = parentService;
this.serverSendPool = serverSendPool;
......@@ -373,7 +373,6 @@ public class IRestServiceZmqImpl extends CommonServices.IRestService implements
@Override
public boolean init() {
logger = MicroserviceApp.getsInstance().getLogger();
pull = new ZSocket(ZMQ.PULL);
objMapper = new ObjectMapper();
return pull.bind(bindAddress);
......@@ -395,7 +394,7 @@ public class IRestServiceZmqImpl extends CommonServices.IRestService implements
if (receiveMsg != null){
RestContext restContext = getRequestContext(receiveMsg);
if (restContext != null){
String key = Reactor.buildServiceKey(Enums.EnumServiceType.E_REST,
String key = buildServiceKey(Enums.EnumServiceType.E_REST,
restContext.enumRestCommands,
restContext.request.getRelativePath());
reactor.delegate(parentService, key ,restContext);
......@@ -460,7 +459,7 @@ public class IRestServiceZmqImpl extends CommonServices.IRestService implements
*/
reqCtx.rcid = String.valueOf(receiveMsg.rcid());
if (reqCtx.rcid == null) // create a new one
reqCtx.rcid = new UUID().toString();
reqCtx.rcid = IDGenerator.createUUID();
return reqCtx;
......@@ -549,9 +548,12 @@ public class IRestServiceZmqImpl extends CommonServices.IRestService implements
ByteBuffer respBB = ByteBuffer.allocate(CAPACITY);
FlatBufferBuilder respBuilder = new FlatBufferBuilder();
public ServerReply(ILogger logger) {
this.logger = logger;
}
@Override
public boolean init() {
logger = MicroserviceApp.getsInstance().getLogger();
pull = new ZSocket(ZMQ.PULL);
return pull.bind(ADDRESS);
}
......@@ -689,7 +691,7 @@ public class IRestServiceZmqImpl extends CommonServices.IRestService implements
final ByteBuffer existing_bb = clientSendByteBuffer.get();
FlatBufferBuilder requestBuilder = new FlatBufferBuilder(existing_bb);
long rcid = new UUID().getTime();
long rcid = IDGenerator.createUUIDLong();
String url = buildUrl(cmdParams);
final String content = cmdParams.getContent() != null ? cmdParams.getContent() : "";
int reqSize = RestMsg.createRestMsg(requestBuilder,
......@@ -824,11 +826,12 @@ public class IRestServiceZmqImpl extends CommonServices.IRestService implements
}
@Override
public boolean init() {
public boolean init(IApp app) {
boolean retstat = true;
if (MicroserviceApp.getsInstance() != null) {
this.appName = MicroserviceApp.getsInstance().getAppName();
this.logger = MicroserviceApp.getsInstance().getLogger();
this.app = app;
if (app != null) {
this.appName = app.getAppName();
this.logger = app.getLogger();
}
switch (getServiceMode()){
......@@ -855,24 +858,24 @@ public class IRestServiceZmqImpl extends CommonServices.IRestService implements
public boolean allocateServer() {
allWorkersList.add(new ServerReply());
allWorkersList.add(new ServerReply(logger));
/**
* init server send pool
*/
serverSendPool = ZSocketPool.buildPool(ServerReply.ADDRESS,ZMQ.PUSH,numOfServerWorkers);
for (int i = 0; i < numOfServerWorkers; i++){
allWorkersList.add(new ServerWorker(reactor,this,serverSendPool,i));
allWorkersList.add(new ServerWorker(logger,reactor,this,serverSendPool,i));
}
// must be after the workers
allWorkersList.add(new ServerReceive(serverParams,numOfServerWorkers));
allWorkersList.add(new ServerReceive(logger,serverParams,numOfServerWorkers));
return serverSendPool != null;
}
public boolean allocateClient() {
responseCacheClient = new ICacheClientGuavaImpl<>(EXPIRES_MILLI_SECONDS);
for (int i = 0; i < numOfClientWorkers; i++){
allWorkersList.add(new ClientWorker(i,responseCacheClient));
allWorkersList.add(new ClientWorker(logger,i,responseCacheClient));
}
// must be after workers
allWorkersList.add(new ClientReceive(clientReceiveParams,numOfClientWorkers));
......
package microservice.services.protocol.zmq;
package microservice.service.pubsub.impl.protocol.zmq;
import com.eaio.uuid.UUID;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.flatbuffers.FlatBufferBuilder;
import microservice.MicroserviceApp;
import microservice.common.context.ParamValue;
import microservice.common.context.QueueMsg;
import microservice.defs.Constants;
import microservice.defs.Enums;
import microservice.handlers.Reactor;
import microservice.io.iface.ILogger;
import microservice.io.iface.IServiceDiscovery;
import microservice.params.ZMQParams;
import microservice.services.CommonServices;
import common.microservice.io.iface.IApp;
import common.microservice.utils.IDGenerator;
import common.microservice.context.ParamValue;
import common.microservice.context.QueueMsg;
import common.microservice.defs.Constants;
import common.microservice.defs.Enums;
import common.microservice.io.iface.ILogger;
import common.microservice.io.iface.IServiceDiscovery;
import common.microservice.params.ZMQParams;
import common.microservice.services.CommonServices;
import microservice.utils.ZSocketPool;
import org.zeromq.ZMQ;
import org.zeromq.ZSocket;
......@@ -23,8 +22,8 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import static common.microservice.services.CommonServices.buildServiceKey;
/**
* MsgQueue implementation in ZMQ
......@@ -58,7 +57,7 @@ public class IMsgQServiceZmqImpl extends CommonServices.IMsgQService {
QueueMsg queueMsg = QueueMsg.getRootAsQueueMsg(bb);
if (queueMsg != null){
MsgQContext msgQContext = getMsgQContext(queueMsg);
String key = Reactor.buildServiceKey(Enums.EnumServiceType.E_MSGQ,
String key = buildServiceKey(Enums.EnumServiceType.E_MSGQ,
CommonServices.EnumMsgQueueCommands.E_QUEUE,
msgQContext.topic);
reactor.delegate(parentService, key ,msgQContext);
......@@ -84,11 +83,12 @@ public class IMsgQServiceZmqImpl extends CommonServices.IMsgQService {
@Override
public boolean init() {
public boolean init(IApp app) {
boolean retstat = true;
if (MicroserviceApp.getsInstance() != null) {
this.appName = MicroserviceApp.getsInstance().getAppName();
this.logger = MicroserviceApp.getsInstance().getLogger();
this.app = app;
if (app != null) {
this.appName = app.getAppName();
this.logger = app.getLogger();
}
if (getServiceMode() != null) {
......@@ -206,9 +206,9 @@ public class IMsgQServiceZmqImpl extends CommonServices.IMsgQService {
*/
if (msgCtx.parameters == null) {
msgCtx.parameters = new HashMap<>();
msgCtx.parameters.put(Constants.MSGQ_ID, new UUID().toString());
msgCtx.parameters.put(Constants.MSGQ_ID, IDGenerator.createUUID());
} else
msgCtx.parameters.putIfAbsent(Constants.MSGQ_ID, new UUID().toString());
msgCtx.parameters.putIfAbsent(Constants.MSGQ_ID, IDGenerator.createUUID());
int topicOffset = msgBuilder.createString(msgCtx.topic);
int msgOffset = msgBuilder.createString(msgCtx.msg);
......
package microservice.services.protocol.zmq;
package microservice.service.pubsub.impl.protocol.zmq;
import microservice.common.context.RestMsg;
import microservice.io.iface.IRequest;
import microservice.io.iface.IResponse;
import microservice.services.IRestServiceZmqImpl;
import common.microservice.context.RestMsg;
import common.microservice.io.iface.IRequest;
import common.microservice.io.iface.IResponse;
import microservice.service.pubsub.impl.IRestServiceZmqImpl;
import microservice.utils.ZSocketPool;
import org.apache.commons.lang.SerializationUtils;
import org.zeromq.ZMQ;
......
package microservice.services.protocol.zmq;
package microservice.service.pubsub.impl.protocol.zmq;
import com.eaio.uuid.UUID;
import com.fasterxml.jackson.databind.ObjectMapper;
import common.microservice.utils.IDGenerator;
import microservice.MicroserviceApp;
import microservice.common.context.RestContext;
import microservice.common.context.RestMsg;
import microservice.io.iface.ILogger;
import microservice.params.ZMQParams;
import microservice.services.CommonServices;
import common.microservice.context.RestContext;
import common.microservice.io.iface.ILogger;
import common.microservice.params.ZMQParams;
import common.microservice.services.CommonServices;
import org.zeromq.ZMQ;
import org.zeromq.ZSocket;
import java.nio.ByteBuffer;
import java.util.List;
import static microservice.defs.Constants.EXIT_MSG;
import static microservice.defs.Constants.EXIT_MSG_LEN;
import static common.microservice.defs.Constants.EXIT_MSG;
import static common.microservice.defs.Constants.EXIT_MSG_LEN;
/**
* all workers common to rest amd msgQ
......@@ -108,7 +107,7 @@ public class ZmqWorkers {
this.parentService = parentService;
this.workerNumber = workerNumber;
bindAddress = ADDRESS + '/' +
new UUID().toString() + '/' +
IDGenerator.createUUID() + '/' +
String.valueOf(workerNumber);
}
......
package microservice.utils;
import microservice.services.CommonServices;
import microservice.io.iface.IRestClient;
import microservice.io.impl.IRestClientHttpImpl;
import microservice.services.IRestServiceHttpImpl;
import microservice.services.IRestServiceZmqImpl;
import microservice.params.RestClientParams;
import microservice.params.RestServerParams;
import microservice.params.ZMQParams;
import microservice.services.protocol.zmq.IMsgQServiceZmqImpl;
import clients.microservice.impl.IRestClientHttpImpl;
import common.microservice.services.CommonServices;
import common.microservice.io.iface.IRestClient;
import common.microservice.params.PulsarParams;
import microservice.service.pubsub.impl.IRestServiceHttpImpl;
import microservice.service.pubsub.impl.IRestServiceZmqImpl;
import common.microservice.params.RestClientParams;
import common.microservice.params.RestServerParams;
import common.microservice.params.ZMQParams;
import microservice.service.pubsub.impl.protocol.zmq.IMsgQServiceZmqImpl;
import service.microservice.IPubSubServicePulsarImpl;
/**
* Created by amir on 09/05/17.
......@@ -16,6 +18,9 @@ import microservice.services.protocol.zmq.IMsgQServiceZmqImpl;
public class ServiceBuilderFactory {
public static final String FAILED_IN_VALIDATING_PARAMS = " >> Failed in validating params";
public static final String EXCEPTION = "Exception >> ";
public static RestServiceHttpBuilder createRestServiceHttpBuilder(CommonServices.EnumRestServiceMode serviceMode){
return new RestServiceHttpBuilder(serviceMode);
}
......@@ -28,6 +33,10 @@ public class ServiceBuilderFactory {
return new MsgQServiceZmqBuilder(serviceMode);
}
public static PubSubServicePulsarBuilder createPubSubServicePulsarBuilder(CommonServices.EnumPubSubServiceMode serviceMode){
return new PubSubServicePulsarBuilder(serviceMode);
}
public interface IBuilder {
CommonServices.IService build();
}
......@@ -89,11 +98,11 @@ public class ServiceBuilderFactory {
break;
}
} catch (Exception exp){
System.err.println(this.getClass().getName().toString() + "Exception >> " + exp);
System.err.println(this.getClass().getName().toString() + EXCEPTION + exp);
restServiceHttp = null;
}
} else {
System.err.println(this.getClass().getName().toString() + " >> Failed in validating params");
System.err.println(this.getClass().getName().toString() + FAILED_IN_VALIDATING_PARAMS);
}
return restServiceHttp;
}
......@@ -203,11 +212,11 @@ public class ServiceBuilderFactory {
break;
}
} catch (Exception exp){
System.err.println(this.getClass().getName().toString() + "Exception >> " + exp);
System.err.println(this.getClass().getName().toString() + EXCEPTION + exp);
restServiceZmq = null;
}
} else {
System.err.println(this.getClass().getName().toString() + " >> Failed in validating params");
System.err.println(this.getClass().getName().toString() + FAILED_IN_VALIDATING_PARAMS);
}
return restServiceZmq;
}
......@@ -297,11 +306,11 @@ public class ServiceBuilderFactory {
break;
}
} catch (Exception exp){
System.err.println(this.getClass().getName().toString() + "Exception >> " + exp);
System.err.println(this.getClass().getName().toString() + EXCEPTION + exp);
msgQServiceZmq = null;
}
} else {
System.err.println(this.getClass().getName().toString() + " >> Failed in validating params");
System.err.println(this.getClass().getName().toString() + FAILED_IN_VALIDATING_PARAMS);
}
return msgQServiceZmq;
}
......@@ -317,4 +326,89 @@ public class ServiceBuilderFactory {
}
}
public static class PubSubServicePulsarBuilder implements IBuilder {
IPubSubServicePulsarImpl pubSubServicePulsar = null;
PulsarParams.PulsarParamsBuilder pulsarParamsBuilder = new PulsarParams.PulsarParamsBuilder();
CommonServices.EnumPubSubServiceMode serviceMode = CommonServices.EnumPubSubServiceMode.E_BOTH;
public PubSubServicePulsarBuilder(CommonServices.EnumPubSubServiceMode serviceMode) {
this.serviceMode = serviceMode;
}
public PubSubServicePulsarBuilder setConsumerPoolSize(int consumerPoolSize) {
this.pulsarParamsBuilder.setConsumersThreadPoolSize(consumerPoolSize);
return this;
}
public PubSubServicePulsarBuilder setServiceUrl(String serviceUrl) {
this.pulsarParamsBuilder.setServiceUrl(serviceUrl);
return this;
}
public PubSubServicePulsarBuilder setAdminUrl(String adminUrl) {
this.pulsarParamsBuilder.setAdminUrl(adminUrl);
return this;
}
public PubSubServicePulsarBuilder setClusters(String clusters) {
this.pulsarParamsBuilder.setClusters(clusters);
return this;
}
public PubSubServicePulsarBuilder setMsgTtl(int msgTtl) {
this.pulsarParamsBuilder.setMsgTtl(msgTtl);
return this;
}
public PubSubServicePulsarBuilder setRetentionTimeMin(int retentionTimeMin) {
this.pulsarParamsBuilder.setRetentionTimeMin(retentionTimeMin);
return this;
}
public PubSubServicePulsarBuilder setRetentionSizeMB(int retentionSizeMB) {
this.pulsarParamsBuilder.setRetentionSizeMB(retentionSizeMB);
return this;
}
/**
* msgs published to all services has 'public' namespace
* @param createPublicNamespace
* @return
*/
public PubSubServicePulsarBuilder setCreatePublicNamespace(boolean createPublicNamespace) {
this.pulsarParamsBuilder.setCreatePublicNamespace(createPublicNamespace);
return this;
}
private boolean validateParams() {
if(pulsarParamsBuilder.getServiceUrl() == null)
return false;
return true;
}
@Override
public CommonServices.IService build() {
if (validateParams()) {
try {
PulsarParams pulsarParams = pulsarParamsBuilder.build();
if (pulsarParams != null){
pubSubServicePulsar = new IPubSubServicePulsarImpl(pulsarParams);
pubSubServicePulsar.setServiceMode(serviceMode);
}
} catch (Exception exp){
System.err.println(this.getClass().getName().toString() + EXCEPTION + exp);
pubSubServicePulsar = null;
}
} else {
System.err.println(this.getClass().getName().toString() + FAILED_IN_VALIDATING_PARAMS);
}
return pubSubServicePulsar;
}
}
}
package microservice.io.impl;
package clients.microservice.impl;
import clients.microservice.utils.RestHttpClient;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.netflix.hystrix.HystrixCommand;
import com.netflix.hystrix.HystrixCommandGroupKey;
import com.netflix.hystrix.HystrixObservableCommand;
import com.netflix.hystrix.contrib.codahalemetricspublisher.HystrixCodaHaleMetricsPublisher;
import com.netflix.hystrix.strategy.HystrixPlugins;
import com.netflix.hystrix.strategy.metrics.HystrixMetricsPublisher;
import microservice.MicroserviceApp;
import microservice.defs.Constants;
import microservice.defs.Enums;
import microservice.io.iface.IRestClient;
import microservice.io.iface.IServiceDiscovery;
import microservice.params.BaseClientParams;
import microservice.params.CommandParams;
import microservice.params.RestClientParams;
import microservice.types.BaseRestResponse;
import microservice.utils.RestHttpClient;
import common.microservice.defs.Constants;
import common.microservice.defs.Enums;
import common.microservice.io.iface.ILogger;
import common.microservice.io.iface.IRestClient;
import common.microservice.io.iface.IServiceDiscovery;
import common.microservice.params.BaseClientParams;
import common.microservice.params.CommandParams;
import common.microservice.params.RestClientParams;
import common.microservice.types.BaseRestResponse;
import rx.Observable;
import rx.Subscriber;
import java.util.Optional;
import java.util.function.Consumer;
import static common.microservice.utils.Logging.logRcid;
public class IRestClientHttpImpl implements IRestClient
{
public static final String HYSTRIX_PLUGIN_HYSTRIX_METRICS_PUBLISHER_IMPLEMENTATION = "hystrix.plugin.HystrixMetricsPublisher.implementation";
private static final int POLLING_DELAY = 500;
private ILogger logger = null;
/*************************************************************************
* COMMANDS
......@@ -47,9 +46,6 @@ public class IRestClientHttpImpl implements IRestClient
@Override
protected Observable<BaseRestResponse> construct() {
// return httpClient.submit(HttpClientRequest.createGet("/mock.json?numItems=" + numItems))
// .flatMap((HttpClientResponse<ByteBuf> r) -> r.getContent()
// .map(b -> BackendResponse.fromJson(new ByteBufInputStream(b))));
return Observable.just(new BaseRestResponse(true,null));
}
......@@ -59,41 +55,6 @@ public class IRestClientHttpImpl implements IRestClient
}
}
private class GetObservableCommand extends HystrixObservableCommand<BaseRestResponse> {
CommandParams reqCtx = null;
public GetObservableCommand(CommandParams reqCtx) {
super(HystrixCommandGroupKey.Factory.asKey("RestClientRestImpl.GetCommand." + reqCtx.getEntity()));
this.reqCtx = reqCtx;
}
@Override
protected Observable<BaseRestResponse> construct()
{
return Observable.create(new Observable.OnSubscribe<BaseRestResponse>() {
@Override
public void call(Subscriber<? super BaseRestResponse> observer)
{
BaseRestResponse brr = null;
try {
if (!observer.isUnsubscribed()) {
if (reqCtx.getParams() != null)
brr = httpRestClient._get(reqCtx.getEntity(), reqCtx.getParams(), reqCtx.getRequestParams());
else
brr = httpRestClient._get(reqCtx.getEntity(), reqCtx.getParamsString(),reqCtx.getRequestParams());
observer.onNext(brr);
observer.onCompleted();
}
} catch (Exception e) {
observer.onError(e);
}
}
} );
}
}
private abstract class Command extends HystrixCommand<BaseRestResponse> {
protected CommandParams reqCtx = null;
......@@ -172,49 +133,10 @@ public class IRestClientHttpImpl implements IRestClient
}
}
/*********************************************************************************************/
/* JSON LISTENER
*******************************************************************************************/
// /**
// * This will be called from another thread so needs to be thread-safe.
// * @ThreadSafe
// */
// private static class MetricJsonListener implements HystrixMetricsPoller.MetricsAsJsonPollerListener {
//
// /**
// * Setting limit to 1000. In a healthy system there isn't any reason to hit this limit so if we do it will throw an exception which causes the poller to stop.
// * <p>
// * This is a safety check against a runaway poller causing memory leaks.
// */
// private final LinkedBlockingQueue<String> jsonMetrics = new LinkedBlockingQueue<String>(1000);
//
// /**
// * Store JSON messages in a queue.
// */
// @Override
// public void handleJsonMetric(String json) {
// jsonMetrics.add(json);
// }
//
// /**
// * Get all JSON messages in the queue.
// *
// * @return
// */
// public List<String> getJsonMetrics() {
// ArrayList<String> metrics = new ArrayList<String>();
// jsonMetrics.drainTo(metrics);
// return metrics;
// }
// }
//
/*********************************************************************************************************************************************/
private static final String COMMAND_ERROR = "Command Error: ";
RestClientParams clientParams = null;
RestHttpClient httpRestClient = null;
// HystrixMetricsPoller poller = null;
// MetricJsonListener jsonListener = null;
Optional<IServiceDiscovery> serviceDiscovery = Optional.empty();
private final ObjectMapper objMapper = new ObjectMapper();
......@@ -237,29 +159,11 @@ public class IRestClientHttpImpl implements IRestClient
if (clientParams.isMetricsEnabled())
{
initMetricsPublisher();
// jsonListener = new MetricJsonListener();
// poller = new HystrixMetricsPoller(jsonListener, POLLING_DELAY);
// poller.start();
}
// initMetricsPublisher();
}
private synchronized void initMetricsPublisher() {
String publisherStr = System.getProperty(HYSTRIX_PLUGIN_HYSTRIX_METRICS_PUBLISHER_IMPLEMENTATION);
if(publisherStr == null) {
try {
IMetricsFactoryImpl factoryImpl = (IMetricsFactoryImpl) IMetricsFactoryImpl.getInstance();
HystrixMetricsPublisher publisher = new HystrixCodaHaleMetricsPublisher(factoryImpl.getMetrics());
HystrixPlugins.getInstance().registerMetricsPublisher(publisher);
System.setProperty(HYSTRIX_PLUGIN_HYSTRIX_METRICS_PUBLISHER_IMPLEMENTATION, publisher.getClass().getName());
}
catch (IllegalStateException ise){
}
}
}
public IRestClientHttpImpl withServiceDiscovery(IServiceDiscovery servDisco)
{
......@@ -270,54 +174,19 @@ public class IRestClientHttpImpl implements IRestClient
@Override
protected void finalize() throws Throwable
{
// if (poller != null)
// poller.shutdown();
if (httpRestClient != null)
httpRestClient.close();
//httpRestClient.shutdown();
super.finalize();
}
// private BaseRestResponse getBaseRestResponse(SimpleRestResponse srr)
// {
// BaseRestResponse brr;
// if (srr != null)
// {
// brr = new BaseRestResponse(srr.isSuccess(), srr.getError());
// brr.objectNode = srr.objectNode;
// }
// else
// brr = new BaseRestResponse(false, COMMAND_ERROR + "null response");
// return brr;
// }
//
// private BaseRestResponse getBaseRestResponse(StringResponse srr)
// {
// BaseRestResponse brr = null;
// if (srr != null)
// {
// if (srr.getStatusCode() == 200){
// final JsonNode jsonNode = JsonHandler.getJsonNodeFromString(srr.getContent());
// /**
// * check for BaseRestResponse format of regular json
// */
// final boolean allMatch = brrFieldNames.stream().allMatch(name -> jsonNode.has(name));
// if (allMatch) {
// brr = (BaseRestResponse)JsonHandler.getNodeAsObject(jsonNode,BaseRestResponse.class);
// } else {
// brr = new BaseRestResponse(true,null);
// brr.objectNode = jsonNode;
// }
// } else {
// brr = new BaseRestResponse(false,COMMAND_ERROR + "Error response: " + String.valueOf(srr.getStatusCode()));
// }
// }
// else
// brr = new BaseRestResponse(false,COMMAND_ERROR + "null response");
// return brr;
// }
@Override
public void init(ILogger logger) {
this.logger = logger;
httpRestClient.setLogger(logger);
}
@Override
public BaseRestResponse create(CommandParams reqCtx)
......@@ -413,19 +282,16 @@ public class IRestClientHttpImpl implements IRestClient
if (cbFunc != null) {
try {
if (reqCtx.getRcid() != null)
MicroserviceApp.getsInstance().logRcid("asyncCreate",reqCtx.getRcid());
logRcid(logger,"asyncCreate",reqCtx.getRcid());
resolveService(reqCtx);
retstat = httpRestClient.executeAsync(Enums.EnumHttpMethod.E_POST.getStrMethod(),reqCtx, brr -> cbFunc.accept(brr));
// new PostCommand(reqCtx).toObservable().subscribe(
// (brr) -> cbFunc.accept(brr),
// (err) -> cbFunc.accept(new BaseRestResponse(false,err.toString()))); // create error BaseRestResponse
} catch (Exception e) {
MicroserviceApp.getsInstance().getLogger().error(this.getClass().getName() + " >> Exception in asyncCreate: " + e.toString());
logger.error(this.getClass().getName() + " >> Exception in asyncCreate: " + e.toString());
retstat = false;
}
} else {
MicroserviceApp.getsInstance().getLogger().error(this.getClass().getName() + " >> null cbFunc in asyncCreate");
logger.error(this.getClass().getName() + " >> null cbFunc in asyncCreate");
retstat = false;
}
return retstat;
......@@ -439,15 +305,12 @@ public class IRestClientHttpImpl implements IRestClient
resolveService();
retstat = httpRestClient.executeAsync(Enums.EnumHttpMethod.E_GET.getStrMethod(),reqCtx, brr -> cbFunc.accept(brr));
// new GetCommand(reqCtx).toObservable().subscribe(
// (brr) -> cbFunc.accept(brr),
// (err) -> cbFunc.accept(new BaseRestResponse(false,err.toString()))); // create error BaseRestResponse
} catch (Exception e) {
MicroserviceApp.getsInstance().getLogger().error(this.getClass().getName() + " >> Exception in asyncRead: " + e.toString());
logger.error(this.getClass().getName() + " >> Exception in asyncRead: " + e.toString());
retstat = false;
}
} else {
MicroserviceApp.getsInstance().getLogger().error(this.getClass().getName() + " >> null cbFunc in asyncRead");
logger.error(this.getClass().getName() + " >> null cbFunc in asyncRead");
retstat = false;
}
return retstat;
......@@ -459,19 +322,16 @@ public class IRestClientHttpImpl implements IRestClient
if (cbFunc != null) {
try {
if (reqCtx.getRcid() != null)
MicroserviceApp.getsInstance().logRcid("asyncUpdate",reqCtx.getRcid());
logRcid(logger,"asyncUpdate",reqCtx.getRcid());
resolveService();
retstat = httpRestClient.executeAsync(Enums.EnumHttpMethod.E_PUT.getStrMethod(),reqCtx, brr -> cbFunc.accept(brr));
// new PutCommand(reqCtx).toObservable().subscribe(
// (brr) -> cbFunc.accept(brr),
// (err) -> cbFunc.accept(new BaseRestResponse(false,err.toString()))); // create error BaseRestResponse
} catch (Exception e) {
MicroserviceApp.getsInstance().getLogger().error(this.getClass().getName() + " >> Exception in asyncUpdate: " + e.toString());
logger.error(this.getClass().getName() + " >> Exception in asyncUpdate: " + e.toString());
retstat = false;
}
} else {
MicroserviceApp.getsInstance().getLogger().error(this.getClass().getName() + " >> null cbFunc in asyncUpdate");
logger.error(this.getClass().getName() + " >> null cbFunc in asyncUpdate");
retstat = false;
}
return retstat;
......@@ -483,19 +343,16 @@ public class IRestClientHttpImpl implements IRestClient
if (cbFunc != null) {
try {
if (reqCtx.getRcid() != null)
MicroserviceApp.getsInstance().logRcid("asyncDelete",reqCtx.getRcid());
logRcid(logger,"asyncDelete",reqCtx.getRcid());
resolveService();
retstat = httpRestClient.executeAsync(Enums.EnumHttpMethod.E_DELETE.getStrMethod(),reqCtx, brr -> cbFunc.accept(brr));
// new DeleteCommand(reqCtx).toObservable().subscribe(
// (brr) -> cbFunc.accept(brr),
// (err) -> cbFunc.accept(new BaseRestResponse(false,err.toString()))); // create error BaseRestResponse
} catch (Exception e) {
MicroserviceApp.getsInstance().getLogger().error(this.getClass().getName() + " >> Exception in asyncDelete: " + e.toString());
logger.error(this.getClass().getName() + " >> Exception in asyncDelete: " + e.toString());
retstat = false;
}
} else {
MicroserviceApp.getsInstance().getLogger().error(this.getClass().getName() + " >> null cbFunc in asyncDelete");
logger.error(this.getClass().getName() + " >> null cbFunc in asyncDelete");
retstat = false;
}
return retstat;
......@@ -506,22 +363,6 @@ public class IRestClientHttpImpl implements IRestClient
{
ArrayNode arrayNode = objMapper.createArrayNode();
// if (poller != null && poller.isRunning())
// {
// List<String> jsonMessages = jsonListener.getJsonMetrics();
// if (!jsonMessages.isEmpty())
// {
// for (String jsonString : jsonMessages)
// {
// try
// {
// arrayNode.add(objMapper.readTree(jsonString));
// } catch (Exception e)
// {
// }
// }
// }
// }
return arrayNode;
}
......
package microservice.utils;
package clients.microservice.utils;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import common.JsonHandler;
import common.microservice.defs.Constants;
import common.microservice.io.iface.ILogger;
import common.microservice.io.iface.IRestClient;
import common.microservice.params.CommandParams;
import common.microservice.types.BaseRestResponse;
import http.StringResponse;
import http.simpleHttpClient.SimpleHttpRequest;
import http.simpleHttpClient.SimpleHttpResponse;
import http.simpleRestClient.SimpleRestClient;
import http.simpleRestClient.SimpleRestResponse;
import microservice.MicroserviceApp;
import microservice.defs.Constants;
import microservice.params.CommandParams;
import microservice.types.BaseRestResponse;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import org.apache.commons.codec.Charsets;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpHead;
import org.apache.http.client.methods.HttpOptions;
import org.apache.http.client.methods.HttpPatch;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.client.methods.HttpTrace;
import okhttp3.*;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.util.EntityUtils;
import java.io.Closeable;
import java.io.IOException;
......@@ -45,13 +20,9 @@ import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
import java.util.function.Supplier;
import static microservice.io.iface.IRestClient.COMMAND_ERROR;
/**
* Created by amir on 10/05/17.
......@@ -92,6 +63,7 @@ public class RestHttpClient extends SimpleRestClient implements Closeable {
.writeTimeout(10, TimeUnit.SECONDS)
.readTimeout(HTTP_SOCKET_READ_TIMEOUT, TimeUnit.SECONDS)
.build();
initBaseRestValidation();
}
......@@ -103,17 +75,17 @@ public class RestHttpClient extends SimpleRestClient implements Closeable {
}
}
private BaseRestResponse executeRequest(String entity, Supplier<BaseRestResponse> executeFunc){
BaseRestResponse ret;
if (domain == null) {
domain = entity;
entity = null;
ret = executeFunc.get();
domain = null;
} else
ret = executeFunc.get();
return ret;
}
// private BaseRestResponse executeRequest(String entity, Supplier<BaseRestResponse> executeFunc){
// BaseRestResponse ret;
// if (domain == null) {
// domain = entity;
// entity = null;
// ret = executeFunc.get();
// domain = null;
// } else
// ret = executeFunc.get();
// return ret;
// }
public BaseRestResponse _get(String entity, String[] params, String requestParams) {
String paramPath = "";
......@@ -147,121 +119,114 @@ public class RestHttpClient extends SimpleRestClient implements Closeable {
}
public BaseRestResponse _post(String entity, String[] params, String requestParams, String content) {
String paramPath = "";
if(params == null) {
return this._post(entity, (String[])null, requestParams, content);
} else {
paramPath = getUrlPath(params, paramPath);
return this._post(entity, paramPath, requestParams, content);
}
}
public BaseRestResponse _post(String entity, String params, String requestParams, String content) {
String resp;
if (domain == null) {
domain = entity;
entity = null;
resp = super.postAsString(entity, params, requestParams,content);
domain = null;
} else
resp = super.postAsString(entity, params, requestParams,content);
return getBaseRestResponse(resp);
}
public BaseRestResponse _put(String entity, String[] params, String requestParams, String content) {
String paramPath = "";
if(params == null) {
return this._put(entity, (String[])null, requestParams, content);
} else {
paramPath = getUrlPath(params, paramPath);
return this._put(entity, paramPath, requestParams, content);
}
}
// public BaseRestResponse _post(String entity, String[] params, String requestParams, String content) {
// String paramPath = "";
// if(params == null) {
// return this._post(entity, (String[])null, requestParams, content);
// } else {
// paramPath = getUrlPath(params, paramPath);
// return this._post(entity, paramPath, requestParams, content);
// }
// }
public BaseRestResponse _put(final String entity, String params, String requestParams, String content) {
BaseRestResponse brr = executeRequest(entity,() -> executePut(entity, params, requestParams, content));
// public BaseRestResponse _post(String entity, String params, String requestParams, String content) {
// String resp;
// if (domain == null) {
// domain = entity;
// entity = null;
// brr = executePut(entity, params, requestParams, content);
// resp = super.postAsString(entity, params, requestParams,content);
// domain = null;
// } else
// brr = executePut(entity, params, requestParams, content);
return brr;
}
// resp = super.postAsString(entity, params, requestParams,content);
// return getBaseRestResponse(resp);
// }
public BaseRestResponse executePut(String entity, String params, String requestParams, String content) {
BaseRestResponse brr;
SimpleHttpRequest request = this.buildPutRequest(entity, params, requestParams, content);
try {
SimpleHttpResponse response = this.processRequest(request);
brr = getBaseRestResponse(response);
} catch (Exception exp) {
brr = new BaseRestResponse(false, exp.toString());
}
return brr;
}
// public BaseRestResponse _put(String entity, String[] params, String requestParams, String content) {
// String paramPath = "";
// if(params == null) {
// return this._put(entity, (String[])null, requestParams, content);
// } else {
// paramPath = getUrlPath(params, paramPath);
// return this._put(entity, paramPath, requestParams, content);
// }
// }
public BaseRestResponse _delete(String entity, String[] params, String requestParams) {
String paramPath = "";
if(params == null) {
return this._delete(entity, (String)null, requestParams);
} else {
paramPath = getUrlPath(params, paramPath);
// public BaseRestResponse _put(final String entity, String params, String requestParams, String content) {
// BaseRestResponse brr = executeRequest(entity,() -> executePut(entity, params, requestParams, content));
//
// return brr;
// }
return this._delete(entity, paramPath, requestParams);
}
}
// public BaseRestResponse executePut(String entity, String params, String requestParams, String content) {
// BaseRestResponse brr;
// SimpleHttpRequest request = this.buildPutRequest(entity, params, requestParams, content);
// try {
// SimpleHttpResponse response = this.processRequest(request);
// brr = getBaseRestResponse(response);
// } catch (Exception exp) {
// brr = new BaseRestResponse(false, exp.toString());
// }
// return brr;
// }
public BaseRestResponse _delete(final String entity, String params, String requestParams) {
BaseRestResponse brr = executeRequest(entity,() -> executeDelete(entity, params, requestParams));
// if (domain == null) {
// domain = entity;
// entity = null;
// brr = executeDelete(entity, params, requestParams);
// domain = null;
// } else
// brr = executeDelete(entity, params, requestParams);
// public BaseRestResponse _delete(String entity, String[] params, String requestParams) {
// String paramPath = "";
// if(params == null) {
// return this._delete(entity, (String)null, requestParams);
// } else {
// paramPath = getUrlPath(params, paramPath);
//
return brr;
}
// return this._delete(entity, paramPath, requestParams);
// }
// }
private BaseRestResponse executeDelete(String entity, String params, String requestParams) {
BaseRestResponse brr;
SimpleHttpRequest request = this.buildDeleteRequest(entity, params, requestParams);
try {
SimpleHttpResponse response = this.processRequest(request);
brr = getBaseRestResponse(response);
} catch (Exception exp) {
brr = new BaseRestResponse(false, exp.toString());
}
return brr;
}
// public BaseRestResponse _delete(final String entity, String params, String requestParams) {
// BaseRestResponse brr = executeRequest(entity,() -> executeDelete(entity, params, requestParams));
//// if (domain == null) {
//// domain = entity;
//// entity = null;
//// brr = executeDelete(entity, params, requestParams);
//// domain = null;
//// } else
//// brr = executeDelete(entity, params, requestParams);
////
// return brr;
// }
private BaseRestResponse getBaseRestResponse(String resp) {
StringResponse srr;
if (resp != null){
srr = new StringResponse(200,resp);
} else {
srr = new StringResponse(400,null);
}
return getBaseRestResponse(srr);
}
// private BaseRestResponse executeDelete(String entity, String params, String requestParams) {
// BaseRestResponse brr;
// SimpleHttpRequest request = this.buildDeleteRequest(entity, params, requestParams);
// try {
// SimpleHttpResponse response = this.processRequest(request);
// brr = getBaseRestResponse(response);
// } catch (Exception exp) {
// brr = new BaseRestResponse(false, exp.toString());
// }
// return brr;
// }
private BaseRestResponse getBaseRestResponse(SimpleRestResponse srr)
{
BaseRestResponse brr;
if (srr != null)
{
brr = new BaseRestResponse(srr.isSuccess(), srr.getError());
brr.objectNode = srr.objectNode;
}
else
brr = new BaseRestResponse(false, COMMAND_ERROR + "null response");
return brr;
}
// private BaseRestResponse getBaseRestResponse(String resp) {
// StringResponse srr;
// if (resp != null){
// srr = new StringResponse(200,resp);
// } else {
// srr = new StringResponse(400,null);
// }
// return getBaseRestResponse(srr);
// }
//
// private BaseRestResponse getBaseRestResponse(SimpleRestResponse srr)
// {
// BaseRestResponse brr;
// if (srr != null)
// {
// brr = new BaseRestResponse(srr.isSuccess(), srr.getError());
// brr.objectNode = srr.objectNode;
// }
// else
// brr = new BaseRestResponse(false, IRestClient.COMMAND_ERROR + "null response");
// return brr;
// }
private BaseRestResponse getBaseRestResponse(StringResponse srr)
{
......@@ -271,11 +236,11 @@ public class RestHttpClient extends SimpleRestClient implements Closeable {
if (srr.getStatusCode() == 200){
brr = getBaseRestResponseFromString(srr.getContent());
} else {
brr = new BaseRestResponse(false,COMMAND_ERROR + "Error response: " + String.valueOf(srr.getStatusCode()));
brr = new BaseRestResponse(false, IRestClient.COMMAND_ERROR + "Error response: " + String.valueOf(srr.getStatusCode()));
}
}
else
brr = new BaseRestResponse(false,COMMAND_ERROR + "null response");
brr = new BaseRestResponse(false, IRestClient.COMMAND_ERROR + "null response");
return brr;
}
......@@ -299,61 +264,61 @@ public class RestHttpClient extends SimpleRestClient implements Closeable {
return brr;
}
private BaseRestResponse getBaseRestResponse(SimpleHttpResponse response) {
BaseRestResponse brr = null;
if (response != null){
if (response.getStatusCode() == 200){
brr = getBaseRestResponse(new StringResponse(200,response.getContent()));
} else {
brr = new BaseRestResponse(false,COMMAND_ERROR + "Error response: " + String.valueOf(response.getStatusCode()));
}
} else {
brr = new BaseRestResponse(false, COMMAND_ERROR + "null response");
}
return brr;
}
private BaseRestResponse getBaseRestResponse(HttpResponse httpResponse) {
BaseRestResponse brr = null;
if (httpResponse != null){
final StatusLine statusLine = httpResponse.getStatusLine();
if (statusLine.getStatusCode() == 200){
brr = getBaseRestResponse(httpResponse.getEntity());
} else {
brr = new BaseRestResponse(false,
new StringBuilder(Constants.STRING_INITIAL_CAPACITY)
.append(COMMAND_ERROR).
append("Error response: ").
append(statusLine.getStatusCode()).
append(" : ").
append(statusLine.getReasonPhrase())
.toString());
}
} else {
brr = new BaseRestResponse(false, COMMAND_ERROR + "null response");
}
return brr;
}
private BaseRestResponse getBaseRestResponse(HttpEntity httpEntity) {
BaseRestResponse brr = null;
try {
if(httpEntity != null) {
String responseStr = EntityUtils.toString(httpEntity, Charsets.UTF_8.toString());
brr = getBaseRestResponseFromString(responseStr);
}
} catch (IOException exp) {
brr = new BaseRestResponse(false,
new StringBuilder(Constants.STRING_INITIAL_CAPACITY)
.append(getClass().getName()).append(" >> ").append(exp.toString())
.toString());
// private BaseRestResponse getBaseRestResponse(SimpleHttpResponse response) {
// BaseRestResponse brr = null;
// if (response != null){
// if (response.getStatusCode() == 200){
// brr = getBaseRestResponse(new StringResponse(200,response.getContent()));
// } else {
// brr = new BaseRestResponse(false, IRestClient.COMMAND_ERROR + "Error response: " + String.valueOf(response.getStatusCode()));
// }
// } else {
// brr = new BaseRestResponse(false, IRestClient.COMMAND_ERROR + "null response");
// }
// return brr;
// }
//
// private BaseRestResponse getBaseRestResponse(HttpResponse httpResponse) {
// BaseRestResponse brr = null;
// if (httpResponse != null){
// final StatusLine statusLine = httpResponse.getStatusLine();
// if (statusLine.getStatusCode() == 200){
// brr = getBaseRestResponse(httpResponse.getEntity());
// } else {
// brr = new BaseRestResponse(false,
// new StringBuilder(Constants.STRING_INITIAL_CAPACITY)
// .append(IRestClient.COMMAND_ERROR).
// append("Error response: ").
// append(statusLine.getStatusCode()).
// append(" : ").
// append(statusLine.getReasonPhrase())
// .toString());
// }
// } else {
// brr = new BaseRestResponse(false, IRestClient.COMMAND_ERROR + "null response");
// }
// return brr;
//
//
// }
}
return brr;
}
// private BaseRestResponse getBaseRestResponse(HttpEntity httpEntity) {
// BaseRestResponse brr = null;
// try {
//
// if(httpEntity != null) {
// String responseStr = EntityUtils.toString(httpEntity, Charsets.UTF_8.toString());
// brr = getBaseRestResponseFromString(responseStr);
// }
// } catch (IOException exp) {
// brr = new BaseRestResponse(false,
// new StringBuilder(Constants.STRING_INITIAL_CAPACITY)
// .append(getClass().getName()).append(" >> ").append(exp.toString())
// .toString());
//
// }
// return brr;
// }
/*********************** NEW DESIGN AND REST CLIENT ***********************************/
public static final int DEFAULT_CONNECT_TIMEOUT_MILLIS = 1000;
......@@ -364,18 +329,11 @@ public class RestHttpClient extends SimpleRestClient implements Closeable {
public static final int DEFAULT_MAX_CONN_TOTAL = 64;
public static final MediaType MEDIA_TYPE_JSON = MediaType.parse("application/json; charset=utf-8");
OkHttpClient client = null;
ILogger logger = null;
// private CloseableHttpAsyncClient createHttpClient() {
// //default timeouts are all infinite
// RequestConfig.Builder requestConfigBuilder = RequestConfig.custom()
// .setConnectTimeout(DEFAULT_CONNECT_TIMEOUT_MILLIS)
// .setSocketTimeout(DEFAULT_SOCKET_TIMEOUT_MILLIS)
// .setConnectionRequestTimeout(DEFAULT_CONNECTION_REQUEST_TIMEOUT_MILLIS);
// HttpAsyncClientBuilder httpClientBuilder = HttpAsyncClientBuilder.create().setDefaultRequestConfig(requestConfigBuilder.build())
// //default settings for connection pooling may be too constraining
// .setMaxConnPerRoute(DEFAULT_MAX_CONN_PER_ROUTE).setMaxConnTotal(DEFAULT_MAX_CONN_TOTAL);
// return httpClientBuilder.build();
// }
public void setLogger(ILogger logger) {
this.logger = logger;
}
public boolean executeAsync(String method, CommandParams commandParams, Consumer<BaseRestResponse> cbFunc) {
......@@ -397,33 +355,9 @@ public class RestHttpClient extends SimpleRestClient implements Closeable {
}
});
// HttpEntity httpEntity = createContent(commandParams);
// Request request = createHttpRequest(method, uri, httpEntity);
// // add headers
// HttpRequest httpRequest = (HttpRequest)request;
// if (commandParams.getHeadersMap() != null) {
// commandParams.getHeadersMap().entrySet().forEach(header -> httpRequest.addHeader(header.getKey(),header.getValue()));
// }
//
// httpAsyncClient.execute(request, new FutureCallback<HttpResponse>() {
// @Override
// public void completed(HttpResponse result) {
// BaseRestResponse brr = getBaseRestResponse(result);
// cbFunc.accept(brr);
// }
//
// @Override
// public void failed(Exception ex) {
// }
//
// @Override
// public void cancelled() {
// cbFunc.accept(new BaseRestResponse(false,"Request Cancelled"));
// }
// });
} catch (Exception exp){
MicroserviceApp.getsInstance().getLogger().error(getClass().getName() + " >> " + exp.toString());
if (logger != null)
logger.error(getClass().getName() + " >> " + exp.toString());
return false;
}
......@@ -433,7 +367,8 @@ public class RestHttpClient extends SimpleRestClient implements Closeable {
public BaseRestResponse executeSync(String method, CommandParams commandParams) {
BaseRestResponse brr = null;
if (method == null || method.length() == 0 || commandParams == null){
MicroserviceApp.getsInstance().getLogger().error(getClass().getName() + " >> No Method in executeSync");
if (logger != null)
logger.error(getClass().getName() + " >> No Method in executeSync");
return null;
}
......@@ -445,7 +380,8 @@ public class RestHttpClient extends SimpleRestClient implements Closeable {
} catch (Exception exp){
MicroserviceApp.getsInstance().getLogger().error(getClass().getName() + " >> " + exp.toString());
if (logger != null)
logger.error(getClass().getName() + " >> " + exp.toString());
brr = new BaseRestResponse(false, new StringBuilder(Constants.STRING_INITIAL_CAPACITY)
.append(getClass().getName()).append(" >> ").append(exp.toString())
.toString());
......@@ -492,50 +428,50 @@ public class RestHttpClient extends SimpleRestClient implements Closeable {
return builder.build();
}
private HttpEntity createContent(CommandParams commandParams) {
if (commandParams.getContent() != null){
return new StringEntity(commandParams.getContent(),ContentType.create("application/json"));
}
return null;
}
// private HttpEntity createContent(CommandParams commandParams) {
// if (commandParams.getContent() != null){
// return new StringEntity(commandParams.getContent(),ContentType.create("application/json"));
// }
// return null;
// }
private HttpRequestBase createHttpRequest(String method, URI uri, HttpEntity entity) {
switch(method.toUpperCase(Locale.ROOT)) {
case HttpDelete.METHOD_NAME:
return addRequestBody(new HttpDelete(uri), entity);
case HttpGet.METHOD_NAME:
return addRequestBody(new HttpGet(uri), null);
case HttpHead.METHOD_NAME:
return addRequestBody(new HttpHead(uri), entity);
case HttpOptions.METHOD_NAME:
return addRequestBody(new HttpOptions(uri), entity);
case HttpPatch.METHOD_NAME:
return addRequestBody(new HttpPatch(uri), entity);
case HttpPost.METHOD_NAME:
HttpPost httpPost = new HttpPost(uri);
addRequestBody(httpPost, entity);
return httpPost;
case HttpPut.METHOD_NAME:
return addRequestBody(new HttpPut(uri), entity);
case HttpTrace.METHOD_NAME:
return addRequestBody(new HttpTrace(uri), entity);
default:
throw new UnsupportedOperationException("http method not supported: " + method);
}
}
private HttpRequestBase addRequestBody(HttpRequestBase httpRequest, HttpEntity entity) {
if (entity != null) {
if (httpRequest instanceof HttpEntityEnclosingRequestBase) {
((HttpEntityEnclosingRequestBase)httpRequest).setEntity(entity);
} else {
throw new UnsupportedOperationException(httpRequest.getMethod() + " with body is not supported");
}
}
return httpRequest;
}
// private HttpRequestBase createHttpRequest(String method, URI uri, HttpEntity entity) {
//
// switch(method.toUpperCase(Locale.ROOT)) {
// case HttpDelete.METHOD_NAME:
// return addRequestBody(new HttpDelete(uri), entity);
// case HttpGet.METHOD_NAME:
// return addRequestBody(new HttpGet(uri), null);
// case HttpHead.METHOD_NAME:
// return addRequestBody(new HttpHead(uri), entity);
// case HttpOptions.METHOD_NAME:
// return addRequestBody(new HttpOptions(uri), entity);
// case HttpPatch.METHOD_NAME:
// return addRequestBody(new HttpPatch(uri), entity);
// case HttpPost.METHOD_NAME:
// HttpPost httpPost = new HttpPost(uri);
// addRequestBody(httpPost, entity);
// return httpPost;
// case HttpPut.METHOD_NAME:
// return addRequestBody(new HttpPut(uri), entity);
// case HttpTrace.METHOD_NAME:
// return addRequestBody(new HttpTrace(uri), entity);
// default:
// throw new UnsupportedOperationException("http method not supported: " + method);
// }
// }
// private HttpRequestBase addRequestBody(HttpRequestBase httpRequest, HttpEntity entity) {
// if (entity != null) {
// if (httpRequest instanceof HttpEntityEnclosingRequestBase) {
// ((HttpEntityEnclosingRequestBase)httpRequest).setEntity(entity);
// } else {
// throw new UnsupportedOperationException(httpRequest.getMethod() + " with body is not supported");
// }
// }
// return httpRequest;
// }
......
......@@ -16,8 +16,6 @@
package com.google.flatbuffers;
import static com.google.flatbuffers.Constants.*;
import java.nio.CharBuffer;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.CharsetEncoder;
......@@ -27,6 +25,10 @@ import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.charset.Charset;
import static com.google.flatbuffers.Constants.FILE_IDENTIFIER_LENGTH;
import static com.google.flatbuffers.Constants.SIZEOF_INT;
import static com.google.flatbuffers.Constants.SIZEOF_SHORT;
/// @file
/// @addtogroup flatbuffers_java_api
/// @{
......@@ -204,7 +206,7 @@ public class FlatBufferBuilder {
*
* @param x A `short` to put into the buffer.
*/
public void putShort (short x) { bb.putShort (space -= Constants.SIZEOF_SHORT, x); }
public void putShort (short x) { bb.putShort (space -= SIZEOF_SHORT, x); }
/**
* Add an `int` to the buffer, backwards from the current location. Doesn't align nor
......@@ -212,7 +214,7 @@ public class FlatBufferBuilder {
*
* @param x An `int` to put into the buffer.
*/
public void putInt (int x) { bb.putInt (space -= Constants.SIZEOF_INT, x); }
public void putInt (int x) { bb.putInt (space -= SIZEOF_INT, x); }
/**
* Add a `long` to the buffer, backwards from the current location. Doesn't align nor
......@@ -258,14 +260,14 @@ public class FlatBufferBuilder {
*
* @param x A `short` to put into the buffer.
*/
public void addShort (short x) { prep(Constants.SIZEOF_SHORT, 0); putShort (x); }
public void addShort (short x) { prep(SIZEOF_SHORT, 0); putShort (x); }
/**
* Add an `int` to the buffer, properly aligned, and grows the buffer (if necessary).
*
* @param x An `int` to put into the buffer.
*/
public void addInt (int x) { prep(Constants.SIZEOF_INT, 0); putInt (x); }
public void addInt (int x) { prep(SIZEOF_INT, 0); putInt (x); }
/**
* Add a `long` to the buffer, properly aligned, and grows the buffer (if necessary).
......@@ -397,7 +399,7 @@ public class FlatBufferBuilder {
*/
public int createVectorOfTables(int[] offsets) {
notNested();
startVector(Constants.SIZEOF_INT, offsets.length, Constants.SIZEOF_INT);
startVector(SIZEOF_INT, offsets.length, SIZEOF_INT);
for(int i = offsets.length - 1; i >= 0; i--) addOffset(offsets[i]);
return endVector();
}
......
// automatically generated by the FlatBuffers compiler, do not modify
package microservice.common.context;
package common.microservice.context;
public final class CrudMethod {
private CrudMethod() { }
......
// automatically generated by the FlatBuffers compiler, do not modify
package microservice.common.context;
package common.microservice.context;
import java.nio.*;
import java.lang.*;
import java.util.*;
import com.google.flatbuffers.*;
@SuppressWarnings("unused")
......
package common.microservice.context;
import common.microservice.services.CommonServices;
import java.util.Deque;
import java.util.Map;
public class PubSubContext implements CommonServices.IMsgContext {
public String[] params = null;
public Map<String, Deque<String>> queryParameters = null;
public Map<String, String> pathParameters = null;
public String rcid; // request correlation id
public String getParameter(String paramName) {
if (queryParameters != null && queryParameters.containsKey(paramName))
return queryParameters.get(paramName).getFirst();
else
return null;
}
public String getPathParameter(String paramName) {
if (pathParameters != null )
return pathParameters.get(paramName);
else
return null;
}
@Override
public void setParameters(Map<String, String> parameters) {
pathParameters = parameters;
}
}
package common.microservice.context;
import java.util.HashMap;
import java.util.Map;
public class PubSubMsg {
String content;
String mcid; // msg context id
Map<String, String> parameters = null;
public PubSubMsg(String content, String mcid) {
this.content = content;
this.mcid = mcid;
}
public PubSubMsg(String content, String mcid, Map<String, String> parameters) {
this.content = content;
this.mcid = mcid;
this.parameters = parameters;
}
public PubSubMsg() {
}
PubSubMsg addParameter(String param, String value){
if (parameters == null)
parameters = new HashMap<>();
parameters.put(param,value);
return this;
}
public Map<String, String> getParameters() {
return parameters;
}
public String getContent() {
return content;
}
public String getMcid() {
return mcid;
}
@Override
public String toString() {
return "mcid: " + mcid + ", content: " + content;
}
}
// automatically generated by the FlatBuffers compiler, do not modify
package microservice.common.context;
package common.microservice.context;
import java.nio.*;
import java.lang.*;
import java.util.*;
import com.google.flatbuffers.*;
@SuppressWarnings("unused")
......
package microservice.common.context;
package common.microservice.context;
import java.util.Deque;
import java.util.Map;
import com.fasterxml.jackson.databind.ObjectMapper;
import microservice.services.CommonServices;
import microservice.io.iface.IContainer;
import microservice.io.iface.IRequest;
import microservice.io.iface.IResponse;
import microservice.types.UserProfile;
import common.microservice.services.CommonServices;
import common.microservice.io.iface.IContainer;
import common.microservice.io.iface.IRequest;
import common.microservice.io.iface.IResponse;
import common.microservice.types.UserProfile;
public class RestContext implements CommonServices.IMsgContext
......
// automatically generated by the FlatBuffers compiler, do not modify
package microservice.common.context;
package common.microservice.context;
import java.nio.*;
import java.lang.*;
import java.util.*;
import com.google.flatbuffers.*;
@SuppressWarnings("unused")
......
// automatically generated by the FlatBuffers compiler, do not modify
package microservice.common.context;
package common.microservice.context;
import java.nio.*;
import java.lang.*;
import java.util.*;
import com.google.flatbuffers.*;
@SuppressWarnings("unused")
......
package microservice.defs;
package common.microservice.defs;
public class Constants
{
......@@ -25,4 +25,5 @@ public class Constants
public static final String TIMER = "Timer:";
public static final String HTTP_SCHEME = "http";
public static final String MSGQ_ID = "mqid";
public static final String DEFAULT_TENANT = "mcx";
}
package common.microservice.io.iface;
public interface IApp {
ILogger getLogger();
String getAppName();
String getServerName();
String getId();
IConfiguration getConfiguration();
}
package microservice.io.iface;
package common.microservice.io.iface;
import common.configuration.IConfigurationProvider;
......
package microservice.io.iface;
package common.microservice.io.iface;
import java.util.regex.Pattern;
import com.fasterxml.jackson.databind.JsonNode;
import microservice.handlers.BaseHandler;
/**
* this interface defines the basic operations
* required from the container implementor
......
package microservice.io.iface;
import java.util.Optional;
import java.util.stream.Stream;
package common.microservice.io.iface;
/**
* logger interface to use in the MS
......
package microservice.io.iface;
package common.microservice.io.iface;
/**
* metrics interface for adding/incerementing metrics counters (collectd/...)
......
package microservice.io.iface;
package common.microservice.io.iface;
import com.fasterxml.jackson.databind.JsonNode;
......
package microservice.io.iface;
package common.microservice.io.iface;
public interface IPubSub
{
......
package microservice.io.iface;
package common.microservice.io.iface;
import java.io.InputStream;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
public interface IRequest
{
......
package microservice.io.iface;
package common.microservice.io.iface;
import java.nio.ByteBuffer;
......
package microservice.io.iface;
package common.microservice.io.iface;
import microservice.params.CommandParams;
import common.microservice.params.CommandParams;
import com.fasterxml.jackson.databind.JsonNode;
import com.netflix.hystrix.HystrixCommand;
import com.netflix.hystrix.HystrixCommandGroupKey;
import microservice.types.BaseRestResponse;
import common.microservice.types.BaseRestResponse;
import java.util.function.Consumer;
public interface IRestClient
{
static final String COMMAND_ERROR = "Command Error: ";
void init(ILogger logger);
public abstract class Command extends HystrixCommand<BaseRestResponse> {
protected CommandParams reqCtx = null;
......
package microservice.io.iface;
package common.microservice.io.iface;
import microservice.defs.Enums;
import common.microservice.defs.Enums;
import common.configuration.IConfigurationProvider;
/**
......
package microservice.io.impl;
package common.microservice.io.impl;
import microservice.io.iface.ILogger;
import common.microservice.io.iface.ILogger;
import java.time.LocalDateTime;
......
package microservice.common.paging;
package common.microservice.paging;
/**
* Created by amir on 23/03/17.
......
package microservice.common.paging;
package common.microservice.paging;
/**
* Created by amir on 23/03/17.
......
package microservice.common.paging;
package common.microservice.paging;
/**
* Created by amir on 23/03/17.
......
package microservice.common.paging;
package common.microservice.paging;
import com.fasterxml.jackson.databind.node.ArrayNode;
......
package microservice.common.paging;
package common.microservice.paging;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
......
package microservice.common.paging;
package common.microservice.paging;
/**
* Created by amir on 23/03/17.
......
package microservice.params;
package common.microservice.params;
import java.util.HashMap;
import java.util.Map;
......
package microservice.params;
package common.microservice.params;
public class RMQClientParams extends BaseClientParams
{
......
package microservice.params;
package common.microservice.params;
public class RestClientParams extends BaseClientParams
{
......
package microservice.params;
package common.microservice.params;
import jdk.nashorn.internal.ir.EmptyNode;
//import jdk.nashorn.internal.ir.EmptyNode;
/**
* Created by amir on 14/05/17.
......
......@@ -12,7 +12,7 @@
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
package microservice.common;
package common.microservice.security;
import java.io.UnsupportedEncodingException;
import java.security.SecureRandom;
......
package microservice.common;
package common.microservice.security;
import common.microservice.types.UserProfile;
import io.jsonwebtoken.*;
import microservice.types.UserProfile;
import javax.crypto.spec.SecretKeySpec;
import javax.xml.bind.DatatypeConverter;
......
package microservice.services;
import microservice.common.context.CrudMethod;
import microservice.defs.Constants;
import microservice.defs.Enums;
import microservice.io.iface.IMetricsFactory;
import microservice.io.iface.IRequest;
import microservice.io.iface.IResponse;
import microservice.io.iface.IServiceDiscovery;
import microservice.params.CommandParams;
import microservice.types.BaseRestResponse;
package common.microservice.services;
import common.microservice.context.CrudMethod;
import common.microservice.defs.Constants;
import common.microservice.defs.Enums;
import common.microservice.io.iface.*;
import common.microservice.params.CommandParams;
import common.microservice.types.BaseRestResponse;
import java.util.List;
import java.util.Map;
......@@ -35,11 +32,12 @@ public class CommonServices {
public static abstract class IService {
protected IServiceReactor reactor = null;
protected IMetricsFactory metricsFactory = null;
public abstract boolean init();
protected IApp app = null;
public abstract boolean init(IApp app);
public abstract void run();
public abstract void shutdown();
public abstract void handleNotImplmented(IMsgContext msgContext);
public abstract Enums.EnumServiceType getServiceType();
/// when relevant
public abstract void register(IServiceDiscovery serviceDiscovery, String id);
......@@ -138,14 +136,15 @@ public class CommonServices {
protected static String DELETE_METRIC_PREFIX = Constants.METER + SEND + Enums.EnumServiceType.E_REST.name() + ':' + CrudMethod.name(CrudMethod.Delete) + ':';
EnumRestServiceMode serviceMode = EnumRestServiceMode.E_UNKNOWN;
protected microservice.io.iface.IRestClient restClient = null;
protected common.microservice.io.iface.IRestClient restClient = null;
public EnumRestServiceMode getServiceMode() { return serviceMode; }
public void setServiceMode(EnumRestServiceMode serviceMode) { this.serviceMode = serviceMode; }
public void setRestClient(microservice.io.iface.IRestClient restClient) {
public void setRestClient(common.microservice.io.iface.IRestClient restClient) {
this.restClient = restClient;
}
@Override
public Enums.EnumServiceType getServiceType() { return Enums.EnumServiceType.E_REST; }
///////////// SYNC //////////////////////////
public abstract BaseRestResponse create(CommandParams cmdParams);
......@@ -166,29 +165,88 @@ public class CommonServices {
public abstract void startAsync(IRequest request,Runnable asyncFunc);
}
enum EnumPubSubCommands implements IServiceCommands {
public enum EnumPubSubCommands implements IServiceCommands {
E_SUBSCRIBE,
E_PUBLISH,
E_NOTIFY
}
public enum EnumPubSubServiceMode {
E_PUBLISHER,
E_SUBSCRIBER,
E_BOTH
}
public static abstract class IPubSubService extends IService {
public class PubSubMsgContext implements IMsgContext {
public String topic;
public String msg;
public static class PubSubMsgContext implements IMsgContext {
private String topic;
private String msg;
private String mcid = null;
public Map<String, String> parameters = null;
public PubSubMsgContext(String topic, String msg) {
this.topic = topic;
this.msg = msg;
}
public PubSubMsgContext(String topic,
String msg,
String mcid,
Map<String, String> parameters) {
this.topic = topic;
this.msg = msg;
this.mcid = mcid;
this.parameters = parameters;
}
public String getTopic() {
return topic;
}
public void setTopic(String topic) {
this.topic = topic;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public String getMcid() {
return mcid;
}
public PubSubMsgContext setMcid(String mcid) {
this.mcid = mcid;
return this;
}
public Map<String, String> getParameters() {
return parameters;
}
@Override
public void setParameters(Map<String, String> parameters) {
this.parameters = parameters;
}
}
EnumPubSubServiceMode serviceMode = EnumPubSubServiceMode.E_BOTH;
public EnumPubSubServiceMode getServiceMode() {
return serviceMode;
}
public void setServiceMode(EnumPubSubServiceMode serviceMode) {
this.serviceMode = serviceMode;
}
@Override
public Enums.EnumServiceType getServiceType() { return Enums.EnumServiceType.E_PUBSUB; }
public abstract void subscribe(String topic, Consumer<PubSubMsgContext> notifyFunc,Consumer<String> errorFunc);
public abstract void unsubscribe(String topic);
public abstract void publish(PubSubMsgContext pubSubMsgContext);
......@@ -227,7 +285,8 @@ public class CommonServices {
public EnumMsgQServiceMode getServiceMode() {
return serviceMode;
}
@Override
public Enums.EnumServiceType getServiceType() { return Enums.EnumServiceType.E_MSGQ; }
public void setServiceMode(EnumMsgQServiceMode serviceMode) {
this.serviceMode = serviceMode;
}
......@@ -277,4 +336,21 @@ public class CommonServices {
public abstract void stop() throws InterruptedException;
}
public static String buildServiceKey(CommonServices.MethodParams methodParams) {
return Constants.SLASH_SEPERATOR +
methodParams.getServiceType().name() + Constants.TYPE_PREFIX_SEPERATOR +
methodParams.getServiceCommand().toString() + Constants.TYPE_PREFIX_SEPERATOR +
methodParams.getResourceUri();
}
public static String buildServiceKey(Enums.EnumServiceType enumServiceType,
CommonServices.IServiceCommands serviceCommands,
String resourceUri) {
return new StringBuilder(32).append(Constants.SLASH_SEPERATOR).append(enumServiceType.name()).append(Constants.TYPE_PREFIX_SEPERATOR)
.append(serviceCommands.toString()).append(Constants.TYPE_PREFIX_SEPERATOR)
.append(resourceUri).toString();
}
}
package microservice.types;
package common.microservice.types;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnore;
......
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;
......
package microservice.utils;
package common.microservice.utils;
@FunctionalInterface
public interface CheckedFunction<T,R> {
......
package microservice.utils;
package common.microservice.utils;
import com.google.gdata.util.common.base.Pair;
......
package microservice.utils;
package common.microservice.utils;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import microservice.services.CommonServices;
import common.microservice.services.CommonServices;
import java.util.concurrent.TimeUnit;
......
package common.microservice.utils;
import com.eaio.uuid.UUID;
public class IDGenerator {
public static String createUUID(){
return new UUID().toString();
}
public static long createUUIDLong(){
return new UUID().getTime();
}
}
package common.microservice.utils;
import common.microservice.io.iface.ILogger;
public class Logging {
/**
* logging utils
*/
public static void logRcid(ILogger logger,String from, String rcid){
logger.info(from + " RCID: " + rcid);
}
public static void logMcid(ILogger logger,String from, String mcid){
logger.info(from + " MCID: " + mcid);
}
}
package common.microservice.params;
import org.apache.pulsar.shade.org.apache.commons.lang.StringUtils;
/**
* parameters to init pulsar pub-sub
* namepaces and topic - admin
*/
public class PulsarParams {
private final int retentionTimeMin;
private final int retentionSizeMB;
int msgTtl;
String clusters;
String serviceUrl;
String adminUrl;
int consumersThreadPoolSize;
boolean createPublicNamespace;
public static class PulsarParamsBuilder {
public static final int DEFAULT_ADMIN_PORT = 8080;
public static final int DEFAULT_SERVICE_PORT = 6650;
public static final String STANDALONE_CLUSTER = "standalone";
int msgTtl = 3600;
String clusters = STANDALONE_CLUSTER;
String serviceUrl;
String adminUrl = null;
int consumersThreadPoolSize = 0;
boolean createPublicNamespace = false;
int retentionTimeMin = 1440; // a day
int retentionSizeMB = 100; // 100MB
public PulsarParamsBuilder() {
// empty
}
public String getServiceUrl() {
return serviceUrl;
}
public PulsarParamsBuilder setServiceUrl(String serviceUrl) {
final String[] parts = serviceUrl.split(":");
if(parts.length < 2 ) { // || !StringUtils.isNumeric(parts[1]))
this.serviceUrl = String.format("%s:%u",serviceUrl, DEFAULT_SERVICE_PORT);
} else if (!StringUtils.isNumeric(parts[1])){
this.serviceUrl = String.format("%s:%u",parts[0], DEFAULT_SERVICE_PORT);
} else {
this.serviceUrl = serviceUrl;
}
return this;
}
public PulsarParamsBuilder setMsgTtl(int msgTtl) {
this.msgTtl = msgTtl;
return this;
}
public PulsarParamsBuilder setClusters(String clusters) {
this.clusters = clusters;
return this;
}
public PulsarParamsBuilder setAdminUrl(String adminUrl) {
this.adminUrl = adminUrl;
return this;
}
public void setConsumersThreadPoolSize(int consumersThreadPoolSize) {
this.consumersThreadPoolSize = consumersThreadPoolSize;
}
public PulsarParamsBuilder setCreatePublicNamespace(boolean createPublicNamespace) {
this.createPublicNamespace = createPublicNamespace;
return this;
}
public PulsarParamsBuilder setRetentionTimeMin(int retentionTimeMin) {
this.retentionTimeMin = retentionTimeMin;
return this;
}
public PulsarParamsBuilder setRetentionSizeMB(int retentionSizeMB) {
this.retentionSizeMB = retentionSizeMB;
return this;
}
public PulsarParams build(){
try {
if (adminUrl == null) {
String host = serviceUrl.substring(0, serviceUrl.indexOf(':'));
adminUrl = String.format("%s:%d",host, DEFAULT_ADMIN_PORT);
}
if (consumersThreadPoolSize == 0){
consumersThreadPoolSize = Runtime.getRuntime().availableProcessors();
}
return new PulsarParams(msgTtl,
retentionTimeMin,
retentionSizeMB,
clusters,
serviceUrl,
adminUrl,
consumersThreadPoolSize,
createPublicNamespace);
} catch (Exception e){
System.err.println("PulsarParamsBuilder > " + e.toString());
}
return null;
}
}
public PulsarParams(int msgTtl,
int retentionTimeMin,
int retentionSizeMB,
String clusters,
String serviceUrl,
String adminUrl,
int consumersThreadPoolSize,
boolean createPublicNamespace) {
this.msgTtl = msgTtl;
this.retentionTimeMin = retentionTimeMin;
this.retentionSizeMB = retentionSizeMB;
this.clusters = clusters;
this.serviceUrl = serviceUrl;
this.adminUrl = adminUrl;
this.consumersThreadPoolSize = consumersThreadPoolSize;
this.createPublicNamespace = createPublicNamespace;
}
public int getMsgTtl() {
return msgTtl;
}
public int getRetentionTimeMin() {
return retentionTimeMin;
}
public int getRetentionSizeMB() {
return retentionSizeMB;
}
public String getClusters() {
return clusters;
}
public String getServiceUrl() {
return serviceUrl;
}
public void setServiceUrl(String serviceUrl) {
this.serviceUrl = serviceUrl;
}
public String getAdminUrl() {
return adminUrl;
}
public void setAdminUrl(String adminUrl) {
this.adminUrl = adminUrl;
}
public int getConsumersThreadPoolSize() {
return consumersThreadPoolSize;
}
public boolean isCreatePublicNamespace() {
return createPublicNamespace;
}
}
package service.microservice;
import common.microservice.context.PubSubMsg;
import common.microservice.defs.Constants;
import common.microservice.defs.Enums;
import common.microservice.io.iface.IApp;
import common.microservice.io.iface.ILogger;
import common.microservice.io.iface.IServiceDiscovery;
import common.microservice.services.CommonServices;
import common.microservice.utils.IDGenerator;
import common.microservice.params.PulsarParams;
import org.apache.pulsar.client.admin.PulsarAdmin;
import org.apache.pulsar.client.admin.PulsarAdminException;
import org.apache.pulsar.client.admin.Tenants;
import org.apache.pulsar.client.admin.internal.PulsarAdminBuilderImpl;
import org.apache.pulsar.client.api.*;
import org.apache.pulsar.client.impl.schema.JSONSchema;
import org.apache.pulsar.common.policies.data.RetentionPolicies;
import org.apache.pulsar.common.policies.data.TenantInfo;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.regex.Pattern;
import static common.microservice.services.CommonServices.buildServiceKey;
/**
* This class implements PubSubService for Apache Pulsar
* The subscription is wildcard for service name (appName): 'persistent://service/.*'
* the part after the service name contains the method path to pass to the reactor
* the consumer part receives a message, creates the pubsub context and dispatch it to
* thread poll executor which handle the message and delegate it to the reactor
* currently not sending message as a flatbuffer, using json so that external tools
* i.e apache flink, can subscribe and inject the messages as well.
*/
public class IPubSubServicePulsarImpl extends CommonServices.IPubSubService {
public static final String PULSAR_PREFIX = "pulsar://";
public static final String SERVICE_NAME = "IPubSubServicePulsarImpl";
public static final String PERSISTENT_PREFIX = "persistent://";
public static final String TOPIC_PREFIX = PERSISTENT_PREFIX + Constants.DEFAULT_TENANT;
public static final String PUBLIC_NAMESPACE = "public";
public static int INIITIAL_PRODUCERS_SIZE = 5;
private String adminUrl;
private PulsarClient client = null;
// private String serviceUrl;
private Consumer<PubSubMsg> consumer = null;
private ILogger logger = null;
private String clientId;
private Thread consumerThread = null;
// private int consumersThreadPoolSize;
private ExecutorService consumerExecutorService = null;
private ConcurrentHashMap<String,Producer<PubSubMsg>> producersMap = new ConcurrentHashMap<>(INIITIAL_PRODUCERS_SIZE);
private boolean consumerRunning = false;
private Set<String> clusters; // = new HashSet<>(Arrays.asList("standalone"));
private String namespacePrefix;
private PulsarParams pulsarParams;
// public IPubSubServicePulsarImpl(String serviceUrl,
// String adminUrl,
// String clusters,
// int consumersThreadPoolSize) {
// if (!serviceUrl.startsWith(PULSAR_PREFIX))
// serviceUrl = PULSAR_PREFIX + serviceUrl;
// this.serviceUrl = serviceUrl;
// this.adminUrl = adminUrl;
// this.clusters = new HashSet<>(Arrays.asList(clusters.split(",")));
// this.consumersThreadPoolSize = consumersThreadPoolSize;
// }
public IPubSubServicePulsarImpl(PulsarParams pulsarParams) {
this.pulsarParams = pulsarParams;
if (!pulsarParams.getServiceUrl().startsWith(PULSAR_PREFIX))
pulsarParams.setServiceUrl(PULSAR_PREFIX + pulsarParams.getServiceUrl());
this.clusters = new HashSet<>(Arrays.asList(pulsarParams.getClusters().split(",")));
}
@Override
public boolean init(IApp app) {
this.app = app;
if (app != null) {
clientId = app.getAppName() + '-'
+ app.getServerName() + '-'
+ app.getId();
logger = app.getLogger();
namespacePrefix = TOPIC_PREFIX + "/" + app.getAppName();
} else {
// for tests where there is no MicroserviceApp
namespacePrefix = TOPIC_PREFIX + "/" + "testApp";
}
try {
client = PulsarClient.builder()
.serviceUrl(this.pulsarParams.getServiceUrl())
.build();
switch (getServiceMode()){
case E_SUBSCRIBER:
case E_BOTH:
createConsumer();
break;
}
// check for public namespace
if(pulsarParams.isCreatePublicNamespace()) {
adminValidateTenantAndNamespace(Constants.DEFAULT_TENANT, PUBLIC_NAMESPACE);
}
} catch (PulsarClientException e) {
System.err.println(e.toString());
return false;
} catch (PulsarAdminException e) {
System.err.println(e.toString());
return false;
}
return true;
}
@Override
public void run() {
if (getServiceMode() == CommonServices.EnumPubSubServiceMode.E_PUBLISHER)
return;
consumerThread = new Thread(() -> {
// wait for message and dispatch it
consumerRunning = true;
logger.info("Pulsar PubSub started successfully");
try {
while (consumerRunning) {
Message<PubSubMsg> msg = consumer.receive();
try {
final PubSubMsgContext msgCtx = getMsgContext(msg);
if (msgCtx != null) {
logger.debug(SERVICE_NAME + " > Message received: " + msgCtx.toString());
consumerExecutorService.execute(() -> dispatchMsgCtx(msgCtx));
// dispatchMsgCtx(msgCtx);
} else {
logger.warning(SERVICE_NAME + " > Failed to parse Pulsar message: " + msg.getMessageId());
}
} finally {
// Acknowledge the message so that it can be deleted by the message broker
consumer.acknowledge(msg);
}
}
} catch (PulsarClientException e) {
e.printStackTrace();
}
});
consumerThread.setName(getClass().getName() + "::PulsarConsumerThread");
consumerThread.start();
}
/**
* getting the actual topic and
* delegate to reactor
* @param msgCtx
*/
private void dispatchMsgCtx(PubSubMsgContext msgCtx) {
String topic = msgCtx.getTopic();//.substring(TOPIC_PREFIX.length());
if (topic.length() > namespacePrefix.length() &&
topic.startsWith(namespacePrefix)) {
topic = topic.substring(namespacePrefix.length());
String key = buildServiceKey(Enums.EnumServiceType.E_PUBSUB,
CommonServices.EnumPubSubCommands.E_NOTIFY,
topic);
reactor.delegate(this, key, msgCtx);
} else {
logger.warning(SERVICE_NAME + " > dispatchMsgCtx received topic with invalid/no namespace");
}
}
/**
* create the msg context
* @param msg
* @return
*/
private PubSubMsgContext getMsgContext(Message<PubSubMsg> msg) {
final PubSubMsg pubSubMsg = msg.getValue();
if (pubSubMsg != null){
return new PubSubMsgContext(msg.getTopicName(),
pubSubMsg.getContent(),
pubSubMsg.getMcid(),
pubSubMsg.getParameters());
} else {
logger.error(SERVICE_NAME + " > getMsgContext > Failed in parsing json to PubSubMsg");
}
return null;
}
@Override
public void shutdown() {
stopConsumer();
closeProducers();
if (client != null){
try {
client.close();
} catch (PulsarClientException e) {
e.printStackTrace();
}
}
}
private void closeProducers() {
// closing all topic producers
producersMap.values().forEach(producer -> {
if (producer != null){
try {
producer.close();
} catch (PulsarClientException e) {
e.printStackTrace();
}
}
});
}
private void stopConsumer() {
if (consumer != null){
try {
consumer.close();
if (consumerThread != null){
consumerRunning = false;
consumerThread.interrupt();
consumerThread.join(500);
}
} catch (PulsarClientException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
@Override
public void handleNotImplmented(CommonServices.IMsgContext msgContext) {
}
@Override
public void register(IServiceDiscovery serviceDiscovery, String id) {
}
@Override
public void subscribe(String topic, java.util.function.Consumer<PubSubMsgContext> notifyFunc, java.util.function.Consumer<String> errorFunc) {
}
@Override
public void unsubscribe(String topic) {
}
@Override
public void publish(PubSubMsgContext pubSubMsgContext) {
if (validateMsg(pubSubMsgContext)) {
try {
// create mcid if missing
if (pubSubMsgContext.getMcid() == null || pubSubMsgContext.getMcid().isEmpty())
pubSubMsgContext.setMcid(IDGenerator.createUUID());
final Producer<PubSubMsg> producerForTopic = getProducerForTopic(pubSubMsgContext.getTopic());
if(producerForTopic != null) {
// construct msg to send
PubSubMsg pubSubMsg = new PubSubMsg(pubSubMsgContext.getMsg(), pubSubMsgContext.getMcid(), pubSubMsgContext.getParameters());
producerForTopic.sendAsync(pubSubMsg).whenCompleteAsync((messageId, throwable) -> {
if (throwable != null) {
logger.error(SERVICE_NAME + " > Failed in sendAsync: " + throwable.getMessage());
} else if (messageId != null) {
logger.debug(SERVICE_NAME + " > publish > sent msg-id: " + messageId);
}
});
}
} catch (Exception e) {
logger.error(SERVICE_NAME + " > publish > Failed to create producer/send msg: " + e);
}
} else {
logger.warning(SERVICE_NAME + " > publish > message not valid");
}
}
private Producer<PubSubMsg> getProducerForTopic(String topic) {
return producersMap.computeIfAbsent(topic,t -> {
try {
Producer<PubSubMsg> producer = client.newProducer(JSONSchema.of(PubSubMsg.class))
.topic(topic)
.create();
return producer;
//return client.newProducer().topic(topic).create();
} catch (PulsarClientException e) {
logger.error(SERVICE_NAME + " > Failed to create producer for topic " + topic);
}
return null;
});
}
/**
* topic should be in the form [domain]/[method]
* the domain is actually the namespace which is the domain
* @param pubSubMsgContext
* @return
*/
private boolean validateMsg(PubSubMsgContext pubSubMsgContext) {
String topic = pubSubMsgContext.getTopic();
if(topic != null && !topic.isEmpty() &&
pubSubMsgContext.getMsg() != null && !pubSubMsgContext.getMsg().isEmpty()){
// adding the topic prefix
if(!topic.startsWith(TOPIC_PREFIX)){
topic = topic.charAt(0) == '/'? TOPIC_PREFIX + topic : TOPIC_PREFIX + '/' + topic;
pubSubMsgContext.setTopic(topic);
}
return true;
}
return false;
}
private void createConsumer() throws PulsarClientException, PulsarAdminException {
/**
* wildcard topic:
* {persistent|non-persistent}://<tenant>/<namespace>/<regular expression>
*/
String pattern = namespacePrefix + "/.*";
adminValidateTenantAndNamespace(Constants.DEFAULT_TENANT,app.getAppName());
Pattern allTopicsInNamespace = Pattern.compile(pattern);
consumer = client.newConsumer(JSONSchema.of(PubSubMsg.class))
.subscriptionName(clientId)
.subscriptionType(SubscriptionType.Shared) // enable for multi-instance
.topicsPattern(allTopicsInNamespace)
.subscribe();
consumerExecutorService = Executors.newFixedThreadPool(pulsarParams.getConsumersThreadPoolSize());
}
private void adminValidateTenantAndNamespace(String tenant, String namespace) throws PulsarAdminException, PulsarClientException {
if (!pulsarParams.getAdminUrl().startsWith("http")){
pulsarParams.setAdminUrl("http://" + pulsarParams.getAdminUrl());
}
PulsarAdmin admin = new PulsarAdminBuilderImpl().serviceHttpUrl(pulsarParams.getAdminUrl()).build();
final Tenants tenants = admin.tenants();
final List<String> tenantList = tenants.getTenants();
final TenantInfo tenantInfo = new TenantInfo();
tenantInfo.setAllowedClusters(clusters);
if (!tenantList.contains(tenant)){
// creating tenant
logger.info(SERVICE_NAME + " >> Creating tenant: " + tenant);
tenants.createTenant(tenant, tenantInfo);
}
final List<String> namespaces = admin.namespaces().getNamespaces(tenant);
String namespaceStr = tenant + '/' + namespace;
if (!namespaces.contains(namespaceStr)){
logger.info(SERVICE_NAME + " >> Creating namespace: " + namespaceStr);
admin.namespaces().createNamespace(namespaceStr);
admin.namespaces().setNamespaceMessageTTL(namespaceStr,pulsarParams.getMsgTtl());
RetentionPolicies policies = new RetentionPolicies(pulsarParams.getRetentionTimeMin(), pulsarParams.getRetentionSizeMB());
admin.namespaces().setRetention(namespaceStr,policies);
admin.namespaces().setNamespaceReplicationClusters(namespaceStr,clusters);
}
admin.close();
}
}
......@@ -2,13 +2,14 @@ package microservice;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import microservice.io.iface.IRestClient;
import common.microservice.io.iface.ILogger;
import common.microservice.io.iface.IRestClient;
import microservice.io.impl.IRMQClientRestImpl;
import microservice.params.BaseClientParams;
import microservice.params.CommandParams;
import microservice.params.CommandParamsBuilder;
import microservice.params.RMQClientParams;
import microservice.types.BaseRestResponse;
import common.microservice.params.BaseClientParams;
import common.microservice.params.CommandParams;
import common.microservice.params.CommandParamsBuilder;
import common.microservice.params.RMQClientParams;
import common.microservice.types.BaseRestResponse;
import org.junit.Test;
import rx.Observable;
......@@ -48,6 +49,11 @@ public class TestCommandClient {
}
@Override
public void init(ILogger logger) {
}
@Override
public BaseRestResponse create(CommandParams reqCtx) {
BaseRestResponse brr = new BaseRestResponse(true, null);
Optional<Observable<BaseRestResponse>> brro = Optional.empty();
......
package microservice;
import microservice.io.iface.IConfiguration;
import common.microservice.io.iface.IConfiguration;
import microservice.io.impl.IConfigurationConfigPropImpl;
import microservice.io.impl.IServiceDiscoveryConsulImpl;
......
package microservice;
import io.jsonwebtoken.ExpiredJwtException;
import microservice.common.EncryptionUtils;
import microservice.types.UserProfile;
import common.microservice.security.EncryptionUtils;
import common.microservice.types.UserProfile;
import org.junit.Test;
import static microservice.common.EncryptionUtils.checkPassword;
import static common.microservice.security.EncryptionUtils.checkPassword;
/**
* Created by amir on 03/08/16.
......
package microservice;
import microservice.io.iface.ILogger;
import microservice.io.impl.ILoggerConsoleImpl;
import common.microservice.io.iface.ILogger;
import common.microservice.io.impl.ILoggerConsoleImpl;
import org.junit.Test;
import java.time.LocalDateTime;
......
......@@ -3,8 +3,8 @@ package microservice;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.junit.Test;
import microservice.io.iface.INotifyCallback;
import microservice.io.iface.IPubSub;
import common.microservice.io.iface.INotifyCallback;
import common.microservice.io.iface.IPubSub;
import microservice.io.impl.IPubSubMQTTImpl;
import com.fasterxml.jackson.databind.JsonNode;
......
package microservice;
import clients.microservice.impl.IRestClientHttpImpl;
import clients.microservice.utils.RestHttpClient;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import microservice.common.context.CrudMethod;
import microservice.common.context.RestResponse;
import microservice.defs.Constants;
import microservice.defs.Enums;
import microservice.io.iface.IRestClient;
import microservice.io.impl.IRestClientHttpImpl;
import common.microservice.defs.Enums;
import common.microservice.io.iface.IRestClient;
import microservice.io.impl.IServiceDiscoveryConsulImpl;
import microservice.utils.RestHttpClient;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.concurrent.FutureCallback;
import org.apache.http.entity.ContentType;
import org.apache.http.impl.nio.client.CloseableHttpAsyncClient;
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
import org.apache.http.impl.nio.client.HttpAsyncClients;
import org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager;
import org.apache.http.impl.nio.reactor.DefaultConnectingIOReactor;
import org.apache.http.nio.entity.NStringEntity;
import org.apache.http.nio.reactor.ConnectingIOReactor;
import org.apache.http.util.EntityUtils;
import org.elasticsearch.client.Response;
import org.elasticsearch.client.ResponseListener;
import org.elasticsearch.client.RestClient;
import org.junit.Test;
import microservice.MicroserviceClient.EnumRestClientType;
import microservice.params.CommandParams;
import microservice.params.RestClientParams;
import common.microservice.params.CommandParams;
import common.microservice.params.RestClientParams;
import com.fasterxml.jackson.databind.JsonNode;
import microservice.types.BaseRestResponse;
import rx.Observable;
import rx.apache.http.ObservableHttp;
import rx.apache.http.ObservableHttpResponse;
import common.microservice.types.BaseRestResponse;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.ByteBuffer;
import java.util.Collections;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import static microservice.utils.RestHttpClient.DEFAULT_CONNECTION_REQUEST_TIMEOUT_MILLIS;
import static microservice.utils.RestHttpClient.DEFAULT_CONNECT_TIMEOUT_MILLIS;
import static microservice.utils.RestHttpClient.DEFAULT_MAX_CONN_PER_ROUTE;
import static microservice.utils.RestHttpClient.DEFAULT_MAX_CONN_TOTAL;
import static microservice.utils.RestHttpClient.DEFAULT_SOCKET_TIMEOUT_MILLIS;
import static clients.microservice.utils.RestHttpClient.*;
public class TestMicroClient
......
package microservice;
import clients.microservice.impl.IRestClientHttpImpl;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;
import microservice.common.context.RestContext;
import microservice.defs.Enums;
import microservice.services.CommonServices;
import microservice.io.iface.IRestClient;
import microservice.io.impl.*;
import microservice.services.IRestServiceHttpImpl;
import microservice.params.*;
import microservice.services.IRestServiceZmqImpl;
import microservice.types.BaseRestResponse;
import common.microservice.context.RestContext;
import common.microservice.defs.Enums;
import common.microservice.io.iface.IRestClient;
import common.microservice.params.*;
import common.microservice.services.CommonServices;
import common.microservice.types.BaseRestResponse;
import microservice.io.impl.IRestServerRMQImpl;
import microservice.io.impl.IRestServerUndertowImpl;
import microservice.io.impl.IServiceDiscoveryConsulImpl;
import microservice.service.pubsub.impl.IRestServiceHttpImpl;
import microservice.service.pubsub.impl.IRestServiceZmqImpl;
import microservice.utils.ServiceBuilderFactory;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.junit.Test;
......@@ -37,7 +39,7 @@ public class TestMicroserviceApp {
String appName = "testApp";
//ILogger logger = new ILogger4jImpl(appName);
microservice.MicroserviceApp msApp = new microservice.MicroserviceApp(appName);
MicroserviceApp msApp = new microservice.MicroserviceApp(appName);
msApp.withMetrics()
//.withDefaultServiceAuthorization()
.withPubSub(new microservice.io.impl.IPubSubMQTTImpl("tcp://localhost",0,null,0))
......@@ -52,30 +54,30 @@ public class TestMicroserviceApp {
}
@Test
public void testRMQApp() throws MqttException, Exception
{
System.setProperty("configFile.location","/opt/mcx/config/config.properties");
BaseClientParams clientParams = new RestClientParams("other-service", true, 10, "localhost:32010","localhost:6379");
final IServiceDiscoveryConsulImpl serDisco = new IServiceDiscoveryConsulImpl("localhost", 8500);
IRestClient cmdClient = new IRestClientHttpImpl(clientParams).withServiceDiscovery(serDisco);
String appName = "testApp";
//ILogger logger = new ILogger4jImpl(appName);
microservice.MicroserviceApp msApp = new microservice.MicroserviceApp(appName);
msApp.withMetrics()
//.withDefaultServiceAuthorization()
.withPubSub(new microservice.io.impl.IPubSubMQTTImpl("tcp://localhost",0,null,0))
//.withServiceDiscovery(serDisco)
.withMonitoring()
// .addHandler("/test",new TestMicroserviceHandler())
.addMicroserviceClient(new MicroserviceClient(cmdClient,clientParams))
//.addMicroserviceClient("rabbit-service",new MicroserviceClient(EnumRestClientType.E_RABBITMQ,clientParams))
.addRestServer(new IRestServerUndertowImpl(new RestServerParams(32000, "localhost", 2)))
.addRestServer(new IRestServerRMQImpl(new RMQClientParams(appName, "myFirstQ@localhost", null, 1, 200, "/logs")))
.build()
.run();
}
// @Test
// public void testRMQApp() throws MqttException, Exception
// {
// System.setProperty("configFile.location","/opt/mcx/config/config.properties");
// BaseClientParams clientParams = new RestClientParams("other-service", true, 10, "localhost:32010","localhost:6379");
// final IServiceDiscoveryConsulImpl serDisco = new IServiceDiscoveryConsulImpl("localhost", 8500);
// IRestClient cmdClient = new IRestClientHttpImpl(clientParams).withServiceDiscovery(serDisco);
//
// String appName = "testApp";
// //ILogger logger = new ILogger4jImpl(appName);
// microservice.MicroserviceApp msApp = new microservice.MicroserviceApp(appName);
// msApp.withMetrics()
// //.withDefaultServiceAuthorization()
// .withPubSub(new microservice.io.impl.IPubSubMQTTImpl("tcp://localhost",0,null,0))
// //.withServiceDiscovery(serDisco)
// .withMonitoring()
// // .addHandler("/test",new TestMicroserviceHandler())
// .addMicroserviceClient(new MicroserviceClient(cmdClient,clientParams))
// //.addMicroserviceClient("rabbit-service",new MicroserviceClient(EnumRestClientType.E_RABBITMQ,clientParams))
// .addRestServer(new IRestServerUndertowImpl(new RestServerParams(32000, "localhost", 2)))
// .addRestServer(new IRestServerRMQImpl(new RMQClientParams(appName, "myFirstQ@localhost", null, 1, 200, "/logs")))
// .build()
// .run();
// }
@Test
......@@ -84,7 +86,7 @@ public class TestMicroserviceApp {
System.setProperty("configFile.location","/opt/mcx/config/config.properties");
System.setProperty("influxdb.hostport","172.16.1.244:8086");
String appName = "testApp";
String appName = "activities";
/**
* creating the services
*/
......@@ -100,6 +102,14 @@ public class TestMicroserviceApp {
CommonServices.IService zmqMsgQ = ServiceBuilderFactory.createMsgQServiceZmqBuilder(CommonServices.EnumMsgQServiceMode.E_SERVER)
.setServerParams(new ZMQParams.ServerParams(ZMQParams.ServerParams.EnumProtocol.eTcp,32020,"localhost"))
.build();
CommonServices.IService pulsarPubSub = ServiceBuilderFactory.createPubSubServicePulsarBuilder(CommonServices.EnumPubSubServiceMode.E_BOTH)
.setServiceUrl("localhost:6650")
.setMsgTtl(60)
.setRetentionSizeMB(20)
.setRetentionTimeMin(60)
.setAdminUrl(null)
.setClusters("standalone")
.build();
microservice.MicroserviceApp msApp = new microservice.MicroserviceApp(appName);
msApp.withMetrics()
......@@ -108,6 +118,7 @@ public class TestMicroserviceApp {
.addService(Enums.EnumServiceType.E_REST,httpRestService,"undertowRestService")
.addService(Enums.EnumServiceType.E_REST,zmqRestService,"zmqRestService")
.addService(Enums.EnumServiceType.E_MSGQ,zmqMsgQ,"zmqMsgQService")
.addService(Enums.EnumServiceType.E_PUBSUB,pulsarPubSub,"pulsarPubSub")
.addMethodClass(new MethodClass())
.addMethod(Enums.EnumServiceType.E_REST,CommonServices.EnumRestCommands.E_READ,"/asyncRegistry/{query}",(msgCtx,orgService) -> {
CommonServices.IRestService inRestService = (CommonServices.IRestService)MicroserviceApp.getsInstance().getService(Enums.EnumServiceType.E_REST,"undertowRestService");
......@@ -164,12 +175,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
......@@ -177,6 +204,13 @@ public class TestMicroserviceApp {
private void testZmqMsgQueue(CommonServices.IMsgQService.MsgQContext msgCtx) {
try {
JsonNode jsonNode = objectMapper.readValue(msgCtx.msg, JsonNode.class);
handleJsonTestStartStop(jsonNode);
} catch (IOException e) {
e.printStackTrace();
}
}
private void handleJsonTestStartStop(JsonNode jsonNode) {
String state = jsonNode.path("state").asText();
switch (state){
case "start":
......@@ -188,9 +222,6 @@ public class TestMicroserviceApp {
case "msg":
break;
}
} catch (IOException e) {
e.printStackTrace();
}
}
private void testZmqRead(RestContext msgCtx) {
......
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;
import org.apache.pulsar.client.admin.PulsarAdminException;
import org.apache.pulsar.client.admin.Tenants;
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;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Pattern;
public class TestPulsar {
@Test
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/activity", namespace);
PulsarClient client = PulsarClient.builder()
.serviceUrl(localClusterUrl)
.build();
Producer<PubSubMsg> producer = client
.newProducer(JSONSchema.of(PubSubMsg.class))
.topic(topic)
.create();
// final Producer<byte[]> producer = client.newProducer().topic(topic).create();
// Publish 10 messages to the topic
for (int i = 0; i < 100; i++) {
PubSubMsg pubSubMsg = new PubSubMsg("hello - " + String.valueOf(System.currentTimeMillis()),String.valueOf(i));
producer.send(pubSubMsg);
// producer.send(String.format("Message number %d", i).getBytes());
System.out.println("Sending message");
// final Message message = MessageBuilder.create()
// .setContent(String.format("Message number %d", i).getBytes())
// .build();
// producer.send(message);
}
producer.close();
client.close();
}
@Test
public void testTopicConsumer() throws PulsarClientException {
String localClusterUrl = "pulsar://localhost:6650";
String namespace = "mcx/testApp";
String topic = String.format("persistent://%s/my-topic", namespace);
PulsarClient client = PulsarClient.builder()
.serviceUrl(localClusterUrl)
.build();
//final Consumer consumer = client.newConsumer().subscriptionName("my-sub-1").topic(topic).subscribe();
Pattern allTopicsInNamespace = Pattern.compile("persistent://mcx/testApp/.*");
final Consumer<PubSubMsg> consumer = client.newConsumer(JSONSchema.of(PubSubMsg.class))
.subscriptionName(IDGenerator.createUUID())
.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<PubSubMsg> msg = consumer.receive();
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);
}
consumer.close();
client.close();
}
@Test
public void testPulsarAdmin() throws PulsarClientException, PulsarAdminException {
Set<String> clusters = new HashSet<>(Arrays.asList("standalone"));
PulsarAdmin admin = new PulsarAdminBuilderImpl().serviceHttpUrl("http://localhost:8080").build();
final Tenants tenants = admin.tenants();
final List<String> tenantList = tenants.getTenants();
final TenantInfo tenantInfo = new TenantInfo();
tenantInfo.setAllowedClusters(clusters);
if (!tenantList.contains("mcx")){
// creating tenant
tenants.createTenant("mcx", tenantInfo);
} else {
tenants.updateTenant("mcx",tenantInfo);
}
final List<String> namespaces = admin.namespaces().getNamespaces("mcx");
String namespaceStr = "mcx/testApp";
if (!namespaces.contains(namespaceStr)){
admin.namespaces().createNamespace(namespaceStr);
admin.namespaces().setNamespaceMessageTTL(namespaceStr,3600);
admin.namespaces().setNamespaceReplicationClusters(namespaceStr,clusters);
} else {
admin.namespaces().deleteNamespace(namespaceStr);
}
admin.close();
}
}
package microservice;
import microservice.defs.Enums;
import microservice.io.iface.IServiceDiscovery;
import common.microservice.defs.Enums;
import common.microservice.io.iface.IServiceDiscovery;
import microservice.io.impl.IServiceDiscoveryConsulImpl;
import org.junit.Test;
......
......@@ -14,13 +14,10 @@ import com.google.common.cache.CacheBuilder;
import common.JsonHandler;
import io.undertow.predicate.Predicate;
import io.undertow.util.PathMatcher;
import io.undertow.util.PathTemplateMatcher;
import microservice.params.ZMQParams;
import microservice.services.CommonServices;
import microservice.services.IRestServiceZmqImpl;
import microservice.types.BaseRestResponse;
import common.microservice.params.ZMQParams;
import common.microservice.services.CommonServices;
import microservice.utils.ServiceBuilderFactory;
import org.apache.commons.lang.SerializationUtils;
import org.junit.Assert;
import org.junit.Test;
......@@ -39,7 +36,7 @@ import java.util.stream.Collectors;
*/
public class TestServicesAndMethods {
public static final int ITERATIONS = 10000000;
public static final int ITERATIONS = 10000;
private static class RoutingMatch {
......@@ -264,7 +261,7 @@ public class TestServicesAndMethods {
.setClientParams(new ZMQParams.ServerParams(ZMQParams.ServerParams.EnumProtocol.eTcp, 32020, "localhost"))
.build();
CommonServices.IMsgQService msgQService = (CommonServices.IMsgQService)iService;
msgQService.init();
msgQService.init(null);
msgQService.run();
ObjectNode objectNode = JsonNodeFactory.instance.objectNode().put("state", "start").put("iterations", ITERATIONS);
System.out.println("Testing " + String.valueOf(ITERATIONS) + " iterations");
......@@ -278,4 +275,32 @@ public class TestServicesAndMethods {
msgQService.send(new CommonServices.IMsgQService.MsgQContext("/test/msgQ/zmq", objectNode.toString()));
//Thread.sleep(1000);
}
@Test
public void testPubSubPulsar() throws InterruptedException {
CommonServices.IService iService = ServiceBuilderFactory.createPubSubServicePulsarBuilder(CommonServices.EnumPubSubServiceMode.E_PUBLISHER)
.setServiceUrl("localhost:6650")
.setAdminUrl("localhost:8080")
// .setCreatePublicNamespace(true)
.build();
CommonServices.IPubSubService pubSubService = (CommonServices.IPubSubService)iService;
pubSubService.init(null);
pubSubService.run();
//String topic = "/testApp/activity"; // '[domain]/[method]'
String topic = "/activities/activity"; // '[domain]/[method]'
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(topic, 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(topic, objectNode.toString()));
}
objectNode.put("state", "end");
pubSubService.publish(new CommonServices.IPubSubService.PubSubMsgContext(topic, objectNode.toString()));
System.out.println("Async publish Test of: " + String.valueOf(ITERATIONS) +" took (msec): " + String.valueOf(System.currentTimeMillis() - start));
Thread.sleep(10000);
pubSubService.shutdown();
}
}
package microservice;
import com.fasterxml.jackson.databind.JsonNode;
import com.google.flatbuffers.FlatBufferBuilder;
import itc.ItcMessage;
import itc.ItcMessageQueue;
import microservice.common.context.CrudMethod;
import microservice.common.context.RestMsg;
import microservice.common.context.RestResponse;
import microservice.services.IRestServiceZmqImpl;
import org.apache.commons.lang.SerializationUtils;
import common.microservice.context.CrudMethod;
import common.microservice.context.RestMsg;
import common.microservice.context.RestResponse;
import org.junit.Assert;
import org.junit.Test;
import org.zeromq.*;
......@@ -19,7 +16,6 @@ import zmq.Utils;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
......
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