Package org.jboss.netty.channel

Examples of org.jboss.netty.channel.ChannelFuture


            res.setContent(ChannelBuffers.copiedBuffer(res.getStatus().toString(), CharsetUtil.UTF_8));
            setContentLength(res, res.getContent().readableBytes());
        }

        // Send the response and close the connection if necessary.
        ChannelFuture f = ctx.getChannel().write(res);
        if (!isKeepAlive(req) || res.getStatus().getCode() != 200) {
            f.addListener(ChannelFutureListener.CLOSE);
        }
    }
View Full Code Here


      long newTime = (date.getTime() + random.nextInt());
      Date newDate = new Date(newTime);
      slog("Hey Guys !  I got a date ! [" + date + "] and I modified it to [" + newDate + "]");
      // Send back the reponse
      Channel channel = e.getChannel();
      ChannelFuture channelFuture = Channels.future(e.getChannel());
      ChannelEvent responseEvent = new DownstreamMessageEvent(channel, channelFuture, newDate, channel.getRemoteAddress());
      ctx.sendDownstream(responseEvent);
      // But still send it upstream because there might be another handler
      super.messageReceived(ctx, e);
    }   
View Full Code Here

    options.put("connectTimeoutMillis", ioTimeout);
    // Create the client
    // Not providing any handlers since we're not using any for this test
    SimpleNIOClient client = new SimpleNIOClient(options);
    // Issue a connect operation
    ChannelFuture cf = client.connect("heliosapm.com", 80);   
    // Add a completion listener
    cf.addListener(new ChannelFutureListener() {
      public void operationComplete(ChannelFuture future) throws Exception {
        if(future.isSuccess()) {
          clog("F: Connected:" + future.isDone());
        } else {
          if(future.isCancelled()) {
            clog("Request Cancelled");
          } else {
            clog("Connect Exception:Success: " + future.isSuccess() + "  Done: " + future.isDone()  + "  Cause: "+ future.getCause());
          }
        }
      }
    });
    // Wait at least futureTimeout for the operation to complete
    cf.awaitUninterruptibly(futureTimeout);
    // If the operation is not complete, cancel it.
    if(!cf.isDone()) {
      clog("Channel Future Still Waiting. Cancelled:" + cf.cancel());
    }
  }
View Full Code Here

    }
   
    HttpResponse response = new DefaultHttpResponse(HTTP_1_1, OK);
    response.setContent(ChannelBuffers.copiedBuffer("\n" + message.toString() + "\n", CharsetUtil.UTF_8));
    response.setHeader(CONTENT_TYPE, "application/json");
    ChannelFuture cf = Channels.future(channel);
    cf.addListener(new ChannelFutureListener(){
      public void operationComplete(ChannelFuture f) throws Exception {
        channel.close();
      }
    });
    ctx.sendDownstream(new DownstreamMessageEvent(channel, cf, response, channel.getRemoteAddress()));
View Full Code Here

        if (httpRequest != null) {
            HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
            response.setHeader("Content-Length", output.readableBytes());
            response.setContent(output);
            ChannelFuture future = e.getChannel().write(response);
            if (!HttpHeaders.isKeepAlive(httpRequest)) {
                future.addListener(ChannelFutureListener.CLOSE);
            }
        } else {
            e.getChannel().write(output);
        }
    }
View Full Code Here

      // Phew. Ok. We built all that. Now what ?
      InetSocketAddress addressToConnectTo = new InetSocketAddress("localhost", 8080);
      //ChannelFuture cf = bootstrap.connect(addressToConnectTo);
      clog("Issuing Channel Connect...");
      // Waiting on a connect. (Pick one)
      ChannelFuture cf = bootstrap.connect(addressToConnectTo);
      // wait interruptibly
//      cf.await();
      // wait interruptibly with a timeout of 2000 ms.
//      cf.await(2000, TimeUnit.MILLISECONDS);
      // wait uninterruptibly
      clog("Waiting for Channel Connect...");
      cf.awaitUninterruptibly();
      // wait uninterruptibly with a timeout of 2000 ms.
//      cf.awaitUninterruptibly(2000, TimeUnit.MILLISECONDS);
      // add a ChannelFutureListener that writes the Date when the connect is complete
//      cf.addListener(new ChannelFutureListener(){
//        public void operationComplete(ChannelFuture future) throws Exception {
//          // chek to see if we succeeded
//          if(future.isSuccess()) {
//            Channel channel = future.getChannel();
//            channel.write(new Date());
//            // remember, the write is asynchronous too !
//          }
//        }
//      });
      // if a wait option was selected and the connect did not fail,
      // the Date can now be sent.
      clog("Connected. Sending Date");
      Channel channel = cf.getChannel();
      channel.write(new Date());
    } catch (Exception e) {
      e.printStackTrace(System.err);
    }
  }
View Full Code Here

      };
      ClientBootstrap bootstrap = new ClientBootstrap(channelFactory);
      bootstrap.setPipelineFactory(pipelineFactory);
      InetSocketAddress addressToConnectTo = new InetSocketAddress("localhost", 8080);
      clog("Issuing Channel Connect...");
      ChannelFuture cf = bootstrap.connect(addressToConnectTo);
      clog("Waiting for Channel Connect...");
      cf.awaitUninterruptibly();
      Date dt = new Date();
      clog("Connected. Sending Date [" + dt + "]");
      Channel channel = cf.getChannel();
      channel.write(dt);
    } catch (Exception e) {
      e.printStackTrace(System.err);
    }
  }
View Full Code Here

            datagramChannelFactory = new NioDatagramChannelFactory(workerPool);
        }
    }

    protected ChannelFuture openConnection() throws Exception {
        ChannelFuture answer;

        if (isTcp()) {
            // its okay to create a new bootstrap for each new channel
            ClientBootstrap clientBootstrap = new ClientBootstrap(channelFactory);
            clientBootstrap.setOption("keepAlive", configuration.isKeepAlive());
View Full Code Here

     */
    private final class NettyProducerPoolableObjectFactory implements PoolableObjectFactory<Channel> {

        @Override
        public Channel makeObject() throws Exception {
            ChannelFuture channelFuture = openConnection();
            Channel answer = openChannel(channelFuture);
            LOG.trace("Created channel: {}", answer);
            return answer;
        }
View Full Code Here

     * @param exchange        the exchange
     * @param listener        listener with work to be executed when the operation is complete
     */
    public static void writeBodyAsync(Logger log, Channel channel, SocketAddress remoteAddress, Object body,
                                      Exchange exchange, ChannelFutureListener listener) {
        ChannelFuture future;
        if (remoteAddress != null) {
            if (log.isDebugEnabled()) {
                log.debug("Channel: {} remote address: {} writing body: {}", new Object[]{channel, remoteAddress, body});
            }
            future = channel.write(body, remoteAddress);
        } else {
            if (log.isDebugEnabled()) {
                log.debug("Channel: {} writing body: {}", new Object[]{channel, body});
            }
            future = channel.write(body);
        }

        if (listener != null) {
            future.addListener(listener);
        }
    }
View Full Code Here

TOP

Related Classes of org.jboss.netty.channel.ChannelFuture

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.