Java Code Examples for org.apache.rocketmq.common.UtilAll#crc32()

The following examples show how to use org.apache.rocketmq.common.UtilAll#crc32() . 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: MQClientInstance.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
private void uploadFilterClassToAllFilterServer(final String consumerGroup, final String fullClassName,
    final String topic,
    final String filterClassSource) throws UnsupportedEncodingException {
    byte[] classBody = null;
    int classCRC = 0;
    try {
        classBody = filterClassSource.getBytes(MixAll.DEFAULT_CHARSET);
        classCRC = UtilAll.crc32(classBody);
    } catch (Exception e1) {
        log.warn("uploadFilterClassToAllFilterServer Exception, ClassName: {} {}",
            fullClassName,
            RemotingHelper.exceptionSimpleDesc(e1));
    }

    TopicRouteData topicRouteData = this.topicRouteTable.get(topic);
    if (topicRouteData != null
        && topicRouteData.getFilterServerTable() != null && !topicRouteData.getFilterServerTable().isEmpty()) {
        Iterator<Entry<String, List<String>>> it = topicRouteData.getFilterServerTable().entrySet().iterator();
        while (it.hasNext()) {
            Entry<String, List<String>> next = it.next();
            List<String> value = next.getValue();
            for (final String fsAddr : value) {
                try {
                    this.mQClientAPIImpl.registerMessageFilterClass(fsAddr, consumerGroup, topic, fullClassName, classCRC, classBody,
                        5000);

                    log.info("register message class filter to {} OK, ConsumerGroup: {} Topic: {} ClassName: {}", fsAddr, consumerGroup,
                        topic, fullClassName);

                } catch (Exception e) {
                    log.error("uploadFilterClassToAllFilterServer Exception", e);
                }
            }
        }
    } else {
        log.warn("register message class filter failed, because no filter server, ConsumerGroup: {} Topic: {} ClassName: {}",
            consumerGroup, topic, fullClassName);
    }
}
 
Example 2
Source File: DefaultRequestProcessor.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
private boolean checksum(ChannelHandlerContext ctx, RemotingCommand request,
    RegisterBrokerRequestHeader requestHeader) {
    if (requestHeader.getBodyCrc32() != 0) {
        final int crc32 = UtilAll.crc32(request.getBody());
        if (crc32 != requestHeader.getBodyCrc32()) {
            log.warn(String.format("receive registerBroker request,crc32 not match,from %s",
                RemotingHelper.parseChannelRemoteAddr(ctx.channel())));
            return false;
        }
    }
    return true;
}
 
Example 3
Source File: MQClientInstance.java    From rocketmq_trans_message with Apache License 2.0 5 votes vote down vote up
private void uploadFilterClassToAllFilterServer(final String consumerGroup, final String fullClassName, final String topic,
    final String filterClassSource) throws UnsupportedEncodingException {
    byte[] classBody = null;
    int classCRC = 0;
    try {
        classBody = filterClassSource.getBytes(MixAll.DEFAULT_CHARSET);
        classCRC = UtilAll.crc32(classBody);
    } catch (Exception e1) {
        log.warn("uploadFilterClassToAllFilterServer Exception, ClassName: {} {}", //
            fullClassName, //
            RemotingHelper.exceptionSimpleDesc(e1));
    }

    TopicRouteData topicRouteData = this.topicRouteTable.get(topic);
    if (topicRouteData != null //
        && topicRouteData.getFilterServerTable() != null && !topicRouteData.getFilterServerTable().isEmpty()) {
        Iterator<Entry<String, List<String>>> it = topicRouteData.getFilterServerTable().entrySet().iterator();
        while (it.hasNext()) {
            Entry<String, List<String>> next = it.next();
            List<String> value = next.getValue();
            for (final String fsAddr : value) {
                try {
                    this.mQClientAPIImpl.registerMessageFilterClass(fsAddr, consumerGroup, topic, fullClassName, classCRC, classBody,
                        5000);

                    log.info("register message class filter to {} OK, ConsumerGroup: {} Topic: {} ClassName: {}", fsAddr, consumerGroup,
                        topic, fullClassName);

                } catch (Exception e) {
                    log.error("uploadFilterClassToAllFilterServer Exception", e);
                }
            }
        }
    } else {
        log.warn("register message class filter failed, because no filter server, ConsumerGroup: {} Topic: {} ClassName: {}",
            consumerGroup, topic, fullClassName);
    }
}
 
Example 4
Source File: FilterClassManager.java    From rocketmq_trans_message with Apache License 2.0 5 votes vote down vote up
private void fetchClassFromRemoteHost() {
    Iterator<Entry<String, FilterClassInfo>> it = this.filterClassTable.entrySet().iterator();
    while (it.hasNext()) {
        try {
            Entry<String, FilterClassInfo> next = it.next();
            FilterClassInfo filterClassInfo = next.getValue();
            String[] topicAndGroup = next.getKey().split("@");
            String responseStr =
                this.filterClassFetchMethod.fetch(topicAndGroup[0], topicAndGroup[1],
                    filterClassInfo.getClassName());
            byte[] filterSourceBinary = responseStr.getBytes("UTF-8");
            int classCRC = UtilAll.crc32(responseStr.getBytes("UTF-8"));
            if (classCRC != filterClassInfo.getClassCRC()) {
                String javaSource = new String(filterSourceBinary, MixAll.DEFAULT_CHARSET);
                Class<?> newClass =
                    DynaCode.compileAndLoadClass(filterClassInfo.getClassName(), javaSource);
                Object newInstance = newClass.newInstance();
                filterClassInfo.setMessageFilter((MessageFilter) newInstance);
                filterClassInfo.setClassCRC(classCRC);

                log.info("fetch Remote class File OK, {} {}", next.getKey(),
                    filterClassInfo.getClassName());
            }
        } catch (Exception e) {
            log.error("fetchClassFromRemoteHost Exception", e);
        }
    }
}
 
Example 5
Source File: MQClientInstance.java    From rocketmq-all-4.1.0-incubating with Apache License 2.0 5 votes vote down vote up
private void uploadFilterClassToAllFilterServer(final String consumerGroup, final String fullClassName, final String topic,
    final String filterClassSource) throws UnsupportedEncodingException {
    byte[] classBody = null;
    int classCRC = 0;
    try {
        classBody = filterClassSource.getBytes(MixAll.DEFAULT_CHARSET);
        classCRC = UtilAll.crc32(classBody);
    } catch (Exception e1) {
        log.warn("uploadFilterClassToAllFilterServer Exception, ClassName: {} {}", //
            fullClassName, //
            RemotingHelper.exceptionSimpleDesc(e1));
    }

    TopicRouteData topicRouteData = this.topicRouteTable.get(topic);
    if (topicRouteData != null //
        && topicRouteData.getFilterServerTable() != null && !topicRouteData.getFilterServerTable().isEmpty()) {
        Iterator<Entry<String, List<String>>> it = topicRouteData.getFilterServerTable().entrySet().iterator();
        while (it.hasNext()) {
            Entry<String, List<String>> next = it.next();
            List<String> value = next.getValue();
            for (final String fsAddr : value) {
                try {
                    this.mQClientAPIImpl.registerMessageFilterClass(fsAddr, consumerGroup, topic, fullClassName, classCRC, classBody,
                        5000);

                    log.info("register message class filter to {} OK, ConsumerGroup: {} Topic: {} ClassName: {}", fsAddr, consumerGroup,
                        topic, fullClassName);

                } catch (Exception e) {
                    log.error("uploadFilterClassToAllFilterServer Exception", e);
                }
            }
        }
    } else {
        log.warn("register message class filter failed, because no filter server, ConsumerGroup: {} Topic: {} ClassName: {}",
            consumerGroup, topic, fullClassName);
    }
}
 
Example 6
Source File: FilterClassManager.java    From rocketmq-all-4.1.0-incubating with Apache License 2.0 5 votes vote down vote up
private void fetchClassFromRemoteHost() {
    Iterator<Entry<String, FilterClassInfo>> it = this.filterClassTable.entrySet().iterator();
    while (it.hasNext()) {
        try {
            Entry<String, FilterClassInfo> next = it.next();
            FilterClassInfo filterClassInfo = next.getValue();
            String[] topicAndGroup = next.getKey().split("@");
            String responseStr =
                this.filterClassFetchMethod.fetch(topicAndGroup[0], topicAndGroup[1],
                    filterClassInfo.getClassName());
            byte[] filterSourceBinary = responseStr.getBytes("UTF-8");
            int classCRC = UtilAll.crc32(responseStr.getBytes("UTF-8"));
            if (classCRC != filterClassInfo.getClassCRC()) {
                String javaSource = new String(filterSourceBinary, MixAll.DEFAULT_CHARSET);
                Class<?> newClass =
                    DynaCode.compileAndLoadClass(filterClassInfo.getClassName(), javaSource);
                Object newInstance = newClass.newInstance();
                filterClassInfo.setMessageFilter((MessageFilter) newInstance);
                filterClassInfo.setClassCRC(classCRC);

                log.info("fetch Remote class File OK, {} {}", next.getKey(),
                    filterClassInfo.getClassName());
            }
        } catch (Exception e) {
            log.error("fetchClassFromRemoteHost Exception", e);
        }
    }
}
 
Example 7
Source File: MQClientInstance.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
private void uploadFilterClassToAllFilterServer(final String consumerGroup, final String fullClassName,
    final String topic,
    final String filterClassSource) throws UnsupportedEncodingException {
    byte[] classBody = null;
    int classCRC = 0;
    try {
        classBody = filterClassSource.getBytes(MixAll.DEFAULT_CHARSET);
        classCRC = UtilAll.crc32(classBody);
    } catch (Exception e1) {
        log.warn("uploadFilterClassToAllFilterServer Exception, ClassName: {} {}",
            fullClassName,
            RemotingHelper.exceptionSimpleDesc(e1));
    }

    TopicRouteData topicRouteData = this.topicRouteTable.get(topic);
    if (topicRouteData != null
        && topicRouteData.getFilterServerTable() != null && !topicRouteData.getFilterServerTable().isEmpty()) {
        Iterator<Entry<String, List<String>>> it = topicRouteData.getFilterServerTable().entrySet().iterator();
        while (it.hasNext()) {
            Entry<String, List<String>> next = it.next();
            List<String> value = next.getValue();
            for (final String fsAddr : value) {
                try {
                    this.mQClientAPIImpl.registerMessageFilterClass(fsAddr, consumerGroup, topic, fullClassName, classCRC, classBody,
                        5000);

                    log.info("register message class filter to {} OK, ConsumerGroup: {} Topic: {} ClassName: {}", fsAddr, consumerGroup,
                        topic, fullClassName);

                } catch (Exception e) {
                    log.error("uploadFilterClassToAllFilterServer Exception", e);
                }
            }
        }
    } else {
        log.warn("register message class filter failed, because no filter server, ConsumerGroup: {} Topic: {} ClassName: {}",
            consumerGroup, topic, fullClassName);
    }
}
 
Example 8
Source File: FilterClassManager.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
private void fetchClassFromRemoteHost() {
    Iterator<Entry<String, FilterClassInfo>> it = this.filterClassTable.entrySet().iterator();
    while (it.hasNext()) {
        try {
            Entry<String, FilterClassInfo> next = it.next();
            FilterClassInfo filterClassInfo = next.getValue();
            String[] topicAndGroup = next.getKey().split("@");
            String responseStr =
                this.filterClassFetchMethod.fetch(topicAndGroup[0], topicAndGroup[1],
                    filterClassInfo.getClassName());
            byte[] filterSourceBinary = responseStr.getBytes("UTF-8");
            int classCRC = UtilAll.crc32(responseStr.getBytes("UTF-8"));
            if (classCRC != filterClassInfo.getClassCRC()) {
                String javaSource = new String(filterSourceBinary, MixAll.DEFAULT_CHARSET);
                Class<?> newClass =
                    DynaCode.compileAndLoadClass(filterClassInfo.getClassName(), javaSource);
                Object newInstance = newClass.newInstance();
                filterClassInfo.setMessageFilter((MessageFilter) newInstance);
                filterClassInfo.setClassCRC(classCRC);

                log.info("fetch Remote class File OK, {} {}", next.getKey(),
                    filterClassInfo.getClassName());
            }
        } catch (Exception e) {
            log.error("fetchClassFromRemoteHost Exception", e);
        }
    }
}
 
Example 9
Source File: MQClientInstance.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
/**
 * 上传过滤类到Filtersrv
 *
 * @param consumerGroup     消费分组
 * @param fullClassName     类名
 * @param topic             Topic
 * @param filterClassSource 过滤类源码文件地址
 * @throws UnsupportedEncodingException 当读取源码文件失败
 */
private void uploadFilterClassToAllFilterServer(final String consumerGroup, final String fullClassName, final String topic,
                                                final String filterClassSource) throws UnsupportedEncodingException {
    byte[] classBody = null;
    int classCRC = 0;
    try {
        classBody = filterClassSource.getBytes(MixAll.DEFAULT_CHARSET);
        classCRC = UtilAll.crc32(classBody);
    } catch (Exception e1) {
        log.warn("uploadFilterClassToAllFilterServer Exception, ClassName: {} {}", //
            fullClassName, //
            RemotingHelper.exceptionSimpleDesc(e1));
    }

    TopicRouteData topicRouteData = this.topicRouteTable.get(topic);
    if (topicRouteData != null //
        && topicRouteData.getFilterServerTable() != null && !topicRouteData.getFilterServerTable().isEmpty()) {
        Iterator<Entry<String, List<String>>> it = topicRouteData.getFilterServerTable().entrySet().iterator();
        while (it.hasNext()) {
            Entry<String, List<String>> next = it.next();
            List<String> value = next.getValue();
            for (final String fsAddr : value) {
                try {
                    this.mQClientAPIImpl.registerMessageFilterClass(fsAddr, consumerGroup, topic, fullClassName, classCRC, classBody,
                        5000);

                    log.info("register message class filter to {} OK, ConsumerGroup: {} Topic: {} ClassName: {}", fsAddr, consumerGroup,
                        topic, fullClassName);

                } catch (Exception e) {
                    log.error("uploadFilterClassToAllFilterServer Exception", e);
                }
            }
        }
    } else {
        log.warn("register message class filter failed, because no filter server, ConsumerGroup: {} Topic: {} ClassName: {}",
            consumerGroup, topic, fullClassName);
    }
}
 
Example 10
Source File: FilterClassManager.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
private void fetchClassFromRemoteHost() {
    Iterator<Entry<String, FilterClassInfo>> it = this.filterClassTable.entrySet().iterator();
    while (it.hasNext()) {
        try {
            Entry<String, FilterClassInfo> next = it.next();
            FilterClassInfo filterClassInfo = next.getValue();
            String[] topicAndGroup = next.getKey().split("@");
            String responseStr =
                this.filterClassFetchMethod.fetch(topicAndGroup[0], topicAndGroup[1],
                    filterClassInfo.getClassName());
            byte[] filterSourceBinary = responseStr.getBytes("UTF-8");
            int classCRC = UtilAll.crc32(responseStr.getBytes("UTF-8"));
            if (classCRC != filterClassInfo.getClassCRC()) {
                String javaSource = new String(filterSourceBinary, MixAll.DEFAULT_CHARSET);
                Class<?> newClass =
                    DynaCode.compileAndLoadClass(filterClassInfo.getClassName(), javaSource);
                Object newInstance = newClass.newInstance();
                filterClassInfo.setMessageFilter((MessageFilter) newInstance);
                filterClassInfo.setClassCRC(classCRC);

                log.info("fetch Remote class File OK, {} {}", next.getKey(),
                    filterClassInfo.getClassName());
            }
        } catch (Exception e) {
            log.error("fetchClassFromRemoteHost Exception", e);
        }
    }
}
 
Example 11
Source File: DefaultRequestProcessor.java    From rocketmq-read with Apache License 2.0 5 votes vote down vote up
/**
 * 进行crc32的校验,保证请求的完整性
 * @param ctx ctx
 * @param request  request
 * @param requestHeader request
 * @return ;
 */
private boolean checksum(ChannelHandlerContext ctx, RemotingCommand request,
    RegisterBrokerRequestHeader requestHeader) {
    if (requestHeader.getBodyCrc32() != 0) {
        final int crc32 = UtilAll.crc32(request.getBody());
        if (crc32 != requestHeader.getBodyCrc32()) {
            log.warn(String.format("receive registerBroker request,crc32 not match,from %s",
                RemotingHelper.parseChannelRemoteAddr(ctx.channel())));
            return false;
        }
    }
    return true;
}
 
Example 12
Source File: DefaultRequestProcessor.java    From rocketmq-4.3.0 with Apache License 2.0 5 votes vote down vote up
private boolean checksum(ChannelHandlerContext ctx, RemotingCommand request,
    RegisterBrokerRequestHeader requestHeader) {
    if (requestHeader.getBodyCrc32() != 0) {
        final int crc32 = UtilAll.crc32(request.getBody());
        if (crc32 != requestHeader.getBodyCrc32()) {
            log.warn(String.format("receive registerBroker request,crc32 not match,from %s",
                RemotingHelper.parseChannelRemoteAddr(ctx.channel())));
            return false;
        }
    }
    return true;
}
 
Example 13
Source File: FilterClassManager.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
private void fetchClassFromRemoteHost() {
    Iterator<Entry<String, FilterClassInfo>> it = this.filterClassTable.entrySet().iterator();
    while (it.hasNext()) {
        try {
            Entry<String, FilterClassInfo> next = it.next();
            FilterClassInfo filterClassInfo = next.getValue();
            String[] topicAndGroup = next.getKey().split("@");
            String responseStr =
                this.filterClassFetchMethod.fetch(topicAndGroup[0], topicAndGroup[1],
                    filterClassInfo.getClassName());
            byte[] filterSourceBinary = responseStr.getBytes("UTF-8");
            int classCRC = UtilAll.crc32(responseStr.getBytes("UTF-8"));
            if (classCRC != filterClassInfo.getClassCRC()) {
                String javaSource = new String(filterSourceBinary, MixAll.DEFAULT_CHARSET);
                Class<?> newClass =
                    DynaCode.compileAndLoadClass(filterClassInfo.getClassName(), javaSource);
                Object newInstance = newClass.newInstance();
                filterClassInfo.setMessageFilter((MessageFilter) newInstance);
                filterClassInfo.setClassCRC(classCRC);

                log.info("fetch Remote class File OK, {} {}", next.getKey(),
                    filterClassInfo.getClassName());
            }
        } catch (Exception e) {
            log.error("fetchClassFromRemoteHost Exception", e);
        }
    }
}
 
Example 14
Source File: CommitLog.java    From rocketmq-read with Apache License 2.0 4 votes vote down vote up
public ByteBuffer encode(final MessageExtBatch messageExtBatch) {
    msgBatchMemory.clear(); //not thread-safe
    int totalMsgLen = 0;
    ByteBuffer messagesByteBuff = messageExtBatch.wrap();
    while (messagesByteBuff.hasRemaining()) {
        // 1 TOTALSIZE
        messagesByteBuff.getInt();
        // 2 MAGICCODE
        messagesByteBuff.getInt();
        // 3 BODYCRC
        messagesByteBuff.getInt();
        // 4 FLAG
        int flag = messagesByteBuff.getInt();
        // 5 BODY
        int bodyLen = messagesByteBuff.getInt();
        int bodyPos = messagesByteBuff.position();
        int bodyCrc = UtilAll.crc32(messagesByteBuff.array(), bodyPos, bodyLen);
        messagesByteBuff.position(bodyPos + bodyLen);
        // 6 properties
        short propertiesLen = messagesByteBuff.getShort();
        int propertiesPos = messagesByteBuff.position();
        messagesByteBuff.position(propertiesPos + propertiesLen);

        final byte[] topicData = messageExtBatch.getTopic().getBytes(MessageDecoder.CHARSET_UTF8);

        final int topicLength = topicData.length;

        final int msgLen = calMsgLength(bodyLen, topicLength, propertiesLen);

        // Exceeds the maximum message
        if (msgLen > this.maxMessageSize) {
            CommitLog.log.warn("message size exceeded, msg total size: " + msgLen + ", msg body size: " + bodyLen
                + ", maxMessageSize: " + this.maxMessageSize);
            throw new RuntimeException("message size exceeded");
        }

        totalMsgLen += msgLen;
        // Determines whether there is sufficient free space
        if (totalMsgLen > maxMessageSize) {
            throw new RuntimeException("message size exceeded");
        }

        // 1 TOTALSIZE
        this.msgBatchMemory.putInt(msgLen);
        // 2 MAGICCODE
        this.msgBatchMemory.putInt(CommitLog.MESSAGE_MAGIC_CODE);
        // 3 BODYCRC
        this.msgBatchMemory.putInt(bodyCrc);
        // 4 QUEUEID
        this.msgBatchMemory.putInt(messageExtBatch.getQueueId());
        // 5 FLAG
        this.msgBatchMemory.putInt(flag);
        // 6 QUEUEOFFSET
        this.msgBatchMemory.putLong(0);
        // 7 PHYSICALOFFSET
        this.msgBatchMemory.putLong(0);
        // 8 SYSFLAG
        this.msgBatchMemory.putInt(messageExtBatch.getSysFlag());
        // 9 BORNTIMESTAMP
        this.msgBatchMemory.putLong(messageExtBatch.getBornTimestamp());
        // 10 BORNHOST
        this.resetByteBuffer(hostHolder, 8);
        this.msgBatchMemory.put(messageExtBatch.getBornHostBytes(hostHolder));
        // 11 STORETIMESTAMP
        this.msgBatchMemory.putLong(messageExtBatch.getStoreTimestamp());
        // 12 STOREHOSTADDRESS
        this.resetByteBuffer(hostHolder, 8);
        this.msgBatchMemory.put(messageExtBatch.getStoreHostBytes(hostHolder));
        // 13 RECONSUMETIMES
        this.msgBatchMemory.putInt(messageExtBatch.getReconsumeTimes());
        // 14 Prepared Transaction Offset, batch does not support transaction
        this.msgBatchMemory.putLong(0);
        // 15 BODY
        this.msgBatchMemory.putInt(bodyLen);
        if (bodyLen > 0) {
            this.msgBatchMemory.put(messagesByteBuff.array(), bodyPos, bodyLen);
        }
        // 16 TOPIC
        this.msgBatchMemory.put((byte) topicLength);
        this.msgBatchMemory.put(topicData);
        // 17 PROPERTIES
        this.msgBatchMemory.putShort(propertiesLen);
        if (propertiesLen > 0) {
            this.msgBatchMemory.put(messagesByteBuff.array(), propertiesPos, propertiesLen);
        }
    }
    msgBatchMemory.flip();
    return msgBatchMemory;
}
 
Example 15
Source File: MQClientInstance.java    From rocketmq-read with Apache License 2.0 4 votes vote down vote up
/**
 * This method will be removed in the version 5.0.0,because filterServer was removed,and method <code>subscribe(final String topic, final MessageSelector messageSelector)</code>
 * is recommended.
 * 上传FilterClassToAllFilterServer去所有的FilterServer
 */
@Deprecated
private void uploadFilterClassToAllFilterServer(final String consumerGroup, final String fullClassName,
    final String topic,
    final String filterClassSource) throws UnsupportedEncodingException {

    byte[] classBody = null;
    int classCRC = 0;
    try {
        classBody = filterClassSource.getBytes(MixAll.DEFAULT_CHARSET);
        classCRC = UtilAll.crc32(classBody);
    } catch (Exception e1) {
        log.warn("uploadFilterClassToAllFilterServer Exception, ClassName: {} {}",
            fullClassName,
            RemotingHelper.exceptionSimpleDesc(e1));
    }

    TopicRouteData topicRouteData = this.topicRouteTable.get(topic);
    if (topicRouteData != null
        && topicRouteData.getFilterServerTable() != null && !topicRouteData.getFilterServerTable().isEmpty()) {

        Iterator<Entry<String, List<String>>> it = topicRouteData.getFilterServerTable().entrySet().iterator();
        while (it.hasNext()) {
            Entry<String, List<String>> next = it.next();
            List<String> value = next.getValue();
            for (final String fsAddr : value) {
                try {
                    //遍历注册MessageFilterClass给所有的FilterServertable
                    this.mQClientAPIImpl.registerMessageFilterClass(fsAddr, consumerGroup, topic, fullClassName, classCRC, classBody,
                        5000);

                    log.info("register message class filter to {} OK, ConsumerGroup: {} Topic: {} ClassName: {}", fsAddr, consumerGroup,
                        topic, fullClassName);

                } catch (Exception e) {
                    log.error("uploadFilterClassToAllFilterServer Exception", e);
                }
            }
        }
    } else {
        log.warn("register message class filter failed, because no filter server, ConsumerGroup: {} Topic: {} ClassName: {}",
            consumerGroup, topic, fullClassName);
    }
}
 
Example 16
Source File: CommitLog.java    From rocketmq-4.3.0 with Apache License 2.0 4 votes vote down vote up
public ByteBuffer encode(final MessageExtBatch messageExtBatch) {
    msgBatchMemory.clear(); //not thread-safe
    int totalMsgLen = 0;
    ByteBuffer messagesByteBuff = messageExtBatch.wrap();
    while (messagesByteBuff.hasRemaining()) {
        // 1 TOTALSIZE
        messagesByteBuff.getInt();
        // 2 MAGICCODE
        messagesByteBuff.getInt();
        // 3 BODYCRC
        messagesByteBuff.getInt();
        // 4 FLAG
        int flag = messagesByteBuff.getInt();
        // 5 BODY
        int bodyLen = messagesByteBuff.getInt();
        int bodyPos = messagesByteBuff.position();
        int bodyCrc = UtilAll.crc32(messagesByteBuff.array(), bodyPos, bodyLen);
        messagesByteBuff.position(bodyPos + bodyLen);
        // 6 properties
        short propertiesLen = messagesByteBuff.getShort();
        int propertiesPos = messagesByteBuff.position();
        messagesByteBuff.position(propertiesPos + propertiesLen);

        final byte[] topicData = messageExtBatch.getTopic().getBytes(MessageDecoder.CHARSET_UTF8);

        final int topicLength = topicData.length;

        final int msgLen = calMsgLength(bodyLen, topicLength, propertiesLen);

        // Exceeds the maximum message
        if (msgLen > this.maxMessageSize) {
            CommitLog.log.warn("message size exceeded, msg total size: " + msgLen + ", msg body size: " + bodyLen
                + ", maxMessageSize: " + this.maxMessageSize);
            throw new RuntimeException("message size exceeded");
        }

        totalMsgLen += msgLen;
        // Determines whether there is sufficient free space
        if (totalMsgLen > maxMessageSize) {
            throw new RuntimeException("message size exceeded");
        }

        // 1 TOTALSIZE
        this.msgBatchMemory.putInt(msgLen);
        // 2 MAGICCODE
        this.msgBatchMemory.putInt(CommitLog.MESSAGE_MAGIC_CODE);
        // 3 BODYCRC
        this.msgBatchMemory.putInt(bodyCrc);
        // 4 QUEUEID
        this.msgBatchMemory.putInt(messageExtBatch.getQueueId());
        // 5 FLAG
        this.msgBatchMemory.putInt(flag);
        // 6 QUEUEOFFSET
        this.msgBatchMemory.putLong(0);
        // 7 PHYSICALOFFSET
        this.msgBatchMemory.putLong(0);
        // 8 SYSFLAG
        this.msgBatchMemory.putInt(messageExtBatch.getSysFlag());
        // 9 BORNTIMESTAMP
        this.msgBatchMemory.putLong(messageExtBatch.getBornTimestamp());
        // 10 BORNHOST
        this.resetByteBuffer(hostHolder, 8);
        this.msgBatchMemory.put(messageExtBatch.getBornHostBytes(hostHolder));
        // 11 STORETIMESTAMP
        this.msgBatchMemory.putLong(messageExtBatch.getStoreTimestamp());
        // 12 STOREHOSTADDRESS
        this.resetByteBuffer(hostHolder, 8);
        this.msgBatchMemory.put(messageExtBatch.getStoreHostBytes(hostHolder));
        // 13 RECONSUMETIMES
        this.msgBatchMemory.putInt(messageExtBatch.getReconsumeTimes());
        // 14 Prepared Transaction Offset, batch does not support transaction
        this.msgBatchMemory.putLong(0);
        // 15 BODY
        this.msgBatchMemory.putInt(bodyLen);
        if (bodyLen > 0)
            this.msgBatchMemory.put(messagesByteBuff.array(), bodyPos, bodyLen);
        // 16 TOPIC
        this.msgBatchMemory.put((byte) topicLength);
        this.msgBatchMemory.put(topicData);
        // 17 PROPERTIES
        this.msgBatchMemory.putShort(propertiesLen);
        if (propertiesLen > 0)
            this.msgBatchMemory.put(messagesByteBuff.array(), propertiesPos, propertiesLen);
    }
    msgBatchMemory.flip();
    return msgBatchMemory;
}
 
Example 17
Source File: CommitLog.java    From rocketmq-all-4.1.0-incubating with Apache License 2.0 4 votes vote down vote up
public ByteBuffer encode(final MessageExtBatch messageExtBatch) {
    msgBatchMemory.clear(); //not thread-safe
    int totalMsgLen = 0;
    ByteBuffer messagesByteBuff = messageExtBatch.wrap();
    while (messagesByteBuff.hasRemaining()) {
        // 1 TOTALSIZE
        messagesByteBuff.getInt();
        // 2 MAGICCODE
        messagesByteBuff.getInt();
        // 3 BODYCRC
        messagesByteBuff.getInt();
        // 4 FLAG
        int flag = messagesByteBuff.getInt();
        // 5 BODY
        int bodyLen = messagesByteBuff.getInt();
        int bodyPos = messagesByteBuff.position();
        int bodyCrc = UtilAll.crc32(messagesByteBuff.array(), bodyPos, bodyLen);
        messagesByteBuff.position(bodyPos + bodyLen);
        // 6 properties
        short propertiesLen = messagesByteBuff.getShort();
        int propertiesPos = messagesByteBuff.position();
        messagesByteBuff.position(propertiesPos + propertiesLen);

        final byte[] topicData = messageExtBatch.getTopic().getBytes(MessageDecoder.CHARSET_UTF8);

        final int topicLength = topicData.length;

        final int msgLen = calMsgLength(bodyLen, topicLength, propertiesLen);

        // Exceeds the maximum message
        if (msgLen > this.maxMessageSize) {
            CommitLog.log.warn("message size exceeded, msg total size: " + msgLen + ", msg body size: " + bodyLen
                + ", maxMessageSize: " + this.maxMessageSize);
            throw new RuntimeException("message size exceeded");
        }

        totalMsgLen += msgLen;
        // Determines whether there is sufficient free space
        if (totalMsgLen > maxMessageSize) {
            throw new RuntimeException("message size exceeded");
        }

        // 1 TOTALSIZE
        this.msgBatchMemory.putInt(msgLen);
        // 2 MAGICCODE
        this.msgBatchMemory.putInt(CommitLog.MESSAGE_MAGIC_CODE);
        // 3 BODYCRC
        this.msgBatchMemory.putInt(bodyCrc);
        // 4 QUEUEID
        this.msgBatchMemory.putInt(messageExtBatch.getQueueId());
        // 5 FLAG
        this.msgBatchMemory.putInt(flag);
        // 6 QUEUEOFFSET
        this.msgBatchMemory.putLong(0);
        // 7 PHYSICALOFFSET
        this.msgBatchMemory.putLong(0);
        // 8 SYSFLAG
        this.msgBatchMemory.putInt(messageExtBatch.getSysFlag());
        // 9 BORNTIMESTAMP
        this.msgBatchMemory.putLong(messageExtBatch.getBornTimestamp());
        // 10 BORNHOST
        this.resetByteBuffer(hostHolder, 8);
        this.msgBatchMemory.put(messageExtBatch.getBornHostBytes(hostHolder));
        // 11 STORETIMESTAMP
        this.msgBatchMemory.putLong(messageExtBatch.getStoreTimestamp());
        // 12 STOREHOSTADDRESS
        this.resetByteBuffer(hostHolder, 8);
        this.msgBatchMemory.put(messageExtBatch.getStoreHostBytes(hostHolder));
        // 13 RECONSUMETIMES
        this.msgBatchMemory.putInt(messageExtBatch.getReconsumeTimes());
        // 14 Prepared Transaction Offset, batch does not support transaction
        this.msgBatchMemory.putLong(0);
        // 15 BODY
        this.msgBatchMemory.putInt(bodyLen);
        if (bodyLen > 0)
            this.msgBatchMemory.put(messagesByteBuff.array(), bodyPos, bodyLen);
        // 16 TOPIC
        this.msgBatchMemory.put((byte) topicLength);
        this.msgBatchMemory.put(topicData);
        // 17 PROPERTIES
        this.msgBatchMemory.putShort(propertiesLen);
        if (propertiesLen > 0)
            this.msgBatchMemory.put(messagesByteBuff.array(), propertiesPos, propertiesLen);
    }
    msgBatchMemory.flip();
    return msgBatchMemory;
}
 
Example 18
Source File: MQClientInstance.java    From rocketmq-4.3.0 with Apache License 2.0 4 votes vote down vote up
private void uploadFilterClassToAllFilterServer(final String consumerGroup, final String fullClassName,
        final String topic,
        final String filterClassSource) throws UnsupportedEncodingException {
        byte[] classBody = null;
        int classCRC = 0;
        try {
            classBody = filterClassSource.getBytes(MixAll.DEFAULT_CHARSET);
//            压缩
            classCRC = UtilAll.crc32(classBody);
        } catch (Exception e1) {
            log.warn("uploadFilterClassToAllFilterServer Exception, ClassName: {} {}",
                fullClassName,
                RemotingHelper.exceptionSimpleDesc(e1));
        }

//        获取topic的路由信息
        TopicRouteData topicRouteData = this.topicRouteTable.get(topic);
        if (topicRouteData != null
            && topicRouteData.getFilterServerTable() != null && !topicRouteData.getFilterServerTable().isEmpty()) {
//            遍历的过滤server地址
            Iterator<Entry<String, List<String>>> it = topicRouteData.getFilterServerTable().entrySet().iterator();
            while (it.hasNext()) {
                Entry<String, List<String>> next = it.next();
                List<String> value = next.getValue();
                for (final String fsAddr : value) {
                    try {
//                        注册过滤类的消息=》
                        this.mQClientAPIImpl.registerMessageFilterClass(fsAddr, consumerGroup, topic, fullClassName, classCRC, classBody,
                            5000);

                        log.info("register message class filter to {} OK, ConsumerGroup: {} Topic: {} ClassName: {}", fsAddr, consumerGroup,
                            topic, fullClassName);

                    } catch (Exception e) {
                        log.error("uploadFilterClassToAllFilterServer Exception", e);
                    }
                }
            }
        } else {
            log.warn("register message class filter failed, because no filter server, ConsumerGroup: {} Topic: {} ClassName: {}",
                consumerGroup, topic, fullClassName);
        }
    }
 
Example 19
Source File: MQClientInstance.java    From rocketmq with Apache License 2.0 4 votes vote down vote up
/**
 * This method will be removed in the version 5.0.0,because filterServer was removed,and method
 * <code>subscribe(final String topic, final MessageSelector messageSelector)</code> is recommended.
 */
@Deprecated
private void uploadFilterClassToAllFilterServer(final String consumerGroup, final String fullClassName,
    final String topic,
    final String filterClassSource) throws UnsupportedEncodingException {
    byte[] classBody = null;
    int classCRC = 0;
    try {
        classBody = filterClassSource.getBytes(MixAll.DEFAULT_CHARSET);
        classCRC = UtilAll.crc32(classBody);
    } catch (Exception e1) {
        log.warn("uploadFilterClassToAllFilterServer Exception, ClassName: {} {}",
            fullClassName,
            RemotingHelper.exceptionSimpleDesc(e1));
    }

    TopicRouteData topicRouteData = this.topicRouteTable.get(topic);
    if (topicRouteData != null
        && topicRouteData.getFilterServerTable() != null && !topicRouteData.getFilterServerTable().isEmpty()) {
        Iterator<Entry<String, List<String>>> it = topicRouteData.getFilterServerTable().entrySet().iterator();
        while (it.hasNext()) {
            Entry<String, List<String>> next = it.next();
            List<String> value = next.getValue();
            for (final String fsAddr : value) {
                try {
                    this.mQClientAPIImpl.registerMessageFilterClass(fsAddr, consumerGroup, topic, fullClassName, classCRC, classBody,
                        5000);

                    log.info("register message class filter to {} OK, ConsumerGroup: {} Topic: {} ClassName: {}", fsAddr, consumerGroup,
                        topic, fullClassName);

                } catch (Exception e) {
                    log.error("uploadFilterClassToAllFilterServer Exception", e);
                }
            }
        }
    } else {
        log.warn("register message class filter failed, because no filter server, ConsumerGroup: {} Topic: {} ClassName: {}",
            consumerGroup, topic, fullClassName);
    }
}
 
Example 20
Source File: CommitLog.java    From rocketmq with Apache License 2.0 4 votes vote down vote up
public ByteBuffer encode(final MessageExtBatch messageExtBatch) {
    msgBatchMemory.clear(); //not thread-safe
    int totalMsgLen = 0;
    ByteBuffer messagesByteBuff = messageExtBatch.wrap();

    int sysFlag = messageExtBatch.getSysFlag();
    int bornHostLength = (sysFlag & MessageSysFlag.BORNHOST_V6_FLAG) == 0 ? 4 + 4 : 16 + 4;
    int storeHostLength = (sysFlag & MessageSysFlag.STOREHOSTADDRESS_V6_FLAG) == 0 ? 4 + 4 : 16 + 4;
    ByteBuffer bornHostHolder = ByteBuffer.allocate(bornHostLength);
    ByteBuffer storeHostHolder = ByteBuffer.allocate(storeHostLength);

    while (messagesByteBuff.hasRemaining()) {
        // 1 TOTALSIZE
        messagesByteBuff.getInt();
        // 2 MAGICCODE
        messagesByteBuff.getInt();
        // 3 BODYCRC
        messagesByteBuff.getInt();
        // 4 FLAG
        int flag = messagesByteBuff.getInt();
        // 5 BODY
        int bodyLen = messagesByteBuff.getInt();
        int bodyPos = messagesByteBuff.position();
        int bodyCrc = UtilAll.crc32(messagesByteBuff.array(), bodyPos, bodyLen);
        messagesByteBuff.position(bodyPos + bodyLen);
        // 6 properties
        short propertiesLen = messagesByteBuff.getShort();
        int propertiesPos = messagesByteBuff.position();
        messagesByteBuff.position(propertiesPos + propertiesLen);

        final byte[] topicData = messageExtBatch.getTopic().getBytes(MessageDecoder.CHARSET_UTF8);

        final int topicLength = topicData.length;

        final int msgLen = calMsgLength(messageExtBatch.getSysFlag(), bodyLen, topicLength, propertiesLen);

        // Exceeds the maximum message
        if (msgLen > this.maxMessageSize) {
            CommitLog.log.warn("message size exceeded, msg total size: " + msgLen + ", msg body size: " + bodyLen
                + ", maxMessageSize: " + this.maxMessageSize);
            throw new RuntimeException("message size exceeded");
        }

        totalMsgLen += msgLen;
        // Determines whether there is sufficient free space
        if (totalMsgLen > maxMessageSize) {
            throw new RuntimeException("message size exceeded");
        }

        // 1 TOTALSIZE
        this.msgBatchMemory.putInt(msgLen);
        // 2 MAGICCODE
        this.msgBatchMemory.putInt(CommitLog.MESSAGE_MAGIC_CODE);
        // 3 BODYCRC
        this.msgBatchMemory.putInt(bodyCrc);
        // 4 QUEUEID
        this.msgBatchMemory.putInt(messageExtBatch.getQueueId());
        // 5 FLAG
        this.msgBatchMemory.putInt(flag);
        // 6 QUEUEOFFSET
        this.msgBatchMemory.putLong(0);
        // 7 PHYSICALOFFSET
        this.msgBatchMemory.putLong(0);
        // 8 SYSFLAG
        this.msgBatchMemory.putInt(messageExtBatch.getSysFlag());
        // 9 BORNTIMESTAMP
        this.msgBatchMemory.putLong(messageExtBatch.getBornTimestamp());
        // 10 BORNHOST
        this.resetByteBuffer(bornHostHolder, bornHostLength);
        this.msgBatchMemory.put(messageExtBatch.getBornHostBytes(bornHostHolder));
        // 11 STORETIMESTAMP
        this.msgBatchMemory.putLong(messageExtBatch.getStoreTimestamp());
        // 12 STOREHOSTADDRESS
        this.resetByteBuffer(storeHostHolder, storeHostLength);
        this.msgBatchMemory.put(messageExtBatch.getStoreHostBytes(storeHostHolder));
        // 13 RECONSUMETIMES
        this.msgBatchMemory.putInt(messageExtBatch.getReconsumeTimes());
        // 14 Prepared Transaction Offset, batch does not support transaction
        this.msgBatchMemory.putLong(0);
        // 15 BODY
        this.msgBatchMemory.putInt(bodyLen);
        if (bodyLen > 0)
            this.msgBatchMemory.put(messagesByteBuff.array(), bodyPos, bodyLen);
        // 16 TOPIC
        this.msgBatchMemory.put((byte) topicLength);
        this.msgBatchMemory.put(topicData);
        // 17 PROPERTIES
        this.msgBatchMemory.putShort(propertiesLen);
        if (propertiesLen > 0)
            this.msgBatchMemory.put(messagesByteBuff.array(), propertiesPos, propertiesLen);
    }
    msgBatchMemory.flip();
    return msgBatchMemory;
}