-
-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
104 additions
and
68 deletions.
There are no files selected for viewing
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
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
63 changes: 63 additions & 0 deletions
63
examples/src/main/scala/com/github/gvolpe/fs2rabbit/examples/GenericDemo.scala
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,63 @@ | ||
package com.github.gvolpe.fs2rabbit.examples | ||
|
||
import cats.effect.Effect | ||
import com.github.gvolpe.fs2rabbit.Fs2Rabbit.{createAckerConsumer, createConnectionChannel, createPublisher, declareQueue} | ||
import com.github.gvolpe.fs2rabbit.Fs2Utils.asyncF | ||
import com.github.gvolpe.fs2rabbit.{EffectScheduler, StreamLoop} | ||
import com.github.gvolpe.fs2rabbit.json.Fs2JsonEncoder.jsonEncode | ||
import com.github.gvolpe.fs2rabbit.model._ | ||
import fs2.{Pipe, Stream} | ||
|
||
import scala.concurrent.ExecutionContext | ||
import scala.language.higherKinds | ||
|
||
class GenericDemo[F[_]](implicit F: Effect[F], ES: EffectScheduler[F]) { | ||
|
||
implicit val appS = scala.concurrent.ExecutionContext.Implicits.global | ||
implicit val appR = fs2.Scheduler.fromFixedDaemonPool(2, "restarter") | ||
|
||
val queueName: QueueName = "test" | ||
val routingKey: RoutingKey = "test" | ||
|
||
def logPipe: Pipe[F, AmqpEnvelope, AckResult] = { streamMsg => | ||
for { | ||
amqpMsg <- streamMsg | ||
_ <- asyncF[F, Unit](println(s"Consumed: $amqpMsg")) | ||
} yield Ack(amqpMsg.deliveryTag) | ||
} | ||
|
||
val program = () => for { | ||
connAndChannel <- createConnectionChannel[F]() | ||
(_, channel) = connAndChannel | ||
_ <- declareQueue[F](channel, queueName) | ||
(acker, consumer) = createAckerConsumer[F](channel, queueName) | ||
publisher = createPublisher[F](channel, "", routingKey) | ||
result <- new Flow(consumer, acker, logPipe, publisher).flow | ||
} yield result | ||
|
||
StreamLoop.run(program) | ||
|
||
} | ||
|
||
class Flow[F[_]](consumer: StreamConsumer[F], | ||
acker: StreamAcker[F], | ||
logger: Pipe[F, AmqpEnvelope, AckResult], | ||
publisher: StreamPublisher[F]) | ||
(implicit ec: ExecutionContext, F: Effect[F]) { | ||
|
||
import io.circe.generic.auto._ | ||
|
||
case class Address(number: Int, streetName: String) | ||
case class Person(id: Long, name: String, address: Address) | ||
|
||
val simpleMessage = AmqpMessage("Hey!", AmqpProperties(None, None, Map("demoId" -> LongVal(123), "app" -> StringVal("fs2RabbitDemo")))) | ||
val classMessage = AmqpMessage(Person(1L, "Sherlock", Address(212, "Baker St")), AmqpProperties.empty) | ||
|
||
val flow: Stream[F, Unit] = | ||
Stream( | ||
Stream(simpleMessage).covary[F] to publisher, | ||
Stream(classMessage).covary[F] through jsonEncode[F, Person] to publisher, | ||
consumer through logger to acker | ||
).join(3) | ||
|
||
} |
59 changes: 1 addition & 58 deletions
59
examples/src/main/scala/com/github/gvolpe/fs2rabbit/examples/IODemo.scala
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,62 +1,5 @@ | ||
package com.github.gvolpe.fs2rabbit.examples | ||
|
||
import cats.effect.IO | ||
import com.github.gvolpe.fs2rabbit.Fs2Rabbit._ | ||
import com.github.gvolpe.fs2rabbit.Fs2Utils._ | ||
import com.github.gvolpe.fs2rabbit.StreamLoop | ||
import com.github.gvolpe.fs2rabbit.model._ | ||
import com.github.gvolpe.fs2rabbit.json.Fs2JsonEncoder._ | ||
import fs2.{Pipe, Stream} | ||
|
||
import scala.concurrent.ExecutionContext | ||
|
||
object IODemo extends App { | ||
|
||
implicit val appS = scala.concurrent.ExecutionContext.Implicits.global | ||
implicit val appR = fs2.Scheduler.fromFixedDaemonPool(2, "restarter") | ||
|
||
val queueName: QueueName = "test" | ||
val routingKey: RoutingKey = "test" | ||
|
||
def logPipe: Pipe[IO, AmqpEnvelope, AckResult] = { streamMsg => | ||
for { | ||
amqpMsg <- streamMsg | ||
_ <- asyncF[IO, Unit](println(s"Consumed: $amqpMsg")) | ||
} yield Ack(amqpMsg.deliveryTag) | ||
} | ||
|
||
val program = () => for { | ||
connAndChannel <- createConnectionChannel[IO]() | ||
(_, channel) = connAndChannel | ||
_ <- declareQueue[IO](channel, queueName) | ||
(acker, consumer) = createAckerConsumer[IO](channel, queueName) | ||
publisher = createPublisher[IO](channel, "", routingKey) | ||
result <- new Flow(consumer, acker, logPipe, publisher).flow | ||
} yield result | ||
|
||
StreamLoop.run(program) | ||
|
||
} | ||
|
||
class Flow(consumer: StreamConsumer[IO], | ||
acker: StreamAcker[IO], | ||
logger: Pipe[IO, AmqpEnvelope, AckResult], | ||
publisher: StreamPublisher[IO]) | ||
(implicit ec: ExecutionContext) { | ||
|
||
import io.circe.generic.auto._ | ||
|
||
case class Address(number: Int, streetName: String) | ||
case class Person(id: Long, name: String, address: Address) | ||
|
||
val simpleMessage = AmqpMessage("Hey!", AmqpProperties(None, None, Map("demoId" -> LongVal(123), "app" -> StringVal("fs2RabbitDemo")))) | ||
val classMessage = AmqpMessage(Person(1L, "Sherlock", Address(212, "Baker St")), AmqpProperties.empty) | ||
|
||
val flow: Stream[IO, Unit] = | ||
Stream( | ||
Stream(simpleMessage).covary[IO] to publisher, | ||
Stream(classMessage).covary[IO] through jsonEncode[IO, Person] to publisher, | ||
consumer through logger to acker | ||
).join(3) | ||
|
||
} | ||
object IODemo extends GenericDemo[IO] with App |
5 changes: 5 additions & 0 deletions
5
examples/src/main/scala/com/github/gvolpe/fs2rabbit/examples/MonixTaskDemo.scala
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,5 @@ | ||
package com.github.gvolpe.fs2rabbit.examples | ||
|
||
import monix.eval.Task | ||
|
||
object MonixTaskDemo extends GenericDemo[Task] with App |
5 changes: 3 additions & 2 deletions
5
examples/src/main/scala/com/github/gvolpe/fs2rabbit/examples/package.scala
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,9 +1,10 @@ | ||
package com.github.gvolpe.fs2rabbit | ||
|
||
import com.github.gvolpe.fs2rabbit.examples.scheduler.IOEffectScheduler | ||
import com.github.gvolpe.fs2rabbit.examples.scheduler.{IOEffectScheduler, MonixEffectScheduler} | ||
|
||
package object examples { | ||
|
||
implicit val iOEffectScheduler = IOEffectScheduler | ||
implicit val iOEffectScheduler = IOEffectScheduler | ||
implicit val monixEffectScheduler = MonixEffectScheduler | ||
|
||
} |
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
21 changes: 21 additions & 0 deletions
21
.../src/main/scala/com/github/gvolpe/fs2rabbit/examples/scheduler/MonixEffectScheduler.scala
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,21 @@ | ||
package com.github.gvolpe.fs2rabbit.examples.scheduler | ||
|
||
import com.github.gvolpe.fs2rabbit.EffectScheduler | ||
import fs2.Scheduler | ||
import monix.eval.Task | ||
import monix.execution.Scheduler.Implicits.global | ||
|
||
import scala.concurrent.{Await, ExecutionContext} | ||
import scala.concurrent.duration._ | ||
|
||
object MonixEffectScheduler extends EffectScheduler[Task] { | ||
|
||
override def schedule[A](effect: Task[A], delay: FiniteDuration) | ||
(implicit ec: ExecutionContext, s: Scheduler) = { | ||
effect.delayExecution(delay) | ||
} | ||
|
||
override def unsafeRunSync(effect: Task[Unit]) = { | ||
Await.result(effect.runAsync, 1.second) | ||
} | ||
} |