Skip to content

Instantly share code, notes, and snippets.

Avatar
👻
hakking

Johan Andrén johanandren

👻
hakking
View GitHub Profile
View TcpServerOneResponseThenComplete2.scala
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
View TcpServerOneResponseThenComplete.scala
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 Jan 2, 2020
Java mock actor child
View stuff.java
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 Oct 4, 2018
Three node Akka cluster in one JVM with minimal fuzz
View ProgrammaticJoin.scala
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
View PartitionOnType.scala
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()
View MinimalWebSocketServer.java
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 Oct 13, 2017
Combining multiple unmarshallers for different dataformats for one path
View MultipleUnmarshallers.scala
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 Jul 14, 2017
Sample for a custom rate limiting directive for Akka HTTP
View RateLimit.scala
/**
* 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}
View TypedAndUnionTypes.scala
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 =>
View SerializerAndPathDependentTypes.scala
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 {