how to write to channels periodically
W T
m4io at hotmail.com
Tue Jul 5 05:06:36 EDT 2011
Sometimes answers come up when after you posted a question.
In this case the simple answer is to pass the instance to all relevant classes:
In the main code:
...
final DiscardServerHandler serverHandler = new DiscardServerHandler();
bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
public ChannelPipeline getPipeline() {
return Channels.pipeline(
new BasicFrameDecoder()
,serverHandler );
}
});
...
Thread thread = new MainLoopThread( serverHandler );
...
and then:
public class MainLoopThread extends Thread {
// This method is called when the thread runs
private Timer timer;
private int seconds = 1;
private DiscardServerHandler _handler;
public MainLoopThread( DiscardServerHandler handler) {
System.out.println("Thread MainLoop: constructor.");
_handler = handler;
}
public void run() {
System.out.println("Thread MainLoop started.");
timer = new Timer();
timer.schedule(new RemindTask( _handler ), 0, seconds * 1000);
}
}
...
class RemindTask extends TimerTask {
private int iterations = 0;
private DiscardServerHandler handler;
private Utils utils = new Utils();
public RemindTask(DiscardServerHandler referenceToHandler) {
System.out.println("RemindTask: constructor.");
handler = referenceToHandler;
}
public void run() {
System.out.print((char)13 + "Timer [" + iterations + "]");
broadCast();
}
private void broadCast() {
iterations++;
int totalChannels = 0;
for ( Channel c: handler.getChannels() ) {
System.out.println("Channel " + totalChannels);
totalChannels++;
// c.write( )
}
}
}
...
Very likely this is 101 for most, hope it can help others.
From: m4io at hotmail.com
To: netty-users at lists.jboss.org
Subject: how to write to channels periodically
Date: Tue, 5 Jul 2011 10:23:36 +0200
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!
_______________________________________________
netty-users mailing list
netty-users at lists.jboss.org
https://lists.jboss.org/mailman/listinfo/netty-users
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/netty-users/attachments/20110705/0d3e1f46/attachment.html
More information about the netty-users
mailing list