how to write to channels periodically
W T
m4io at hotmail.com
Tue Jul 5 04:23:36 EDT 2011
Hi,
I want to run some main-thread on the server which periodically checks something, later this will be some watchdog-process.
How can I reach all channels from that main thread ?
Snippets:
....
bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
public ChannelPipeline getPipeline() {
return Channels.pipeline(
new BasicFrameDecoder()
,new DiscardServerHandler());
}
});
bootstrap.setOption("child.tcpNoDelay", true);
bootstrap.setOption("child.keepAlive", true);
System.out.print("Binding to " + host + ":" + port + ".\n");
bootstrap.bind(new InetSocketAddress(host, port));
// Create and start the thread
Thread thread = new MainLoopThread();
thread.start();
....
///////////////
public class MainLoopThread extends Thread {
// This method is called when the thread runs
private Timer timer;
private int seconds = 3;
public void run() {
System.out.println("Thread MainLoop started.");
timer = new Timer();
timer.schedule(new RemindTask(), seconds * 1000);
}
}
///////////////
class RemindTask extends TimerTask {
private int iterations = 0;
public void run() {
System.out.println("Timer [" + iterations + "]");
iterations++;
// how to reach the channels of the connected users ?
}
}
As you can see the RemindTask wants to reach the channels.
In my ServerHandler I keep track of the channels like this:
static final ChannelGroup channels = new DefaultChannelGroup();
And the channelConnected event adds a channel to that group and the channelDisconnected event removes it.
How do I access the ChannelGroup from my thread ? I can't just instantiate the ServerHandler again from my TimerTask and then call some public method of the ServerHandler ?
Thanks in advance!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/netty-users/attachments/20110705/e3271eec/attachment.html
More information about the netty-users
mailing list