Skip to content

Instantly share code, notes, and snippets.

View ericacm's full-sized avatar

Some Dude ericacm

View GitHub Profile
@ericacm
ericacm / oauth2_jpa_provider.scala
Created July 2, 2011 13:11
Security OAuth 2 Provider and JPA Domain Classes
class ProviderTokenServices(dataSource: DataSource) extends JdbcOAuth2ProviderTokenServices(dataSource) with InitializingBean {
var log: Logger = LoggerFactory.getLogger(this.getClass.getName)
@Autowired var domainManager: DomainManager = _
@Autowired var schedulerService: SchedulerService = _
@Value("${providerTokenServices.pruneSchedule:0 0 12 * * *}")
var pruneSchedule: String = _
override def storeAccessToken(token: OAuth2AccessToken, authentication: OAuth2Authentication[_ <: Authentication, _ <: Authentication]) {
@ericacm
ericacm / minscalaactors.scala
Created August 10, 2012 15:41 — forked from viktorklang/minscalaactors.scala
Minimalist Scala Actors
// ©2012 Viktor Klang
object MiniActor {
import java.util.concurrent.{ConcurrentLinkedQueue, Executor}
import java.util.concurrent.atomic.AtomicInteger
type Behavior = Any => Effect
sealed trait Effect extends (Behavior => Behavior)
case object Stay extends Effect { def apply(old: Behavior): Behavior = old }
@ericacm
ericacm / CacheSystem.scala
Created August 25, 2012 16:55
Auto Updating Caching System - CacheSystem
class CacheSystem(name: String, updateIntervalMin: Int,
cacheManager: CacheManager) {
var caches = List.empty[Cache]
val actorSystem = ActorSystem("cache_" + name)
val DEFAULT_TTL_SEC = 86400 // 1 day
def addCache(name: String, size: Int,
ttlSeconds: Int = DEFAULT_TTL_SEC): Cache = {
@ericacm
ericacm / CachingBusinessService.scala
Created August 25, 2012 16:57
Auto Updating Caching System - CachingBusinessService
class CachingBusinessService(bizService: BusinessService)
extends BusinessService {
implicit val timeout = Timeout(60 seconds)
val service1CacheActor =
cacheSystem.createCacheActor("service1", DATE_CACHE_SIZE, 0 seconds,
new Service1CacheActor(_, _, bizService))
// ... more actors created here
@ericacm
ericacm / CacheActorObject.scala
Created August 25, 2012 16:59
Auto Updating Caching System - CacheActorObject.scala
object CacheActor {
case class FindValue(params: Params)
trait Params {
def cacheKey: String
}
// Thread pool used by findValueForSender()
val FUTURE_POOL_SIZE = 25
@ericacm
ericacm / CacheActor.scala
Created August 25, 2012 17:05
Auto Updating Caching System - CacheActor.scala
abstract class CacheActor[V](cacheSystem: CacheSystem)
extends Actor with Logging {
def findValueReceive: Receive = {
case FindValue(params) => findValueForSender(params, sender)
}
def findValueForSender(params: Params, sender: ActorRef) {
val key = params.cacheKey
val elem = cache.get(key)
@ericacm
ericacm / DateCacheActor.scala
Created August 25, 2012 17:15
Auto Updating Caching System - DateCacheActor.scala
abstract class DateCacheActor[V](cacheSystem: CacheSystem)
extends CacheActor[V](cacheSystem) {
override def receive = findValueReceive orElse {
case UpdateCacheForNow =>
updateCacheForNow()
case UpdateCacheForPreviousBusinessDay =>
updateCacheForPreviousBusinessDay()
}
@ericacm
ericacm / Service1CacheActor.scala
Created August 25, 2012 17:19
Auto Updating Caching System - Service1CacheActor.scala
class Service1CacheActor(val cache: Cache, cacheSystem: CacheSystem,
bizService: BusinessService)
extends DateCacheActor[JList[Service1Result]](cacheSystem) {
override def receive = super.receive
override def updateCacheForDate(date: Date) {
import DateCacheActor._
Future { findObject(new Service1Params(date, true)) }
Future { findObject(new Service1Params(date, false)) }
@ericacm
ericacm / FutureTimeoutSupport.scala
Last active July 11, 2016 22:32
Future timeout support
import akka.util.{Duration, NonFatal}
import akka.actor.Scheduler
import akka.dispatch.{Promise, ExecutionContext, Future}
// Copied from Akka 2.1-M1
trait FutureTimeoutSupport {
/**
* Returns a [[akka.dispatch.Future]] that will be completed with the success or failure of the provided value
* after the specified duration.
*/