001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.fabric.netty.util;
016    
017    import com.liferay.portal.kernel.concurrent.FutureListener;
018    import com.liferay.portal.kernel.concurrent.NoticeableFuture;
019    import com.liferay.portal.kernel.log.Log;
020    import com.liferay.portal.kernel.log.LogFactoryUtil;
021    
022    import io.netty.channel.Channel;
023    import io.netty.channel.ChannelInitializer;
024    import io.netty.channel.ChannelPipeline;
025    import io.netty.channel.EventLoop;
026    import io.netty.channel.embedded.EmbeddedChannel;
027    
028    import java.util.concurrent.Future;
029    import java.util.concurrent.TimeUnit;
030    
031    /**
032     * @author Shuyang Zhou
033     */
034    public class NettyUtil {
035    
036            public static ChannelPipeline createEmptyChannelPipeline() {
037                    Channel channel = new EmbeddedChannel(
038                            new ChannelInitializer<Channel>() {
039    
040                                    @Override
041                                    protected void initChannel(Channel channel) {
042                                    }
043    
044                            });
045    
046                    ChannelPipeline channelPipeline = channel.pipeline();
047    
048                    channelPipeline.removeLast();
049    
050                    return channelPipeline;
051            }
052    
053            public static <T> void scheduleCancellation(
054                    Channel channel, final NoticeableFuture<T> noticeableFuture,
055                    long timeout) {
056    
057                    EventLoop eventLoop = channel.eventLoop();
058    
059                    final Future<?> cancellationFuture = eventLoop.schedule(
060                            new Runnable() {
061    
062                                    @Override
063                                    public void run() {
064                                            if (noticeableFuture.cancel(true) && _log.isWarnEnabled()) {
065                                                    _log.warn("Cancelled timeout " + noticeableFuture);
066                                            }
067                                    }
068    
069                            },
070                            timeout, TimeUnit.MILLISECONDS);
071    
072                    noticeableFuture.addFutureListener(
073                            new FutureListener<T>() {
074    
075                                    @Override
076                                    public void complete(Future<T> future) {
077                                            if (cancellationFuture.cancel(true) &&
078                                                    _log.isDebugEnabled()) {
079    
080                                                    _log.debug(
081                                                            "Cancelled scheduled cancellation for " +
082                                                                    noticeableFuture);
083                                            }
084                                    }
085    
086                            });
087            }
088    
089            private static final Log _log = LogFactoryUtil.getLog(NettyUtil.class);
090    
091    }