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.AbstractSendMessageProcessor.java

License:Apache License

protected MessageExtBrokerInner buildInnerMsg(final ChannelHandlerContext ctx,
        final SendMessageRequestHeader requestHeader, final byte[] body, TopicConfig topicConfig) {
    int queueIdInt = requestHeader.getQueueId();
    // ??/* www. ja  v a 2  s  . com*/
    if (queueIdInt < 0) {
        queueIdInt = Math.abs(this.random.nextInt() % 99999999) % topicConfig.getWriteQueueNums();
    }
    int sysFlag = requestHeader.getSysFlag();

    // ??
    if (TopicFilterType.MULTI_TAG == topicConfig.getTopicFilterType()) {
        sysFlag |= MessageSysFlag.MultiTagsFlag;
    }

    MessageExtBrokerInner msgInner = new MessageExtBrokerInner();
    msgInner.setTopic(requestHeader.getTopic());
    msgInner.setBody(body);
    msgInner.setFlag(requestHeader.getFlag());
    MessageAccessor.setProperties(msgInner,
            MessageDecoder.string2messageProperties(requestHeader.getProperties()));
    msgInner.setPropertiesString(requestHeader.getProperties());
    msgInner.setTagsCode(
            MessageExtBrokerInner.tagsString2tagsCode(topicConfig.getTopicFilterType(), msgInner.getTags()));

    msgInner.setQueueId(queueIdInt);
    msgInner.setSysFlag(sysFlag);
    msgInner.setBornTimestamp(requestHeader.getBornTimestamp());
    msgInner.setBornHost(ctx.channel().remoteAddress());
    msgInner.setStoreHost(this.getStoreHost());
    msgInner.setReconsumeTimes(
            requestHeader.getReconsumeTimes() == null ? 0 : requestHeader.getReconsumeTimes());
    return msgInner;
}

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

License:Apache License

protected RemotingCommand msgContentCheck(final ChannelHandlerContext ctx,
        final SendMessageRequestHeader requestHeader, RemotingCommand request, final RemotingCommand response) {
    // message topic
    if (requestHeader.getTopic().length() > Byte.MAX_VALUE) {
        log.warn("putMessage message topic length too long " + requestHeader.getTopic().length());
        response.setCode(ResponseCode.MESSAGE_ILLEGAL);
        return response;
    }//  ww w .  j a v  a 2s  .  c om
    // message properties
    if (requestHeader.getProperties() != null && requestHeader.getProperties().length() > Short.MAX_VALUE) {
        log.warn("putMessage message properties length too long " + requestHeader.getProperties().length());
        response.setCode(ResponseCode.MESSAGE_ILLEGAL);
        return response;
    }
    if (request.getBody().length > DBMsgConstants.maxBodySize) {
        log.warn(" topic {}  msg body size {}  from {}", requestHeader.getTopic(), request.getBody().length,
                ChannelUtil.getRemoteIp(ctx.channel()));
        response.setRemark("msg body must be less 64KB");
        response.setCode(ResponseCode.MESSAGE_ILLEGAL);
        return response;
    }
    return response;
}

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

License:Apache License

protected RemotingCommand msgCheck(final ChannelHandlerContext ctx,
        final SendMessageRequestHeader requestHeader, final RemotingCommand response) {
    // ?roker?,  nameserver ????
    if (!PermName.isWriteable(this.brokerController.getBrokerConfig().getBrokerPermission())
            && this.brokerController.getTopicConfigManager().isOrderTopic(requestHeader.getTopic())) {
        response.setCode(ResponseCode.NO_PERMISSION);
        response.setRemark("the broker[" + this.brokerController.getBrokerConfig().getBrokerIP1()
                + "] sending message is forbidden");
        return response;
    }//from  www.j  ava2  s.com
    // Topic????????
    if (!this.brokerController.getTopicConfigManager().isTopicCanSendMessage(requestHeader.getTopic())) {
        String errorMsg = "the topic[" + requestHeader.getTopic() + "] is conflict with system reserved words.";
        log.warn(errorMsg);
        response.setCode(ResponseCode.SYSTEM_ERROR);
        response.setRemark(errorMsg);
        return response;
    }

    // ?opic??
    TopicConfig topicConfig = this.brokerController.getTopicConfigManager()
            .selectTopicConfig(requestHeader.getTopic());
    if (null == topicConfig) {
        // ???? topic ??
        int topicSysFlag = 0;
        if (requestHeader.isUnitMode()) {
            if (requestHeader.getTopic().startsWith(MixAll.RETRY_GROUP_TOPIC_PREFIX)) {
                topicSysFlag = TopicSysFlag.buildSysFlag(false, true);
            } else {
                topicSysFlag = TopicSysFlag.buildSysFlag(true, false);
            }
        }

        log.warn("the topic " + requestHeader.getTopic() + " not exist, producer: "
                + ctx.channel().remoteAddress());
        topicConfig = this.brokerController.getTopicConfigManager().createTopicInSendMessageMethod(//
                requestHeader.getTopic(), //
                requestHeader.getDefaultTopic(), //
                RemotingHelper.parseChannelRemoteAddr(ctx.channel()), //
                requestHeader.getDefaultTopicQueueNums(), topicSysFlag);

        // ?????????
        if (null == topicConfig) {
            if (requestHeader.getTopic().startsWith(MixAll.RETRY_GROUP_TOPIC_PREFIX)) {
                topicConfig = this.brokerController.getTopicConfigManager().createTopicInSendMessageBackMethod(
                        requestHeader.getTopic(), 1, PermName.PERM_WRITE | PermName.PERM_READ, topicSysFlag);
            }
        }

        if (null == topicConfig) {
            response.setCode(ResponseCode.TOPIC_NOT_EXIST);
            response.setRemark("topic[" + requestHeader.getTopic() + "] not exist, apply first please!"
                    + FAQUrl.suggestTodo(FAQUrl.APPLY_TOPIC_URL));
            return response;
        }
    }

    /**
     * Broker?Topic????ame Server?Client
     */
    // // ?opic?
    // if (!PermName.isWriteable(topicConfig.getPerm())) {
    // response.setCode(ResponseCode.NO_PERMISSION);
    // response.setRemark("the topic[" + requestHeader.getOriginTopic() +
    // "] sending message is forbidden");
    // return response;
    // }

    // ??????
    int queueIdInt = requestHeader.getQueueId();
    int idValid = Math.max(topicConfig.getWriteQueueNums(), topicConfig.getReadQueueNums());
    if (queueIdInt >= idValid) {
        String errorInfo = String.format("request queueId[%d] is illagal, %s Producer: %s", //
                queueIdInt, //
                topicConfig.toString(), //
                RemotingHelper.parseChannelRemoteAddr(ctx.channel()));

        log.warn(errorInfo);
        response.setCode(ResponseCode.SYSTEM_ERROR);
        response.setRemark(errorInfo);

        return response;
    }
    return response;
}

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

License:Apache License

public void executeSendMessageHookBefore(final ChannelHandlerContext ctx, final RemotingCommand request,
        SendMessageContext context) {//w ww  .  ja va  2 s.c om
    if (hasSendMessageHook()) {
        for (SendMessageHook hook : this.sendMessageHookList) {
            try {
                final SendMessageRequestHeader requestHeader = (SendMessageRequestHeader) request
                        .decodeCommandCustomHeader(SendMessageRequestHeader.class);
                context.setProducerGroup(requestHeader.getProducerGroup());
                context.setTopic(requestHeader.getTopic());
                context.setBodyLength(request.getBody().length);
                context.setMsgProps(requestHeader.getProperties());
                context.setBornHost(RemotingHelper.parseChannelRemoteAddr(ctx.channel()));
                context.setBrokerAddr(this.brokerController.getBrokerAddr());
                context.setQueueId(requestHeader.getQueueId());
                hook.sendMessageBefore(context);
                requestHeader.setProperties(context.getMsgProps());
            } catch (Throwable e) {
            }
        }
    }
}

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

License:Apache License

private RemotingCommand registerFilterServer(ChannelHandlerContext ctx, RemotingCommand request)
        throws RemotingCommandException {
    final RemotingCommand response = RemotingCommand
            .createResponseCommand(RegisterFilterServerResponseHeader.class);
    final RegisterFilterServerResponseHeader responseHeader = (RegisterFilterServerResponseHeader) response
            .readCustomHeader();//from ww  w .  j  a v  a  2  s  .  c o  m
    final RegisterFilterServerRequestHeader requestHeader = (RegisterFilterServerRequestHeader) request
            .decodeCommandCustomHeader(RegisterFilterServerRequestHeader.class);

    this.brokerController.getFilterServerManager().registerFilterServer(ctx.channel(),
            requestHeader.getFilterServerAddr());

    responseHeader.setBrokerId(this.brokerController.getBrokerConfig().getBrokerId());
    responseHeader.setBrokerName(this.brokerController.getBrokerConfig().getBrokerName());

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

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

License:Apache License

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

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

    SubscriptionGroupConfig config = RemotingSerializable.decode(request.getBody(),
            SubscriptionGroupConfig.class);
    if (config != null) {
        this.brokerController.getSubscriptionGroupManager().updateSubscriptionGroupConfig(config);
    }/*www.  j  a v  a2  s  .co m*/

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

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

License:Apache License

private RemotingCommand getAllSubscriptionGroup(ChannelHandlerContext ctx, RemotingCommand request)
        throws RemotingCommandException {
    final RemotingCommand response = RemotingCommand.createResponseCommand(null);
    String content = this.brokerController.getSubscriptionGroupManager().encode();
    if (content != null && content.length() > 0) {
        try {/*from   ww w  .ja  v a  2  s . co m*/
            response.setBody(content.getBytes(MixAll.DEFAULT_CHARSET));
        } catch (UnsupportedEncodingException e) {
            log.error("", e);

            response.setCode(ResponseCode.SYSTEM_ERROR);
            response.setRemark("UnsupportedEncodingException " + e);
            return response;
        }
    } else {
        log.error("No subscription group in this broker, client: " + ctx.channel().remoteAddress());
        response.setCode(ResponseCode.SYSTEM_ERROR);
        response.setRemark("No subscription group 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 updateAndCreateTopic(ChannelHandlerContext ctx, RemotingCommand request)
        throws RemotingCommandException {
    final RemotingCommand response = RemotingCommand.createResponseCommand(null);
    final CreateTopicRequestHeader requestHeader = (CreateTopicRequestHeader) request
            .decodeCommandCustomHeader(CreateTopicRequestHeader.class);
    log.info("updateAndCreateTopic called by {}", RemotingHelper.parseChannelRemoteAddr(ctx.channel()));

    // Topic?????
    if (requestHeader.getTopic().equals(this.brokerController.getBrokerConfig().getBrokerClusterName())) {
        String errorMsg = "the topic[" + requestHeader.getTopic() + "] is conflict with system reserved words.";
        log.warn(errorMsg);/*w ww .  ja  v  a  2  s  . c om*/
        response.setCode(ResponseCode.SYSTEM_ERROR);
        response.setRemark(errorMsg);
        return response;
    }

    TopicConfig topicConfig = new TopicConfig(requestHeader.getTopic());
    topicConfig.setReadQueueNums(requestHeader.getReadQueueNums());
    topicConfig.setWriteQueueNums(requestHeader.getWriteQueueNums());
    topicConfig.setTopicFilterType(requestHeader.getTopicFilterTypeEnum());
    topicConfig.setPerm(requestHeader.getPerm());
    topicConfig.setTopicSysFlag(requestHeader.getTopicSysFlag() == null ? 0 : requestHeader.getTopicSysFlag());

    this.brokerController.getTopicConfigManager().updateTopicConfig(topicConfig);

    this.brokerController.registerBrokerAll(false, true);

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

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

License:Apache License

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

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

    this.brokerController.getTopicConfigManager().deleteTopicConfig(requestHeader.getTopic());
    this.brokerController.addDeleteTopicTask();

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

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

License:Apache License

private RemotingCommand getAllTopicConfig(ChannelHandlerContext ctx, RemotingCommand request) {
    final RemotingCommand response = RemotingCommand
            .createResponseCommand(GetAllTopicConfigResponseHeader.class);
    // final GetAllTopicConfigResponseHeader responseHeader =
    // (GetAllTopicConfigResponseHeader) response.readCustomHeader();

    String content = this.brokerController.getTopicConfigManager().encode();
    if (content != null && content.length() > 0) {
        try {//from   w w w  .  ja  va2 s  .  c  o  m
            response.setBody(content.getBytes(MixAll.DEFAULT_CHARSET));
        } catch (UnsupportedEncodingException e) {
            log.error("", e);

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

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

    return response;
}