Last active
March 9, 2022 09:43
-
-
Save Carlos-Augusto/de071df7d3167bf0f13788f7597d2b1e to your computer and use it in GitHub Desktop.
Cassandra Cluster Connection Configuration
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.ubirch | |
package services.cluster | |
import com.ubirch.ConfPaths.CassandraClusterConfPaths | |
import com.ubirch.services.lifeCycle.Lifecycle | |
import com.typesafe.config.Config | |
import com.typesafe.scalalogging.LazyLogging | |
import io.getquill.{ CassandraStreamContext, NamingStrategy, SnakeCase } | |
import javax.inject._ | |
import scala.concurrent.Future | |
/** | |
* Component that represent the basic configuration for the ConnectionService Component. | |
*/ | |
trait ConnectionServiceConfig { | |
val keyspace: String | |
val preparedStatementCacheSize: Long | |
} | |
/** | |
* Component that represents a Connection Service. | |
* A Connection Service represents the connection established to the | |
* Cassandra database. | |
*/ | |
trait ConnectionServiceBase extends ConnectionServiceConfig { | |
type N <: NamingStrategy | |
val context: CassandraStreamContext[N] | |
} | |
/** | |
* Component that represents a Connection Service whose Naming Strategy | |
* is ShakeCase. | |
*/ | |
trait ConnectionService extends ConnectionServiceBase { | |
type N = SnakeCase.type | |
} | |
/** | |
* Default Implementation of the Connection Service Component. | |
* It add shutdown hooks. | |
* @param clusterService Cluster Service Component. | |
* @param config Configuration injected component. | |
* @param lifecycle Lifecycle injected component that allows for shutdown hooks. | |
*/ | |
@Singleton | |
class DefaultConnectionService @Inject() (clusterService: ClusterService, config: Config, lifecycle: Lifecycle) | |
extends ConnectionService with CassandraClusterConfPaths with LazyLogging { | |
val keyspace: String = config.getString(KEYSPACE) | |
val preparedStatementCacheSize: Long = config.getLong(PREPARED_STATEMENT_CACHE_SIZE) | |
if (keyspace.isEmpty) { | |
throw NoKeyspaceException("Keyspace must be provided.") | |
} | |
private def createContext() = new CassandraStreamContext( | |
SnakeCase, | |
clusterService.cluster, | |
keyspace, | |
preparedStatementCacheSize | |
) | |
override val context = { | |
val conn = createContext() | |
logger.info("Connected to keyspace: " + keyspace) | |
conn | |
} | |
lifecycle.addStopHook { () => | |
logger.info("Shutting down Connection Service") | |
Future.successful(context.close()) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment