-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactored publish package to be functional. added Chain of Responsib…
…ility pattern implementation in commons.util.Chain
- Loading branch information
Showing
7 changed files
with
77 additions
and
76 deletions.
There are no files selected for viewing
16 changes: 0 additions & 16 deletions
16
akka/src/main/scala/peds/akka/publish/EventPublisher.scala
This file was deleted.
Oops, something went wrong.
23 changes: 0 additions & 23 deletions
23
akka/src/main/scala/peds/akka/publish/LocalPublisher.scala
This file was deleted.
Oops, something went wrong.
This file contains 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
This file contains 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
14 changes: 0 additions & 14 deletions
14
akka/src/main/scala/peds/akka/publish/SilentPublisher.scala
This file was deleted.
Oops, something went wrong.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,35 @@ | ||
package peds.akka | ||
|
||
import scala.util.Try | ||
import akka.actor.{ ActorContext, ActorLogging } | ||
import com.typesafe.scalalogging.LazyLogging | ||
import shapeless.syntax.typeable._ | ||
import peds.commons.log.Trace | ||
import peds.akka.envelope.Envelope | ||
import peds.commons.util.Chain | ||
|
||
|
||
package object publish { | ||
package object publish extends LazyLogging { | ||
|
||
sealed trait ReliableProtocol | ||
case class ReliableMessage( deliveryId: Long, message: Envelope ) extends ReliableProtocol | ||
case class Confirm( deliveryId: Long ) extends ReliableProtocol | ||
private[this] val trace = Trace( "peds.akka.publish", logger ) | ||
|
||
type Publisher = Chain.Link[Envelope, Unit] | ||
|
||
case class RedeliveryFailedException( message: Any ) extends RuntimeException | ||
|
||
trait EventPublisher extends ActorStack with ActorLogging { | ||
def publish: Publisher = local | ||
} | ||
|
||
|
||
def local( implicit context: ActorContext ): Publisher = ( event: Envelope ) => trace.block( s"publish.local($event)" ) { | ||
val target = context.system.eventStream | ||
event.cast[target.Event] foreach { e => | ||
logger info s"local stream publishing event:${e} on target:${target}" | ||
//DMR: somehow need to update envelope per EnvelopeSending.update | ||
target publish e | ||
} | ||
Left( event ) | ||
} | ||
|
||
val silent: Publisher = ( event: Envelope ) => trace.block( s"publish.silent($event)" ) { Left( event ) } | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package peds.commons.util | ||
|
||
|
||
object Chain { | ||
type Link[A, B] = A => Either[A, B] | ||
implicit def func2Chain[A, B]( f: Link[A, B] ): Chain[A, B] = new Chain[A, B]( f ) | ||
} | ||
|
||
|
||
/** | ||
* Implements a functional version of the Chain of Responsibility pattern. The Chain of Responsibility pattern is a GoF design | ||
* pattern consisting of a source of command objects and a series of processing objects. Each processing object contains logic | ||
* that defines the types of command objects that it can handle; the rest are passed to the next processing object in the chain. | ||
* | ||
* In this Scala version, processing steps are defined as functions, which the Chain links together via either the chainWith() or | ||
* +> operations. The return value is defined as an Either[A, B]: | ||
* - Left[A] to direct processing by the next handler | ||
* - Right[B] to direct that processing must stop with the preceeding result. | ||
*/ | ||
class Chain[A, B]( f: Chain.Link[A, B] ) { | ||
|
||
def chainWith( next: => Chain.Link[A, B] ): Chain.Link[A, B] = f( _ ).left.flatMap( next ) // call next handler if not yet processed | ||
|
||
def +>( next: => Chain.Link[A, B] ): Chain.Link[A, B] = chainWith( next ) | ||
} |