Commit 99f80084 by Amir Aharon

version 2.3.1

- add retention policy and ttl for pulsar, move PulsarParams from common
parent a15d7791
### Microservice Framework in JAVA ### Microservice Framework in JAVA
## 2.3.1
- add retention policy and ttl for pulsar, move PulsarParams from common
## 2.3.0 ## 2.3.0
- Seperate Services from main lib - Seperate Services from main lib
- Split to common,clients,servicePubSub and app jars - Split to common,clients,servicePubSub and app jars
......
plugins {
id "org.sonarqube" version "2.7"
}
group 'com.ipgallery.common' group 'com.ipgallery.common'
version '2.3.0' version '2.3.1'
apply plugin: 'java' apply plugin: 'java'
apply plugin: 'maven-publish' apply plugin: 'maven-publish'
//for mavenLocal //for mavenLocal
//apply plugin: 'maven' //apply plugin: 'maven'
sourceCompatibility = 1.8 sourceCompatibility = 1.8
repositories { repositories {
...@@ -105,6 +108,8 @@ dependencies { ...@@ -105,6 +108,8 @@ dependencies {
testCompile ( testCompile (
files('build/common/microservice-common.jar'),
files('build/clients/microservice-clients.jar'),
'junit:junit:4.11', 'junit:junit:4.11',
'org.zeromq:jeromq:0.4.0', 'org.zeromq:jeromq:0.4.0',
'com.github.stephenc.eaio-uuid:uuid:3.4.0' 'com.github.stephenc.eaio-uuid:uuid:3.4.0'
...@@ -114,7 +119,7 @@ dependencies { ...@@ -114,7 +119,7 @@ dependencies {
task commonJar(type: Jar) { task commonJar(type: Jar) {
from configurations.commonCompile.collect { zipTree it } from configurations.commonCompile.collect { zipTree it }
from sourceSets.common.output from sourceSets.common.output
version = '1.0.1' version = '1.0.2'
archiveName = "microservice-common.jar" archiveName = "microservice-common.jar"
destinationDir = file("build/common") destinationDir = file("build/common")
} }
...@@ -125,7 +130,7 @@ task servicePubsubJar(type: Jar) { ...@@ -125,7 +130,7 @@ task servicePubsubJar(type: Jar) {
mustRunAfter commonJar mustRunAfter commonJar
//from configurations.servicePubsubCompile.collect { zipTree it } //from configurations.servicePubsubCompile.collect { zipTree it }
from sourceSets.servicePubsub.output from sourceSets.servicePubsub.output
version = '1.0.0' version = '1.0.1'
archiveName = "microservice-service-pubsub.jar" archiveName = "microservice-service-pubsub.jar"
destinationDir = file("build/servicePubsub") destinationDir = file("build/servicePubsub")
} }
...@@ -171,6 +176,15 @@ assemble { ...@@ -171,6 +176,15 @@ assemble {
// destinationDir = file("build/main") // 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 { publishing {
publications { publications {
......
...@@ -18,6 +18,9 @@ import service.microservice.IPubSubServicePulsarImpl; ...@@ -18,6 +18,9 @@ import service.microservice.IPubSubServicePulsarImpl;
public class ServiceBuilderFactory { 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){ public static RestServiceHttpBuilder createRestServiceHttpBuilder(CommonServices.EnumRestServiceMode serviceMode){
return new RestServiceHttpBuilder(serviceMode); return new RestServiceHttpBuilder(serviceMode);
} }
...@@ -95,11 +98,11 @@ public class ServiceBuilderFactory { ...@@ -95,11 +98,11 @@ public class ServiceBuilderFactory {
break; break;
} }
} catch (Exception exp){ } catch (Exception exp){
System.err.println(this.getClass().getName().toString() + "Exception >> " + exp); System.err.println(this.getClass().getName().toString() + EXCEPTION + exp);
restServiceHttp = null; restServiceHttp = null;
} }
} else { } 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; return restServiceHttp;
} }
...@@ -209,11 +212,11 @@ public class ServiceBuilderFactory { ...@@ -209,11 +212,11 @@ public class ServiceBuilderFactory {
break; break;
} }
} catch (Exception exp){ } catch (Exception exp){
System.err.println(this.getClass().getName().toString() + "Exception >> " + exp); System.err.println(this.getClass().getName().toString() + EXCEPTION + exp);
restServiceZmq = null; restServiceZmq = null;
} }
} else { } 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; return restServiceZmq;
} }
...@@ -303,11 +306,11 @@ public class ServiceBuilderFactory { ...@@ -303,11 +306,11 @@ public class ServiceBuilderFactory {
break; break;
} }
} catch (Exception exp){ } catch (Exception exp){
System.err.println(this.getClass().getName().toString() + "Exception >> " + exp); System.err.println(this.getClass().getName().toString() + EXCEPTION + exp);
msgQServiceZmq = null; msgQServiceZmq = null;
} }
} else { } 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; return msgQServiceZmq;
} }
...@@ -355,6 +358,21 @@ public class ServiceBuilderFactory { ...@@ -355,6 +358,21 @@ public class ServiceBuilderFactory {
return this; 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 * msgs published to all services has 'public' namespace
* @param createPublicNamespace * @param createPublicNamespace
...@@ -382,11 +400,11 @@ public class ServiceBuilderFactory { ...@@ -382,11 +400,11 @@ public class ServiceBuilderFactory {
} }
} catch (Exception exp){ } catch (Exception exp){
System.err.println(this.getClass().getName().toString() + "Exception >> " + exp); System.err.println(this.getClass().getName().toString() + EXCEPTION + exp);
pubSubServicePulsar = null; pubSubServicePulsar = null;
} }
} else { } else {
System.err.println(this.getClass().getName().toString() + " >> Failed in validating params"); System.err.println(this.getClass().getName().toString() + FAILED_IN_VALIDATING_PARAMS);
} }
return pubSubServicePulsar; return pubSubServicePulsar;
} }
......
package common.microservice.params; package common.microservice.params;
import org.apache.commons.lang.StringUtils; import org.apache.pulsar.shade.org.apache.commons.lang.StringUtils;
/** /**
* parameters to init pulsar pub-sub * parameters to init pulsar pub-sub
* namepaces and topic - admin * namepaces and topic - admin
*/ */
public class PulsarParams { public class PulsarParams {
private final int retentionTimeMin;
private final int retentionSizeMB;
int msgTtl; int msgTtl;
String clusters = "standalone";; String clusters;
String serviceUrl; String serviceUrl;
String adminUrl; String adminUrl;
int consumersThreadPoolSize; int consumersThreadPoolSize;
...@@ -24,8 +26,11 @@ public class PulsarParams { ...@@ -24,8 +26,11 @@ public class PulsarParams {
String adminUrl = null; String adminUrl = null;
int consumersThreadPoolSize = 0; int consumersThreadPoolSize = 0;
boolean createPublicNamespace = false; boolean createPublicNamespace = false;
int retentionTimeMin = 1440; // a day
int retentionSizeMB = 100; // 100MB
public PulsarParamsBuilder() { public PulsarParamsBuilder() {
// empty
} }
public String getServiceUrl() { public String getServiceUrl() {
...@@ -68,6 +73,16 @@ public class PulsarParams { ...@@ -68,6 +73,16 @@ public class PulsarParams {
return this; 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(){ public PulsarParams build(){
try { try {
if (adminUrl == null) { if (adminUrl == null) {
...@@ -78,6 +93,8 @@ public class PulsarParams { ...@@ -78,6 +93,8 @@ public class PulsarParams {
consumersThreadPoolSize = Runtime.getRuntime().availableProcessors(); consumersThreadPoolSize = Runtime.getRuntime().availableProcessors();
} }
return new PulsarParams(msgTtl, return new PulsarParams(msgTtl,
retentionTimeMin,
retentionSizeMB,
clusters, clusters,
serviceUrl, serviceUrl,
adminUrl, adminUrl,
...@@ -90,8 +107,17 @@ public class PulsarParams { ...@@ -90,8 +107,17 @@ public class PulsarParams {
} }
} }
public PulsarParams(int msgTtl, String clusters, String serviceUrl, String adminUrl, int consumersThreadPoolSize, boolean createPublicNamespace) { public PulsarParams(int msgTtl,
int retentionTimeMin,
int retentionSizeMB,
String clusters,
String serviceUrl,
String adminUrl,
int consumersThreadPoolSize,
boolean createPublicNamespace) {
this.msgTtl = msgTtl; this.msgTtl = msgTtl;
this.retentionTimeMin = retentionTimeMin;
this.retentionSizeMB = retentionSizeMB;
this.clusters = clusters; this.clusters = clusters;
this.serviceUrl = serviceUrl; this.serviceUrl = serviceUrl;
this.adminUrl = adminUrl; this.adminUrl = adminUrl;
...@@ -103,16 +129,16 @@ public class PulsarParams { ...@@ -103,16 +129,16 @@ public class PulsarParams {
return msgTtl; return msgTtl;
} }
public void setMsgTtl(int msgTtl) { public int getRetentionTimeMin() {
this.msgTtl = msgTtl; return retentionTimeMin;
} }
public String getClusters() { public int getRetentionSizeMB() {
return clusters; return retentionSizeMB;
} }
public void setClusters(String clusters) { public String getClusters() {
this.clusters = clusters; return clusters;
} }
public String getServiceUrl() { public String getServiceUrl() {
...@@ -135,10 +161,6 @@ public class PulsarParams { ...@@ -135,10 +161,6 @@ public class PulsarParams {
return consumersThreadPoolSize; return consumersThreadPoolSize;
} }
public void setConsumersThreadPoolSize(int consumersThreadPoolSize) {
this.consumersThreadPoolSize = consumersThreadPoolSize;
}
public boolean isCreatePublicNamespace() { public boolean isCreatePublicNamespace() {
return createPublicNamespace; return createPublicNamespace;
} }
......
...@@ -15,6 +15,7 @@ import org.apache.pulsar.client.admin.Tenants; ...@@ -15,6 +15,7 @@ import org.apache.pulsar.client.admin.Tenants;
import org.apache.pulsar.client.admin.internal.PulsarAdminBuilderImpl; import org.apache.pulsar.client.admin.internal.PulsarAdminBuilderImpl;
import org.apache.pulsar.client.api.*; import org.apache.pulsar.client.api.*;
import org.apache.pulsar.client.impl.schema.JSONSchema; 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 org.apache.pulsar.common.policies.data.TenantInfo;
...@@ -368,6 +369,8 @@ public class IPubSubServicePulsarImpl extends CommonServices.IPubSubService { ...@@ -368,6 +369,8 @@ public class IPubSubServicePulsarImpl extends CommonServices.IPubSubService {
logger.info(SERVICE_NAME + " >> Creating namespace: " + namespaceStr); logger.info(SERVICE_NAME + " >> Creating namespace: " + namespaceStr);
admin.namespaces().createNamespace(namespaceStr); admin.namespaces().createNamespace(namespaceStr);
admin.namespaces().setNamespaceMessageTTL(namespaceStr,pulsarParams.getMsgTtl()); 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.namespaces().setNamespaceReplicationClusters(namespaceStr,clusters);
} }
admin.close(); admin.close();
......
...@@ -2,6 +2,7 @@ package microservice; ...@@ -2,6 +2,7 @@ package microservice;
import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import common.microservice.io.iface.ILogger;
import common.microservice.io.iface.IRestClient; import common.microservice.io.iface.IRestClient;
import microservice.io.impl.IRMQClientRestImpl; import microservice.io.impl.IRMQClientRestImpl;
import common.microservice.params.BaseClientParams; import common.microservice.params.BaseClientParams;
...@@ -48,6 +49,11 @@ public class TestCommandClient { ...@@ -48,6 +49,11 @@ public class TestCommandClient {
} }
@Override @Override
public void init(ILogger logger) {
}
@Override
public BaseRestResponse create(CommandParams reqCtx) { public BaseRestResponse create(CommandParams reqCtx) {
BaseRestResponse brr = new BaseRestResponse(true, null); BaseRestResponse brr = new BaseRestResponse(true, null);
Optional<Observable<BaseRestResponse>> brro = Optional.empty(); Optional<Observable<BaseRestResponse>> brro = Optional.empty();
......
package microservice; package microservice;
import clients.microservice.impl.IRestClientHttpImpl;
import clients.microservice.utils.RestHttpClient;
import com.fasterxml.jackson.databind.node.JsonNodeFactory; import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import common.microservice.defs.Enums; import common.microservice.defs.Enums;
import common.microservice.io.iface.IRestClient; import common.microservice.io.iface.IRestClient;
import microservice.io.impl.IRestClientHttpImpl;
import microservice.io.impl.IServiceDiscoveryConsulImpl; import microservice.io.impl.IServiceDiscoveryConsulImpl;
import microservice.utils.RestHttpClient;
import okhttp3.Call; import okhttp3.Call;
import okhttp3.Callback; import okhttp3.Callback;
import okhttp3.OkHttpClient; import okhttp3.OkHttpClient;
...@@ -38,11 +38,7 @@ import java.util.concurrent.CountDownLatch; ...@@ -38,11 +38,7 @@ import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future; import java.util.concurrent.Future;
import static microservice.utils.RestHttpClient.DEFAULT_CONNECTION_REQUEST_TIMEOUT_MILLIS; import static clients.microservice.utils.RestHttpClient.*;
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;
public class TestMicroClient public class TestMicroClient
......
...@@ -54,30 +54,30 @@ public class TestMicroserviceApp { ...@@ -54,30 +54,30 @@ public class TestMicroserviceApp {
} }
@Test // @Test
public void testRMQApp() throws MqttException, Exception // public void testRMQApp() throws MqttException, Exception
{ // {
System.setProperty("configFile.location","/opt/mcx/config/config.properties"); // System.setProperty("configFile.location","/opt/mcx/config/config.properties");
BaseClientParams clientParams = new RestClientParams("other-service", true, 10, "localhost:32010","localhost:6379"); // BaseClientParams clientParams = new RestClientParams("other-service", true, 10, "localhost:32010","localhost:6379");
final IServiceDiscoveryConsulImpl serDisco = new IServiceDiscoveryConsulImpl("localhost", 8500); // final IServiceDiscoveryConsulImpl serDisco = new IServiceDiscoveryConsulImpl("localhost", 8500);
IRestClient cmdClient = new IRestClientHttpImpl(clientParams).withServiceDiscovery(serDisco); // IRestClient cmdClient = new IRestClientHttpImpl(clientParams).withServiceDiscovery(serDisco);
//
String appName = "testApp"; // String appName = "testApp";
//ILogger logger = new ILogger4jImpl(appName); // //ILogger logger = new ILogger4jImpl(appName);
microservice.MicroserviceApp msApp = new microservice.MicroserviceApp(appName); // microservice.MicroserviceApp msApp = new microservice.MicroserviceApp(appName);
msApp.withMetrics() // msApp.withMetrics()
//.withDefaultServiceAuthorization() // //.withDefaultServiceAuthorization()
.withPubSub(new microservice.io.impl.IPubSubMQTTImpl("tcp://localhost",0,null,0)) // .withPubSub(new microservice.io.impl.IPubSubMQTTImpl("tcp://localhost",0,null,0))
//.withServiceDiscovery(serDisco) // //.withServiceDiscovery(serDisco)
.withMonitoring() // .withMonitoring()
// .addHandler("/test",new TestMicroserviceHandler()) // // .addHandler("/test",new TestMicroserviceHandler())
.addMicroserviceClient(new MicroserviceClient(cmdClient,clientParams)) // .addMicroserviceClient(new MicroserviceClient(cmdClient,clientParams))
//.addMicroserviceClient("rabbit-service",new MicroserviceClient(EnumRestClientType.E_RABBITMQ,clientParams)) // //.addMicroserviceClient("rabbit-service",new MicroserviceClient(EnumRestClientType.E_RABBITMQ,clientParams))
.addRestServer(new IRestServerUndertowImpl(new RestServerParams(32000, "localhost", 2))) // .addRestServer(new IRestServerUndertowImpl(new RestServerParams(32000, "localhost", 2)))
.addRestServer(new IRestServerRMQImpl(new RMQClientParams(appName, "myFirstQ@localhost", null, 1, 200, "/logs"))) // .addRestServer(new IRestServerRMQImpl(new RMQClientParams(appName, "myFirstQ@localhost", null, 1, 200, "/logs")))
.build() // .build()
.run(); // .run();
} // }
@Test @Test
...@@ -104,6 +104,9 @@ public class TestMicroserviceApp { ...@@ -104,6 +104,9 @@ public class TestMicroserviceApp {
.build(); .build();
CommonServices.IService pulsarPubSub = ServiceBuilderFactory.createPubSubServicePulsarBuilder(CommonServices.EnumPubSubServiceMode.E_BOTH) CommonServices.IService pulsarPubSub = ServiceBuilderFactory.createPubSubServicePulsarBuilder(CommonServices.EnumPubSubServiceMode.E_BOTH)
.setServiceUrl("localhost:6650") .setServiceUrl("localhost:6650")
.setMsgTtl(60)
.setRetentionSizeMB(20)
.setRetentionTimeMin(60)
.setAdminUrl(null) .setAdminUrl(null)
.setClusters("standalone") .setClusters("standalone")
.build(); .build();
......
...@@ -261,7 +261,7 @@ public class TestServicesAndMethods { ...@@ -261,7 +261,7 @@ public class TestServicesAndMethods {
.setClientParams(new ZMQParams.ServerParams(ZMQParams.ServerParams.EnumProtocol.eTcp, 32020, "localhost")) .setClientParams(new ZMQParams.ServerParams(ZMQParams.ServerParams.EnumProtocol.eTcp, 32020, "localhost"))
.build(); .build();
CommonServices.IMsgQService msgQService = (CommonServices.IMsgQService)iService; CommonServices.IMsgQService msgQService = (CommonServices.IMsgQService)iService;
msgQService.init(); msgQService.init(null);
msgQService.run(); msgQService.run();
ObjectNode objectNode = JsonNodeFactory.instance.objectNode().put("state", "start").put("iterations", ITERATIONS); ObjectNode objectNode = JsonNodeFactory.instance.objectNode().put("state", "start").put("iterations", ITERATIONS);
System.out.println("Testing " + String.valueOf(ITERATIONS) + " iterations"); System.out.println("Testing " + String.valueOf(ITERATIONS) + " iterations");
...@@ -284,7 +284,7 @@ public class TestServicesAndMethods { ...@@ -284,7 +284,7 @@ public class TestServicesAndMethods {
// .setCreatePublicNamespace(true) // .setCreatePublicNamespace(true)
.build(); .build();
CommonServices.IPubSubService pubSubService = (CommonServices.IPubSubService)iService; CommonServices.IPubSubService pubSubService = (CommonServices.IPubSubService)iService;
pubSubService.init(); pubSubService.init(null);
pubSubService.run(); pubSubService.run();
//String topic = "/testApp/activity"; // '[domain]/[method]' //String topic = "/testApp/activity"; // '[domain]/[method]'
String topic = "/activities/activity"; // '[domain]/[method]' String topic = "/activities/activity"; // '[domain]/[method]'
......
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