2013-07-01 56 views
0

我正在做一個spring-integration + rabbitMQ應用程序。我從一個主類調用一個網關發送給rabbitmq我的消息,它完美的工作,但由於一些奇怪的原因,我的主要方法繼續運行,起初我想我可能在我的春天背景中留下了一個輪詢器,但那'事實並非如此。 這裏是我的代碼:爲什麼我的主要方法繼續運行?

public final class Main { 

    private static final Logger LOGGER = Logger.getLogger(Main.class); 

    @Autowired 
    static 
    ChatGateway chatGateway; 

    private Main() { } 

    /** 
    * Load the Spring Integration Application Context 
    * 
    * @param args - command line arguments 
    */ 
    public static void main(String args[]) { 
     Mensaje mensaje = new Mensaje(); 
     mensaje.setClienteID("clienteXXX"); 

     ClassPathXmlApplicationContext ctx = new 
     ClassPathXmlApplicationContext("classpath:META-INF/spring/integration/spring-integration-context.xml"); 
     chatGateway = (ChatGateway) ctx.getBean("chatGateway"); 

     chatGateway.enviarAlarma(mensaje); 

    } 
} 

,這裏是我的Spring上下文:

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:int="http://www.springframework.org/schema/integration" 
    xmlns:int-amqp="http://www.springframework.org/schema/integration/amqp" 
    xmlns:rabbit="http://www.springframework.org/schema/rabbit" 
    xmlns:int-stream="http://www.springframework.org/schema/integration/stream" 
    xsi:schemaLocation="http://www.springframework.org/schema/integration/amqp http://www.springframework.org/schema/integration/amqp/spring-integration-amqp.xsd 
     http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd 
     http://www.springframework.org/schema/integration/stream http://www.springframework.org/schema/integration/stream/spring-integration-stream.xsd 
     http://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit.xsd 
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> 

    <!-- From STDIN To RabbitMQ 

    <int-stream:stdin-channel-adapter id="consoleIn" 
     channel="toRabbit"> 
     <int:poller fixed-delay="1000" max-messages-per-poll="1" /> 
    </int-stream:stdin-channel-adapter> 
    --> 

    <int:channel id="toRabbit" /> 

    <int:gateway id="chatGateway" 
     service-interface="com.praxis.chat.gateway.ChatGateway" 
     default-request-channel="toRabbit" /> 

    <int-amqp:outbound-channel-adapter 
     channel="toRabbit" amqp-template="amqpTemplate" exchange-name="si.test.exchange" 
     routing-key="si.test.binding" /> 


    <!-- Infrastructure --> 

    <rabbit:connection-factory id="connectionFactory" /> 

    <rabbit:template id="amqpTemplate" connection-factory="connectionFactory" /> 

    <rabbit:admin connection-factory="connectionFactory" /> 

    <rabbit:queue name="si.test.queue" /> 

    <rabbit:direct-exchange name="si.test.exchange"> 
     <rabbit:bindings> 
      <rabbit:binding queue="si.test.queue" key="si.test.binding" /> 
     </rabbit:bindings> 
    </rabbit:direct-exchange> 

</beans> 

爲什麼我的主要方法繼續運行即使它發送的消息? 在此先感謝。

回答

1

它會繼續前進,因爲它沒有被告知結束。

你可以使用:

System.exit(0);

return;

main(String[] args)

+0

末這似乎做的工作。我認爲一個主要的方法不會完成,除非它有一個無限循環。 – linker85

+1

實際上,問題在於RabbitMQ客戶端運行非守護線程當您準備關閉應用程序時,可以使用'ctx.destroy()',它將在連接工廠調用'destroy()',關閉連接並停止連接的線程。 –

相關問題