Skip to content
This repository has been archived by the owner on Jan 20, 2022. It is now read-only.

Commit

Permalink
Merge pull request #1 from hechaoli/listen_async
Browse files Browse the repository at this point in the history
Make startListeningOnPort() asynchronous
  • Loading branch information
hechaoli committed Apr 17, 2018
2 parents c0c577a + 1de5922 commit 4feb990
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,7 @@ System.out.println(Arrays.toString(dbs));
From above example we can see the steps of getting an `OvsdbClient` object.

(1) Construct a `OvsdbPassiveConnectionListener`. The `OvsdbPassiveConnectionListenerImpl`
takes a `ScheduledExecutorService` for asynchronous operations. The thread pool has to contain at
least 2 idle threads because 1 thread will be used to listen on the port.
takes a `ScheduledExecutorService` for asynchronous operations.
(2) Implement the `ConnectionCallback` interface and construct a callback object.
(3) Start listening on the port.
(4) Get the `OvsdbClient` object from the callback and use it for operations on the OVSDB server.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,15 @@ public void startListening(
int port, ConnectionCallback connectionCallback
) {
isListeningCheckWithThrow(port);
executorService.submit(() -> startListeningOnPort(port, null, connectionCallback));
startListeningOnPort(port, null, connectionCallback);
}

@Override
public void startListeningWithSsl(
int port, SslContext sslContext, ConnectionCallback connectionCallback
) {
isListeningCheckWithThrow(port);
executorService.submit(() -> startListeningOnPort(port, sslContext, connectionCallback));
startListeningOnPort(port, sslContext, connectionCallback);
}

@Override
Expand Down Expand Up @@ -104,15 +104,17 @@ private void startListeningOnPort(
ChannelFuture channelFuture = serverBootstrap.bind(port).sync();
serverChannelMap.put(port, channelFuture.channel());

// Wait until the server socket is closed.
channelFuture.channel().closeFuture().sync();
channelFuture.channel().closeFuture().addListener(future -> {
// Shut down all event loops to terminate all threads.
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
LOGGER.info("Ovsdb listener at port {} stopped.", port);
});
} catch (InterruptedException ex) {
LOGGER.error("Netty server is interrupted while starting listener at port " + port, ex);
} finally {
// Shut down all event loops to terminate all threads.
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
LOGGER.info("Ovsdb listener at port {} stopped.", port);
}
}

Expand Down

0 comments on commit 4feb990

Please sign in to comment.