本文整理汇总了Java中org.jboss.netty.channel.ChannelFutureListener类的典型用法代码示例。如果您正苦于以下问题:Java ChannelFutureListener类的具体用法?Java ChannelFutureListener怎么用?Java ChannelFutureListener使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
ChannelFutureListener类属于org.jboss.netty.channel包,在下文中一共展示了ChannelFutureListener类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: send
import org.jboss.netty.channel.ChannelFutureListener; //导入依赖的package包/类
/**
* Sends this response to all the passed channels as a {@link TextWebSocketFrame}
* @param listener A channel future listener to attach to each channel future. Ignored if null.
* @param channels The channels to send this response to
* @return An array of the futures for the write of this response to each channel written to
*/
public ChannelFuture[] send(ChannelFutureListener listener, Channel...channels) {
if(channels!=null && channels.length>0) {
Set<ChannelFuture> futures = new HashSet<ChannelFuture>(channels.length);
if(opCode==null) {
opCode = "ok";
}
TextWebSocketFrame frame = new TextWebSocketFrame(this.toChannelBuffer());
for(Channel channel: channels) {
if(channel!=null && channel.isWritable()) {
ChannelFuture cf = Channels.future(channel);
if(listener!=null) cf.addListener(listener);
channel.getPipeline().sendDownstream(new DownstreamMessageEvent(channel, cf, frame, channel.getRemoteAddress()));
futures.add(cf);
}
}
return futures.toArray(new ChannelFuture[futures.size()]);
}
return EMPTY_CHANNEL_FUTURE_ARR;
}
示例2: decode
import org.jboss.netty.channel.ChannelFutureListener; //导入依赖的package包/类
@Override
protected Object decode(
Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
HttpRequest request = (HttpRequest) msg;
QueryStringDecoder decoder = new QueryStringDecoder(request.getUri());
DeviceSession deviceSession = getDeviceSession(
channel, remoteAddress, decoder.getParameters().get("UserName").get(0));
if (deviceSession == null) {
return null;
}
Parser parser = new Parser(PATTERN, decoder.getParameters().get("LOC").get(0));
if (!parser.matches()) {
return null;
}
Position position = new Position();
position.setProtocol(getProtocolName());
position.setDeviceId(deviceSession.getDeviceId());
position.setTime(parser.nextDateTime(Parser.DateTimeFormat.DMY_HMS));
position.setValid(true);
position.setLatitude(parser.nextDouble(0));
position.setLongitude(parser.nextDouble(0));
position.setAltitude(parser.nextDouble(0));
position.setSpeed(parser.nextDouble(0));
position.setCourse(parser.nextDouble(0));
if (channel != null) {
HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
channel.write(response).addListener(ChannelFutureListener.CLOSE);
}
return position;
}
示例3: handle
import org.jboss.netty.channel.ChannelFutureListener; //导入依赖的package包/类
@Override
public void handle(Channel channel, Token<DelegationTokenIdentifier> token,
String serviceUrl) throws IOException {
Assert.assertEquals(testToken, token);
Credentials creds = new Credentials();
creds.addToken(new Text(serviceUrl), token);
DataOutputBuffer out = new DataOutputBuffer();
creds.write(out);
int fileLength = out.getData().length;
ChannelBuffer cbuffer = ChannelBuffers.buffer(fileLength);
cbuffer.writeBytes(out.getData());
HttpResponse response = new DefaultHttpResponse(HTTP_1_1, OK);
response.setHeader(HttpHeaders.Names.CONTENT_LENGTH,
String.valueOf(fileLength));
response.setContent(cbuffer);
channel.write(response).addListener(ChannelFutureListener.CLOSE);
}
示例4: messageReceived
import org.jboss.netty.channel.ChannelFutureListener; //导入依赖的package包/类
@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e)
throws Exception {
super.messageReceived(ctx, e);
System.out.println("-------- Server Channel messageRecieved "
+ System.currentTimeMillis());
if (induceError.get()) {
System.out
.println("Inducing Error in Server messageReceived method");
throw new IOException("Induced error ");
}
MessageEventBag bag = new MessageEventBag();
bag.setBytes(e);
bagList.add(bag);
ChannelBuffer buffer = ChannelBuffers.dynamicBuffer();
buffer.writeInt(200);
ChannelFuture future = e.getChannel().write(buffer);
future.addListener(ChannelFutureListener.CLOSE);
}
示例5: exceptionCaught
import org.jboss.netty.channel.ChannelFutureListener; //导入依赖的package包/类
@Override
public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e)
throws Exception {
System.out.println("Server Exception Caught");
e.getCause().printStackTrace();
/**
* Very important to respond here.
* The agent will always be listening for some kind of feedback.
*/
ChannelBuffer buffer = ChannelBuffers.dynamicBuffer();
buffer.writeInt(500);
ChannelFuture future = e.getChannel().write(buffer);
future.addListener(ChannelFutureListener.CLOSE);
}
示例6: messageReceived
import org.jboss.netty.channel.ChannelFutureListener; //导入依赖的package包/类
@Override
public void messageReceived(ChannelHandlerContext context, MessageEvent messageEvent) throws Exception {
context.getChannel().getPipeline().remove(this);
Response response = (Response) context.getChannel().getAttachment();
if (response == null) {
logger.debug("response is null");
HttpResponse httpResponse = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.INTERNAL_SERVER_ERROR);
httpResponse.headers().set(HttpHeaders.Names.CONTENT_LENGTH, "0");
httpResponse.setContent(ChannelBuffers.EMPTY_BUFFER);
httpResponse.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.CLOSE);
context.getChannel().write(httpResponse).addListener(ChannelFutureListener.CLOSE);
return;
}
handleHttpResponse(context, response);
}
示例7: writeBodyAsync
import org.jboss.netty.channel.ChannelFutureListener; //导入依赖的package包/类
/**
* Writes the given body to Netty channel. Will <b>not</b >wait until the body has been written.
*
* @param log logger to use
* @param channel the Netty channel
* @param remoteAddress the remote address when using UDP
* @param body the body to write (send)
* @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);
}
}
示例8: closeChannel
import org.jboss.netty.channel.ChannelFutureListener; //导入依赖的package包/类
/**
* Avoid channel double close
*
* @param channel
*/
void closeChannel(final Channel channel) {
synchronized (this) {
if (closingChannel.contains(channel)) {
LOG.info(channel.toString() + " is already closed");
return ;
}
closingChannel.add(channel);
}
LOG.debug(channel.toString() + " begin to closed");
ChannelFuture closeFuture = channel.close();
closeFuture.addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future)
throws Exception {
synchronized (this) {
closingChannel.remove(channel);
}
LOG.debug(channel.toString() + " finish closed");
}
});
}
示例9: channelConnected
import org.jboss.netty.channel.ChannelFutureListener; //导入依赖的package包/类
@Override
public void channelConnected(final ChannelHandlerContext ctx, final ChannelStateEvent e) {
//prevent javax.net.ssl.SSLException: Received close_notify during handshake
final SslHandler sslHandler = ctx.getPipeline().get(SslHandler.class);
if (sslHandler == null) {
return;
}
final ChannelFuture handshakeFuture = sslHandler.handshake();
handshakeFuture.addListener(new ChannelFutureListener() {
@Override
public void operationComplete(final ChannelFuture future) throws Exception {
if (logger.isTraceEnabled()) {
logger.trace("Node to Node encryption cipher is {}/{}", sslHandler.getEngine().getSession().getProtocol(), sslHandler
.getEngine().getSession().getCipherSuite());
}
ctx.sendUpstream(e);
}
});
}
示例10: closeChannel
import org.jboss.netty.channel.ChannelFutureListener; //导入依赖的package包/类
/**
* Avoid channel double close
*
* @param channel
*/
void closeChannel(final Channel channel) {
synchronized (this) {
if (closingChannel.contains(channel)) {
LOG.info(channel.toString() + " is already closed");
return ;
}
closingChannel.add(channel);
}
LOG.debug(channel.toString() + " begin to closed");
ChannelFuture closeFuture = channel.close();
closeFuture.addListener(new ChannelFutureListener() {
public void operationComplete(ChannelFuture future)
throws Exception {
synchronized (this) {
closingChannel.remove(channel);
}
LOG.debug(channel.toString() + " finish closed");
}
});
}
示例11: decode
import org.jboss.netty.channel.ChannelFutureListener; //导入依赖的package包/类
protected Object decode(ChannelHandlerContext ctx, Channel channel, ChannelBuffer buffer) throws Exception {
if (buffer.readableBytes() < 2) {
return null;
}
final int magic1 = buffer.getUnsignedByte(buffer.readerIndex());
final int magic2 = buffer.getUnsignedByte(buffer.readerIndex() + 1);
boolean isFlashPolicyRequest = (magic1 == '<' && magic2 == 'p');
if (isFlashPolicyRequest) {
l.info("flash policy requested");
buffer.skipBytes(buffer.readableBytes()); // Discard everything
channel.write(policyResponse).addListener(ChannelFutureListener.CLOSE);
return null;
}
// Remove ourselves, important since the byte length check at top can hinder frame decoding
// down the pipeline
ctx.getPipeline().remove(this);
return buffer.readBytes(buffer.readableBytes());
}
示例12: shutdown
import org.jboss.netty.channel.ChannelFutureListener; //导入依赖的package包/类
public void shutdown() {
L4j.getL4j().info(SHUTDOWN);
try {
this.isShuttingDown = true;
Channel channel = this.pipeline.getCurrentPipeline().getChannel();
this.channelFuture.getChannel().write(ChannelBuffers.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);
L4j.getL4j().info(CLOSE_CHANNEL);
channelFactory.releaseExternalResources();
L4j.getL4j().info(RELEASE_FACTORY);
clientBootstrap.releaseExternalResources();
L4j.getL4j().info(RELEASE_CLIENT);
} catch (Exception e) {
L4j.getL4j().error(new StringBuilder(ERROR_CHANNEL).append(e.toString()).toString(), e);
}
}
示例13: closeChannel
import org.jboss.netty.channel.ChannelFutureListener; //导入依赖的package包/类
/**
* Avoid channel double close
*
* @param channel
*/
void closeChannel(final Channel channel) {
synchronized (channelClosing) {
if (closingChannel.contains(channel)) {
LOG.info(channel.toString() + " is already closed");
return;
}
closingChannel.add(channel);
}
LOG.debug(channel.toString() + " begin to closed");
ChannelFuture closeFuture = channel.close();
closeFuture.addListener(new ChannelFutureListener() {
public void operationComplete(ChannelFuture future) throws Exception {
synchronized (channelClosing) {
closingChannel.remove(channel);
}
LOG.debug(channel.toString() + " finish closed");
}
});
}
示例14: sendClosedPacket
import org.jboss.netty.channel.ChannelFutureListener; //导入依赖的package包/类
private void sendClosedPacket(Channel channel) {
if (!channel.isConnected()) {
logger.debug("channel already closed. skip sendClosedPacket() {}", channel);
return;
}
logger.debug("write ClientClosePacket");
ClientClosePacket clientClosePacket = new ClientClosePacket();
ChannelFuture write = channel.write(clientClosePacket);
write.addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) throws Exception {
if (!future.isSuccess()) {
logger.warn("ClientClosePacket write failed. channel:{}", future.getCause(), future.getCause());
} else {
logger.debug("ClientClosePacket write success. channel:{}", future.getChannel());
}
}
});
write.awaitUninterruptibly(3000, TimeUnit.MILLISECONDS);
}
示例15: SocketChannel
import org.jboss.netty.channel.ChannelFutureListener; //导入依赖的package包/类
public SocketChannel(final Channel channel, long timeoutMillis, Timer timer) {
if (channel == null) {
throw new NullPointerException("channel");
}
if (timer == null) {
throw new NullPointerException("channel");
}
this.channel = channel;
this.timeoutMillis = timeoutMillis;
this.timer = timer;
this.requestManager = new RequestManager(this.timer);
this.responseWriteFail = new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) throws Exception {
if (!future.isSuccess()) {
logger.warn("responseWriteFail. {}", channel);
}
}
};
}