AVA NIO 多线程服务器 1.2版

    reactor 模式的 java nio 多线程服务器
public class miniserver extends thread<br>{<br>    private static final log log = logfactory.getlog(miniserver.class);<br><br>    private final selector s;<br>    private final serversocketchannel ssc;<br>    private executorservice executor;<br><br>    public miniserver(int portnumber,executorservice executor) throws ioexception<br>    {<br>        this.executor=executor;<br>        s = selector.open();<br>        ssc = serversocketchannel.open();<br>        ssc.socket().bind(new inetsocketaddress(portnumber));<br>        ssc.configureblocking(false);<br>        ssc.register(s,selectionkey.op_accept);<br>    }<br><br>    public void run()<br>    {<br>        try<br>        {<br>            while(s.isopen())<br>            {<br>                int nkeys=s.select();<br>                if(nkeys>0)<br>                {<br>                    iterator<selectionkey> it = s.selectedkeys().iterator();<br>                    while (it.hasnext()) <br>                    {<br>                        selectionkey key = it.next();<br>                        it.remove();<br>                        if (!key.isvalid() || !key.channel().isopen())<br>                            continue;<br>                        if(key.isacceptable())<br>                        {<br>                            socketchannel sc = ssc.accept();<br>                            if (sc != null)<br>                            {<br>                                sc.configureblocking(false);<br>                                sc.register(s, selectionkey.op_read, new reader(executor));<br>                            }<br>                        }<br>                        else if(key.isreadable()||key.iswritable())<br>                        {<br>                            reactor reactor = (reactor) key.attachment();<br>                            reactor.execute(key);<br>                        }<br>                    }<br>                }<br>            }<br>        }<br>        catch(ioexception e)<br>        {<br>            log.info(e);<br>        }<br>    }<br>}
<br>public interface reactor <br>{<br>    void execute(selectionkey key);<br>}
<br>public class reader implements reactor <br>{<br>    private static final log log = logfactory.getlog(reader.class);<br><br>    private byte[] bytes=new byte[0];<br>    private executorservice executor;<br><br>    public reader(executorservice executor)<br>    {<br>        this.executor=executor;<br>    }<br><br>    @override<br>    public void execute(selectionkey key)<br>    {<br>        socketchannel sc = (socketchannel) key.channel();<br>        try<br>        {<br>            bytebuffer buffer=bytebuffer.allocate(1024);<br>            int len=-1;<br>            while(sc.isconnected() &amp;&amp; (len=sc.read(buffer))>0)<br>            {<br>                buffer.flip();<br>                  byte [] content = new byte[buffer.limit()];<br>                buffer.get(content);<br>                bytes=nututil.arraycoalition(bytes,content);<br>                buffer.clear();<br>            }<br>            if(len==0)<br>            {<br>                key.interestops(selectionkey.op_read);<br>                key.selector().wakeup(); <br>            }<br>            else if(len==-1)<br>            {<br>                callable<byte[]> call=new processcallable(bytes);<br>                future<byte[]> task=executor.submit(call);<br>                bytebuffer output=bytebuffer.wrap(task.get());<br>                sc.register(key.selector(), selectionkey.op_write, new writer(output));<br>            }<br>        }<br>        catch(exception e)<br>        {<br>            log.info(e);<br>        }<br>    }<br>}
<br>public class writer implements reactor <br>{<br>    private static final log log = logfactory.getlog(writer.class);<br><br>    private bytebuffer output;<br><br>    public writer(bytebuffer output)<br>    {<br>        this.output=output;<br>    }<br><br>    public void execute(selectionkey key)<br>    {<br>        socketchannel sc = (socketchannel) key.channel();<br>        try<br>        {<br>            while(sc.isconnected() &amp;&amp; output.hasremaining())<br>            {<br>                int len=sc.write(output);<br>                if(len<0)<br>                { <br>                    throw new eofexception(); <br>                } <br>                if(len==0) <br>                { <br>                    key.interestops(selectionkey.op_write); <br>                    key.selector().wakeup(); <br>                    break; <br>                }<br>            }<br>            if(!output.hasremaining())<br>            {<br>                output.clear();<br>                key.cancel();<br>                sc.close();<br>            }<br>        }<br>        catch(ioexception e)<br>        {<br>            log.info(e);<br>        }<br>    }<br>}
 

你可能感兴趣的:(java,工作)