diff --git a/dynamixs-ai-rag/src/main/java/ai/dynamixs/DynamixsSetupService.java b/dynamixs-ai-rag/src/main/java/ai/dynamixs/DynamixsSetupService.java index 36bbea2..af6fb00 100644 --- a/dynamixs-ai-rag/src/main/java/ai/dynamixs/DynamixsSetupService.java +++ b/dynamixs-ai-rag/src/main/java/ai/dynamixs/DynamixsSetupService.java @@ -27,25 +27,16 @@ package ai.dynamixs; -import java.util.logging.Level; import java.util.logging.Logger; import org.imixs.workflow.engine.SetupEvent; import org.imixs.workflow.office.config.AccessRoleController; import org.imixs.workflow.office.config.PropertiesConfigSource; -import ai.dynamixs.rag.ClusterException; -import ai.dynamixs.rag.ClusterService; -import jakarta.annotation.Resource; import jakarta.annotation.security.DeclareRoles; import jakarta.annotation.security.RolesAllowed; import jakarta.ejb.Stateless; -import jakarta.ejb.Timeout; -import jakarta.ejb.Timer; -import jakarta.ejb.TimerConfig; -import jakarta.ejb.TimerService; import jakarta.enterprise.event.Observes; -import jakarta.inject.Inject; /** * This DynamixsSetupService reacts on the @@ -69,42 +60,12 @@ import jakarta.inject.Inject; public class DynamixsSetupService { private static Logger logger = Logger.getLogger(DynamixsSetupService.class.getName()); - @Inject - ClusterService clusterService; - - @Resource - private TimerService timerService; - /** - * Try to connect to Cassandra + * Init.... * * @param setupEvent */ public void setup(@Observes SetupEvent setupEvent) { - scheduleClusterCheck(); - } - - /** - * Versucht, die Session herzustellen. Bei Fehler wird ein neuer Timer geplant. - */ - private void scheduleClusterCheck() { - try { - logger.log(Level.INFO, "├── Connecting Cassandra cluster..."); - clusterService.getSession(); - logger.log(Level.INFO, "├── ✅ Successfully connected to Cassandra!"); - } catch (ClusterException e) { - logger.log(Level.WARNING, "├── " + e.getMessage()); - logger.log(Level.WARNING, "├── ⚠️ Cluster not ready yet. Retrying in 10 seconds..."); - // retry in 10 sec - timerService.createSingleActionTimer(10000, new TimerConfig()); - } - } - - /** - * called by timer. Tries to connect cassandra clauster - */ - @Timeout - public void retryClusterCheck(Timer timer) { - scheduleClusterCheck(); // retry Check + // } } diff --git a/dynamixs-ai-rag/src/main/java/ai/dynamixs/rag/ClusterService.java b/dynamixs-ai-rag/src/main/java/ai/dynamixs/rag/ClusterService.java index f099115..655d12b 100644 --- a/dynamixs-ai-rag/src/main/java/ai/dynamixs/rag/ClusterService.java +++ b/dynamixs-ai-rag/src/main/java/ai/dynamixs/rag/ClusterService.java @@ -4,6 +4,7 @@ import java.net.InetSocketAddress; import java.time.Instant; import java.util.Optional; import java.util.UUID; +import java.util.logging.Level; import java.util.logging.Logger; import org.apache.commons.compress.archivers.ArchiveException; @@ -15,8 +16,15 @@ import com.datastax.oss.driver.api.core.cql.BoundStatement; import com.datastax.oss.driver.api.core.cql.PreparedStatement; import com.datastax.oss.driver.api.core.servererrors.InvalidQueryException; +import jakarta.annotation.PostConstruct; import jakarta.annotation.PreDestroy; +import jakarta.annotation.Resource; import jakarta.ejb.Singleton; +import jakarta.ejb.Startup; +import jakarta.ejb.Timeout; +import jakarta.ejb.Timer; +import jakarta.ejb.TimerConfig; +import jakarta.ejb.TimerService; import jakarta.inject.Inject; /** @@ -35,6 +43,7 @@ import jakarta.inject.Inject; * */ @Singleton +@Startup public class ClusterService { public static final String KEYSPACE_REGEX = "^[a-z_]*[^-]$"; @@ -109,23 +118,36 @@ public class ClusterService { private CqlSession session; - /** - * This method tries to initalize the cassandara session object. - * - * Note: the initialization is only successful if the cassandra cluster is fully - * started. - * - * @throws ClusterException - */ - private void init() throws ClusterException { - logger.info("├── initializing cluster..."); - session = initSession(); - if (session != null) { - logger.info("│ ├── keySpace connection status = OK"); - initKeySpace(); - logger.info("├── ✅ initializing cluster successful."); - } + @Resource + private TimerService timerService; + @PostConstruct + public void init() { + scheduleClusterCheck(); + } + + /** + * Versucht, die Session herzustellen. Bei Fehler wird ein neuer Timer geplant. + */ + private void scheduleClusterCheck() { + try { + logger.log(Level.INFO, "├── Connecting Cassandra cluster..."); + getSession(); + logger.log(Level.INFO, "├── ✅ Successfully connected to Cassandra!"); + } catch (ClusterException e) { + logger.log(Level.WARNING, "├── " + e.getMessage()); + logger.log(Level.WARNING, "├── ⚠️ Cluster not ready yet. Retrying in 10 seconds..."); + // retry in 10 sec + timerService.createSingleActionTimer(10000, new TimerConfig()); + } + } + + /** + * called by timer. Tries to connect cassandra clauster + */ + @Timeout + public void retryClusterCheck(Timer timer) { + scheduleClusterCheck(); // retry Check } @PreDestroy @@ -140,13 +162,24 @@ public class ClusterService { * Returns the Cassandra Session object. If the session is not yet created, the * method tries to init the session the first time. If initialization failed the * method throws a ClusterException + *
+ * Note: the initialization is only successful if the cassandra cluster is fully + * started. + *
+ * The method also verifies the keyspace and creates a table schema if not yet + * available. * * @return */ public CqlSession getSession() throws ClusterException { if (session == null) { try { - init(); + logger.info("├── initializing cluster..."); + session = initSession(); + if (session != null) { + logger.info("│ ├── keySpace connection status = OK"); + initKeySpace(); + } } catch (Exception e) { throw new ClusterException(ClusterException.CLUSTER_ERROR, "Failed to init cassandra session: " + e.getMessage(), e);