Example usage for org.springframework.context.support ClassPathXmlApplicationContext close

List of usage examples for org.springframework.context.support ClassPathXmlApplicationContext close

Introduction

In this page you can find the example usage for org.springframework.context.support ClassPathXmlApplicationContext close.

Prototype

@Override
public void close() 

Source Link

Document

Close this application context, destroying all beans in its bean factory.

Usage

From source file:org.springframework.integration.core.MessageIdGenerationTests.java

@Test
public void testCustomIdGenerationWithChildRegistrarClosed() throws Exception {
    ClassPathXmlApplicationContext parent = new ClassPathXmlApplicationContext(
            "MessageIdGenerationTests-context.xml", this.getClass());
    ClassPathXmlApplicationContext child = new ClassPathXmlApplicationContext(
            new String[] { "MessageIdGenerationTests-context-withGenerator.xml" }, this.getClass(), parent);

    IdGenerator idGenerator = child.getBean("idGenerator", IdGenerator.class);
    Mockito.reset(idGenerator);/*from  w w w .jav a 2  s.c om*/
    MessageChannel inputChannel = child.getBean("input", MessageChannel.class);
    inputChannel.send(new GenericMessage<Integer>(0));
    verify(idGenerator, atLeastOnce()).generateId();
    child.close();
    parent.close();
    this.assertDestroy();
}

From source file:org.springframework.integration.core.MessageIdGenerationTests.java

@Test
public void testCustomIdGenerationWithParentChildIndependentCreationChildrenRegistrarsOneAtTheTime()
        throws Exception {
    ClassPathXmlApplicationContext parent = new ClassPathXmlApplicationContext(
            "MessageIdGenerationTests-context.xml", this.getClass());

    GenericXmlApplicationContext childA = new GenericXmlApplicationContext();
    childA.load(/*from   w w  w .j  ava  2 s . c  o  m*/
            "classpath:/org/springframework/integration/core/MessageIdGenerationTests-context-withGenerator.xml");
    childA.setParent(parent);
    childA.refresh();

    childA.close();

    GenericXmlApplicationContext childB = new GenericXmlApplicationContext();
    childB.load(
            "classpath:/org/springframework/integration/core/MessageIdGenerationTests-context-withGenerator.xml");
    childB.setParent(parent);
    childB.refresh();

    parent.close();
    childB.close();
    this.assertDestroy();
}

From source file:org.springframework.integration.jms.request_reply.RequestReplyScenariosWithTempReplyQueuesTests.java

@SuppressWarnings("resource")
@Test//from   ww  w  . java  2 s  .c om
public void messageCorrelationBasedOnRequestMessageId() throws Exception {
    ActiveMqTestUtils.prepare();

    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
            "producer-temp-reply-consumers.xml", this.getClass());
    RequestReplyExchanger gateway = context.getBean(RequestReplyExchanger.class);
    CachingConnectionFactory connectionFactory = context.getBean(CachingConnectionFactory.class);
    final JmsTemplate jmsTemplate = new JmsTemplate(connectionFactory);

    final Destination requestDestination = context.getBean("siOutQueue", Destination.class);

    new Thread(() -> {
        final Message requestMessage = jmsTemplate.receive(requestDestination);
        Destination replyTo = null;
        try {
            replyTo = requestMessage.getJMSReplyTo();
        } catch (Exception e) {
            fail();
        }
        jmsTemplate.send(replyTo, (MessageCreator) session -> {
            try {
                TextMessage message = session.createTextMessage();
                message.setText("bar");
                message.setJMSCorrelationID(requestMessage.getJMSMessageID());
                return message;
            } catch (Exception e) {
                // ignore
            }
            return null;
        });
    }).start();
    gateway.exchange(new GenericMessage<String>("foo"));
    context.close();
}

From source file:org.springframework.integration.jms.request_reply.RequestReplyScenariosWithTempReplyQueuesTests.java

@Test
public void messageCorrelationBasedOnRequestCorrelationIdTimedOutFirstReply() throws Exception {
    ActiveMqTestUtils.prepare();/*from  www  .  j a v  a2  s  .  co  m*/
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
            "producer-temp-reply-consumers.xml", this.getClass());
    RequestReplyExchanger gateway = context.getBean(RequestReplyExchanger.class);
    ConnectionFactory connectionFactory = context.getBean(ConnectionFactory.class);

    final Destination requestDestination = context.getBean("siOutQueue", Destination.class);

    DefaultMessageListenerContainer dmlc = new DefaultMessageListenerContainer();
    dmlc.setConnectionFactory(connectionFactory);
    dmlc.setDestination(requestDestination);
    dmlc.setMessageListener((SessionAwareMessageListener<Message>) (message, session) -> {
        Destination replyTo = null;
        try {
            replyTo = message.getJMSReplyTo();
        } catch (Exception e1) {
            fail();
        }
        String requestPayload = (String) extractPayload(message);
        if (requestPayload.equals("foo")) {
            try {
                Thread.sleep(6000);
            } catch (Exception e2) {
                /*ignore*/ }
        }
        try {
            TextMessage replyMessage = session.createTextMessage();
            replyMessage.setText(requestPayload);
            replyMessage.setJMSCorrelationID(message.getJMSMessageID());
            MessageProducer producer = session.createProducer(replyTo);
            producer.send(replyMessage);
        } catch (Exception e3) {
            // ignore. the test will fail
        }
    });
    dmlc.afterPropertiesSet();
    dmlc.start();

    try {
        gateway.exchange(new GenericMessage<String>("foo"));
    } catch (Exception e) {
        // ignore
    }
    Thread.sleep(1000);
    try {
        assertEquals("bar", gateway.exchange(new GenericMessage<String>("bar")).getPayload());
    } catch (Exception e) {
        e.printStackTrace();
        fail();
    }
    context.close();
}

From source file:org.springframework.integration.jms.request_reply.RequestReplyScenariosWithTempReplyQueuesTests.java

/**
 * Validates that JOG will recreate a temporary queue
 * once a failure detected and that the messages will still be properly correlated
 *//*from   w  w w .ja  va 2 s . c  o  m*/
@Test
public void brokenBrokerTest() throws Exception {

    BrokerService broker = new BrokerService();
    broker.setPersistent(false);
    broker.setUseJmx(false);
    broker.setTransportConnectorURIs(new String[] { "tcp://localhost:61623" });
    broker.setDeleteAllMessagesOnStartup(true);
    broker.start();

    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("broken-broker.xml",
            this.getClass());

    final RequestReplyExchanger gateway = context.getBean(RequestReplyExchanger.class);

    int replyCounter = 0;
    int timeoutCounter = 0;
    for (int i = 0; i < 50; i++) {
        try {
            assertEquals(i + "", gateway.exchange(new GenericMessage<String>(String.valueOf(i))).getPayload());
            replyCounter++;
        } catch (Exception e) {
            timeoutCounter++;
        }
        if (i == 0 || i == 20 || i == 40) {
            Object replyDestination = TestUtils.getPropertyValue(context.getBean("jog"),
                    "handler.replyDestination");
            if (replyDestination != null) {
                broker.removeDestination((ActiveMQDestination) replyDestination);
            }
        }
    }
    assertEquals(50, replyCounter + timeoutCounter);
    context.close();
}

From source file:org.springframework.integration.jms.request_reply.RequestReplyScenariosWithTempReplyQueuesTests.java

@Test
public void testConcurrently() throws Exception {
    ActiveMqTestUtils.prepare();//from w  ww  .  j av  a2  s  .c  o  m
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
            "mult-producer-and-consumers-temp-reply.xml", this.getClass());
    final RequestReplyExchanger gateway = context.getBean(RequestReplyExchanger.class);
    Executor executor = Executors.newFixedThreadPool(10);
    final int testNumbers = 100;
    final CountDownLatch latch = new CountDownLatch(testNumbers);
    final AtomicInteger failures = new AtomicInteger();
    final AtomicInteger timeouts = new AtomicInteger();
    final AtomicInteger missmatches = new AtomicInteger();
    for (int i = 0; i < testNumbers; i++) {
        final int y = i;
        executor.execute(() -> {
            try {

                String reply = (String) gateway.exchange(new GenericMessage<String>(String.valueOf(y)))
                        .getPayload();
                if (!String.valueOf(y).equals(reply)) {
                    missmatches.incrementAndGet();
                }
            } catch (Exception e) {
                if (e instanceof MessageDeliveryException) {
                    timeouts.incrementAndGet();
                } else {
                    failures.incrementAndGet();
                }
            }
            //               if (latch.getCount()%100 == 0){
            //                  long count = testNumbers-latch.getCount();
            //                  if (count > 0){
            //                     print(failures, timeouts, missmatches, testNumbers-latch.getCount());
            //                  }
            //               }
            latch.countDown();
        });
    }
    latch.await();
    print(failures, timeouts, missmatches, testNumbers);
    Thread.sleep(5000);
    assertEquals(0, missmatches.get());
    assertEquals(0, failures.get());
    assertEquals(0, timeouts.get());
    context.close();
}

From source file:org.springframework.integration.monitor.HandlerMonitoringIntegrationTests.java

@Test
public void testErrorLogger() throws Exception {

    ClassPathXmlApplicationContext context = createContext("anonymous-handler.xml", "anonymous");
    try {//w w w .j  a v a 2 s . c o  m
        assertTrue(Arrays.asList(messageHandlersMonitor.getHandlerNames()).contains("errorLogger"));
    } finally {
        context.close();
    }

}

From source file:org.springframework.integration.monitor.HandlerMonitoringIntegrationTests.java

private void doTest(String config, String channelName, String monitor) throws Exception {

    ClassPathXmlApplicationContext context = createContext(config, channelName);

    try {/*from   w w w.  j a v a  2 s . co m*/

        int before = service.getCounter();
        channel.send(new GenericMessage<String>("bar"));
        assertEquals(before + 1, service.getCounter());

        int count = messageHandlersMonitor.getHandlerDuration(monitor).getCount();
        assertTrue("No statistics for input channel", count > 0);

    } finally {
        context.close();
    }

}

From source file:org.springframework.integration.monitor.MessageChannelsMonitorIntegrationTests.java

@Test
public void testRates() throws Exception {

    ClassPathXmlApplicationContext context = createContext("anonymous-channel.xml", "anonymous");

    try {/*from  w  ww  .  j  a  v  a2s.c  o  m*/

        int before = service.getCounter();
        for (int i = 0; i < 50; i++) {
            channel.send(new GenericMessage<String>("bar"));
            Thread.sleep(20L);
        }
        assertEquals(before + 50, service.getCounter());

        // The handler monitor is registered under the endpoint id (since it is explicit)
        int sends = messageChannelsMonitor.getChannelSendRate("" + channel).getCount();
        assertEquals("No send statistics for input channel", 50, sends, 0.01);

    } finally {
        context.close();
    }

}

From source file:org.springframework.integration.monitor.MessageChannelsMonitorIntegrationTests.java

@Test
public void testErrors() throws Exception {

    ClassPathXmlApplicationContext context = createContext("anonymous-channel.xml", "anonymous");
    try {/*from   w  w  w.  jav a2s .c o  m*/
        int before = service.getCounter();
        for (int i = 0; i < 5; i++) {
            channel.send(new GenericMessage<String>("bar"));
            Thread.sleep(20L);
        }
        try {
            channel.send(new GenericMessage<String>("fail"));
        } catch (MessageHandlingException e) {
            // ignore
        }
        for (int i = 0; i < 5; i++) {
            channel.send(new GenericMessage<String>("bar"));
            Thread.sleep(20L);
        }
        assertEquals(before + 10, service.getCounter());

        // The handler monitor is registered under the endpoint id (since it is explicit)
        int sends = messageChannelsMonitor.getChannelSendRate("" + channel).getCount();
        assertEquals("No send statistics for input channel", 11, sends, 0.01);
        int errors = messageChannelsMonitor.getChannelErrorRate("" + channel).getCount();
        assertEquals("No error statistics for input channel", 1, errors, 0.01);
    } finally {
        context.close();
    }

}