Spring 向控制台重复启动相同的代理消息

Spring Boot same broker messages repeated to console

我目前正在做一个 Spring 引导项目,此文本在停止前每秒持续打印到控制台 30 秒。

15:18:02.416  o.a.activemq.broker.TransportConnector : Connector vm://localhost started
15:18:03.480  o.a.activemq.broker.TransportConnector : Connector vm://localhost stopped
15:18:03.480  o.apache.activemq.broker.BrokerService : Apache ActiveMQ 5.10.1 (localhost, ID:Jordan-801993-L.local-55074-1432703875573-0:7) is shutting down
15:18:03.481  o.apache.activemq.broker.BrokerService : Apache ActiveMQ 5.10.1 (localhost, ID:Jordan-801993-L.local-55074-1432703875573-0:7) uptime 1.069 seconds
15:18:03.481  o.apache.activemq.broker.BrokerService : Apache ActiveMQ 5.10.1 (localhost, ID:Jordan-801993-L.local-55074-1432703875573-0:7) is shutdown
15:18:03.542  o.apache.activemq.broker.BrokerService : Using Persistence Adapter: MemoryPersistenceAdapter
15:18:03.543  o.apache.activemq.broker.BrokerService : Apache ActiveMQ 5.10.1 (localhost, ID:Jordan-801993-L.local-55074-1432703875573-0:8) is starting
15:18:03.543  o.apache.activemq.broker.BrokerService : Apache ActiveMQ 5.10.1 (localhost, ID:Jordan-801993-L.local-55074-1432703875573-0:8) started
15:18:03.543  o.apache.activemq.broker.BrokerService : For help or more information please see: http://activemq.apache.org
15:18:03.544  o.a.a.broker.jmx.ManagementContext     : JMX consoles can connect to service:jmx:rmi:///jndi/rmi://localhost:1099/jmxrmi
15:18:03.544  o.a.activemq.broker.TransportConnector : Connector vm://localhost started

该项目仍然运行良好,只是很烦人。有人知道为什么会这样吗?

我无法深入解释为什么会发生这种情况,但这与 ConnectionFactory 的自动配置方式有关。

摆脱嵌入式代理不断重启的一种方法是在 application.properties:

中启用池化
spring.activemq.pooled=true

为了使用它,您还必须将以下依赖项添加到您的 pom.xml:

<dependency>
    <groupId>org.apache.activemq</groupId>
    <artifactId>activemq-pool</artifactId>
</dependency> 

我翻阅了一些文档并最终找到了 this

页面底部显示:

Using ActiveMQConnectionFactory
...
The broker will be created upon creation of the first connection.
...

同样,这并不能完全解释发生了什么,但是当我发现启用池可以阻止这种行为发生后,我就停止了挖掘。

遇到了同样的问题,但接受的答案没有帮助。找到 the thread with explanation

ActiveMQ will only keep an in-memory queue alive if there is something holding onto it.

If the Queue has been setup in spring to not cache the queue then ActiveMQ will keep starting/stopping the connector. A full appserver would effectively cache queue stuff in a pool, thus keeping it alive.

Stick in the bean def for the Spring JMS container (org.springframework.jms.listener.DefaultMessageLi stenerContainer) to fix the problem.

@Bean
public JmsListenerContainerFactory<?> myFactory(ConnectionFactory connectionFactory,
                                                DefaultJmsListenerContainerFactoryConfigurer configurer) {
    DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
    factory.setCacheLevelName("CACHE_CONNECTION"); //<-- the line fixed the problem
    configurer.configure(factory, connectionFactory);
    return factory;
}

我所做的是显式创建一个代理,并告诉 Spring 使用它而不是它的隐式代理。这样,broker 肯定会存活:

@Configuration
public class MessagingConfig {

    private final static String BROKER_URL = "tcp://localhost:61616";

    @Bean
    public BrokerService brokerService() {
        BrokerService broker = new BrokerService();
        broker.addConnector(BROKER_URL);
        broker.setPersistent(false);
        broker.start();
        return broker;
    }

    @Bean
    public ActiveMQConnectionFactory connectionFactory() {
        ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory();
        connectionFactory.setBrokerURL(BROKER_URL);
        return connectionFactory;
    }

}

接受的答案不适合我,找到了另一个解决方案。也许会有帮助:

我正在使用 Spring Boot 应用程序,不需要 PooledConnectionFactory,然后将此行添加到您的应用程序 class:

@SpringBootApplication(exclude = {ActiveMQAutoConfiguration.class})
public class MyApplication{...}

这将排除 activemq 自动配置,您将不会在日志中看到该垃圾。

我们有 spring 引导 1.5 服务,它通过 JMSTemplate 使用和发布并且没有这个问题,然后我们有一个只发布(所以没有 JmsListener)并用这些消息填充日志 - 加上相关的WARN Temporary Store limit is 51200 mb ... resetting to maximum available disk space - 在每次 GET 时将它们写入 /healthcheck。您需要考虑其影响,但可以使用 属性 设置禁用此行为以及所有相关的日志记录:

management.health.jms.enabled=false

您会看到 jms 块因此从您的 /healthcheck 输出中消失 - 因此请检查它是否确实反映了您的服务所需的任何内容。 我们在此线程上的其他答案以及我们从 Startup error of embedded ActiveMQ: Temporary Store limit is 51200 mb.

中尝试的答案都没有成功