RocketMQ pollable support anonymous group.

pull/2769/head
sorie 3 years ago committed by Steve Rao
parent 6ee93ebe37
commit dfefc030c6

@ -17,8 +17,6 @@ spring:
destination: pollable destination: pollable
pollable-in-0: pollable-in-0:
destination: pollable destination: pollable
group: pollable-group
logging: logging:
level: level:
org.springframework.context.support: debug org.springframework.context.support: debug

@ -125,7 +125,7 @@ public class RocketMQMessageChannelBinder extends
throw new RuntimeException( throw new RuntimeException(
"group must be configured for DLQ" + destination.getName()); "group must be configured for DLQ" + destination.getName());
} }
group = anonymous ? anonymousGroup(destination.getName()) : group; group = anonymous ? RocketMQUtils.anonymousGroup(destination.getName()) : group;
RocketMQUtils.mergeRocketMQProperties(binderConfigurationProperties, RocketMQUtils.mergeRocketMQProperties(binderConfigurationProperties,
extendedConsumerProperties.getExtension()); extendedConsumerProperties.getExtension());
@ -182,15 +182,6 @@ public class RocketMQMessageChannelBinder extends
}; };
} }
/**
* generate anonymous group.
* @param destination not null
* @return anonymous group name.
*/
private static String anonymousGroup(final String destination) {
return RocketMQConst.DEFAULT_GROUP + "_" + destination;
}
/** /**
* Binders can return an {@link ErrorMessageStrategy} for building error messages; * Binders can return an {@link ErrorMessageStrategy} for building error messages;
* binder implementations typically might add extra headers to the error message. * binder implementations typically might add extra headers to the error message.

@ -25,6 +25,7 @@ import org.apache.rocketmq.client.consumer.AllocateMessageQueueStrategy;
import org.apache.rocketmq.client.consumer.DefaultLitePullConsumer; import org.apache.rocketmq.client.consumer.DefaultLitePullConsumer;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer; import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.rebalance.AllocateMessageQueueAveragely; import org.apache.rocketmq.client.consumer.rebalance.AllocateMessageQueueAveragely;
import org.apache.rocketmq.common.protocol.NamespaceUtil;
import org.apache.rocketmq.common.protocol.heartbeat.MessageModel; import org.apache.rocketmq.common.protocol.heartbeat.MessageModel;
import org.apache.rocketmq.remoting.RPCHook; import org.apache.rocketmq.remoting.RPCHook;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -97,15 +98,28 @@ public final class RocketMQConsumerFactory {
/** /**
* todo Compatible with versions less than 4.6 ? * todo Compatible with versions less than 4.6 ?
* @param topic
* @param extendedConsumerProperties extendedConsumerProperties * @param extendedConsumerProperties extendedConsumerProperties
* @return DefaultLitePullConsumer * @return DefaultLitePullConsumer
*/ */
public static DefaultLitePullConsumer initPullConsumer( public static DefaultLitePullConsumer initPullConsumer(
String topic,
ExtendedConsumerProperties<RocketMQConsumerProperties> extendedConsumerProperties) { ExtendedConsumerProperties<RocketMQConsumerProperties> extendedConsumerProperties) {
RocketMQConsumerProperties consumerProperties = extendedConsumerProperties RocketMQConsumerProperties consumerProperties = extendedConsumerProperties
.getExtension(); .getExtension();
Assert.notNull(consumerProperties.getGroup(), boolean anonymous = !StringUtils.hasLength(consumerProperties.getGroup());
"Property 'group' is required - consumerGroup"); /***
* When using DLQ, at least the group property must be provided for proper naming of the DLQ destination
* According to https://docs.spring.io/spring-cloud-stream/docs/3.2.1/reference/html/spring-cloud-stream.html#spring-cloud-stream-reference
*/
if (anonymous && NamespaceUtil.isDLQTopic(topic)) {
throw new RuntimeException(
"group must be configured for DLQ" + topic);
}
if (anonymous) {
consumerProperties.setGroup(RocketMQUtils.anonymousGroup(topic));
}
Assert.notNull(consumerProperties.getNameServer(), Assert.notNull(consumerProperties.getNameServer(),
"Property 'nameServer' is required"); "Property 'nameServer' is required");
AllocateMessageQueueStrategy allocateMessageQueueStrategy = RocketMQBeanContainerCache AllocateMessageQueueStrategy allocateMessageQueueStrategy = RocketMQBeanContainerCache

@ -87,7 +87,7 @@ public class RocketMQMessageSource extends AbstractMessageSource<Object>
"pull consumer already running. " + this.toString()); "pull consumer already running. " + this.toString());
} }
this.consumer = RocketMQConsumerFactory this.consumer = RocketMQConsumerFactory
.initPullConsumer(extendedConsumerProperties); .initPullConsumer(topic, extendedConsumerProperties);
// This parameter must be 1, otherwise doReceive cannot be handled singly. // This parameter must be 1, otherwise doReceive cannot be handled singly.
// this.consumer.setPullBatchSize(1); // this.consumer.setPullBatchSize(1);
this.consumer.subscribe(topic, messageSelector); this.consumer.subscribe(topic, messageSelector);

@ -100,4 +100,12 @@ public final class RocketMQUtils {
return MessageSelector.byTag(expression); return MessageSelector.byTag(expression);
} }
/**
* generate anonymous group.
* @param destination not null
* @return anonymous group name.
*/
public static String anonymousGroup(final String destination) {
return RocketMQConst.DEFAULT_GROUP + "_" + destination;
}
} }

Loading…
Cancel
Save