Hi,
I am currently working on a little service that listens to a streaming service, transforms the messages to some other format, applies some business logic and distributes the results among a bunch of connected websocket clients.
So far I created two netty Bootstraps, one that listens to the streaming service for incoming messages and one that handles the connected websocket clients:
public class TickSource {
private static final byte FS = 28;
public void run() throws Exception {
String host = "localhost";
int port = 20077;
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
Bootstrap b = new Bootstrap();
b.group(workerGroup);
b.channel(NioSocketChannel.class);
b.option(ChannelOption.SO_KEEPALIVE, true);
b.handler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new DelimiterBasedFrameDecoder(81920, Unpooled.wrappedBuffer(new byte[]{FS})));
ch.pipeline().addLast(new TickSourceHandler());
}
});
ChannelFuture f = b.connect(host, port).sync();
f.channel().closeFuture().sync();
} finally {
workerGroup.shutdownGracefully();
}
}
}and
public class TickDistributor {
public void run() throws Exception {
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
final ServerBootstrap sb = new ServerBootstrap();
sb.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(final SocketChannel ch) throws Exception {
ch.pipeline().addLast(
new HttpRequestDecoder(),
new HttpObjectAggregator(65536),
new HttpResponseEncoder(),
new WebSocketServerProtocolHandler("/ticks"),
new TickDistributorHandler()
);
}
}
);
Channel ch = sb.bind(8080).sync().channel();
ch.closeFuture().sync();
} finally {
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
}
So each component on its own works nicely. The problem is, I don't have a clue how to connect them both in a nice netty-way. I thought about storing the the ServerChannels in the TickSourceHandler and write a message received from the streaming service to each of them individually, not sure though, if this is achievable in that way or if there is a nicer way to do it with netty mechanics.
Reading through Netty In Action I also read about LocalChannels and bootstrapping new client connections in the same EventLoop if you have a 1->1 relation, but I guess it's not suitable in my case with a relation of 1->n?
I'd be happy about any hint!
Thx,
u6f6o