Hi,

is Akka HTTP still using Netty? To solve the problem I built a small HTTP 
server using Netty that just creates a Akka HttpRequest from a Netty 
HttpRequest. This is a bit of a hack but I needed a quick solution because 
our customer gets really impatient and I just needed something that works 
quickly. I did not get bind exceptions starting / stopping Netty on 
Windows. I use the following code (shutdown code from Netty in Action book):

  private val bossGroup = new NioEventLoopGroup(1)
  private val workerGroup = new NioEventLoopGroup()
  private val bootstrap = new ServerBootstrap()
  bootstrap.group(bossGroup, 
workerGroup).channel(classOf[NioServerSocketChannel])
    .option(ChannelOption.SO_BACKLOG.asInstanceOf[ChannelOption[Any]], 200)
    .childOption(ChannelOption.ALLOCATOR.asInstanceOf[ChannelOption[Any]], 
PooledByteBufAllocator.DEFAULT)
    .childHandler(new ChannelInitializer[SocketChannel] {
    override def initChannel(ch: SocketChannel): Unit = {
      val p = ch.pipeline()
      p.addLast("decoder", new HttpRequestDecoder)
      p.addLast("encoder", new HttpResponseEncoder)
      p.addLast("aggregator", new HttpObjectAggregator(1048576))
      p.addLast("handler", new HttpRequestHandler(modules))
    }
  })

  logger.info("Trying to start netty on " + interface + ":" + port)
  private val serverChannelFuture = bootstrap.bind(interface, port)
  serverChannelFuture.addListener(new ChannelFutureListener {
    override def operationComplete(future: ChannelFuture): Unit = {
      if (serverChannelFuture.isSuccess) {
        logger.info("HTTP server bound to " + interface + ":" + port)
      } else {
        logger.info("Could not bind HTTP server to " + interface + ":" + 
port, serverChannelFuture.cause())
      }
    }
  })

override protected def dispose(): Unit = {
    logger.info("Trying to stop HTTP server")
    bossGroup.shutdownGracefully().sync()
    workerGroup.shutdownGracefully().sync()
    logger.info("Stopped boss and worker groups")
    bossGroup.terminationFuture().sync()
    workerGroup.terminationFuture().sync()
    logger.info("Terminated boss and worker groups")
  }

Sorry I didn't have any time to figure out what the problem is. I hope I 
can get rid of my hack when the next version of Akka is released. Looking 
at several Http frameworks and hacking my own Netty based solution makes me 
realize even more how nice Akka Http actually is.

Thanks for the great work,
Michael

On Thursday, April 7, 2016 at 9:15:58 PM UTC+2, Antti Nevala wrote:
>
> No problem, glad to help you.
>
> -Antti
>
> On Thursday, April 7, 2016 at 3:57:09 PM UTC+3, drewhk wrote:
>>
>> Oh, you are our savior! I am 99% sure that that is the solution we are 
>> looking after, thanks!
>>
>> -Endre
>>
>> On Thu, Apr 7, 2016 at 2:35 PM, Antti Nevala <aene...@gmail.com> wrote:
>>
>>> Hi,
>>>
>>> I'm not sure if this is related but found similar problem from another 
>>> project and workaround how to solve it:
>>>
>>> https://github.com/kaazing/nuklei/issues/20
>>>
>>> -Antti
>>>
>>> On Thursday, April 7, 2016 at 11:07:03 AM UTC+3, drewhk wrote:
>>>>
>>>> Hi,
>>>>
>>>>
>>>> On Tue, Apr 5, 2016 at 8:40 PM, Michi <michael...@physik.tu-muenchen.de
>>>> > wrote:
>>>>
>>>>> Hi,
>>>>>
>>>>> I think SO_REUSEADDR is enabled by default for Akka HTTP. At least it 
>>>>> looks like that when I looked at the code. But even if it is disabled, 
>>>>> the 
>>>>> OS should not need ten minutes to release the socket.  Maybe I write a 
>>>>> simple test program tomorrow that demonstrates the problem. 
>>>>>
>>>>
>>>> Ok, this might be a bug then, please file a ticket. Strange that it 
>>>> works on non-Windows though :( (we have test for the behavior)
>>>>  
>>>>
>>>>>
>>>>> My problem is that I am running out of time. Our customer is getting 
>>>>> impatient and the server-side rest interface is just a small part of the 
>>>>> application.
>>>>>
>>>> I think it is probably best if I just use something else for now and go 
>>>>> back to Akka HTTP if I have some more time. Can anyone suggest a 
>>>>> lightweight, easy to use HTTP Server library for Java to provide a REST 
>>>>> interface and deliver some HTML, CSS and Javascript files?
>>>>>
>>>>
>>>> You can use Play for example.
>>>>
>>>> -Endre
>>>>  
>>>>
>>>>>
>>>>> Thanks,
>>>>> Michael
>>>>>
>>>>> On Tuesday, April 5, 2016 at 6:34:26 PM UTC+2, drewhk wrote:
>>>>>>
>>>>>> Have you tried to add a configuration (ServerSettings) when you bind 
>>>>>> the TCP which enables the ReuseAddress (
>>>>>> http://doc.akka.io/api/akka/2.4.3/#akka.io.Inet$$SO$$ReuseAddress) 
>>>>>> option?
>>>>>>
>>>>>> -Endre
>>>>>>
>>>>>> On Tue, Apr 5, 2016 at 6:29 PM, Endre Varga <endre...@lightbend.com> 
>>>>>> wrote:
>>>>>>
>>>>>>> But that code uses SO_REUSEADDR.
>>>>>>>
>>>>>>> -Endre
>>>>>>>
>>>>>>> On Tue, Apr 5, 2016 at 5:50 PM, Michi <
>>>>>>> michael...@physik.tu-muenchen.de> wrote:
>>>>>>>
>>>>>>>> Hi Endre,
>>>>>>>>
>>>>>>>> I wrote a small test program that creates a socket channel, closes 
>>>>>>>> it and creates another one using the same port:
>>>>>>>>
>>>>>>>> import java.io.IOException;
>>>>>>>> import java.net.InetSocketAddress;
>>>>>>>> import java.net.SocketAddress;
>>>>>>>> import java.net.StandardSocketOptions;
>>>>>>>> import java.nio.channels.NetworkChannel;
>>>>>>>> import java.nio.channels.ServerSocketChannel;
>>>>>>>>
>>>>>>>> public class Main {
>>>>>>>>
>>>>>>>>     public static void main(String[] args) throws IOException, 
>>>>>>>> InterruptedException {
>>>>>>>>         int port = Integer.parseInt(args[0]);
>>>>>>>>         SocketAddress addr = new InetSocketAddress(port);
>>>>>>>>         System.out.println("Binding server socket to " + addr);
>>>>>>>>         ServerSocketChannel s = ServerSocketChannel.open();
>>>>>>>>         s.setOption(StandardSocketOptions.SO_REUSEADDR, true);
>>>>>>>>         System.out.println("Created server socket channel " + s);
>>>>>>>>         NetworkChannel c = s.bind(addr);
>>>>>>>>         System.out.println("Bound server socket to " + addr);
>>>>>>>>         s.close();
>>>>>>>>         System.out.println("Closed channel: " + s);
>>>>>>>>         Thread.sleep(1000);
>>>>>>>>         System.out.println("Binding server socket to " + addr);
>>>>>>>>         ServerSocketChannel s2 = ServerSocketChannel.open();
>>>>>>>>         s2.setOption(StandardSocketOptions.SO_REUSEADDR, true);
>>>>>>>>         System.out.println("Created server socket channel " + s2);
>>>>>>>>         NetworkChannel c2 = s2.bind(addr);
>>>>>>>>         System.out.println("Bound server socket to " + addr);
>>>>>>>>         s2.close();
>>>>>>>>         System.out.println("Closed channel: " + s2);
>>>>>>>>
>>>>>>>>     }
>>>>>>>> }
>>>>>>>>
>>>>>>>> It works perfectly both on Linux and on Windows. Here is the output on 
>>>>>>>> Windows:
>>>>>>>>
>>>>>>>> C:\Users\Michael\Desktop>java Main 8888
>>>>>>>> Binding server socket to 0.0.0.0/0.0.0.0:8888
>>>>>>>> Created server socket channel 
>>>>>>>> sun.nio.ch.ServerSocketChannelImpl[unbound]
>>>>>>>> Bound server socket to 0.0.0.0/0.0.0.0:8888
>>>>>>>> Closed channel: sun.nio.ch.ServerSocketChannelImpl[closed]
>>>>>>>> Binding server socket to 0.0.0.0/0.0.0.0:8888
>>>>>>>> Created server socket channel 
>>>>>>>> sun.nio.ch.ServerSocketChannelImpl[unbound]
>>>>>>>> Bound server socket to 0.0.0.0/0.0.0.0:8888
>>>>>>>> Closed channel: sun.nio.ch.ServerSocketChannelImpl[closed]
>>>>>>>>
>>>>>>>>
>>>>>>>> There is also AsynchronousServerSocketChannel, but I doubt this 
>>>>>>>> makes a big difference. I really don't think that NIO does not 
>>>>>>>> properly 
>>>>>>>> unbind sockets on Windows. There might be a small delay, but certainly 
>>>>>>>> not 
>>>>>>>> 10 Minutes! As far as I know StandardSocketOptions.SO_REUSEADDR 
>>>>>>>> should allow to reuse sockets that are no longer in use, even if the 
>>>>>>>> operating system did not unbind them yet.
>>>>>>>>
>>>>>>>> If there is anything I can do to help, let me know.
>>>>>>>>
>>>>>>>> Best regards,
>>>>>>>> Michael
>>>>>>>>
>>>>>>>> On Tuesday, April 5, 2016 at 5:08:09 PM UTC+2, drewhk wrote:
>>>>>>>>
>>>>>>>>>
>>>>>>>>>
>>>>>>>>> On Tue, Apr 5, 2016 at 5:04 PM, Michi <
>>>>>>>>> michael...@physik.tu-muenchen.de> wrote:
>>>>>>>>>
>>>>>>>>>> Hi Endre,
>>>>>>>>>>
>>>>>>>>>> even after 10 Minutes TCPView shows that the socket is still 
>>>>>>>>>> bound by jawaw.exe. If I close the application, the socket gets 
>>>>>>>>>> unbound 
>>>>>>>>>> immediately. That's why I am wondering if I do something wrong or if 
>>>>>>>>>> this 
>>>>>>>>>> is a bug in Akka HTTP. 
>>>>>>>>>>
>>>>>>>>>
>>>>>>>>> We use NIO underneath and don't do anything special, so I am not 
>>>>>>>>> sure what we can do here (it works on all other OSes).
>>>>>>>>>  
>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>> I tried to use a independent Actor System for the Http Server 
>>>>>>>>>> class and shut the system down after unbinding, but that does not 
>>>>>>>>>> help 
>>>>>>>>>> either.
>>>>>>>>>>
>>>>>>>>>
>>>>>>>>> At that point Akka had cleaned up every actor, so I am not sure 
>>>>>>>>> what else to do :(
>>>>>>>>>  
>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>> Unfortunately our customer uses Windows and I am wondering if 
>>>>>>>>>> there is anything I can do about it or if it would be better to 
>>>>>>>>>> reimplement 
>>>>>>>>>> the code with Apache Http Components.
>>>>>>>>>>
>>>>>>>>>
>>>>>>>>> You can try out other frameworks, if they happen to work (and use 
>>>>>>>>> NIO) then at least we can see what magic they do to make Windows 
>>>>>>>>> happy :(
>>>>>>>>>
>>>>>>>>> -Endre
>>>>>>>>>  
>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>> best regards,
>>>>>>>>>> Michael
>>>>>>>>>>
>>>>>>>>>> On Tuesday, April 5, 2016 at 2:07:52 PM UTC+2, drewhk wrote:
>>>>>>>>>>>
>>>>>>>>>>> Hi,
>>>>>>>>>>>
>>>>>>>>>>> As far as I know event though NIO reports the unbind, unbinding 
>>>>>>>>>>> on Windows is not immediate and you are not guaranteed to be able 
>>>>>>>>>>> to bind 
>>>>>>>>>>> again immediately. I don't know if this comes from Windows or the 
>>>>>>>>>>> NIO 
>>>>>>>>>>> windows drivers though.
>>>>>>>>>>>
>>>>>>>>>>> -Endre
>>>>>>>>>>>
>>>>>>>>>>> On Tue, Apr 5, 2016 at 2:05 PM, Michi <
>>>>>>>>>>> michael...@physik.tu-muenchen.de> wrote:
>>>>>>>>>>>
>>>>>>>>>>>> Hi,
>>>>>>>>>>>>
>>>>>>>>>>>> we have a problem with Akka HTTP on Windows: after creating a 
>>>>>>>>>>>> server binding and unbinding it, it is not possible to bind it 
>>>>>>>>>>>> again. We 
>>>>>>>>>>>> get a akka.stream.BindFailedException. On Linux, this works 
>>>>>>>>>>>> without 
>>>>>>>>>>>> problems.
>>>>>>>>>>>>
>>>>>>>>>>>> The application uses the following code to create the server 
>>>>>>>>>>>> binding:
>>>>>>>>>>>>
>>>>>>>>>>>>   private val binding: Future[ServerBinding] = {
>>>>>>>>>>>>     val httpsContext = if (protocol == Protocol.Https) 
>>>>>>>>>>>> Some(HttpsContexts.serverContext) else None
>>>>>>>>>>>>     val serverSource: Source[Http.IncomingConnection, 
>>>>>>>>>>>> Future[Http.ServerBinding]] = Http(actorSystem).bind(interface = 
>>>>>>>>>>>> interface, 
>>>>>>>>>>>> port = port, httpsContext = httpsContext)
>>>>>>>>>>>>
>>>>>>>>>>>>     logger.info("Binding to interface " + interface + ":" + 
>>>>>>>>>>>> port + " using " + protocol)
>>>>>>>>>>>>
>>>>>>>>>>>>     val bindingFuture: Future[ServerBinding] = 
>>>>>>>>>>>> serverSource.to(Sink.foreach { connection =>
>>>>>>>>>>>>       connection.handleWith(route)
>>>>>>>>>>>>     }).run()
>>>>>>>>>>>>     bindingFuture
>>>>>>>>>>>>   }
>>>>>>>>>>>>
>>>>>>>>>>>>   binding onComplete {
>>>>>>>>>>>>     case Success(result) => logger.info("Bound to interface " 
>>>>>>>>>>>> + interface + ":" + port + " using " + protocol)
>>>>>>>>>>>>     case Failure(reason) => logger.error("Could not bind to 
>>>>>>>>>>>> interface " + interface + ":" + port + " using " + protocol, 
>>>>>>>>>>>> reason)
>>>>>>>>>>>>   }
>>>>>>>>>>>>
>>>>>>>>>>>> protected override def dispose(): Unit = {
>>>>>>>>>>>>     // stop webserver
>>>>>>>>>>>>     for(binding <- binding) {
>>>>>>>>>>>>       binding.unbind() onComplete {
>>>>>>>>>>>>         case Success(result) => logger.info("Unbound " + 
>>>>>>>>>>>> binding)
>>>>>>>>>>>>         case Failure(reason) => logger.error("Could not unbind 
>>>>>>>>>>>> " + binding, reason)
>>>>>>>>>>>>       }
>>>>>>>>>>>>     }
>>>>>>>>>>>>   }
>>>>>>>>>>>>
>>>>>>>>>>>> Restarting the application solves the problem.
>>>>>>>>>>>>
>>>>>>>>>>>> Is there anything I can do to solve the problem?
>>>>>>>>>>>>
>>>>>>>>>>>> Thanks,
>>>>>>>>>>>> Michael
>>>>>>>>>>>>
>>>>>>>>>>>> -- 
>>>>>>>>>>>> >>>>>>>>>> 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 akka-user+...@googlegroups.com.
>>>>>>>>>>>> To post to this group, send email to akka...@googlegroups.com.
>>>>>>>>>>>> Visit this group at https://groups.google.com/group/akka-user.
>>>>>>>>>>>> For more options, visit https://groups.google.com/d/optout.
>>>>>>>>>>>>
>>>>>>>>>>>
>>>>>>>>>>> -- 
>>>>>>>>>> >>>>>>>>>> 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 akka-user+...@googlegroups.com.
>>>>>>>>>> To post to this group, send email to akka...@googlegroups.com.
>>>>>>>>>> Visit this group at https://groups.google.com/group/akka-user.
>>>>>>>>>> For more options, visit https://groups.google.com/d/optout.
>>>>>>>>>>
>>>>>>>>>
>>>>>>>>> -- 
>>>>>>>> >>>>>>>>>> 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 akka-user+...@googlegroups.com.
>>>>>>>> To post to this group, send email to akka...@googlegroups.com.
>>>>>>>> Visit this group at https://groups.google.com/group/akka-user.
>>>>>>>> For more options, visit https://groups.google.com/d/optout.
>>>>>>>>
>>>>>>>
>>>>>>>
>>>>>> -- 
>>>>> >>>>>>>>>> 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 akka-user+...@googlegroups.com.
>>>>> To post to this group, send email to akka...@googlegroups.com.
>>>>> Visit this group at https://groups.google.com/group/akka-user.
>>>>> For more options, visit https://groups.google.com/d/optout.
>>>>>
>>>>
>>>> -- 
>>> >>>>>>>>>> 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 akka-user+...@googlegroups.com.
>>> To post to this group, send email to akka...@googlegroups.com.
>>> Visit this group at https://groups.google.com/group/akka-user.
>>> For more options, visit https://groups.google.com/d/optout.
>>>
>>
>>

-- 
>>>>>>>>>>      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 akka-user+unsubscr...@googlegroups.com.
To post to this group, send email to akka-user@googlegroups.com.
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