Java Code Examples for org.apache.rocketmq.remoting.common.RemotingUtil#closeChannel()

The following examples show how to use org.apache.rocketmq.remoting.common.RemotingUtil#closeChannel() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: NettyRemotingServer.java    From DDMQ with Apache License 2.0 6 votes vote down vote up
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
    if (evt instanceof IdleStateEvent) {
        IdleStateEvent event = (IdleStateEvent) evt;
        if (event.state().equals(IdleState.ALL_IDLE)) {
            final String remoteAddress = RemotingHelper.parseChannelRemoteAddr(ctx.channel());
            log.warn("NETTY SERVER PIPELINE: IDLE exception [{}]", remoteAddress);
            RemotingUtil.closeChannel(ctx.channel());
            if (NettyRemotingServer.this.channelEventListener != null) {
                NettyRemotingServer.this
                    .putNettyEvent(new NettyEvent(NettyEventType.IDLE, remoteAddress, ctx.channel()));
            }
        }
    }

    ctx.fireUserEventTriggered(evt);
}
 
Example 2
Source File: NettyRemotingServer.java    From rocketmq-all-4.1.0-incubating with Apache License 2.0 6 votes vote down vote up
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
    if (evt instanceof IdleStateEvent) {
    	/**
         *IdleStateEvent事件,在指定时间没有进行读写,会进行回调
         */
        IdleStateEvent event = (IdleStateEvent) evt;
        if (event.state().equals(IdleState.ALL_IDLE)) {
            final String remoteAddress = RemotingHelper.parseChannelRemoteAddr(ctx.channel());
            log.warn("NETTY SERVER PIPELINE: IDLE exception [{}]", remoteAddress);
            RemotingUtil.closeChannel(ctx.channel());  //关闭channel
            if (NettyRemotingServer.this.channelEventListener != null) {
                NettyRemotingServer.this
                    .putNettyEvent(new NettyEvent(NettyEventType.IDLE, remoteAddress, ctx.channel()));
            }
        }
    }

    ctx.fireUserEventTriggered(evt);
}
 
Example 3
Source File: RouteInfoManager.java    From rocketmq-read with Apache License 2.0 6 votes vote down vote up
/**
 * 扫描不活跃的Broker
 */
public void scanNotActiveBroker() {
    Iterator<Entry<String, BrokerLiveInfo>> it = this.brokerLiveTable.entrySet().iterator();
    while (it.hasNext()) {
        Entry<String, BrokerLiveInfo> next = it.next();
        long last = next.getValue().getLastUpdateTimestamp();
        //如果超过2分钟没有上报心跳了,就移除Channel
        if ((last + BROKER_CHANNEL_EXPIRED_TIME) < System.currentTimeMillis()) {
            RemotingUtil.closeChannel(next.getValue().getChannel());
            //移除
            it.remove();
            log.warn("The broker channel expired, {} {}ms", next.getKey(), BROKER_CHANNEL_EXPIRED_TIME);
            this.onChannelDestroy(next.getKey(), next.getValue().getChannel());
        }
    }
}
 
Example 4
Source File: NettyEncoder.java    From DDMQ with Apache License 2.0 6 votes vote down vote up
@Override
public void encode(ChannelHandlerContext ctx, RemotingCommand remotingCommand, ByteBuf out)
    throws Exception {
    try {
        ByteBuffer header = remotingCommand.encodeHeader();
        out.writeBytes(header);
        byte[] body = remotingCommand.getBody();
        if (body != null) {
            out.writeBytes(body);
        }
    } catch (Exception e) {
        log.error("encode exception, " + RemotingHelper.parseChannelRemoteAddr(ctx.channel()), e);
        if (remotingCommand != null) {
            log.error(remotingCommand.toString());
        }
        RemotingUtil.closeChannel(ctx.channel());
    }
}
 
Example 5
Source File: NettyEncoder.java    From rocketmq with Apache License 2.0 6 votes vote down vote up
@Override
public void encode(ChannelHandlerContext ctx, RemotingCommand remotingCommand, ByteBuf out)
    throws Exception {
    try {
        ByteBuffer header = remotingCommand.encodeHeader();
        out.writeBytes(header);
        byte[] body = remotingCommand.getBody();
        if (body != null) {
            out.writeBytes(body);
        }
    } catch (Exception e) {
        log.error("encode exception, " + RemotingHelper.parseChannelRemoteAddr(ctx.channel()), e);
        if (remotingCommand != null) {
            log.error(remotingCommand.toString());
        }
        RemotingUtil.closeChannel(ctx.channel());
    }
}
 
Example 6
Source File: NettyEncoder.java    From rocketmq with Apache License 2.0 6 votes vote down vote up
@Override
public void encode(ChannelHandlerContext ctx, RemotingCommand remotingCommand, ByteBuf out)
    throws Exception {
    try {
        ByteBuffer header = remotingCommand.encodeHeader();
        out.writeBytes(header);
        byte[] body = remotingCommand.getBody();
        if (body != null) {
            out.writeBytes(body);
        }
    } catch (Exception e) {
        log.error("encode exception, " + RemotingHelper.parseChannelRemoteAddr(ctx.channel()), e);
        if (remotingCommand != null) {
            log.error(remotingCommand.toString());
        }
        RemotingUtil.closeChannel(ctx.channel());
    }
}
 
Example 7
Source File: ProducerManager.java    From rocketmq-all-4.1.0-incubating with Apache License 2.0 5 votes vote down vote up
public void scanNotActiveChannel() {
    try {
        if (this.groupChannelLock.tryLock(LOCK_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS)) {
            try {
                for (final Map.Entry<String, HashMap<Channel, ClientChannelInfo>> entry : this.groupChannelTable
                    .entrySet()) {
                    final String group = entry.getKey();
                    final HashMap<Channel, ClientChannelInfo> chlMap = entry.getValue();

                    Iterator<Entry<Channel, ClientChannelInfo>> it = chlMap.entrySet().iterator();
                    while (it.hasNext()) {
                        Entry<Channel, ClientChannelInfo> item = it.next();
                        // final Integer id = item.getKey();
                        final ClientChannelInfo info = item.getValue();

                        long diff = System.currentTimeMillis() - info.getLastUpdateTimestamp();
                        if (diff > CHANNEL_EXPIRED_TIMEOUT) {
                            it.remove();
                            log.warn(
                                "SCAN: remove expired channel[{}] from ProducerManager groupChannelTable, producer group name: {}",
                                RemotingHelper.parseChannelRemoteAddr(info.getChannel()), group);
                            RemotingUtil.closeChannel(info.getChannel());
                        }
                    }
                }
            } finally {
                this.groupChannelLock.unlock();
            }
        } else {
            log.warn("ProducerManager scanNotActiveChannel lock timeout");
        }
    } catch (InterruptedException e) {
        log.error("", e);
    }
}
 
Example 8
Source File: ConsumerManager.java    From rocketmq_trans_message with Apache License 2.0 5 votes vote down vote up
public void scanNotActiveChannel() {
    Iterator<Entry<String, ConsumerGroupInfo>> it = this.consumerTable.entrySet().iterator();
    while (it.hasNext()) {
        Entry<String, ConsumerGroupInfo> next = it.next();
        String group = next.getKey();
        ConsumerGroupInfo consumerGroupInfo = next.getValue();
        ConcurrentHashMap<Channel, ClientChannelInfo> channelInfoTable =
            consumerGroupInfo.getChannelInfoTable();

        Iterator<Entry<Channel, ClientChannelInfo>> itChannel = channelInfoTable.entrySet().iterator();
        while (itChannel.hasNext()) {
            Entry<Channel, ClientChannelInfo> nextChannel = itChannel.next();
            ClientChannelInfo clientChannelInfo = nextChannel.getValue();
            long diff = System.currentTimeMillis() - clientChannelInfo.getLastUpdateTimestamp();
            if (diff > CHANNEL_EXPIRED_TIMEOUT) {
                log.warn(
                    "SCAN: remove expired channel from ConsumerManager consumerTable. channel={}, consumerGroup={}",
                    RemotingHelper.parseChannelRemoteAddr(clientChannelInfo.getChannel()), group);
                RemotingUtil.closeChannel(clientChannelInfo.getChannel());
                itChannel.remove();
            }
        }

        if (channelInfoTable.isEmpty()) {
            log.warn(
                "SCAN: remove expired channel from ConsumerManager consumerTable, all clear, consumerGroup={}",
                group);
            it.remove();
        }
    }
}
 
Example 9
Source File: ProducerManager.java    From rocketmq-read with Apache License 2.0 5 votes vote down vote up
/**
 * 扫描不活跃的channel
 */
public void scanNotActiveChannel() {
    try {
        if (this.groupChannelLock.tryLock(LOCK_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS)) {
            try {
                for (final Map.Entry<String, HashMap<Channel, ClientChannelInfo>> entry : this.groupChannelTable
                    .entrySet()) {
                    final String group = entry.getKey();
                    final HashMap<Channel, ClientChannelInfo> chlMap = entry.getValue();

                    Iterator<Entry<Channel, ClientChannelInfo>> it = chlMap.entrySet().iterator();
                    while (it.hasNext()) {
                        Entry<Channel, ClientChannelInfo> item = it.next();
                        // final Integer id = item.getKey();
                        final ClientChannelInfo info = item.getValue();

                        long diff = System.currentTimeMillis() - info.getLastUpdateTimestamp();
                        if (diff > CHANNEL_EXPIRED_TIMEOUT) {
                            it.remove();
                            log.warn(
                                "SCAN: remove expired channel[{}] from ProducerManager groupChannelTable, producer group name: {}",
                                RemotingHelper.parseChannelRemoteAddr(info.getChannel()), group);
                            RemotingUtil.closeChannel(info.getChannel());
                        }
                    }
                }
            } finally {
                this.groupChannelLock.unlock();
            }
        } else {
            log.warn("ProducerManager scanNotActiveChannel lock timeout");
        }
    } catch (InterruptedException e) {
        log.error("", e);
    }
}
 
Example 10
Source File: NettyRemotingServer.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
    final String remoteAddress = RemotingHelper.parseChannelRemoteAddr(ctx.channel());
    log.warn("NETTY SERVER PIPELINE: exceptionCaught {}", remoteAddress);
    log.warn("NETTY SERVER PIPELINE: exceptionCaught exception.", cause);

    if (NettyRemotingServer.this.channelEventListener != null) {
        NettyRemotingServer.this.putNettyEvent(new NettyEvent(NettyEventType.EXCEPTION, remoteAddress, ctx.channel()));
    }

    RemotingUtil.closeChannel(ctx.channel());
}
 
Example 11
Source File: RouteInfoManager.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
public void destroyByBrokerAddr(String addr) {
    BrokerLiveInfo brokerLiveInfo = brokerLiveTable.get(addr);
    if (brokerLiveInfo == null || brokerLiveInfo.getChannel() == null) {
        log.warn("do not get channel by broker addr:{}", addr);
        return;
    }
    RemotingUtil.closeChannel(brokerLiveInfo.getChannel());
    this.onChannelDestroy(addr, brokerLiveInfo.getChannel());
}
 
Example 12
Source File: RouteInfoManager.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
public void scanNotActiveBroker() {
    Iterator<Entry<String, BrokerLiveInfo>> it = this.brokerLiveTable.entrySet().iterator();
    while (it.hasNext()) {
        Entry<String, BrokerLiveInfo> next = it.next();
        long last = next.getValue().getLastUpdateTimestamp();
        if ((last + BROKER_CHANNEL_EXPIRED_TIME) < System.currentTimeMillis()) {
            RemotingUtil.closeChannel(next.getValue().getChannel());
            it.remove();
            log.warn("The broker channel expired, {} {}ms", next.getKey(), BROKER_CHANNEL_EXPIRED_TIME);
            this.onChannelDestroy(next.getKey(), next.getValue().getChannel());
        }
    }
}
 
Example 13
Source File: FilterServerManager.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
public void scanNotActiveChannel() {

        Iterator<Entry<Channel, FilterServerInfo>> it = this.filterServerTable.entrySet().iterator();
        while (it.hasNext()) {
            Entry<Channel, FilterServerInfo> next = it.next();
            long timestamp = next.getValue().getLastUpdateTimestamp();
            Channel channel = next.getKey();
            if ((System.currentTimeMillis() - timestamp) > FILTER_SERVER_MAX_IDLE_TIME_MILLS) {
                log.info("The Filter Server<{}> expired, remove it", next.getKey());
                it.remove();
                RemotingUtil.closeChannel(channel);
            }
        }
    }
 
Example 14
Source File: FilterServerManager.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
/**

     */
    public void scanNotActiveChannel() {

        Iterator<Entry<Channel, FilterServerInfo>> it = this.filterServerTable.entrySet().iterator();
        while (it.hasNext()) {
            Entry<Channel, FilterServerInfo> next = it.next();
            long timestamp = next.getValue().getLastUpdateTimestamp();
            Channel channel = next.getKey();
            if ((System.currentTimeMillis() - timestamp) > FILTER_SERVER_MAX_IDLE_TIME_MILLS) {
                log.info("The Filter Server<{}> expired, remove it", next.getKey());
                it.remove();
                RemotingUtil.closeChannel(channel);
            }
        }
    }
 
Example 15
Source File: ProducerManager.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
public void scanNotActiveChannel() {
    try {
        if (this.groupChannelLock.tryLock(LOCK_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS)) {
            try {
                for (final Map.Entry<String, HashMap<Channel, ClientChannelInfo>> entry : this.groupChannelTable
                    .entrySet()) {
                    final String group = entry.getKey();
                    final HashMap<Channel, ClientChannelInfo> chlMap = entry.getValue();

                    Iterator<Entry<Channel, ClientChannelInfo>> it = chlMap.entrySet().iterator();
                    while (it.hasNext()) {
                        Entry<Channel, ClientChannelInfo> item = it.next();
                        // final Integer id = item.getKey();
                        final ClientChannelInfo info = item.getValue();

                        long diff = System.currentTimeMillis() - info.getLastUpdateTimestamp();
                        if (diff > CHANNEL_EXPIRED_TIMEOUT) {
                            it.remove();
                            log.warn(
                                "SCAN: remove expired channel[{}] from ProducerManager groupChannelTable, producer group name: {}",
                                RemotingHelper.parseChannelRemoteAddr(info.getChannel()), group);
                            RemotingUtil.closeChannel(info.getChannel());
                        }
                    }
                }
            } finally {
                this.groupChannelLock.unlock();
            }
        } else {
            log.warn("ProducerManager scanNotActiveChannel lock timeout");
        }
    } catch (InterruptedException e) {
        log.error("", e);
    }
}
 
Example 16
Source File: FilterServerManager.java    From rocketmq-all-4.1.0-incubating with Apache License 2.0 5 votes vote down vote up
/**

     */
    public void scanNotActiveChannel() {

        Iterator<Entry<Channel, FilterServerInfo>> it = this.filterServerTable.entrySet().iterator();
        while (it.hasNext()) {
            Entry<Channel, FilterServerInfo> next = it.next();
            long timestamp = next.getValue().getLastUpdateTimestamp();
            Channel channel = next.getKey();
            if ((System.currentTimeMillis() - timestamp) > FILTER_SERVER_MAX_IDLE_TIME_MILLS) {
                log.info("The Filter Server<{}> expired, remove it", next.getKey());
                it.remove();
                RemotingUtil.closeChannel(channel);
            }
        }
    }
 
Example 17
Source File: RouteInfoManager.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
public void scanNotActiveBroker() {
    Iterator<Entry<String, BrokerLiveInfo>> it = this.brokerLiveTable.entrySet().iterator();
    while (it.hasNext()) {
        Entry<String, BrokerLiveInfo> next = it.next();
        long last = next.getValue().getLastUpdateTimestamp();
        if ((last + BROKER_CHANNEL_EXPIRED_TIME) < System.currentTimeMillis()) {
            RemotingUtil.closeChannel(next.getValue().getChannel());
            it.remove();
            log.warn("The broker channel expired, {} {}ms", next.getKey(), BROKER_CHANNEL_EXPIRED_TIME);
            this.onChannelDestroy(next.getKey(), next.getValue().getChannel());
        }
    }
}
 
Example 18
Source File: ProducerManager.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
public void scanNotActiveChannel() {
    try {
        if (this.groupChannelLock.tryLock(LOCK_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS)) {
            try {
                for (final Map.Entry<String, HashMap<Channel, ClientChannelInfo>> entry : this.groupChannelTable
                    .entrySet()) {
                    final String group = entry.getKey();
                    final HashMap<Channel, ClientChannelInfo> chlMap = entry.getValue();

                    Iterator<Entry<Channel, ClientChannelInfo>> it = chlMap.entrySet().iterator();
                    while (it.hasNext()) {
                        Entry<Channel, ClientChannelInfo> item = it.next();
                        // final Integer id = item.getKey();
                        final ClientChannelInfo info = item.getValue();

                        long diff = System.currentTimeMillis() - info.getLastUpdateTimestamp();
                        if (diff > CHANNEL_EXPIRED_TIMEOUT) {
                            it.remove();
                            log.warn(
                                "SCAN: remove expired channel[{}] from ProducerManager groupChannelTable, producer group name: {}",
                                RemotingHelper.parseChannelRemoteAddr(info.getChannel()), group);
                            RemotingUtil.closeChannel(info.getChannel());
                        }
                    }
                }
            } finally {
                this.groupChannelLock.unlock();
            }
        } else {
            log.warn("ProducerManager scanNotActiveChannel lock timeout");
        }
    } catch (InterruptedException e) {
        log.error("", e);
    }
}
 
Example 19
Source File: FilterServerManager.java    From rocketmq_trans_message with Apache License 2.0 5 votes vote down vote up
/**

     */
    public void scanNotActiveChannel() {

        Iterator<Entry<Channel, FilterServerInfo>> it = this.filterServerTable.entrySet().iterator();
        while (it.hasNext()) {
            Entry<Channel, FilterServerInfo> next = it.next();
            long timestamp = next.getValue().getLastUpdateTimestamp();
            Channel channel = next.getKey();
            if ((System.currentTimeMillis() - timestamp) > FILTER_SERVER_MAX_IDLE_TIME_MILLS) {
                log.info("The Filter Server<{}> expired, remove it", next.getKey());
                it.remove();
                RemotingUtil.closeChannel(channel);
            }
        }
    }
 
Example 20
Source File: RouteInfoManager.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
/**
 * 每10S扫描一次Borker,当超过2M未更新心跳时间,则关闭连接
 */
public void scanNotActiveBroker() {
    Iterator<Entry<String, BrokerLiveInfo>> it = this.brokerLiveTable.entrySet().iterator();
    while (it.hasNext()) {
        Entry<String, BrokerLiveInfo> next = it.next();
        long last = next.getValue().getLastUpdateTimestamp();
        if ((last + BROKER_CHANNEL_EXPIRED_TIME) < System.currentTimeMillis()) {
            RemotingUtil.closeChannel(next.getValue().getChannel());
            it.remove();
            log.warn("The broker channel expired, {} {}ms", next.getKey(), BROKER_CHANNEL_EXPIRED_TIME);
            this.onChannelDestroy(next.getKey(), next.getValue().getChannel());
        }
    }
}