Example usage for io.netty.channel ChannelHandlerContext channel

List of usage examples for io.netty.channel ChannelHandlerContext channel

Introduction

In this page you can find the example usage for io.netty.channel ChannelHandlerContext channel.

Prototype

Channel channel();

Source Link

Document

Return the Channel which is bound to the ChannelHandlerContext .

Usage

From source file:com.alibaba.rocketmq.broker.processor.AdminBrokerProcessor.java

License:Apache License

private RemotingCommand updateBrokerConfig(ChannelHandlerContext ctx, RemotingCommand request) {
    final RemotingCommand response = RemotingCommand.createResponseCommand(null);

    log.info("updateBrokerConfig called by {}", RemotingHelper.parseChannelRemoteAddr(ctx.channel()));

    byte[] body = request.getBody();
    if (body != null) {
        try {// w w w  . j av a  2s .  c o  m
            String bodyStr = new String(body, MixAll.DEFAULT_CHARSET);
            Properties properties = MixAll.string2Properties(bodyStr);
            if (properties != null) {
                log.info("updateBrokerConfig, new config: " + properties + " client: "
                        + ctx.channel().remoteAddress());
                this.brokerController.updateAllConfig(properties);
            } else {
                log.error("string2Properties error");
                response.setCode(ResponseCode.SYSTEM_ERROR);
                response.setRemark("string2Properties error");
                return response;
            }
        } catch (UnsupportedEncodingException e) {
            log.error("", e);
            response.setCode(ResponseCode.SYSTEM_ERROR);
            response.setRemark("UnsupportedEncodingException " + e);
            return response;
        }
    }

    response.setCode(ResponseCode.SUCCESS);
    response.setRemark(null);
    return response;
}

From source file:com.alibaba.rocketmq.broker.processor.AdminBrokerProcessor.java

License:Apache License

private RemotingCommand getAllConsumerOffset(ChannelHandlerContext ctx, RemotingCommand request) {
    final RemotingCommand response = RemotingCommand.createResponseCommand(null);

    String content = this.brokerController.getConsumerOffsetManager().encode();
    if (content != null && content.length() > 0) {
        try {//from   w w  w .  j a v a 2 s  . com
            response.setBody(content.getBytes(MixAll.DEFAULT_CHARSET));
        } catch (UnsupportedEncodingException e) {
            log.error("get all consumer offset from master error.", e);

            response.setCode(ResponseCode.SYSTEM_ERROR);
            response.setRemark("UnsupportedEncodingException " + e);
            return response;
        }
    } else {
        log.error("No consumer offset in this broker, client: " + ctx.channel().remoteAddress());
        response.setCode(ResponseCode.SYSTEM_ERROR);
        response.setRemark("No consumer offset in this broker");
        return response;
    }

    response.setCode(ResponseCode.SUCCESS);
    response.setRemark(null);

    return response;
}

From source file:com.alibaba.rocketmq.broker.processor.AdminBrokerProcessor.java

License:Apache License

private RemotingCommand getAllDelayOffset(ChannelHandlerContext ctx, RemotingCommand request) {
    final RemotingCommand response = RemotingCommand.createResponseCommand(null);

    String content = ((DefaultMessageStore) this.brokerController.getMessageStore()).getScheduleMessageService()
            .encode();//from  w w w.j a  va 2  s . c  om
    if (content != null && content.length() > 0) {
        try {
            response.setBody(content.getBytes(MixAll.DEFAULT_CHARSET));
        } catch (UnsupportedEncodingException e) {
            log.error("get all delay offset from master error.", e);

            response.setCode(ResponseCode.SYSTEM_ERROR);
            response.setRemark("UnsupportedEncodingException " + e);
            return response;
        }
    } else {
        log.error("No delay offset in this broker, client: " + ctx.channel().remoteAddress());
        response.setCode(ResponseCode.SYSTEM_ERROR);
        response.setRemark("No delay offset in this broker");
        return response;
    }

    response.setCode(ResponseCode.SUCCESS);
    response.setRemark(null);

    return response;
}

From source file:com.alibaba.rocketmq.broker.processor.AdminBrokerProcessor.java

License:Apache License

private RemotingCommand deleteSubscriptionGroup(ChannelHandlerContext ctx, RemotingCommand request)
        throws RemotingCommandException {
    final RemotingCommand response = RemotingCommand.createResponseCommand(null);
    DeleteSubscriptionGroupRequestHeader requestHeader = (DeleteSubscriptionGroupRequestHeader) request
            .decodeCommandCustomHeader(DeleteSubscriptionGroupRequestHeader.class);

    log.info("deleteSubscriptionGroup called by {}", RemotingHelper.parseChannelRemoteAddr(ctx.channel()));

    this.brokerController.getSubscriptionGroupManager()
            .deleteSubscriptionGroupConfig(requestHeader.getGroupName());

    response.setCode(ResponseCode.SUCCESS);
    response.setRemark(null);//from ww  w .j av  a 2 s.  c  o m
    return response;
}

From source file:com.alibaba.rocketmq.broker.processor.AdminBrokerProcessor.java

License:Apache License

public RemotingCommand resetOffset(ChannelHandlerContext ctx, RemotingCommand request)
        throws RemotingCommandException {
    final ResetOffsetRequestHeader requestHeader = (ResetOffsetRequestHeader) request
            .decodeCommandCustomHeader(ResetOffsetRequestHeader.class);
    log.info("[reset-offset] reset offset started by {}. topic={}, group={}, timestamp={}, isForce={}",
            new Object[] { RemotingHelper.parseChannelRemoteAddr(ctx.channel()), requestHeader.getTopic(),
                    requestHeader.getGroup(), requestHeader.getTimestamp(), requestHeader.isForce() });
    return this.brokerController.getBroker2Client().resetOffset(requestHeader.getTopic(),
            requestHeader.getGroup(), requestHeader.getTimestamp(), requestHeader.isForce());
}

From source file:com.alibaba.rocketmq.broker.processor.AdminBrokerProcessor.java

License:Apache License

public RemotingCommand getConsumerStatus(ChannelHandlerContext ctx, RemotingCommand request)
        throws RemotingCommandException {
    final GetConsumerStatusRequestHeader requestHeader = (GetConsumerStatusRequestHeader) request
            .decodeCommandCustomHeader(GetConsumerStatusRequestHeader.class);

    log.info("[get-consumer-status] get consumer status by {}. topic={}, group={}",
            new Object[] { RemotingHelper.parseChannelRemoteAddr(ctx.channel()), requestHeader.getTopic(),
                    requestHeader.getGroup() });

    return this.brokerController.getBroker2Client().getConsumeStatus(requestHeader.getTopic(),
            requestHeader.getGroup(), requestHeader.getClientAddr());
}

From source file:com.alibaba.rocketmq.broker.processor.ClientManageProcessor.java

License:Apache License

private RemotingCommand updateConsumerOffset(ChannelHandlerContext ctx, RemotingCommand request)
        throws RemotingCommandException {
    final RemotingCommand response = RemotingCommand
            .createResponseCommand(UpdateConsumerOffsetResponseHeader.class);
    // final UpdateConsumerOffsetResponseHeader responseHeader =
    // (UpdateConsumerOffsetResponseHeader) response.readCustomHeader();
    final UpdateConsumerOffsetRequestHeader requestHeader = (UpdateConsumerOffsetRequestHeader) request
            .decodeCommandCustomHeader(UpdateConsumerOffsetRequestHeader.class);

    // ????? offset ?
    if (this.hasConsumeMessageHook()) {
        // hook//  w  w  w.ja  v  a 2  s  .c  o  m
        ConsumeMessageContext context = new ConsumeMessageContext();
        context.setConsumerGroup(requestHeader.getConsumerGroup());
        context.setTopic(requestHeader.getTopic());
        context.setClientHost(RemotingHelper.parseChannelRemoteAddr(ctx.channel()));
        context.setSuccess(true);
        context.setStatus(ConsumeConcurrentlyStatus.CONSUME_SUCCESS.toString());
        final SocketAddress storeHost = new InetSocketAddress(brokerController.getBrokerConfig().getBrokerIP1(),
                brokerController.getNettyServerConfig().getListenPort());

        long preOffset = this.brokerController.getConsumerOffsetManager().queryOffset(
                requestHeader.getConsumerGroup(), requestHeader.getTopic(), requestHeader.getQueueId());
        Map<String, Long> messageIds = this.brokerController.getMessageStore().getMessageIds(
                requestHeader.getTopic(), requestHeader.getQueueId(), preOffset,
                requestHeader.getCommitOffset(), storeHost);
        context.setMessageIds(messageIds);
        this.executeConsumeMessageHookAfter(context);
    }
    this.brokerController.getConsumerOffsetManager().commitOffset(requestHeader.getConsumerGroup(),
            requestHeader.getTopic(), requestHeader.getQueueId(), requestHeader.getCommitOffset());
    response.setCode(ResponseCode.SUCCESS);
    response.setRemark(null);
    return response;
}

From source file:com.alibaba.rocketmq.broker.processor.ClientManageProcessor.java

License:Apache License

public RemotingCommand getConsumerListByGroup(ChannelHandlerContext ctx, RemotingCommand request)
        throws RemotingCommandException {
    final RemotingCommand response = RemotingCommand
            .createResponseCommand(GetConsumerListByGroupResponseHeader.class);
    final GetConsumerListByGroupRequestHeader requestHeader = (GetConsumerListByGroupRequestHeader) request
            .decodeCommandCustomHeader(GetConsumerListByGroupRequestHeader.class);

    ConsumerGroupInfo consumerGroupInfo = this.brokerController.getConsumerManager()
            .getConsumerGroupInfo(requestHeader.getConsumerGroup());
    if (consumerGroupInfo != null) {
        List<String> clientIds = consumerGroupInfo.getAllClientId();
        if (!clientIds.isEmpty()) {
            GetConsumerListByGroupResponseBody body = new GetConsumerListByGroupResponseBody();
            body.setConsumerIdList(clientIds);
            response.setBody(body.encode());
            response.setCode(ResponseCode.SUCCESS);
            response.setRemark(null);/*  w  w  w.j  a v  a2 s.c o m*/
            return response;
        } else {
            log.warn("getAllClientId failed, {} {}", requestHeader.getConsumerGroup(),
                    RemotingHelper.parseChannelRemoteAddr(ctx.channel()));
        }
    } else {
        log.warn("getConsumerGroupInfo failed, {} {}", requestHeader.getConsumerGroup(),
                RemotingHelper.parseChannelRemoteAddr(ctx.channel()));
    }

    response.setCode(ResponseCode.SYSTEM_ERROR);
    response.setRemark("no consumer for this group, " + requestHeader.getConsumerGroup());
    return response;
}

From source file:com.alibaba.rocketmq.broker.processor.ClientManageProcessor.java

License:Apache License

public RemotingCommand unregisterClient(ChannelHandlerContext ctx, RemotingCommand request)
        throws RemotingCommandException {
    final RemotingCommand response = RemotingCommand
            .createResponseCommand(UnregisterClientResponseHeader.class);
    final UnregisterClientRequestHeader requestHeader = (UnregisterClientRequestHeader) request
            .decodeCommandCustomHeader(UnregisterClientRequestHeader.class);

    ClientChannelInfo clientChannelInfo = new ClientChannelInfo(//
            ctx.channel(), //
            requestHeader.getClientID(), //
            request.getLanguage(), //
            request.getVersion()//
    );//from   w  w w.ja  v a  2  s. c o m

    // Producer
    {
        final String group = requestHeader.getProducerGroup();
        if (group != null) {
            this.brokerController.getProducerManager().unregisterProducer(group, clientChannelInfo);
        }
    }

    // Consumer
    {
        final String group = requestHeader.getConsumerGroup();
        if (group != null) {
            this.brokerController.getConsumerManager().unregisterConsumer(group, clientChannelInfo);
        }
    }

    response.setCode(ResponseCode.SUCCESS);
    response.setRemark(null);
    return response;
}

From source file:com.alibaba.rocketmq.broker.processor.ClientManageProcessor.java

License:Apache License

public RemotingCommand heartBeat(ChannelHandlerContext ctx, RemotingCommand request) {
    RemotingCommand response = RemotingCommand.createResponseCommand(null);

    HeartbeatData heartbeatData = HeartbeatData.decode(request.getBody(), HeartbeatData.class);

    ClientChannelInfo clientChannelInfo = new ClientChannelInfo(//
            ctx.channel(), //
            heartbeatData.getClientID(), //
            request.getLanguage(), //
            request.getVersion()//
    );//from  ww w  .  ja  v a 2s . co  m

    // Consumer
    for (ConsumerData data : heartbeatData.getConsumerDataSet()) {
        SubscriptionGroupConfig subscriptionGroupConfig = this.brokerController.getSubscriptionGroupManager()
                .findSubscriptionGroupConfig(data.getGroupName());
        if (null != subscriptionGroupConfig) {
            // ?? topic 
            int topicSysFlag = 0;
            if (data.isUnitMode()) {
                topicSysFlag = TopicSysFlag.buildSysFlag(false, true);
            }
            String newTopic = MixAll.getRetryTopic(data.getGroupName());
            this.brokerController.getTopicConfigManager().createTopicInSendMessageBackMethod(//
                    newTopic, //
                    subscriptionGroupConfig.getRetryQueueNums(), //
                    PermName.PERM_WRITE | PermName.PERM_READ, topicSysFlag);
        }

        boolean changed = this.brokerController.getConsumerManager().registerConsumer(//
                data.getGroupName(), //
                clientChannelInfo, //
                data.getConsumeType(), //
                data.getMessageModel(), //
                data.getConsumeFromWhere(), //
                data.getSubscriptionDataSet()//
        );

        if (changed) {
            log.info("registerConsumer info changed {} {}", //
                    data.toString(), //
                    RemotingHelper.parseChannelRemoteAddr(ctx.channel())//
            );

            // todo:???
            // for (SubscriptionData subscriptionData :
            // data.getSubscriptionDataSet()) {
            // this.brokerController.getTopicConfigManager().updateTopicUnitSubFlag(
            // subscriptionData.getTopic(), data.isUnitMode());
            // }
        }
    }

    // Producer
    for (ProducerData data : heartbeatData.getProducerDataSet()) {
        this.brokerController.getProducerManager().registerProducer(data.getGroupName(), clientChannelInfo);
    }

    response.setCode(ResponseCode.SUCCESS);
    response.setRemark(null);
    return response;
}