Yang Zhang wrote:
Mark wrote:
The "Configuring Thread Model" page should also apply for UDP-based
systems.  Just make sure you are not using an ExecutorFilter and you can
pass in an Executor that only uses one thread.

Hi, Mark,

I suppose I should have been more clear on the questions I still had after reading that page.

1. DatagramAcceptor doesn't take a max number of IO threads, unlike SocketAcceptor.

2. DatagramAcceptor takes an executor - I have never been able to find any mention in the docs on what exactly gets submitted to this executor (all I know is that it's not related to IO threads, which is parameterized by the above int) - but experimentally, I found I cannot pass in a single-threaded executor, since it seems that the call to bind() for all but the first DatagramAcceptor will block. And I did not add any filters.

Thanks for answers.

Here's a simple program that explains what I mean.

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import org.apache.mina.common.IoHandlerAdapter;
import org.apache.mina.common.IoSession;
import org.apache.mina.common.ThreadModel;
import org.apache.mina.transport.socket.nio.DatagramAcceptor;
import sun.nio.ch.DatagramSocketAdaptor;

public class Test {

    public static void main(String[] args) throws Exception {
        InetAddress addr = InetAddress.getLocalHost();
        ExecutorService executor = Executors.newSingleThreadExecutor();
        for (int i = 0; i < 2; i++) {
            DatagramAcceptor a = new DatagramAcceptor(executor);
            a.getDefaultConfig().setThreadModel(ThreadModel.MANUAL);
            a.bind(new InetSocketAddress(addr, 10000 + i),
                    new IoHandlerAdapter() {

public void messageReceived(IoSession session, Object obj)
                                throws Exception {
System.out.println("received " + obj + " on " + session.getLocalAddress());
                        }
                    });
        }
        System.out.println("hello");
        while (true) {
            Thread.sleep(1000);
            byte[] buf = new byte[2];
            DatagramSocket s = new DatagramSocket();
            s.send(new DatagramPacket(buf, buf.length, addr, 10000));
            s.send(new DatagramPacket(buf, buf.length, addr, 10001));
        }
    }
}

--
Yang Zhang
http://www.mit.edu/~y_z/

Reply via email to