SpringBoot Camel ActiveMQ Embedded代理意外停止并重新启动

时间:2018-05-04 13:37:20

标签: java activemq spring-camel

我的SpringBoot Camel App存在问题。

非嵌入式代理非常好用。这是它的配置: URL是以字符串形式指向独立的ActiveMQ服务器或嵌入式代理URL(vm:// dpcgbroker?broker.persistent = false)

@Configuration
public class CamelConfiguration {

@Value("${broker.mqURL}")
String mqURL;


@Bean
CamelContextConfiguration contextConfiguration() {
    return new CamelContextConfiguration() {
        @Override
        public void beforeApplicationStart(CamelContext context) {


            ActiveMQComponent activeMQComponent =  ActiveMQComponent.activeMQComponent(mqURL);
            activeMQComponent.setConfiguration(getJmsConfiguration());

            context.addComponent("jms", activeMQComponent);

            DefaultShutdownStrategy shutdownStrategy = new DefaultShutdownStrategy();
        shutdownStrategy.setTimeUnit(TimeUnit.SECONDS);
            shutdownStrategy.setTimeout(20);
            context.setShutdownStrategy(shutdownStrategy);
        }



        @Override
        public void afterApplicationStart(CamelContext camelContext) {

        }


    };
}


public PooledConnectionFactory getPooledConnectionFactory() {
    PooledConnectionFactory pooledConnectionFactory = new PooledConnectionFactory(getConnectionFactory());

    pooledConnectionFactory.setMaxConnections(30);

    return pooledConnectionFactory;
}

public ActiveMQConnectionFactory getConnectionFactory() {
    ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory();
    connectionFactory.setBrokerURL(mqURL);
    connectionFactory.setTrustAllPackages(true);
    return connectionFactory;
}


public JmsConfiguration getJmsConfiguration() {
    JmsConfiguration jmsConfiguration = new JmsConfiguration();
    jmsConfiguration.setDisableTimeToLive(true);
    jmsConfiguration.setTransacted(true);
    jmsConfiguration.setLazyCreateTransactionManager(false);
    jmsConfiguration.setConnectionFactory(getConnectionFactory());
    jmsConfiguration.setCacheLevelName("CACHE_CONSUMER");
    return jmsConfiguration;
}



@Bean
@Scope(value = ConfigurableBeanFactory.SCOPE_SINGLETON)
public PlatformTransactionManager getTransactionManager() {

    PlatformTransactionManager platformTransactionManager = new JmsTransactionManager(getConnectionFactory());
    return platformTransactionManager;
}

当我切换到嵌入式代理时,行为是不同的,Apache ActiveMQ正在启动然后停止,然后重新启动。我不知道为什么,也没有例外

以下是日志的摘录

15967 INFO  - 5063 -  [Camel (camel-1) thread #1 - file://conf/DR3/planets/] org.apache.activemq.broker.BrokerService     - Using Persistence Adapter: MemoryPersistenceAdapter
15991 INFO  - 5063 -  [JMX connector] org.apache.activemq.broker.jmx.ManagementContext     - JMX consoles can connect to service:jmx:rmi:///jndi/rmi://localhost:1099/jmxrmi
16106 INFO  - 5063 -  [Camel (camel-1) thread #1 - file://conf/DR3/planets/] org.apache.activemq.broker.BrokerService     - Apache ActiveMQ 5.15.2 (dpcgbroker, ID:Greg.local-59246-1525440325893-0:1) is starting
16111 INFO  - 5063 -  [Camel (camel-1) thread #1 - file://conf/DR3/planets/] org.apache.activemq.broker.BrokerService     - Apache ActiveMQ 5.15.2 (dpcgbroker, ID:Greg.local-59246-1525440325893-0:1) started
16111 INFO  - 5063 -  [Camel (camel-1) thread #1 - file://conf/DR3/planets/] org.apache.activemq.broker.BrokerService     - For help or more information please see: http://activemq.apache.org
16132 INFO  - 5063 -  [Camel (camel-1) thread #1 - file://conf/DR3/planets/] org.apache.activemq.broker.TransportConnector     - Connector vm://dpcgbroker started
16205 INFO  - 5063 -  [Camel (camel-1) thread #1 - file://conf/DR3/planets/] org.apache.activemq.broker.TransportConnector     - Connector vm://dpcgbroker stopped
16205 INFO  - 5063 -  [Camel (camel-1) thread #1 - file://conf/DR3/planets/] org.apache.activemq.broker.BrokerService     - Apache ActiveMQ 5.15.2 (dpcgbroker, ID:Greg.local-59246-1525440325893-0:1) is shutting down
16213 INFO  - 5063 -  [Camel (camel-1) thread #1 - file://conf/DR3/planets/] org.apache.activemq.broker.BrokerService     - Apache ActiveMQ 5.15.2 (dpcgbroker, ID:Greg.local-59246-1525440325893-0:1) uptime 0.274 seconds
16213 INFO  - 5063 -  [Camel (camel-1) thread #1 - file://conf/DR3/planets/] org.apache.activemq.broker.BrokerService     - Apache ActiveMQ 5.15.2 (dpcgbroker, ID:Greg.local-59246-1525440325893-0:1) is shutdown
16618 INFO  - 5063 -  [Camel (camel-1) thread #1 - file://conf/DR3/planets/] org.apache.camel.processor.aggregate.AggregateProcessor     - Defaulting to MemoryAggregationRepository
16621 INFO  - 5063 -  [Camel (camel-1) thread #1 - file://conf/DR3/planets/] org.apache.camel.processor.aggregate.AggregateProcessor     - Using CompletionTimeout to trigger after 10 millis of inactivity.
16647 INFO  - 5063 -  [Camel (camel-1) thread #1 - file://conf/DR3/planets/] org.apache.camel.spring.SpringCamelContext     - Skipping starting of route jobStatusRoute as its configured with autoStartup=false
16656 INFO  - 5063 -  [Camel (camel-1) thread #1 - file://conf/DR3/planets/] org.apache.activemq.broker.BrokerService     - Using Persistence Adapter: MemoryPersistenceAdapter
16656 INFO  - 5063 -  [Camel (camel-1) thread #1 - file://conf/DR3/planets/] org.apache.activemq.broker.BrokerService     - Apache ActiveMQ 5.15.2 (dpcgbroker, ID:Greg.local-59246-1525440325893-0:2) is starting
16657 INFO  - 5063 -  [JMX connector] org.apache.activemq.broker.jmx.ManagementContext     - JMX consoles can connect to service:jmx:rmi:///jndi/rmi://localhost:1099/jmxrmi
16657 INFO  - 5063 -  [Camel (camel-1) thread #1 - file://conf/DR3/planets/] org.apache.activemq.broker.BrokerService     - Apache ActiveMQ 5.15.2 (dpcgbroker, ID:Greg.local-59246-1525440325893-0:2) started

问题是某些消息是在代理的第一个实例中发送的,当它停止时会丢失,然后第二个实例以空的消息列表开始。 我不知道为什么它会停止。

PS:我不想使用持久性嵌入式代理,这是无用的解决方法

1 个答案:

答案 0 :(得分:2)

过了一会儿,我明白了发生了什么。整个问题是嵌入式vm代理具有特殊行为,在documentation中描述了

  

一旦关闭了与代理的所有VM连接,嵌入式代理将自动关闭。

我的应用程序正在启动,然后发送消息然后启动了消费者。当我发送消息时,连接已关闭,导致代理关闭。

所以解决方案只是先启动消费者,然后让生产者发布消息。

PS:可能有用的解决方法是使用PooledConnectionFactory。我测试了它也有效,因为我猜池保持连接存活。通过这些技巧,您可以按照自己喜欢的顺序启动procuders和consumer