-
Notifications
You must be signed in to change notification settings - Fork 912
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
90 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
.../src/main/java/org/apache/rocketmq/client/autoconfigure/RocketMQSpringInitialization.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package org.apache.rocketmq.client.autoconfigure; | ||
|
||
import org.apache.rocketmq.client.support.AssertSkipInitialization; | ||
import org.apache.rocketmq.client.support.DefaultListenerContainer; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.boot.ApplicationArguments; | ||
import org.springframework.boot.ApplicationRunner; | ||
import org.springframework.context.ApplicationContext; | ||
import org.springframework.context.ApplicationContextAware; | ||
import org.springframework.context.ConfigurableApplicationContext; | ||
import org.springframework.core.Ordered; | ||
|
||
import javax.annotation.Resource; | ||
import java.util.List; | ||
|
||
public class RocketMQSpringInitialization implements ApplicationRunner, ApplicationContextAware, Ordered { | ||
|
||
private final static Logger log = LoggerFactory.getLogger(RocketMQSpringInitialization.class); | ||
|
||
private ConfigurableApplicationContext applicationContext; | ||
|
||
@Resource | ||
private ListenerContainerConfiguration listenerContainerConfiguration; | ||
|
||
@Override | ||
public int getOrder() { | ||
return LOWEST_PRECEDENCE - 20; | ||
} | ||
|
||
@Override | ||
public void run(ApplicationArguments args) { | ||
// spring cloud init context skip | ||
if (AssertSkipInitialization.shouldSkipInitialization(applicationContext.getEnvironment().getPropertySources())) { | ||
return; | ||
} | ||
|
||
List<DefaultListenerContainer> containers = listenerContainerConfiguration.getContainers(); | ||
|
||
for (DefaultListenerContainer container : containers) { | ||
if (!container.isRunning()) { | ||
try { | ||
container.start(); | ||
} catch (Exception e) { | ||
log.error("Started container failed. {}", container, e); | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public void setApplicationContext(ApplicationContext applicationContext) { | ||
this.applicationContext = (ConfigurableApplicationContext) applicationContext; | ||
} | ||
|
||
} | ||
|
18 changes: 18 additions & 0 deletions
18
...pring-boot/src/main/java/org/apache/rocketmq/client/support/AssertSkipInitialization.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package org.apache.rocketmq.client.support; | ||
|
||
import org.springframework.core.env.MutablePropertySources; | ||
|
||
import java.util.Objects; | ||
|
||
public class AssertSkipInitialization { | ||
|
||
private static final String BOOTSTRAP_PROPERTY_SOURCE = "bootstrap"; | ||
|
||
public static Boolean shouldSkipInitialization(MutablePropertySources mutablePropertySources) { | ||
|
||
if (Objects.isNull(mutablePropertySources)) { | ||
return Boolean.FALSE; | ||
} | ||
return mutablePropertySources.contains(BOOTSTRAP_PROPERTY_SOURCE); | ||
} | ||
} |