Commit fb98bcf4aa40f746a52a0e684ba4ea0606f6a190
1 parent
8954eba1
1.0.6
Showing
3 changed files
with
33 additions
and
11 deletions
pom.xml
| ... | ... | @@ -22,7 +22,7 @@ |
| 22 | 22 | |
| 23 | 23 | <groupId>org.apache.rocketmq</groupId> |
| 24 | 24 | <artifactId>spring-boot-starter-rocketmq</artifactId> |
| 25 | - <version>1.0.5-SNAPSHOT</version> | |
| 25 | + <version>1.0.6-SNAPSHOT</version> | |
| 26 | 26 | |
| 27 | 27 | <name>Spring Boot Rocket Starter</name> |
| 28 | 28 | <description>Starter for messaging using Apache RocketMQ</description> |
| ... | ... | @@ -103,7 +103,7 @@ |
| 103 | 103 | </dependency> |
| 104 | 104 | </dependencies> |
| 105 | 105 | </dependencyManagement> |
| 106 | - <!-- <distributionManagement> | |
| 106 | + <distributionManagement> | |
| 107 | 107 | <repository> |
| 108 | 108 | <id>nexus_releases</id> |
| 109 | 109 | <name>core Release Repository</name> |
| ... | ... | @@ -114,7 +114,7 @@ |
| 114 | 114 | <name>core Snapshots Repository</name> |
| 115 | 115 | <url>http://192.168.1.195:9999/nexus/content/repositories/snapshots/</url> |
| 116 | 116 | </snapshotRepository> |
| 117 | - </distributionManagement> --> | |
| 117 | + </distributionManagement> | |
| 118 | 118 | <build> |
| 119 | 119 | <plugins> |
| 120 | 120 | <plugin> | ... | ... |
src/main/java/org/apache/rocketmq/spring/starter/AliyunRocketMQAutoConfiguration.java
| ... | ... | @@ -32,6 +32,7 @@ import static org.apache.rocketmq.spring.starter.core.DefaultRocketMQListenerCon |
| 32 | 32 | import java.util.Map; |
| 33 | 33 | import java.util.Objects; |
| 34 | 34 | import java.util.Properties; |
| 35 | +import java.util.UUID; | |
| 35 | 36 | import java.util.concurrent.atomic.AtomicLong; |
| 36 | 37 | |
| 37 | 38 | import javax.annotation.Resource; |
| ... | ... | @@ -94,7 +95,9 @@ public class AliyunRocketMQAutoConfiguration { |
| 94 | 95 | |
| 95 | 96 | Properties producerProperties = new Properties(); |
| 96 | 97 | //生成者ProducerId添加前缀:PID_+环境标识_+groupName |
| 97 | - producerProperties.setProperty(PropertyKeyConst.ProducerId, "PID_"+environmentPrefix+"_"+groupName); | |
| 98 | + String pid = "PID_"+environmentPrefix+"_"+groupName; | |
| 99 | + log.info("注册生产者PID:"+pid); | |
| 100 | + producerProperties.setProperty(PropertyKeyConst.ProducerId, pid); | |
| 98 | 101 | producerProperties.setProperty(PropertyKeyConst.AccessKey, accessKey); |
| 99 | 102 | producerProperties.setProperty(PropertyKeyConst.SecretKey, secretKey); |
| 100 | 103 | producerProperties.setProperty(PropertyKeyConst.ONSAddr, onsAddr); |
| ... | ... | @@ -127,7 +130,7 @@ public class AliyunRocketMQAutoConfiguration { |
| 127 | 130 | |
| 128 | 131 | @Configuration |
| 129 | 132 | @EnableConfigurationProperties(RocketMQProperties.class) |
| 130 | - @ConditionalOnProperty(prefix = "spring.rocketmq", value = "nameServer") | |
| 133 | + @ConditionalOnProperty(prefix = "spring.rocketmq", value = {"environmentPrefix", "producer.group"}) | |
| 131 | 134 | @Order |
| 132 | 135 | public static class ListenerContainerConfiguration implements ApplicationContextAware, InitializingBean { |
| 133 | 136 | private ConfigurableApplicationContext applicationContext; |
| ... | ... | @@ -169,20 +172,24 @@ public class AliyunRocketMQAutoConfiguration { |
| 169 | 172 | } |
| 170 | 173 | |
| 171 | 174 | private void registerContainer(String beanName, Object bean) { |
| 175 | + String uuid = UUID.randomUUID().toString(); | |
| 176 | + log.info(uuid+"开始注册消费者,beanName:"+beanName); | |
| 172 | 177 | Class<?> clazz = AopUtils.getTargetClass(bean); |
| 173 | 178 | |
| 174 | 179 | if (!RocketMQListener.class.isAssignableFrom(bean.getClass())) { |
| 175 | 180 | throw new IllegalStateException(clazz + " is not instance of " + RocketMQListener.class.getName()); |
| 176 | 181 | } |
| 177 | - | |
| 178 | 182 | RocketMQListener rocketMQListener = (RocketMQListener) bean; |
| 179 | 183 | RocketMQMessageListener annotation = clazz.getAnnotation(RocketMQMessageListener.class); |
| 180 | 184 | BeanDefinitionBuilder beanBuilder = BeanDefinitionBuilder.rootBeanDefinition(AliyunRocketMQListenerContainer.class); |
| 181 | 185 | beanBuilder.addPropertyValue(PROP_ONS_Addr, rocketMQProperties.getOnsAddr()); |
| 182 | - beanBuilder.addPropertyValue(PROP_TOPIC, rocketMQProperties.getEnvironmentPrefix()+"_"+environment.resolvePlaceholders(annotation.topic())); | |
| 183 | - | |
| 186 | + String topic = rocketMQProperties.getEnvironmentPrefix()+"_"+environment.resolvePlaceholders(annotation.topic()); | |
| 187 | + log.info(uuid+"订阅的主题topic:"+topic); | |
| 188 | + beanBuilder.addPropertyValue(PROP_TOPIC, topic); | |
| 189 | + String cid = "CID_"+rocketMQProperties.getEnvironmentPrefix()+"_"+environment.resolvePlaceholders(annotation.consumerGroup()); | |
| 190 | + log.info(uuid+"消费者CID:"+cid); | |
| 184 | 191 | //消费者ConsumerId添加前缀:PID_+环境标识_+groupName |
| 185 | - beanBuilder.addPropertyValue(PROP_CONSUMER_GROUP, "CID_"+rocketMQProperties.getEnvironmentPrefix()+"_"+environment.resolvePlaceholders(annotation.consumerGroup())); | |
| 192 | + beanBuilder.addPropertyValue(PROP_CONSUMER_GROUP, cid); | |
| 186 | 193 | beanBuilder.addPropertyValue(PROP_CONSUME_MODE, annotation.consumeMode()); |
| 187 | 194 | beanBuilder.addPropertyValue(PROP_CONSUME_THREAD_MAX, annotation.consumeThreadMax()); |
| 188 | 195 | beanBuilder.addPropertyValue(PROP_MESSAGE_MODEL, annotation.messageModel()); |
| ... | ... | @@ -200,6 +207,7 @@ public class AliyunRocketMQAutoConfiguration { |
| 200 | 207 | beanBuilder.addPropertyValue(PROP_SECRET_KEY, rocketMQProperties.getSecretKey()); |
| 201 | 208 | |
| 202 | 209 | String containerBeanName = String.format("%s_%s", AliyunRocketMQListenerContainer.class.getName(), counter.incrementAndGet()); |
| 210 | + log.info("消费者容器beanName:"+containerBeanName); | |
| 203 | 211 | DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) applicationContext.getBeanFactory(); |
| 204 | 212 | beanFactory.registerBeanDefinition(containerBeanName, beanBuilder.getBeanDefinition()); |
| 205 | 213 | ... | ... |
src/main/java/org/apache/rocketmq/spring/starter/core/AliyunRocketMQListenerContainer.java
| ... | ... | @@ -149,6 +149,12 @@ public class AliyunRocketMQListenerContainer implements InitializingBean, Rocket |
| 149 | 149 | if (Objects.nonNull(consumer)) { |
| 150 | 150 | consumer.shutdown(); |
| 151 | 151 | } |
| 152 | + if (Objects.nonNull(orderConsumer)) { | |
| 153 | + orderConsumer.shutdown(); | |
| 154 | + } | |
| 155 | + if (Objects.nonNull(batchConsumer)) { | |
| 156 | + batchConsumer.shutdown(); | |
| 157 | + } | |
| 152 | 158 | log.info("container destroyed, {}", this.toString()); |
| 153 | 159 | } |
| 154 | 160 | |
| ... | ... | @@ -164,7 +170,15 @@ public class AliyunRocketMQListenerContainer implements InitializingBean, Rocket |
| 164 | 170 | this.messageType = getMessageType(); |
| 165 | 171 | log.debug("msgType: {}", messageType.getName()); |
| 166 | 172 | |
| 167 | - consumer.start(); | |
| 173 | + if (Objects.nonNull(consumer)) { | |
| 174 | + consumer.start(); | |
| 175 | + } | |
| 176 | + if (Objects.nonNull(orderConsumer)) { | |
| 177 | + orderConsumer.start(); | |
| 178 | + } | |
| 179 | + if (Objects.nonNull(batchConsumer)) { | |
| 180 | + batchConsumer.start(); | |
| 181 | + } | |
| 168 | 182 | this.setStarted(true); |
| 169 | 183 | |
| 170 | 184 | log.info("started container: {}", this.toString()); |
| ... | ... | @@ -207,7 +221,7 @@ public class AliyunRocketMQListenerContainer implements InitializingBean, Rocket |
| 207 | 221 | */ |
| 208 | 222 | private void sendConsumeMsgFailed(Message message, Exception e,Date consumeBeginTime) { |
| 209 | 223 | log.info("消费消息失败,开始发送消费失败MQ"); |
| 210 | - String topic = environmentPrefix+"_"+CONSUMEFAILED_TOPIC; | |
| 224 | + String topic = CONSUMEFAILED_TOPIC; | |
| 211 | 225 | String tag = CONSUMEFAILED_TAG; |
| 212 | 226 | try{ |
| 213 | 227 | Date consumeEndTime = new Date(); | ... | ... |