title: Netty and Scheduled Executor Service
body: <p>I'm trying to create a TCP server that read data periodically from a database (Redis) and send it to the appropriate client.</p>
  
  <p>However, since I'm pretty new to Netty, I don't know how could I schedule this. I do know that I need to use a Scheduled Executor Service like this:</p>
  
  <pre><code>ScheduledExecutorService e = Executors.newSingleThreadScheduledExecutor();
  e.scheduleAtFixedRate(() -&gt; {
      System.out.println("Calling...");
      // Do something
  }, 1, 1, TimeUnit.SECONDS);
  </code></pre>
  
  <p>However, when I tried to put that in the server code, It's only calling the method once. I've tried to put that in different place but still can't seem to get it right. What should I do?</p>
  
  <p>Here's the code of the server:</p>
  
  <pre><code>package com.example.test.app;
  
  import io.netty.bootstrap.ServerBootstrap;
  import io.netty.channel.ChannelFuture;
  import io.netty.channel.ChannelInitializer;
  import io.netty.channel.ChannelOption;
  import io.netty.channel.EventLoopGroup;
  import io.netty.channel.nio.NioEventLoopGroup;
  import io.netty.channel.socket.SocketChannel;
  import io.netty.channel.socket.nio.NioServerSocketChannel;
  import java.util.concurrent.Executors;
  import java.util.concurrent.ScheduledExecutorService;
  import java.util.concurrent.TimeUnit;
  
  public class Server {
  
      public static void main(String[] args) throws Exception
      {
          EventLoopGroup bossGroup = new NioEventLoopGroup();
          EventLoopGroup workerGroup = new NioEventLoopGroup();
  
          final ServerHandler handler = new ServerHandler();
  
          try {
  
              ServerBootstrap b = new ServerBootstrap();
              b.group(bossGroup, workerGroup);
              b.channel(NioServerSocketChannel.class);
              b.childHandler(new ChannelInitializer&lt;SocketChannel&gt;() {
                  @Override
                  protected void initChannel(SocketChannel ch) throws Exception
                  {
                      ch.pipeline().addLast(handler);
                  }
  
              });
              b.option(ChannelOption.SO_BACKLOG, 128);
              b.childOption(ChannelOption.SO_KEEPALIVE, true);
  
              ScheduledExecutorService e = Executors.newSingleThreadScheduledExecutor();
              e.scheduleAtFixedRate(() -&gt; {
                  System.out.println("Calling...");
                  handler.saySomething();
              }, 1, 1, TimeUnit.SECONDS);
  
              ChannelFuture f = b.bind(1337).sync();
              f.channel().closeFuture().sync();
  
          } finally {
              workerGroup.shutdownGracefully();
              bossGroup.shutdownGracefully();
          }
      }
  
  }
  </code></pre>
  
  <p>And here's the server handler:</p>
  
  <pre><code>package com.example.test.app;
  
  import io.netty.channel.ChannelFuture;
  import io.netty.channel.ChannelFutureListener;
  import io.netty.channel.ChannelHandlerContext;
  import io.netty.channel.ChannelInboundHandlerAdapter;
  
  public class ServerHandler extends ChannelInboundHandlerAdapter {
  
      private ChannelHandlerContext ctx;
  
      @Override
      public void channelActive(ChannelHandlerContext ctx)
      {
          this.ctx = ctx;
          System.out.println("Someone's connedted!");
      }
  
      public void saySomething()
      {
          final ChannelFuture f = ctx.writeAndFlush("Sup!");
          f.addListener((ChannelFutureListener) (ChannelFuture future) -&gt; {
              System.out.println("Something has been said!");
          });
      }
  
  }
  </code></pre>