Skip to content

Instantly share code, notes, and snippets.

@piotrga
piotrga / gist:1705083
Created January 30, 2012 15:50
Managed
import io.Source
import java.io.FileWriter
object ManagedDemo extends App{
import Managed._
import Managed.{managed => ®}
for (
fw <- ®(new FileWriter("target/test-out.txt"));
@piotrga
piotrga / gist:1664313
Created January 23, 2012 17:13
Producer - use-case
import akka.actor.{ ActorRef, Terminated, AllForOneStrategy, Actor, Props, ActorSystem }
import org.apache.camel.builder.RouteBuilder
import org.apache.camel.impl.DefaultMessage
import org.apache.camel.{ Exchange, Processor}
import org.apache.camel.spi.Synchronization
import akka.camel.{Message, Failure, CamelExtension}
import akka.dispatch.Await
object CamelRoutes extends App{
val system = ActorSystem("Test")
@piotrga
piotrga / gist:1659645
Created January 23, 2012 00:35
ProducerRegistry
private[camel] case class RegisterProducer(actorRef: ActorRef, endpointUri: String)
/**
* Watches the end of life of <code>Producer</code>s.
* Removes a <code>Producer</code> from the <code>ProducerRegistry</code> when it is <code>Terminated</code>,
* which in turn stops the <code>SendProcessor</code>.
*/
private[camel] class CamelProducerIdempotentRegistry(camelContext: CamelContext) extends Actor {
private val camelObjects = new HashMap[ActorRef, (Endpoint,SendProcessor)]()
@piotrga
piotrga / gist:1578160
Created January 8, 2012 12:15
tryAll
object ErrorUtils{
/**
* Executes a block and returns the result wrapped by Right class, or exception wrapped by Left class.
*/
def either[T](block:() => T) : Either[Throwable,T] = try {Right(block())} catch {case e => Left(e)}
/**
* Executes all blocks in order and collects exceptions. It guarantees to execute all blocks, even if some of them fail.
* It throws a BlockException, if any number of blocks fail. BlockException contains a list of thrown exceptions.
*
@piotrga
piotrga / gist:1570689
Created January 6, 2012 13:48
Mockito fun 1
import org.mockito.Mockito._
import org.scalatest.mock.MockitoSugar
import org.mockito.Matchers
object MockitoFun1 extends App with MockitoSugar{
trait X{
def x
}
@piotrga
piotrga / gist:1547297
Created January 1, 2012 13:12
mockito in scala
package akka.camel
import akka.actor._
import org.scalatest.FlatSpec
import org.scalatest.matchers.ShouldMatchers
import org.scalatest.mock.MockitoSugar
import org.mockito.Mockito._
import org.mockito.Matchers.{eq => the, any}
class ConsumerScalaTest extends FlatSpec with ShouldMatchers with MockitoSugar{
@piotrga
piotrga / gist:1533463
Created December 29, 2011 10:45
save to file
val f = new FileWriter("some/file/name.txt")
f.write("My string I want to save")
f.close()
@piotrga
piotrga / gist:1533459
Created December 29, 2011 10:44
SaveAs
"My string I want to save".saveAs("some/file/name.txt")
@piotrga
piotrga / gist:1533450
Created December 29, 2011 10:43
Pimping the String
implicit def toRichString(s:String) : RichString = new RichString(s)
class RichString(s: String){
def saveAs(fileName: String) = write(fileName, s)
private[this] def write(fileName: String, content: String) {
val f = new FileWriter(fileName)
f.write(content)
f.close()
}
@piotrga
piotrga / gist:1528130
Created December 28, 2011 14:30
Camel integration
object Camel{
lazy val context = new DefaultCamelContext
def addRoute(routeBuilder: RouteBuilder) {context addRoutes routeBuilder}
def start { context start }
}
trait Consum{ self: Actor =>
val camel = Camel
val consumer = self
def from(route:String) = camel.addRoute(new RouteBuilder(){