ecos-camel-core Library
The library is an extension built on top of Apache Camel, adapted for working with the Citeck platform.
Note
An enterprise license is required to use the library.
Setup
Add the dependency to the required microservice:
<dependency>
<groupId>ru.citeck.ecos.ent.camel</groupId>
<artifactId>ecos-camel-core</artifactId>
</dependency>
The appropriate version will be downloaded from the parent POM.
Usage Example
Create a component with route configurations that will launch a new context after the application starts:
package ru.citeck.ecos.webapp.demo;
import jakarta.annotation.PostConstruct;
import kotlin.Unit;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.apache.camel.Endpoint;
import org.apache.camel.builder.RouteBuilder;
import org.apache.groovy.util.Maps;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.stereotype.Component;
import ru.citeck.ecos.camel.context.EcosCamelContext;
import ru.citeck.ecos.camel.context.EcosCamelContextFactory;
import ru.citeck.ecos.commons.data.DataValue;
import ru.citeck.ecos.records2.predicate.model.Predicates;
import ru.citeck.ecos.webapp.api.EcosWebAppApi;
@Component
@RequiredArgsConstructor
public class EcosCamelExample extends RouteBuilder implements DisposableBean {
// для базовой работы нужны два бина:
private final EcosWebAppApi ecosWebAppApi; // запуск после инициализации приложения
private final EcosCamelContextFactory contextFactory; // создание нового контекста
// ссылку на контекст сохраняем в поле для последующей остановки
private EcosCamelContext context = null;
@SneakyThrows
@PostConstruct
public void init() {
// создаём и запускаем контекст только после инициализации приложения
ecosWebAppApi.doBeforeAppReady(0, () -> {
createAndStartContext();
return Unit.INSTANCE;
});
}
@SneakyThrows
private void createAndStartContext() {
// создаём новый контекст и регистрируем роуты
context = contextFactory.createContext().build();
context.addRoutes(this);
context.start();
}
@Override
public void configure() {
// endpoint для прослушивания событий ECOS с типом "record-created"
// и фильтром по типу записей "demo-type"
Endpoint endpoint = getContext().getEndpoint(
"ecos-event:record-created",
Maps.of(
"attributes", Maps.of(
"recordRef", "record?id",
"id", "record?localId",
"name", "record?disp",
"textField", "record.textField"
),
"filter", Predicates.eq("typeDef.id", "demo-type"),
"transactional", true
)
);
from(endpoint)
.to("log:after-event-received")
.process(exchange -> {
DataValue body = exchange.getMessage().getBody(DataValue.class);
DataValue newBody = DataValue.createObj();
newBody.set("id", body.get("id"));
newBody.set("name", body.get("name"));
newBody.set("textField", body.get("textField"));
exchange.getMessage().setBody(newBody);
})
.to("log:after-processing")
// создаём новую сущность в источнике данных emodel/demo-type-2
.to("ecos-records-mutate:?sourceId=emodel/demo-type-2");
}
@Override
public void destroy() throws Exception {
if (context != null) {
context.close();
}
}
}
The example demonstrates the following scenario:
When the application starts, a new
EcosCamelContextis created and launched via@PostConstruct.The route subscribes to the
record-createdevent of typedemo-type, extracting the required record attributes.In the
processstep, a new object is formed with the fields required to create a record in the target source.The result is passed to the
ecos-records-mutateendpoint, which creates a new entity inemodel/demo-type-2.When the application stops, the context is properly terminated in the
destroy()method.
See also
More about ecos-records-mutate