users@grizzly.java.net

Re: knowledge needed for migration guide nio to grizzly

From: Survivant 00 <survivant00_at_gmail.com>
Date: Thu, 5 Jun 2008 18:42:24 -0400

With NIO, you never hold a thread per connection. You use threads for
processing request, but you never have to hold the thread per connection. So
your ProtocolFilter implementation should never hold the thread. With NIO,
the JDK will changes the state of SelectionKey when there is read or write
operations ready.

I think that's my problem. I didn't use Selector. I didn't know what that
was.. but I think that is the next step that I have to change in my
program. I need to understand what Grizzly do under the hood first.



2008/6/5 Jeanfrancois Arcand <Jeanfrancois.Arcand_at_sun.com>:

> Salut
>
> Survivant 00 wrote:
>
>> thanks for the answer.
>>
>> I suppose this part of code will solve my problem of 1 thread by
>> connection ?
>>
>> DefaultPipeline defp = new DefaultPipeline();
>> defp.setMinThreads(20);
>> defp.setMaxThreads(100);
>>
>
> With NIO, you never hold a thread per connection. You use threads for
> processing request, but you never have to hold the thread per connection. So
> your ProtocolFilter implementation should never hold the thread. With NIO,
> the JDK will changes the state of SelectionKey when there is read or write
> operations ready.
>
>
>
>> controller.setPipeline(defp);
>>
>> but what will happen for the 101th connection ?
>>
>>
>> I saw that grizzly needed JDK5+. I didn't check the source (yet :) ) of
>> grizzly, but basicaly what is the reason ?
>> Just to know. I learn that our production servers are on RH3 and they
>> support jdk 1.4, and I can't put jdk5 onless a upgrade OS. and it seems
>> that Grizzly is not enough to justify it :)
>>
>
> We stopped supporting 1.4 a while ago....aside generics, I don't think we
> are using anything special. So I suspect you can build Grizzly using jdk5 to
> run on jdk4 (we have to try that).
>
> Thanks
>
> -- Jeanfrancois
>
>
>
>>
>>
>> 2008/6/5 John ROM <snake-john_at_gmx.de <mailto:snake-john_at_gmx.de>>:
>>
>> Hi,
>> I did read your mail completely (-:
>>
>> I think it is very simple to migrate your code to grizzly.
>>
>> Basicly for the server-client side you just need to setup a
>> ProtocolChain with a ProtocolParserFilter
>> and another Filter to execute commands.
>>
>> You probably can use the ProtocolParserFilter in mail
>> https://grizzly.dev.java.net/servlets/ReadMsg?list=users&msgNo=895
>> <https://grizzly.dev.java.net/servlets/ReadMsg?list=users&msgNo=895>
>>
>> ProtocolParser implementation II from (Ken--_at_newsgroupstats.hk
>> <mailto:Ken--_at_newsgroupstats.hk>) and Alexey
>>
>>
>> as a guide.
>>
>> I have attached a new version of SocketConnectionListener.java where
>> I sketch out what I would
>> do. (In that I keep idle connections for 30 minutes. I' am not sure
>> if that's what you wanted)
>>
>> The code is really just how I would start.
>> No TimeoutHandling/No Exception Handling./No keepalive testing
>>
>>
>> Just tell me if this post helped....
>>
>> Many Greetings
>> John
>>
>>
>> ------------------------ Code ---------------
>> package org.sample.nio.connection.handler;
>>
>>
>> import java.io.IOException;
>> import java.nio.channels.ServerSocketChannel;
>>
>> import org.sample.nio.QuoteManager;
>> import com.sun.grizzly.*;
>> import com.sun.grizzly.filter.ParserProtocolFilter;
>>
>> /**
>> * This class listen for incoming connection
>> *
>> * @author Sebastien Dionne
>> */
>> public class SocketConnectionListener implements Runnable {
>>
>> protected String f_name;
>> protected int f_port;
>> protected QuoteManager f_quoteManager;
>> protected boolean f_shutdown;
>> protected ThreadGroup f_threadGroup;
>> protected ServerSocketChannel f_serverSocket;
>>
>> /**
>> * @return the quoteManager
>> */
>> public QuoteManager getQuoteManager() {
>> return f_quoteManager;
>> }
>>
>> /**
>> * @param quoteManager the quoteManager
>> */
>> public void setQuoteManager(QuoteManager quoteManager) {
>> this.f_quoteManager = quoteManager;
>> }
>>
>> /**
>> * @param port port for the incomming connection
>> */
>> public void setPort(int port) {
>> f_port = port;
>> }
>>
>> /**
>> * @return the listening port
>> */
>> public int getPort() {
>> return f_port;
>> }
>>
>> private Controller controller;
>>
>> /**
>> * Init
>> */
>> public void init() {
>>
>> System.out.println("listening for incomming TCP Connections
>> on port : " + f_port);
>> try {
>>
>> controller = new Controller();
>> DefaultPipeline defp = new DefaultPipeline();
>> defp.setMinThreads(20);
>> defp.setMaxThreads(100);
>>
>> controller.setPipeline(defp);
>>
>> TCPSelectorHandler tcpSelectorHandler = new
>> TCPSelectorHandler();
>>
>> DefaultSelectionKeyHandler keyHandler = new
>> DefaultSelectionKeyHandler();
>> //keep connection for 30 minutes
>> keyHandler.setTimeout(30 * 1000 * 60);
>> tcpSelectorHandler.setSelectionKeyHandler(keyHandler);
>>
>> tcpSelectorHandler.setPort(f_port);
>> controller.addSelectorHandler(tcpSelectorHandler);
>>
>> } catch (Exception e) {
>> System.exit(-10);
>> }
>>
>> QueryQuoteManagerFilter quoteManagerFilter = new
>> QueryQuoteManagerFilter(f_quoteManager);
>> final ProtocolFilter parserProtocolFilter = new
>> ParserProtocolFilter() {
>>
>>
>>
>> public ProtocolParser newProtocolParser() {
>> return new YourProtocolParser();
>> }
>> };
>> final ProtocolChain protocolChain = new DefaultProtocolChain();
>> protocolChain.addFilter(parserProtocolFilter);
>> protocolChain.addFilter(quoteManagerFilter);
>> ((DefaultProtocolChain)
>> protocolChain).setContinuousExecution(true);
>>
>>
>> ProtocolChainInstanceHandler pciHandler = new
>> DefaultProtocolChainInstanceHandler() {
>>
>> public ProtocolChain poll() {
>>
>> return protocolChain;
>> }
>>
>> public boolean offer(ProtocolChain protocolChain) {
>> return false;
>>
>> }
>> };
>>
>> controller.setProtocolChainInstanceHandler(pciHandler);
>> try {
>> controller.start();
>> } catch (IOException e) {
>> e.printStackTrace();
>> }
>>
>> }
>>
>> /**
>> * @return the shutdown
>> */
>> public boolean isShutdown() {
>> return f_shutdown;
>> }
>>
>> /**
>> * shutdown the socket listener
>> */
>> public void shutdown() {
>>
>> }
>>
>> /**
>> * processing
>> * <p/>
>> * - Open socket
>> * - a new Thread (ClientConnectionHandler)
>> * - start the new thread
>> */
>> public void run() {
>>
>> init();
>>
>> }
>>
>> public class QueryQuoteManagerFilter implements ProtocolFilter {
>> private QuoteManager manager;
>>
>> public QueryQuoteManagerFilter(QuoteManager manager) {
>> this.manager = manager;
>> }
>>
>> public boolean execute(Context context) throws IOException {
>> String query = (String)
>> context.removeAttribute(ProtocolParser.MESSAGE);
>> //here should be the parsed query command string with
>> // which query should be able to create a command
>> // I have no time to rewrite ClientConnectionHandler
>> //Basicly you have in context a link to client connection
>> //with that and forexample TCPSelectorHandler your
>> commands can send data to client
>> manager.processQuery(null, query);
>>
>> return false;
>> }
>>
>> public boolean postExecute(Context context) throws IOException {
>> return true;
>> }
>> }
>>
>> public abstract class YourProtocolParser implements ProtocolParser {
>> // here you have to write your own parser
>> // I think parser in mail
>> //
>> https://grizzly.dev.java.net/servlets/ReadMsg?list=users&msgNo=895
>> <https://grizzly.dev.java.net/servlets/ReadMsg?list=users&msgNo=895>
>> // by Ken--_at_newsgroupstats.hk
>> <mailto:Ken--_at_newsgroupstats.hk> and Alexey would be a good starting
>> point
>> }
>>
>>
>>
>> }
>>
>>
>>
>>
>> --
>> Super-Aktion nur in der GMX Spieleflat: 10 Tage für 1 Euro.
>> Über 180 Spiele downloaden: http://flat.games.gmx.de
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe_at_grizzly.dev.java.net
>> <mailto:users-unsubscribe_at_grizzly.dev.java.net>
>> For additional commands, e-mail: users-help_at_grizzly.dev.java.net
>> <mailto:users-help_at_grizzly.dev.java.net>
>>
>>
>>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_grizzly.dev.java.net
> For additional commands, e-mail: users-help_at_grizzly.dev.java.net
>
>