Git Product home page Git Product logo

akka-esper-integration's Introduction

akka-esper-integration

Example how one could integrate Esper with Akka by embedding an esper engine inside an Akka event bus or Actor. Events submitted to the bus are inserted into the engine, whereas actors subscribing to the event bus will receive the events published as a result of the esper rules firing.

Example

class EsperEventBusExample extends ActorEventBus with EsperClassification {

  type EsperEvents = union[Price] #or [Sell] #or [Buy]

  override def esperEventTypes = new Union[EsperEvents]

  // generate a Buy order for a quantity of 1000 at the newest price,
  // if the simple average of the last 4 prices is greater than the oldest price in that collection of 4 prices
  // This is just one way you could do this in Esper, not necessarily the best way...
  epl(
    """
      insert into Buy
      select a.symbol as symbol, d.price as price, 1000 as amount
      from pattern[every a=Price -> b=Price(symbol=a.symbol) -> c=Price(symbol=a.symbol) -> d=Price(symbol=a.symbol)]
      where (a.price + b.price + c.price + d.price) > 4*a.price
    """)
}

Note the use of union types (from scalavro), this allows us to submit events of different types into the bus without those types requiring a common super type.

The above event bus is used as follows:

class BuyingActor extends Actor {
  def receive = {
    case Buy(sym,price,amt) => println(s"Got a new buy: $amt $sym @ $$$price")
  }
}

val system = ActorSystem()
val evtBus = new EsperEventBusExample
val buyer = system.actorOf(Props(classOf[BuyingActor]))

// subscribe to buys
evtBus.subscribe(buyer, "Buy")

After submitting the following data you'll see a Buy being generated:

val prices = Array(
Price("BP", 7.61), Price("RDSA", 2101.00), Price("RDSA", 2209.00),
Price("BP",7.66), Price("BP", 7.64), Price("BP", 7.67)
)

// feed in the market data
prices foreach (evtBus.publishEvent(_))

Output:

Got a new buy: 1000 BP @ $7.67

Update:

##Modules

Now also supports deploying esper modules (as traits). Here is an example module:

trait ExampleEsperModule extends EsperModule {
  self: EsperClassification =>

  val windowSize = 4
  val orderSize = 1000

  installModule(
    s"""
      module SimpleAverageTrader;

      @Name("Delayed")
      insert rstream into Delayed
      select rstream symbol,price
      from Price.std:groupwin(symbol).win:length(${windowSize-1});

      @Name("Averages")
      insert into Averages
      select symbol,avg(price) as price
      from Price.std:groupwin(symbol).win:length_batch($windowSize) group by symbol;

      @Name("Buy")
      insert into Buy
      select p.symbol, p.price, $orderSize as amount
      from Price.std:unique(symbol) p
      join Delayed.std:unique(symbol) d on d.symbol = p.symbol
      join Averages a unidirectional on a.symbol = p.symbol
      where a.price > d.price;
    """)
}

And the event bus definition becomes trivial:

class EsperEventBusWithModuleExample extends ActorEventBus with EsperClassification with ExampleEsperModule {
  type EsperEvents = union[Price] #or [Sell] #or [Buy]
  override def esperEventTypes = new Union[EsperEvents]
}

Loading modules from external resources is also supported. You can read from a File, InputStream, URL, or a scala Source

#Akka Streans

Now also includes an example of stream processing using Akka Streams instead of Esper.

akka-esper-integration's People

Contributors

fsauer65 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.