Skip to content

Instantly share code, notes, and snippets.

View johanandren's full-sized avatar
👻
hakking

Johan Andrén johanandren

👻
hakking
View GitHub Profile
import akka.actor.ActorSystem
import akka.stream.scaladsl.Flow
import akka.stream.scaladsl.Tcp
import akka.util.ByteString
object TcpServerOneResponseThenComplete2 {
def main(args: Array[String]): Unit = {
implicit val system = ActorSystem()
import akka.stream.scaladsl.Framing
import akka.actor.ActorSystem
import akka.stream.scaladsl.Flow
import akka.stream.scaladsl.Tcp
import akka.util.ByteString
object TcpServerOneResponseThenComplete {
def main(args: Array[String]): Unit = {
implicit val system = ActorSystem()
import akka.stream.scaladsl.Framing
@johanandren
johanandren / stuff.java
Created January 2, 2020 15:34
Java mock actor child
class MyActor extends AbstractActor {
public Receive createReceive() {
return receiveBuilder()
.match(Request.class, this::onRequest)
.build();
}
private void onRequest(Request request) {
ActorRef child = createChildForRequest(request);
@johanandren
johanandren / ProgrammaticJoin.scala
Created October 4, 2018 17:30
Three node Akka cluster in one JVM with minimal fuzz
import akka.actor.ActorSystem
import akka.cluster.Cluster
import com.typesafe.config.ConfigFactory
object ProgrammaticJoin extends App {
val commonConfig = ConfigFactory.parseString(
"""
akka {
actor.provider = cluster
remote.artery.enabled = true
import akka.NotUsed
import akka.actor.ActorSystem
import akka.stream.scaladsl.{GraphDSL, Partition, Sink, Source}
import akka.stream.{ActorMaterializer, SinkShape}
import scala.reflect.ClassTag
object PartitionOnType extends App {
implicit val system = ActorSystem()
import akka.NotUsed;
import akka.actor.ActorSystem;
import akka.http.javadsl.ConnectHttp;
import akka.http.javadsl.Http;
import akka.http.javadsl.model.HttpRequest;
import akka.http.javadsl.model.HttpResponse;
import akka.http.javadsl.model.ws.Message;
import akka.http.javadsl.model.ws.TextMessage;
import akka.http.javadsl.server.Route;
import akka.stream.ActorMaterializer;
@johanandren
johanandren / MultipleUnmarshallers.scala
Created October 13, 2017 14:56
Combining multiple unmarshallers for different dataformats for one path
import akka.actor.ActorSystem
import akka.http.scaladsl.Http
import akka.http.scaladsl.model.ContentTypes
import akka.http.scaladsl.unmarshalling.{FromEntityUnmarshaller, Unmarshaller}
import akka.stream.ActorMaterializer
object MultipleUnmarshallers extends App {
implicit val system = ActorSystem()
implicit val materializer = ActorMaterializer()
@johanandren
johanandren / RateLimit.scala
Created July 14, 2017 10:05
Sample for a custom rate limiting directive for Akka HTTP
/**
* Copyright (C) 2009-2017 Lightbend Inc. <http://www.lightbend.com>
*/
package http
import java.util.concurrent.atomic.AtomicInteger
import akka.actor.{Actor, ActorRef, ActorSystem, Props}
import akka.http.scaladsl.Http
import akka.http.scaladsl.model.{HttpResponse, StatusCodes, Uri}
import akka.typed.Behavior
import akka.typed.scaladsl.Actor._
sealed trait LifecycleCommand
object Stop extends LifecycleCommand
val lifecycleBehavior: Behavior[LifecycleCommand] =
immutable[LifecycleCommand]((ctx, msg) =>
msg match {
case Stop =>
package basics
import akka.actor.{ActorSystem, ExtendedActorSystem}
import akka.serialization.{JavaSerializer, SerializationExtension}
import com.typesafe.config.ConfigFactory
trait MyPayload
trait OtherPayload
trait PersistentExecutorProtocol extends Serializable {