[
https://issues.apache.org/jira/browse/ROCKETMQ-120?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16226363#comment-16226363
]
ASF GitHub Bot commented on ROCKETMQ-120:
-----------------------------------------
zhouxinyu commented on a change in pull request #32: [ROCKETMQ-120] Provider a
adapter for spring and spring-boot
URL: https://github.com/apache/rocketmq-externals/pull/32#discussion_r147908649
##########
File path:
spring-boot-starter-rocketmq/src/main/java/org/apache/rocketmq/spring/starter/core/DefaultRocketMQListenerContainer.java
##########
@@ -0,0 +1,293 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.rocketmq.spring.starter.core;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import org.apache.rocketmq.spring.starter.enums.ConsumeMode;
+import org.apache.rocketmq.spring.starter.enums.SelectorType;
+import java.lang.reflect.ParameterizedType;
+import java.lang.reflect.Type;
+import java.nio.charset.Charset;
+import java.util.List;
+import java.util.Objects;
+import lombok.Getter;
+import lombok.Setter;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
+import org.apache.rocketmq.client.consumer.MessageSelector;
+import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
+import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
+import org.apache.rocketmq.client.consumer.listener.ConsumeOrderlyContext;
+import org.apache.rocketmq.client.consumer.listener.ConsumeOrderlyStatus;
+import
org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
+import org.apache.rocketmq.client.consumer.listener.MessageListenerOrderly;
+import org.apache.rocketmq.client.exception.MQClientException;
+import org.apache.rocketmq.common.message.MessageExt;
+import org.apache.rocketmq.common.protocol.heartbeat.MessageModel;
+import org.springframework.beans.factory.InitializingBean;
+import org.springframework.util.Assert;
+
+@SuppressWarnings("WeakerAccess")
+@Slf4j
+public class DefaultRocketMQListenerContainer implements InitializingBean,
RocketMQListenerContainer {
+
+ @Setter
+ @Getter
+ private long suspendCurrentQueueTimeMillis = 1000;
+
+ /**
+ * Message consume retry strategy<br> -1,no retry,put into DLQ
directly<br> 0,broker control retry frequency<br>
+ * >0,client control retry frequency
+ */
+ @Setter
+ @Getter
+ private int delayLevelWhenNextConsume = 0;
+
+ @Setter
+ @Getter
+ private String consumerGroup;
+
+ @Setter
+ @Getter
+ private String nameServer;
+
+ @Setter
+ @Getter
+ private String topic;
+
+ @Setter
+ @Getter
+ private ConsumeMode consumeMode = ConsumeMode.CONCURRENTLY;
+
+ @Setter
+ @Getter
+ private SelectorType selectorType = SelectorType.TAG;
+
+ @Setter
+ @Getter
+ private String selectorExpress = "*";
+
+ @Setter
+ @Getter
+ private MessageModel messageModel = MessageModel.CLUSTERING;
+
+ @Setter
+ @Getter
+ private int consumeThreadMax = 64;
+
+ @Getter
+ @Setter
+ private String charset = "UTF-8";
+
+ @Setter
+ @Getter
+ private ObjectMapper objectMapper = new ObjectMapper();
+
+ @Setter
+ @Getter
+ private boolean started;
+
+ @Setter
+ private RocketMQListener rocketMQListener;
+
+ private DefaultMQPushConsumer consumer;
+
+ private Class messageType;
+
+ public void setupMessageListener(RocketMQListener rocketMQListener) {
+ this.rocketMQListener = rocketMQListener;
+ }
+
+ @Override
+ public void destroy() throws Exception {
+ this.setStarted(false);
+ if (Objects.nonNull(consumer)) {
+ consumer.shutdown();
+ }
+ log.info("container destroyed, {}", this.toString());
+ }
+
+ public synchronized void start() throws MQClientException {
+
+ if (this.isStarted()) {
+ throw new IllegalStateException("container already started. " +
this.toString());
+ }
+
+ initRocketMQPushConsumer();
+
+ // parse message type
+ this.messageType = getMessageType();
+ log.debug("msgType: {}", messageType.getName());
+
+ consumer.start();
+ this.setStarted(true);
+
+ log.info("started container: {}", this.toString());
+ }
+
+ public class DefaultMessageListenerConcurrently implements
MessageListenerConcurrently {
+
+ @SuppressWarnings("unchecked")
+ public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs,
ConsumeConcurrentlyContext context) {
+ for (MessageExt messageExt : msgs) {
+ log.debug("received msg: {}", messageExt);
+ try {
+ long now = System.currentTimeMillis();
+ rocketMQListener.onMessage(doConvertMessage(messageExt));
+ long costTime = System.currentTimeMillis() - now;
+ log.info("consume {} cost: {} ms", messageExt.getMsgId(),
costTime);
Review comment:
Hi, it's not a good idea to log trace info here, it may bring performance
loss, let users do this work in their own `onMessage` callback.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
> Provide a adapter for spring and spring-boot
> --------------------------------------------
>
> Key: ROCKETMQ-120
> URL: https://issues.apache.org/jira/browse/ROCKETMQ-120
> Project: Apache RocketMQ
> Issue Type: Wish
> Reporter: yukon
> Assignee: dongeforever
> Priority: Minor
> Fix For: 4.2.0
>
>
> Provider a rocketmq-spring adapter for the convenience of the spring user.
--
This message was sent by Atlassian JIRA
(v6.4.14#64029)