A naive approach would be to do something like :
while (true) {
session.write ( <your data to the client> );
Thread.sleep( 1000L );
}
it does the trick *IF* you don't have a lot of clients (and you'll have
to add an Executor filter in the chain, otherwise you wo'nt be able to
deal with more than the number of threads you have defined in the acceptor).
However, this is not the best solution. The problem is that in order to
process a request, a thread is selected, and released when you quit the
handler (ie, once the message has been processed on the server side).
A better solution would be, as suggested by mark, to have a separate
thread having a list of all your client's IoSession. When the clock
tiks, you get all the active sessions and you simply write a message
into them :
while ( true ) {
for ( IoSession session : MySessions ) {
if ( session.isConnected() && ! session.isWriteSuspended()) {
session.write( <time to wake up !> );
} else {
// some cleanup here, as the session has been closed.
}
}
Thread.sleep( 1000L );
}
Hope it helps too.
Mark Wallsgrove wrote:
Hey.. I haven't been using Mina for long, but what you could do is
create a new object that runs on a thread that uses the filter chain
to fire a event every x seconds. Your handler should then do the rest.
Once the event has been fired the thread should then go back to sleep.
I guess you could use the connection object to gather the sessions /
filter chains? Hope this helps..
Best regards,
Mark Wallsgrove
On 10/3/09, CoolHedin <[email protected]> wrote:
Hi. I develop an application using your excellent library. But I had a
difficulty.
I need to send messages from server to clients, by timer.
and I do not understand how to do it.
for example, i send every 1 minutes 'Hi everyone' message to all conection
--
Sincerely yours,
Sergey Paramonov
Adept Team, www.adept-project.com
--
--
cordialement, regards,
Emmanuel Lécharny
www.iktek.com
directory.apache.org