<html>
<head>
<style><!--
.hmmessage P
{
margin:0px;
padding:0px
}
body.hmmessage
{
font-size: 10pt;
font-family:Tahoma
}
--></style>
</head>
<body class='hmmessage'><div dir='ltr'>
Sometimes answers come up when after you posted a question.<br><br>In this case the simple answer is to pass the instance to all relevant classes:<br><br>In the main code:<br>...<br>final DiscardServerHandler serverHandler = new DiscardServerHandler();<br><br> bootstrap.setPipelineFactory(new ChannelPipelineFactory() {<br> public ChannelPipeline getPipeline() {<br> return Channels.pipeline(<br> new BasicFrameDecoder()<br> ,<b>serverHandler </b>);<br> }<br> });<br>...<br>Thread thread = new MainLoopThread( <b>serverHandler </b>);<br>...<br><br>and then:<br><br>public class MainLoopThread extends Thread {<br> // This method is called when the thread runs<br> <br> private Timer timer;<br> private int seconds = 1;<br> private DiscardServerHandler _handler;<br> <br> public MainLoopThread( DiscardServerHandler handler) {<br> System.out.println("Thread MainLoop: constructor.");<br> _handler = handler;<br> }<br> <br> public void run() {<br> System.out.println("Thread MainLoop started.");<br> timer = new Timer();<br> timer.schedule(new RemindTask( _handler ), 0, seconds * 1000); <br> }<br><br>}<br><br>...<br><br>class RemindTask extends TimerTask {<br> private int iterations = 0;<br> private DiscardServerHandler handler;<br> private Utils utils = new Utils();<br> <br> public RemindTask(DiscardServerHandler referenceToHandler) {<br> System.out.println("RemindTask: constructor."); <br> handler = referenceToHandler;<br> }<br> <br> public void run() {<br> System.out.print((char)13 + "Timer [" + iterations + "]");<br> <br> broadCast();<br> <br> <br> }<br> <br> private void broadCast() {<br> iterations++;<br> int totalChannels = 0; <br> for ( Channel c: handler.getChannels() ) {<br> System.out.println("Channel " + totalChannels);<br> totalChannels++;<br> // c.write( )<br> }<br> }<br>}<br><br>...<br><br>Very likely this is 101 for most, hope it can help others.<br><br><br><div><hr id="stopSpelling">From: m4io@hotmail.com<br>To: netty-users@lists.jboss.org<br>Subject: how to write to channels periodically<br>Date: Tue, 5 Jul 2011 10:23:36 +0200<br><br>
<meta http-equiv="Content-Type" content="text/html; charset=unicode">
<meta name="Generator" content="Microsoft SafeHTML">
<style>
.ExternalClass .ecxhmmessage P
{padding:0px;}
.ExternalClass body.ecxhmmessage
{font-size:10pt;font-family:Tahoma;}
</style>
<div dir="ltr">
Hi,<br><br>I want to run some main-thread on the server which periodically checks something, later this will be some watchdog-process.<br><br>How can I reach all channels from that main thread ? <br><br>Snippets:<br><br>....<br> bootstrap.setPipelineFactory(new ChannelPipelineFactory() {<br> public ChannelPipeline getPipeline() {<br> return Channels.pipeline(<br> new BasicFrameDecoder()<br> ,new DiscardServerHandler());<br> }<br> });<br><br> bootstrap.setOption("child.tcpNoDelay", true);<br> bootstrap.setOption("child.keepAlive", true);<br> System.out.print("Binding to " + host + ":" + port + ".\n");<br> bootstrap.bind(new InetSocketAddress(host, port));<br> // Create and start the thread<br> Thread thread = new MainLoopThread();<br> thread.start();<br>....<br><br>///////////////<br><br>public class MainLoopThread extends Thread {<br> // This method is called when the thread runs<br> <br> private Timer timer;<br> private int seconds = 3;<br> <br> <br> public void run() {<br> System.out.println("Thread MainLoop started.");<br> timer = new Timer();<br> timer.schedule(new RemindTask(), seconds * 1000); <br> }<br><br>}<br><br>///////////////<br><br>class RemindTask extends TimerTask {<br> private int iterations = 0;<br> <br> public void run() {<br> System.out.println("Timer [" + iterations + "]");<br> iterations++;<br> <br> // how to reach the channels of the connected users ?<br> <br> <br> }<br>}<br><br>As you can see the RemindTask wants to reach the channels.<br><br><br>In my ServerHandler I keep track of the channels like this:<br><br>static final ChannelGroup channels = new DefaultChannelGroup();<br><br>And the channelConnected event adds a channel to that group and the channelDisconnected event removes it.<br><br>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 ? <br><br>Thanks in advance!<br><br><br>                                            </div>
<br>_______________________________________________
netty-users mailing list
netty-users@lists.jboss.org
https://lists.jboss.org/mailman/listinfo/netty-users</div>                                            </div></body>
</html>