Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added microservice entry-point #278

Draft
wants to merge 11 commits into
base: master
Choose a base branch
from
Draft
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# th2 common library (Java) (5.13.1)
# th2 common library (Java) (5.14.0)

## Usage

Expand Down Expand Up @@ -511,6 +511,11 @@ dependencies {

## Release notes

### 5.14.0-dev
#### Feature:
+ Added common microservice entry point
+ Added configuration provider to common factory

### 5.13.1-dev

+ Provided ability to set either of raw body of several dody data to `Event` builder
Expand Down
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ dependencies {

implementation "io.github.microutils:kotlin-logging:3.0.5"

testImplementation 'org.hamcrest:hamcrest:2.2'
testImplementation 'javax.annotation:javax.annotation-api:1.3.2'
testImplementation "org.junit.jupiter:junit-jupiter:$junitVersion"
testImplementation "org.mockito.kotlin:mockito-kotlin:5.2.1"
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
release_version=5.13.1
release_version=5.14.0
kotlin_version=1.8.22
description='th2 common library (Java)'
vcs_url=https://github.com/th2-net/th2-common-j
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,16 @@
import com.exactpro.th2.common.metrics.PrometheusConfiguration;
import com.exactpro.th2.common.schema.box.configuration.BoxConfiguration;
import com.exactpro.th2.common.schema.configuration.ConfigurationManager;
import com.exactpro.th2.common.schema.configuration.impl.JsonConfigurationProvider;
import com.exactpro.th2.common.schema.cradle.CradleConfidentialConfiguration;
import com.exactpro.th2.common.schema.cradle.CradleNonConfidentialConfiguration;
import com.exactpro.th2.common.schema.dictionary.DictionaryType;
import com.exactpro.th2.common.schema.event.EventBatchRouter;
import com.exactpro.th2.common.schema.exception.CommonFactoryException;
import com.exactpro.th2.common.schema.grpc.configuration.GrpcConfiguration;
import com.exactpro.th2.common.schema.grpc.configuration.GrpcRouterConfiguration;
import com.exactpro.th2.common.schema.grpc.router.GrpcRouter;
import com.exactpro.th2.common.schema.grpc.router.impl.DefaultGrpcRouter;
import com.exactpro.th2.common.schema.message.MessageRouter;
import com.exactpro.th2.common.schema.message.MessageRouterContext;
import com.exactpro.th2.common.schema.message.MessageRouterMonitor;
Expand All @@ -55,24 +58,22 @@
import com.exactpro.th2.common.schema.message.impl.rabbitmq.connection.ConnectionManager;
import com.exactpro.th2.common.schema.message.impl.rabbitmq.custom.MessageConverter;
import com.exactpro.th2.common.schema.message.impl.rabbitmq.custom.RabbitCustomRouter;
import com.exactpro.th2.common.schema.message.impl.rabbitmq.group.RabbitMessageGroupBatchRouter;
import com.exactpro.th2.common.schema.message.impl.rabbitmq.notification.NotificationEventBatchRouter;
import com.exactpro.th2.common.schema.message.impl.rabbitmq.parsed.RabbitParsedBatchRouter;
import com.exactpro.th2.common.schema.message.impl.rabbitmq.raw.RabbitRawBatchRouter;
import com.exactpro.th2.common.schema.message.impl.rabbitmq.transport.GroupBatch;
import com.exactpro.th2.common.schema.message.impl.rabbitmq.transport.TransportGroupBatchRouter;
import com.exactpro.th2.common.schema.strategy.route.json.RoutingStrategyModule;
import com.exactpro.th2.common.schema.util.Log4jConfigUtils;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.module.kotlin.KotlinFeature;
import com.fasterxml.jackson.module.kotlin.KotlinModule;
import io.prometheus.client.exporter.HTTPServer;
import io.prometheus.client.hotspot.DefaultExports;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.text.StringSubstitutor;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
Expand All @@ -94,7 +95,6 @@
import static com.exactpro.th2.common.schema.factory.LazyProvider.lazy;
import static com.exactpro.th2.common.schema.factory.LazyProvider.lazyAutocloseable;
import static java.util.Objects.requireNonNull;
import static org.apache.commons.lang3.StringUtils.defaultIfBlank;

/**
* Class for load <b>JSON</b> schema configuration and create {@link GrpcRouter} and {@link MessageRouter}
Expand All @@ -111,25 +111,9 @@ public abstract class AbstractCommonFactory implements AutoCloseable {
protected static final Path LOG4J_PROPERTIES_DEFAULT_PATH = Path.of("/var/th2/config");
protected static final String LOG4J2_PROPERTIES_NAME = "log4j2.properties";

public static final ObjectMapper MAPPER = new ObjectMapper();

static {
MAPPER.registerModules(
new KotlinModule.Builder()
.withReflectionCacheSize(512)
.configure(KotlinFeature.NullToEmptyCollection, false)
.configure(KotlinFeature.NullToEmptyMap, false)
.configure(KotlinFeature.NullIsSameAsDefault, false)
.configure(KotlinFeature.SingletonSupport, false)
.configure(KotlinFeature.StrictNullChecks, false)
.build(),
new RoutingStrategyModule(MAPPER),
new JavaTimeModule()
);
}

static final String CUSTOM_CFG_ALIAS = "custom";
public static final ObjectMapper MAPPER = JsonConfigurationProvider.MAPPER;
private static final Logger LOGGER = LoggerFactory.getLogger(AbstractCommonFactory.class);
private final StringSubstitutor stringSubstitutor;

private final Class<? extends MessageRouter<MessageBatch>> messageRouterParsedBatchClass;
private final Class<? extends MessageRouter<RawMessageBatch>> messageRouterRawBatchClass;
Expand Down Expand Up @@ -168,6 +152,15 @@ public abstract class AbstractCommonFactory implements AutoCloseable {
configureLogger();
}

protected AbstractCommonFactory() {
messageRouterParsedBatchClass = RabbitParsedBatchRouter.class ;
messageRouterRawBatchClass = RabbitRawBatchRouter.class;
messageRouterMessageGroupBatchClass = RabbitMessageGroupBatchRouter.class;
eventBatchRouterClass = EventBatchRouter.class;
grpcRouterClass = DefaultGrpcRouter.class;
notificationEventBatchRouterClass = NotificationEventBatchRouter.class;
}

/**
* Create factory with non-default implementations schema classes
*
Expand All @@ -180,7 +173,6 @@ public AbstractCommonFactory(FactorySettings settings) {
eventBatchRouterClass = settings.getEventBatchRouterClass();
grpcRouterClass = settings.getGrpcRouterClass();
notificationEventBatchRouterClass = settings.getNotificationEventBatchRouterClass();
stringSubstitutor = new StringSubstitutor(key -> defaultIfBlank(settings.getVariables().get(key), System.getenv(key)));
}

public void start() {
Expand Down Expand Up @@ -321,11 +313,11 @@ public <T> MessageRouter<T> getCustomMessageRouter(Class<T> messageClass) {
}

/**
* @return Configuration by specified path
* @return Configuration by specified alias
* @throws IllegalStateException if can not read configuration
*/
public <T> T getConfiguration(Path configPath, Class<T> configClass, ObjectMapper customObjectMapper) {
return getConfigurationManager().loadConfiguration(customObjectMapper, stringSubstitutor, configClass, configPath, false);
public <T> T getConfiguration(String configAlias, Class<T> configClass, ObjectMapper customObjectMapper) {
OptimumCode marked this conversation as resolved.
Show resolved Hide resolved
return getConfigurationManager().loadConfiguration(configClass, configAlias, false);
}

/**
Expand All @@ -336,7 +328,7 @@ public <T> T getConfiguration(Path configPath, Class<T> configClass, ObjectMappe
* @return configuration object
*/
protected <T> T getConfigurationOrLoad(Class<T> configClass, boolean optional) {
return getConfigurationManager().getConfigurationOrLoad(MAPPER, stringSubstitutor, configClass, optional);
return getConfigurationManager().getConfigurationOrLoad(configClass, optional);
}

public RabbitMQConfiguration getRabbitMqConfiguration() {
Expand All @@ -352,7 +344,7 @@ public MessageRouterConfiguration getMessageRouterConfiguration() {
}

public GrpcConfiguration getGrpcConfiguration() {
return getConfigurationManager().getConfigurationOrLoad(MAPPER, stringSubstitutor, GrpcConfiguration.class, false);
return getConfigurationManager().getConfigurationOrLoad(GrpcConfiguration.class, false);
}

public GrpcRouterConfiguration getGrpcRouterConfiguration() {
Expand Down Expand Up @@ -510,17 +502,7 @@ private CradleManager createCradleManager() {
* @throws IllegalStateException if can not read configuration
*/
public <T> T getCustomConfiguration(Class<T> confClass, ObjectMapper customObjectMapper) {
File configFile = getPathToCustomConfiguration().toFile();
if (!configFile.exists()) {
try {
return confClass.getConstructor().newInstance();
} catch (InstantiationException | IllegalAccessException | InvocationTargetException |
NoSuchMethodException e) {
return null;
}
}

return getConfiguration(getPathToCustomConfiguration(), confClass, customObjectMapper);
return getConfiguration(CUSTOM_CFG_ALIAS, confClass, customObjectMapper);
}

/**
Expand Down Expand Up @@ -569,7 +551,8 @@ public <T> T getCustomConfiguration(Class<T> confClass) {
* @param dictionaryType desired type of dictionary
* @return Dictionary as {@link InputStream}
* @throws IllegalStateException if can not read dictionary
* @deprecated Dictionary types will be removed in future releases of infra, use alias instead
* @deprecated Dictionary types will be removed in future releases of infra, use alias instead.
* Please use {@link #loadDictionary(String)}
*/
@Deprecated(since = "3.33.0", forRemoval = true)
public abstract InputStream readDictionary(DictionaryType dictionaryType);
Expand Down Expand Up @@ -606,25 +589,6 @@ private EventID createRootEventID() throws IOException {

protected abstract ConfigurationManager getConfigurationManager();

/**
* @return Path to custom configuration
*/
protected abstract Path getPathToCustomConfiguration();
OptimumCode marked this conversation as resolved.
Show resolved Hide resolved

/**
* @return Path to dictionaries with type dir
*/
@Deprecated(since = "3.33.0", forRemoval = true)
protected abstract Path getPathToDictionaryTypesDir();

/**
* @return Path to dictionaries with alias dir
*/
protected abstract Path getPathToDictionaryAliasesDir();

@Deprecated(since = "3.33.0", forRemoval = true)
protected abstract Path getOldPathToDictionariesDir();

/**
* @return Context for all routers except event router
*/
Expand Down
Loading