Skip to content

Instantly share code, notes, and snippets.

@Carlos-Augusto
Last active March 9, 2022 09:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Carlos-Augusto/de071df7d3167bf0f13788f7597d2b1e to your computer and use it in GitHub Desktop.
Save Carlos-Augusto/de071df7d3167bf0f13788f7597d2b1e to your computer and use it in GitHub Desktop.
Cassandra Cluster Connection Configuration
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