You can use ScheduledExecutorThreadPool or Quartz open source library as
an alternative.

It doesn't matter in whatever thread you write a message except for the
I/O processor thread.  So scheduling a task in an I/O processor thread
doesn't have any advantage because the task will eventually run in a
thread which is not an I/O processor thread.

To run a task in an I/O processor thread, you need to make sure there's
no ExecutorFilter between the I/O processor thread and the task you are
going to execute.  For example, you could remove ExecutorFilter from the
filter chain and execute tasks in IoHandler.

However, if you want to send a message every X milliseconds in an I/O
processor thread for minimal latency, I can't think of any easy way to
do so.

BTW is there any problem with writing message from non I/O processor
threads?

huanxing shan wrote:
> I want to write a game server using Mina. The server compute the new
> state for each client  and send message to each client every 40ms so
> that every client can display a new frame.
> 
> some code followed:
> ------------------------------------------------------------------
>     private static Timer timer = new Timer();
> 
>     //a mothed of Main class
>     public static void main(String[] args) {
>         //execute once every 40 ms to update the display of client
>         timer.scheduleAtFixedRate(new MatchTimerTask(), 0, 40);
>     }
> 
>     public class MyTimerTask extends TimerTask {
>         public void run() {
>             //there is about several hundred sessions.
>             //do some business logic and send message to each client
>             for (int i = 0; i < sessionList.size(); i++) {
>                 businessLogic();
>                 //the session object is a instance of
> org.apache.mina.common.IoSession
>                 session.write(message);
>             }
>         }
>     }
> 
>     //a mothed of ServerSessionHandler class
>     public void messageReceived(IoSession session, Object message) {
>         recordClientCommand(session, (CilentCommand)message);
>     }
> ------------------------------------------------------------------
> 
> May I use some better class instead of the Timer and the TimerTask class?
> 
> Where should I put the scheduled task code  so that it may run in the
> I/O processor thread?
> 
> thanks,
> 
> Yu X.W.

-- 
Trustin Lee - Principal Software Engineer, JBoss, Red Hat
--
what we call human nature is actually human habit
--
http://gleamynode.net/

Attachment: signature.asc
Description: OpenPGP digital signature

Reply via email to