refactoring init/setup method
This commit is contained in:
parent
9e1e8dcf74
commit
a7aade03b7
2 changed files with 52 additions and 58 deletions
|
|
@ -27,25 +27,16 @@
|
||||||
|
|
||||||
package ai.dynamixs;
|
package ai.dynamixs;
|
||||||
|
|
||||||
import java.util.logging.Level;
|
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
import org.imixs.workflow.engine.SetupEvent;
|
import org.imixs.workflow.engine.SetupEvent;
|
||||||
import org.imixs.workflow.office.config.AccessRoleController;
|
import org.imixs.workflow.office.config.AccessRoleController;
|
||||||
import org.imixs.workflow.office.config.PropertiesConfigSource;
|
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.DeclareRoles;
|
||||||
import jakarta.annotation.security.RolesAllowed;
|
import jakarta.annotation.security.RolesAllowed;
|
||||||
import jakarta.ejb.Stateless;
|
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.enterprise.event.Observes;
|
||||||
import jakarta.inject.Inject;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This DynamixsSetupService reacts on the
|
* This DynamixsSetupService reacts on the
|
||||||
|
|
@ -69,42 +60,12 @@ import jakarta.inject.Inject;
|
||||||
public class DynamixsSetupService {
|
public class DynamixsSetupService {
|
||||||
private static Logger logger = Logger.getLogger(DynamixsSetupService.class.getName());
|
private static Logger logger = Logger.getLogger(DynamixsSetupService.class.getName());
|
||||||
|
|
||||||
@Inject
|
|
||||||
ClusterService clusterService;
|
|
||||||
|
|
||||||
@Resource
|
|
||||||
private TimerService timerService;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Try to connect to Cassandra
|
* Init....
|
||||||
*
|
*
|
||||||
* @param setupEvent
|
* @param setupEvent
|
||||||
*/
|
*/
|
||||||
public void setup(@Observes SetupEvent 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
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@ import java.net.InetSocketAddress;
|
||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
import java.util.logging.Level;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
import org.apache.commons.compress.archivers.ArchiveException;
|
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.cql.PreparedStatement;
|
||||||
import com.datastax.oss.driver.api.core.servererrors.InvalidQueryException;
|
import com.datastax.oss.driver.api.core.servererrors.InvalidQueryException;
|
||||||
|
|
||||||
|
import jakarta.annotation.PostConstruct;
|
||||||
import jakarta.annotation.PreDestroy;
|
import jakarta.annotation.PreDestroy;
|
||||||
|
import jakarta.annotation.Resource;
|
||||||
import jakarta.ejb.Singleton;
|
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;
|
import jakarta.inject.Inject;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -35,6 +43,7 @@ import jakarta.inject.Inject;
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
@Singleton
|
@Singleton
|
||||||
|
@Startup
|
||||||
public class ClusterService {
|
public class ClusterService {
|
||||||
|
|
||||||
public static final String KEYSPACE_REGEX = "^[a-z_]*[^-]$";
|
public static final String KEYSPACE_REGEX = "^[a-z_]*[^-]$";
|
||||||
|
|
@ -109,23 +118,36 @@ public class ClusterService {
|
||||||
|
|
||||||
private CqlSession session;
|
private CqlSession session;
|
||||||
|
|
||||||
/**
|
@Resource
|
||||||
* This method tries to initalize the cassandara session object.
|
private TimerService timerService;
|
||||||
*
|
|
||||||
* Note: the initialization is only successful if the cassandra cluster is fully
|
@PostConstruct
|
||||||
* started.
|
public void init() {
|
||||||
*
|
scheduleClusterCheck();
|
||||||
* @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.");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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
|
@PreDestroy
|
||||||
|
|
@ -140,13 +162,24 @@ public class ClusterService {
|
||||||
* Returns the Cassandra Session object. If the session is not yet created, the
|
* 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 tries to init the session the first time. If initialization failed the
|
||||||
* method throws a ClusterException
|
* method throws a ClusterException
|
||||||
|
* <p>
|
||||||
|
* Note: the initialization is only successful if the cassandra cluster is fully
|
||||||
|
* started.
|
||||||
|
* <p>
|
||||||
|
* The method also verifies the keyspace and creates a table schema if not yet
|
||||||
|
* available.
|
||||||
*
|
*
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public CqlSession getSession() throws ClusterException {
|
public CqlSession getSession() throws ClusterException {
|
||||||
if (session == null) {
|
if (session == null) {
|
||||||
try {
|
try {
|
||||||
init();
|
logger.info("├── initializing cluster...");
|
||||||
|
session = initSession();
|
||||||
|
if (session != null) {
|
||||||
|
logger.info("│ ├── keySpace connection status = OK");
|
||||||
|
initKeySpace();
|
||||||
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new ClusterException(ClusterException.CLUSTER_ERROR,
|
throw new ClusterException(ClusterException.CLUSTER_ERROR,
|
||||||
"Failed to init cassandra session: " + e.getMessage(), e);
|
"Failed to init cassandra session: " + e.getMessage(), e);
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue