2013-07-01 7 views
0

Я делаю приложение интеграции с пружиной + 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); 

    } 
} 

и вот моя весна контекст:

<?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()' на фабрике соединений, закрывая соединение и остановка резьбы соединения. –

Смежные вопросы