Merge branch 'RED-7889' into 'main'
Red 7889 See merge request fforesight/database-tenant-commons!13
This commit is contained in:
commit
bf9d2e67d3
1
.gitignore
vendored
1
.gitignore
vendored
@ -31,3 +31,4 @@ build/
|
|||||||
|
|
||||||
### VS Code ###
|
### VS Code ###
|
||||||
.vscode/
|
.vscode/
|
||||||
|
.DS_Store/
|
||||||
|
|||||||
5
pom.xml
5
pom.xml
@ -21,6 +21,11 @@
|
|||||||
<guava.version>32.1.2-jre</guava.version>
|
<guava.version>32.1.2-jre</guava.version>
|
||||||
</properties>
|
</properties>
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.fasterxml.jackson.core</groupId>
|
||||||
|
<artifactId>jackson-databind</artifactId>
|
||||||
|
<version>${jackson-bom.version}</version>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.knecon.fforesight</groupId>
|
<groupId>com.knecon.fforesight</groupId>
|
||||||
<artifactId>tenant-commons</artifactId>
|
<artifactId>tenant-commons</artifactId>
|
||||||
|
|||||||
@ -1,6 +1,5 @@
|
|||||||
package com.knecon.fforesight.databasetenantcommons;
|
package com.knecon.fforesight.databasetenantcommons;
|
||||||
|
|
||||||
|
|
||||||
import org.springframework.amqp.core.Binding;
|
import org.springframework.amqp.core.Binding;
|
||||||
import org.springframework.amqp.core.BindingBuilder;
|
import org.springframework.amqp.core.BindingBuilder;
|
||||||
import org.springframework.amqp.core.Queue;
|
import org.springframework.amqp.core.Queue;
|
||||||
@ -23,13 +22,28 @@ public class TenantMessagingConfiguration {
|
|||||||
@Value("${fforesight.multitenancy.tenant-created-dlq:tenant-created-dlq}")
|
@Value("${fforesight.multitenancy.tenant-created-dlq:tenant-created-dlq}")
|
||||||
private String tenantCreatedDLQ;
|
private String tenantCreatedDLQ;
|
||||||
|
|
||||||
|
@Value("${fforesight.multitenancy.tenant-sync-queue:tenant-sync}")
|
||||||
|
private String tenantSyncEventQueue;
|
||||||
|
|
||||||
|
@Value("${fforesight.multitenancy.tenant-sync-dlq:tenant-sync-dlq}")
|
||||||
|
private String tenantSyncDQL;
|
||||||
|
|
||||||
|
|
||||||
|
@Bean(name = "tenantExchange")
|
||||||
|
TopicExchange tenantExchange(@Value("${fforesight.tenant-exchange.name}") String tenantExchangeName) {
|
||||||
|
|
||||||
|
return new TopicExchange(tenantExchangeName);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@Bean("persistenceServiceTenantCreatedQueue")
|
@Bean("persistenceServiceTenantCreatedQueue")
|
||||||
public Queue persistenceServiceTenantCreatedQueue() {
|
public Queue persistenceServiceTenantCreatedQueue() {
|
||||||
|
|
||||||
return QueueBuilder.durable(tenantCreatedEventQueue)
|
return QueueBuilder.durable(tenantCreatedEventQueue)
|
||||||
.withArgument("x-dead-letter-exchange", "").withArgument("x-dead-letter-routing-key", tenantCreatedDLQ).build();
|
.withArgument("x-dead-letter-exchange", "").withArgument("x-dead-letter-routing-key", tenantCreatedDLQ).build();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public Queue persistenceServiceTenantDLQ() {
|
public Queue persistenceServiceTenantDLQ() {
|
||||||
|
|
||||||
@ -40,13 +54,31 @@ public class TenantMessagingConfiguration {
|
|||||||
@Bean
|
@Bean
|
||||||
public Binding tenantExchangeBinding(@Qualifier("persistenceServiceTenantCreatedQueue") Queue persistenceServiceTenantCreatedQueue,
|
public Binding tenantExchangeBinding(@Qualifier("persistenceServiceTenantCreatedQueue") Queue persistenceServiceTenantCreatedQueue,
|
||||||
@Qualifier("tenantExchange") TopicExchange tenantExchange) {
|
@Qualifier("tenantExchange") TopicExchange tenantExchange) {
|
||||||
|
|
||||||
return BindingBuilder.bind(persistenceServiceTenantCreatedQueue).to(tenantExchange).with("tenant.created");
|
return BindingBuilder.bind(persistenceServiceTenantCreatedQueue).to(tenantExchange).with("tenant.created");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean(name = "tenantExchange")
|
|
||||||
TopicExchange tenantExchange(@Value("${fforesight.tenant-exchange.name}") String tenantExchangeName) {
|
|
||||||
|
|
||||||
return new TopicExchange(tenantExchangeName);
|
@Bean("persistenceServiceTenantSyncQueue")
|
||||||
|
public Queue persistenceServiceTenantSyncQueue() {
|
||||||
|
|
||||||
|
return QueueBuilder.durable(tenantSyncEventQueue)
|
||||||
|
.withArgument("x-dead-letter-exchange", "").withArgument("x-dead-letter-routing-key", tenantSyncDQL).build();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public Queue persistenceServiceTenantSyncDLQ() {
|
||||||
|
|
||||||
|
return QueueBuilder.durable(tenantSyncDQL).build();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public Binding tenantExchangeSyncBinding(@Qualifier("persistenceServiceTenantSyncQueue") Queue persistenceServiceTenantSyncQueue,
|
||||||
|
@Qualifier("tenantExchange") TopicExchange tenantExchange) {
|
||||||
|
|
||||||
|
return BindingBuilder.bind(persistenceServiceTenantSyncQueue).to(tenantExchange).with("tenant.sync");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -45,7 +45,7 @@ public class TenantSpringLiquibaseExecutor implements InitializingBean, Resource
|
|||||||
log.info("DynamicDataSources based multi-tenancy enabled");
|
log.info("DynamicDataSources based multi-tenancy enabled");
|
||||||
try {
|
try {
|
||||||
this.runOnAllTenants(tenantProvider.getTenants());
|
this.runOnAllTenants(tenantProvider.getTenants());
|
||||||
}catch (RetryableException e){
|
} catch (RetryableException e) {
|
||||||
log.warn("Tenant Service not online, skipping liquibase migration: {}", e.getMessage(), e);
|
log.warn("Tenant Service not online, skipping liquibase migration: {}", e.getMessage(), e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -63,11 +63,12 @@ public class TenantSpringLiquibaseExecutor implements InitializingBean, Resource
|
|||||||
encryptionService.decrypt(tenant.getDatabaseConnection().getPassword()))) {
|
encryptionService.decrypt(tenant.getDatabaseConnection().getPassword()))) {
|
||||||
DataSource tenantDataSource = new SingleConnectionDataSource(connection, false);
|
DataSource tenantDataSource = new SingleConnectionDataSource(connection, false);
|
||||||
SpringLiquibase liquibase = this.getSpringLiquibase(tenantDataSource);
|
SpringLiquibase liquibase = this.getSpringLiquibase(tenantDataSource);
|
||||||
|
liquibase.setClearCheckSums(tenantLiquibaseProperties.isClearChecksums());
|
||||||
liquibase.setDefaultSchema(tenant.getDatabaseConnection().getSchema());
|
liquibase.setDefaultSchema(tenant.getDatabaseConnection().getSchema());
|
||||||
liquibase.setLiquibaseSchema(tenant.getDatabaseConnection().getSchema());
|
liquibase.setLiquibaseSchema(tenant.getDatabaseConnection().getSchema());
|
||||||
liquibase.afterPropertiesSet();
|
liquibase.afterPropertiesSet();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("Failed to run liquibase migration on tenant: {}", tenant.getTenantId());
|
log.error("Failed to run liquibase migration on tenant: {}", tenant.getTenantId(), e);
|
||||||
}
|
}
|
||||||
log.info("Liquibase ran for tenant " + tenant.getTenantId());
|
log.info("Liquibase ran for tenant " + tenant.getTenantId());
|
||||||
}
|
}
|
||||||
|
|||||||
@ -0,0 +1,45 @@
|
|||||||
|
package com.knecon.fforesight.databasetenantcommons.providers;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
import org.springframework.amqp.rabbit.annotation.RabbitListener;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import com.knecon.fforesight.databasetenantcommons.providers.TenantLiquibaseInitializer;
|
||||||
|
import com.knecon.fforesight.databasetenantcommons.providers.events.TenantCreatedEvent;
|
||||||
|
import com.knecon.fforesight.databasetenantcommons.providers.events.TenantSyncEvent;
|
||||||
|
|
||||||
|
import jakarta.annotation.PostConstruct;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import lombok.SneakyThrows;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
|
@Service
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class TenantSyncListener {
|
||||||
|
|
||||||
|
private final Optional<TenantSyncService> tenantSyncServices;
|
||||||
|
|
||||||
|
@Value("${fforesight.multitenancy.tenant-sync-queue:tenant-sync}")
|
||||||
|
private String tenantSyncQueue;
|
||||||
|
|
||||||
|
|
||||||
|
@PostConstruct
|
||||||
|
public void postConstruct() {
|
||||||
|
|
||||||
|
log.info("Listener for tenant-sync started for queue: {}", tenantSyncQueue);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@SneakyThrows
|
||||||
|
@RabbitListener(queues = "${fforesight.multitenancy.tenant-sync-queue:tenant-sync}")
|
||||||
|
public void createTenant(TenantSyncEvent tenantSyncEvent) {
|
||||||
|
|
||||||
|
tenantSyncServices.ifPresent(t -> t.syncTenant(tenantSyncEvent));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,9 @@
|
|||||||
|
package com.knecon.fforesight.databasetenantcommons.providers;
|
||||||
|
|
||||||
|
import com.knecon.fforesight.databasetenantcommons.providers.events.TenantSyncEvent;
|
||||||
|
|
||||||
|
public interface TenantSyncService {
|
||||||
|
|
||||||
|
void syncTenant(TenantSyncEvent tenantSyncEvent);
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,17 @@
|
|||||||
|
package com.knecon.fforesight.databasetenantcommons.providers.events;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.databind.JsonNode;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@AllArgsConstructor
|
||||||
|
@NoArgsConstructor
|
||||||
|
public class TenantSyncEvent {
|
||||||
|
|
||||||
|
private String tenantId;
|
||||||
|
private JsonNode payload;
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user