hi, I have a simple http service that binds to a given port and starts 
accepting IncomingConnection. 
the server is created by actor on its preStart callback and stopped on 
postStop.

everything works fine until I try to kill the actor and force http-server 
to stop.

public void start() {
    ActorSystem system = materializer.system();
    Source<IncomingConnection, CompletionStage<ServerBinding>> serverSource = 
Http
            .get(system)
            .bind(ConnectHttp.toHost("localhost", port), materializer);

    Flow<IncomingConnection, IncomingConnection, NotUsed> failureDetection = 
Flow
            .of(IncomingConnection.class)
            .watchTermination((_unused, termination) -> {
                termination.whenComplete((done, cause) -> {
                    if (cause != null) {
                        log.info("Connection was closed.", cause);
                    }
                });
                return NotUsed.getInstance();
            });

    future = serverSource
            .via(failureDetection)
            .to(Sink.actorRef(parentRef, Kill.getInstance())) // force parent 
to restart and recreate http service
            .run(materializer);
    future
            .whenCompleteAsync((binding, failure) -> {
                if (failure != null) {
                    log.error("Could not initialize connection.", failure);
                    parentRef.tell(Kill.getInstance(), ActorRef.noSender());
                } else {
                    log.info("Server listening on port {}.", port);
                    parentRef.tell(STARTED, ActorRef.noSender());
                }
            }, system.dispatcher());
}

public void stop() {
    future
            .whenComplete((binding, failure) -> {
                log.info("Stopping Server on port {}.", port);
                if (failure == null && binding != null) {
                    binding.unbind();
                }
            });
}


Here is part of log:

[DEBUG] [03/29/2016 23:42:58.191] 
[akka-system-akka.actor.default-dispatcher-6] 
[akka://akka-system/user/bSupervisor] received AutoReceiveMessage 
Envelope(PoisonPill,Actor[akka://akka-system/deadLetters])
2016-03-29 23:42:58.192  INFO 1965 --- 
[akka-system-akka.actor.default-dispatcher-3] c.c.v.d.b.actors.Parent     : 
Stopping http server.
[DEBUG] [03/29/2016 23:42:58.192] 
[akka-system-akka.actor.default-dispatcher-6] 
[akka://akka-system/user/bSupervisor] stopping
2016-03-29 23:42:58.194  INFO 1965 --- [ForkJoinPool.commonPool-worker-1] 
c.c.v.d.b.rest.BServer    : Stopping Server on port 8900.
[DEBUG] [03/29/2016 23:42:58.197] 
[akka-system-akka.actor.default-dispatcher-19] 
[akka://akka-system/user/bSupervisor] stopped
[DEBUG] [03/29/2016 23:42:58.199] 
[akka-system-akka.actor.default-dispatcher-2] 
[akka://akka-system/system/IO-TCP/selectors/$a/0] no longer watched by 
Actor[akka://akka-system/user/StreamSupervisor-0/$$a#50876208]
[DEBUG] [03/29/2016 23:42:58.200] 
[akka-system-akka.actor.default-dispatcher-8] 
[akka://akka-system/user/StreamSupervisor-0/flow-0-1-actorRefSink] received 
AutoReceiveMessage 
Envelope(Terminated(Actor[akka://akka-system/user/bSupervisor/parent#655292920]),Actor[akka://akka-system/user/bSupervisor/parent#655292920])
[DEBUG] [03/29/2016 23:42:58.200] 
[akka-system-akka.actor.default-dispatcher-2] 
[akka://akka-system/system/IO-TCP/selectors/$a/0] Unbinding endpoint 
/127.0.0.1:8900
[DEBUG] [03/29/2016 23:42:58.201] 
[akka-system-akka.actor.default-dispatcher-3] 
[akka://akka-system/user/bSupervisor/parent] stopped
[DEBUG] [03/29/2016 23:42:58.205] 
[akka-system-akka.actor.default-dispatcher-2] 
[akka://akka-system/system/IO-TCP/selectors/$a/0] Unbound endpoint 
/127.0.0.1:8900, stopping listener
[DEBUG] [03/29/2016 23:42:58.206] 
[akka-system-akka.actor.default-dispatcher-8] 
[akka://akka-system/user/StreamSupervisor-0/flow-0-1-actorRefSink] stopped
[DEBUG] [03/29/2016 23:42:58.208] 
[akka-system-akka.actor.default-dispatcher-2] 
[akka://akka-system/system/IO-TCP/selectors/$a/0] stopped
[DEBUG] [03/29/2016 23:42:58.208] 
[akka-system-akka.actor.default-dispatcher-6] 
[akka://akka-system/system/IO-TCP/selectors/$a] received AutoReceiveMessage 
Envelope(Terminated(Actor[akka://akka-system/system/IO-TCP/selectors/$a/0#1629651596]),Actor[akka://akka-system/system/IO-TCP/selectors/$a/0#1629651596])
[DEBUG] [03/29/2016 23:42:58.217] 
[akka-system-akka.actor.default-dispatcher-10] 
[akka://akka-system/user/StreamSupervisor-0/flow-0-0-unknown-operation] 
stopped 

*I notice that after unbind I cannot create new connection (ie. from a 
browser) but old, alive connection can still send requests. Am I missing 
something?*
Just looking 
at 
http://doc.akka.io/docs/akka/2.4.2/java/http/server-side/low-level-server-side-api.html#starting-and-stopping
 
I guess I do the right thing. 
but I cannot understand a fragment 
from 
http://doc.akka.io/docs/akka/2.4.2/java/http/server-side/low-level-server-side-api.html#Closing_a_connection,
 
"The HTTP connection will be closed when the handling Flow cancels its 
upstream subscription". 
Should I manually close connection after unbinding? that would be strange, 
I just want to unbind and shutdown, closing all live connections.

here is a last part of Parent actor that creates and stops http-server and 
acts as a sink for connections flow

ReceiveBuilder
        .match(IncomingConnection.class, connection -> {

            log.info("Established new connection from {}.", 
connection.remoteAddress());
            Flow<HttpRequest, HttpResponse, NotUsed> handler = 
handleUpdateRequest(materializer.system());
            handler
                    .watchTermination((_unused, termination) -> {
                        termination.whenComplete((done, cause) -> {
                            log.info("Done {}. connection {}", done, 
connection.remoteAddress());
                            if (cause != null) {
                                log.error("flow HttpR-R was closed.", cause);
                            }
                        });
                        return NotUsed.getInstance();
                    })
                    
.withAttributes(ActorAttributes.withSupervisionStrategy(Supervision.getResumingDecider()));
            connection.handleWith(handler, materializer);
        })...

-- 
>>>>>>>>>>      Read the docs: http://akka.io/docs/
>>>>>>>>>>      Check the FAQ: 
>>>>>>>>>> http://doc.akka.io/docs/akka/current/additional/faq.html
>>>>>>>>>>      Search the archives: https://groups.google.com/group/akka-user
--- 
You received this message because you are subscribed to the Google Groups "Akka 
User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.

Reply via email to