2015-11-01 29 views
4

我有一個簡單的使用AMQP依賴關係的Spring-Boot應用程序(只有'org.springframework.boot:spring-boot-starter-amqp' - 例如沒有web依賴項,因此沒有應用程序服務器被包含在JAR中)。Spring Boot - 針對非web應用程序的長時間運行的應用程序

我只想讓應用程序運行並偵聽隊列,並在收到消息時將某些信息記錄到數據庫中 - 但是,由於沒有應用程序服務器,只要啓動它就會關閉再次(因爲沒有任何事情正在完成)。有沒有最好的方式來保持這個應用程序在偵聽消息的同時運行?

沒有什麼令人驚訝的代碼,只是標準的應用程序配置,則也是一類標有@RabbitListener

@SpringBootApplication 
class PersistenceSeriveApplication { 

    static void main(String[] args) { 
     SpringApplication.run PersistenceSeriveApplication, args 
    } 
} 

@Configuration 
@EnableRabbit 
class QueueConfiguration { 

    @Bean public Queue applicationPersistenceQueue(@Value('${amqp.queues.persistence}') String queueName) { 
     new Queue(queueName) 
    } 
} 

(一種選擇我認爲只是旋轉了預定的過程 - 只是一個心跳或東西,這很可能是反正監測不錯 - 但沒有任何其他更好的/標準方式)

+0

您是否啓動MessageListenerContainer bean? –

+0

@ᴳᵁᴵᴰᴼahhh,是的,你是對的..這只是我的sl - - 它甚至在SpringBoot AMQP入門示例中有MessageListenerContainer - 應該投票回答我自己的問題!如果你想把它作爲答案,我會標記爲正確的(否則將在今晚晚些時候添加詳細的答案) – rhinds

回答

2

你需要確保啓動消息監聽器容器豆,像所示的例子:?

@Bean 
SimpleMessageListenerContainer container(ConnectionFactory connectionFactory, MessageListenerAdapter listenerAdapter) { 
    SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(); 
    container.setConnectionFactory(connectionFactory); 
    container.setQueueNames(queueName); 
    container.setMessageListener(listenerAdapter); 
    return container; 
} 
相關問題