2017-05-04 130 views
0

我正在使用spring-boot與camel和ActiveMQ。ConnectionFactory在駝峯前被破壞

我通過@EnableJms註釋使用activemq autoconfiguration。 但創建我自己的ActiveMQComponent在所有隊列上啓用「事務處理(true)」。

@Bean(name = "activemq") 
@ConditionalOnClass(ActiveMQComponent.class) 
public ActiveMQComponent activeMQComponent(ConnectionFactory connectionFactory) { 
    ActiveMQComponent activeMQComponent = new ActiveMQComponent(); 
    activeMQComponent.setConnectionFactory(connectionFactory); 
    activeMQComponent.setTransacted(true); 
    activeMQComponent.setTransactionManager(jmsTransactionManager(connectionFactory)); 
    return activeMQComponent; 
} 

它運行良好,但是當我嘗試正常關閉應用程序。 在駱駝正常關機發生之前,PooledConnectionFactory會被銷燬。

導致大量的錯誤和路徑無法正確停止。這個錯誤

像20倍:

2017-05-04 18:21:59.748 WARN 12188 --- [er[test.queue]] o.a.activemq.jms.pool.PooledSession  : Caught exception trying rollback() when putting session back into the pool, will invalidate. javax.jms.IllegalStateException: The Session is closed 

其次:

2017-05-04 18:21:59.748 INFO 12188 --- [  Thread-18] o.a.camel.spring.SpringCamelContext  : Apache Camel 2.18.3 (CamelContext: route) is shutting down 

再後來:

2017-05-04 18:21:59.766 INFO 12188 --- [ - ShutdownTask] o.a.camel.impl.DefaultShutdownStrategy : Waiting as there are still 1 inflight and pending exchanges to complete, timeout in 300 seconds. Inflights per route: [test2 = 1]

任何人都可以幫我配置Spring啓動駱駝的ActiveMQ所有加上優雅的關機?

感謝

更新: 這裏是我的pom.xml的一個樣本:

 <properties> 

     <!-- Spring --> 
     <spring-boot.version>1.4.3.RELEASE</spring-boot.version> 

     <!-- Camel --> 
     <camel-spring-boot.version>2.18.3</camel-spring-boot.version> 
    </properties> 
    .... 

    <!-- Camel BOM --> 
     <dependency> 
      <!-- Import dependency management from Spring Boot --> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-dependencies</artifactId> 
      <version>${spring-boot.version}</version> 
      <type>pom</type> 
      <scope>import</scope> 
     </dependency> 
     <dependency> 
      <groupId>org.apache.camel</groupId> 
      <artifactId>camel-spring-boot-dependencies</artifactId> 
      <version>${camel-spring-boot.version}</version> 
      <type>pom</type> 
      <scope>import</scope> 
     </dependency> 
    </dependencies> 
</dependencyManagement> 
... 
    <!-- Spring --> 
    <dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-web</artifactId> 
     <exclusions> 
      <exclusion> 
       <groupId>org.springframework.boot</groupId> 
       <artifactId>spring-boot-starter-tomcat</artifactId> 
      </exclusion> 
     </exclusions> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-jetty</artifactId> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-actuator</artifactId> 
    </dependency> 

    <!-- ActiveMQ --> 
    <dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-activemq</artifactId> 
    </dependency> 
    <dependency> 
     <groupId>org.apache.activemq</groupId> 
     <artifactId>activemq-camel</artifactId> 
    </dependency> 
    <dependency> 
     <groupId>org.apache.activemq</groupId> 
     <artifactId>activemq-pool</artifactId> 
    </dependency> 

    <!-- Camel --> 
    <dependency> 
     <groupId>org.apache.camel</groupId> 
     <artifactId>camel-spring-boot-starter</artifactId> 
    </dependency> 

更新2: 經過進一步調查,並添加每次修改創建一個新項目我一個接一個地隔離了這個問題。

關機工作正確地將直到我添加特定端點:

@EndpointInject(uri = "direct:aaa") 
private Endpoint errorHandling; 

使用:

private String errorHandling = "direct:aaa"; 

不會產生錯誤。

好像使用@EndpointInject正在使ActiveMQ的密切第一

更新3:

研究發現,SpringCamelContext未實現ApplicationListener因而其方法「onApplicationEvent」它不叫處理「shutdownEager 「駱駝。

+0

方法SpringCamelContext.onApplicationEvent調用與ContextStoppedEvent,把你的日誌追蹤或調試 –

+0

增加了斷點,它從未被調用過。創建一個監聽此事件的組件,然後手動調用springCamelContext.onApplicationEvent(event)。它似乎工作 –

回答

0

重要的是要使用駱駝春季啓動啓動。

http://camel.apache.org/spring-boot.html

如何啓用我的春節,啓動應用程序駱駝自動配置?

剛落,駱駝彈簧啓動的jar到classpath中:

<dependency> 
    <groupId>org.apache.camel</groupId> 
    <artifactId>camel-spring-boot</artifactId> 
    <version>${camel.version}</version> <!-- use the same version as your Camel core version --> 
</dependency> 

駱駝彈簧引導罐子自帶spring.factories的文件等,一旦你添加到依賴你的類路徑,Spring Boot會自動爲你自動配置駱駝。好極了!這很快;)。

自動配置的駱駝上下文

的功能由駱駝自動配置所提供的最重要的部分是CamelContext實例。

駱駝自動配置爲您創建SpringCamelContext並處理該上下文的正確初始化和關閉。

創建Camel上下文也在Spring應用程序上下文中註冊(在camelContext bean名稱下),因此您可以像訪問其他任何Spring bean一樣訪問它。

@Configuration 
public class MyAppConfig { 

    @Autowired 
    CamelContext camelContext; 

    @Bean 
    MyService myService() { 
    return new DefaultMyService(camelContext); 
    } 
} 
+0

我更新了我的問題與我的pom.xml的示例。我正在使用駱駝春季引導,並且所有內容都是自動配置的。這個問題實際上是駱駝路線之前被破壞的activemq。 –

+0

添加另一個bean定義像connectionFactories,broker,camel –

+0

更新我的問題,它涉及到@EndpointInject不知道怎麼/爲什麼 –

0

顯然因爲https://issues.apache.org/jira/browse/CAMEL-2607 的SpringCamelContext沒有實現ApplicationListener接口來了。

因爲我使用spring-boot autoconfiguration,所以我沒有使用添加偵聽器的CamelContextFactoryBean。

有一個臨時的解決辦法,我創建了聽取了ApplicationEvent並派遣到SpringCamelContext方法的組成部分:

public class SpringCamelContextFix implements ApplicationListener<ApplicationEvent> { 

private SpringCamelContext camelContext; 

public SpringCamelContextFix(SpringCamelContext camelContext) { 
    this.camelContext = camelContext; 
} 

@Override 
public void onApplicationEvent(ApplicationEvent event) { 
    camelContext.onApplicationEvent(event); 
} 

}