users@grizzly.java.net

Re: code snippet for java client using comet

From: gustav trede <gustav.trede_wsdevel_at_telia.com>
Date: Mon, 12 Jan 2009 20:09:21 +0100

2009/1/9 Sreenivas Munnangi <Sreenivas.Munnangi_at_sun.com>

> Hi,
>
> I am planning to use comet in a java client program, did any one have a
> code snippet or example ?
>
> thanks
> sreeni
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_grizzly.dev.java.net
> For additional commands, e-mail: users-help_at_grizzly.dev.java.net
>
>
heres an example of comet client. sorry i dont have any simpler /cleaner
example.
this is a worker thread of my swing load test client for comet. supporting
long polling and streaming.

 private class ClientWorker extends Thread{
        private URL url;

        public ClientWorker() {
            super();
        }

        @Override
        public void run() {
            DataInputStream in = null;
            long lastdeath = 0;
            allclients.add(this);
            threadcount.incrementAndGet();
            try{
            byte[] ba = new byte[4];
            while (!isInterrupted())
                try {
                    if (url == null)
                        url = new URL(connectstring); //"
http://localhost:8080/"
                    HttpURLConnection con = (HttpURLConnection)
url.openConnection();
                    con.setConnectTimeout(4*1000);
                    con.setReadTimeout(0);
                    con.setUseCaches(false);
                    con.connect();
                    in = new DataInputStream(con.getInputStream());
                    do {
                        int diepos = 0;
                        int dc = deathchance;
                        if (dc>0){
                            long t = System.nanoTime()/1000;
                            if (t%(1+100/dc)==0 &&
t-lastdeath>1000000){//max 1 death per thread per second
                                lastdeath = t;
                                diepos = failnotify ? (int)(t % 2)+1 : 1 ;
                            }
                        }
                        if (diepos == 1){ //todo close , not sleep before...
                            abortcount.getAndIncrement();
                            break;
                        }
                        final int size = in.readInt(); // not interruptable
                        if (diepos == 2){
                            abortcount.getAndIncrement();
                            break;
                        }
                        if (ba.length != size)
                            ba = new byte[size];
                        in.readFully(ba);
                        datathroughput.addAndGet(4+size);
                        messagecount.getAndIncrement();
                    }while(dostreaming && !isInterrupted());
                } catch (Exception ex) {
                    if ( (ex instanceof InterruptedException)){
                        return;
                    }else{
                        failedIOcount.getAndIncrement();
                        //System.out.println());
                    }
                }finally{
                    try {
                        if (reconnectdelay > 0 )
                            Thread.sleep(reconnectdelay);
                    } catch (InterruptedException ex) {
                        return;
                    }finally{
                        try{
                            if (in != null)
                                in.close();
                        }catch(Throwable e) {}
                    }
                 }
            }finally{
                threadcount.decrementAndGet();
                allclients.remove(this);
            }
        }

    }

-- 
your servant
 gustav trede
coding is art - not only something that  bring food on the table,
everybody should be able to feel proud about their code,
that they have performed their best considering the given conditions.