Search in sources :

Example 16 with MessageHandlingException

use of org.springframework.messaging.MessageHandlingException in project spring-integration by spring-projects.

the class MessageChannelsMonitorIntegrationTests method testQueues.

@Test
public void testQueues() throws Exception {
    ClassPathXmlApplicationContext context = createContext("queue-channel.xml", "queue");
    try {
        int before = service.getCounter();
        CountDownLatch latch = new CountDownLatch(10);
        service.setLatch(latch);
        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);
        }
        assertTrue(latch.await(10, TimeUnit.SECONDS));
        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);
        int receives = messageChannelsMonitor.getChannelReceiveCount("" + channel);
        assertEquals("No send statistics for input channel", 11, receives);
        int errors = messageChannelsMonitor.getChannelErrorRate("" + channel).getCount();
        assertEquals("Expect no errors for input channel (handler fails)", 0, errors);
    } finally {
        context.close();
    }
}
Also used : ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) CountDownLatch(java.util.concurrent.CountDownLatch) MessageHandlingException(org.springframework.messaging.MessageHandlingException) Test(org.junit.Test)

Example 17 with MessageHandlingException

use of org.springframework.messaging.MessageHandlingException in project spring-integration by spring-projects.

the class MessageChannelsMonitorIntegrationTests method testErrors.

@Test
public void testErrors() throws Exception {
    ClassPathXmlApplicationContext context = createContext("anonymous-channel.xml", "anonymous");
    try {
        int before = service.getCounter();
        CountDownLatch latch = new CountDownLatch(10);
        service.setLatch(latch);
        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);
        }
        assertTrue(latch.await(10, TimeUnit.SECONDS));
        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();
    }
}
Also used : ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) CountDownLatch(java.util.concurrent.CountDownLatch) MessageHandlingException(org.springframework.messaging.MessageHandlingException) Test(org.junit.Test)

Example 18 with MessageHandlingException

use of org.springframework.messaging.MessageHandlingException in project spring-integration by spring-projects.

the class XmlValidatingMessageSelector method accept.

@Override
public boolean accept(Message<?> message) {
    SAXParseException[] validationExceptions = null;
    try {
        validationExceptions = this.xmlValidator.validate(this.converter.convertToSource(message.getPayload()));
    } catch (Exception e) {
        throw new MessageHandlingException(message, e);
    }
    boolean validationSuccess = ObjectUtils.isEmpty(validationExceptions);
    if (!validationSuccess) {
        if (this.throwExceptionOnRejection) {
            throw new MessageRejectedException(message, "Message was rejected due to XML Validation errors", new AggregatedXmlMessageValidationException(Arrays.<Throwable>asList(validationExceptions)));
        }
        this.logger.debug("Message was rejected due to XML Validation errors");
    }
    return validationSuccess;
}
Also used : AggregatedXmlMessageValidationException(org.springframework.integration.xml.AggregatedXmlMessageValidationException) SAXParseException(org.xml.sax.SAXParseException) MessageRejectedException(org.springframework.integration.MessageRejectedException) IOException(java.io.IOException) AggregatedXmlMessageValidationException(org.springframework.integration.xml.AggregatedXmlMessageValidationException) SAXParseException(org.xml.sax.SAXParseException) MessageRejectedException(org.springframework.integration.MessageRejectedException) MessageHandlingException(org.springframework.messaging.MessageHandlingException) MessageHandlingException(org.springframework.messaging.MessageHandlingException)

Example 19 with MessageHandlingException

use of org.springframework.messaging.MessageHandlingException in project spring-integration by spring-projects.

the class XPathMessageSplitter method splitMessage.

@Override
protected Object splitMessage(Message<?> message) {
    try {
        Object payload = message.getPayload();
        Object result = null;
        if (payload instanceof Node) {
            result = splitNode((Node) payload);
        } else {
            Document document = this.xmlPayloadConverter.convertToDocument(payload);
            Assert.notNull(document, "unsupported payload type [" + payload.getClass().getName() + "]");
            result = splitDocument(document);
        }
        return result;
    } catch (ParserConfigurationException e) {
        throw new MessageConversionException(message, "failed to create DocumentBuilder", e);
    } catch (Exception e) {
        throw new MessageHandlingException(message, "failed to split Message payload", e);
    }
}
Also used : MessageConversionException(org.springframework.messaging.converter.MessageConversionException) Node(org.w3c.dom.Node) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) Document(org.w3c.dom.Document) XPathExpressionException(javax.xml.xpath.XPathExpressionException) TransformerException(javax.xml.transform.TransformerException) XPathException(org.springframework.xml.xpath.XPathException) MessageHandlingException(org.springframework.messaging.MessageHandlingException) MessageConversionException(org.springframework.messaging.converter.MessageConversionException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) MessageHandlingException(org.springframework.messaging.MessageHandlingException)

Example 20 with MessageHandlingException

use of org.springframework.messaging.MessageHandlingException in project spring-integration by spring-projects.

the class SimpleWebServiceOutboundGatewayTests method testDomPoxMessageFactory.

@Test
public void testDomPoxMessageFactory() throws Exception {
    String uri = "http://www.example.org";
    SimpleWebServiceOutboundGateway gateway = new SimpleWebServiceOutboundGateway(uri);
    gateway.setBeanFactory(mock(BeanFactory.class));
    final SettableListenableFuture<WebServiceMessage> requestFuture = new SettableListenableFuture<>();
    ClientInterceptorAdapter interceptorAdapter = new ClientInterceptorAdapter() {

        @Override
        public boolean handleRequest(MessageContext messageContext) throws WebServiceClientException {
            requestFuture.set(messageContext.getRequest());
            return super.handleRequest(messageContext);
        }
    };
    gateway.setInterceptors(interceptorAdapter);
    gateway.setMessageFactory(new DomPoxMessageFactory());
    gateway.afterPropertiesSet();
    String request = "<test>foo</test>";
    try {
        gateway.handleMessage(new GenericMessage<>(request));
        fail("Expected MessageHandlingException");
    } catch (MessageHandlingException e) {
    // expected
    }
    WebServiceMessage requestMessage = requestFuture.get(10, TimeUnit.SECONDS);
    assertNotNull(requestMessage);
    assertThat(requestMessage, instanceOf(PoxMessage.class));
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    StringResult stringResult = new StringResult();
    transformer.transform(requestMessage.getPayloadSource(), stringResult);
    assertEquals(request, stringResult.toString());
}
Also used : SettableListenableFuture(org.springframework.util.concurrent.SettableListenableFuture) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) DomPoxMessageFactory(org.springframework.ws.pox.dom.DomPoxMessageFactory) ClientInterceptorAdapter(org.springframework.ws.client.support.interceptor.ClientInterceptorAdapter) MessageHandlingException(org.springframework.messaging.MessageHandlingException) WebServiceMessage(org.springframework.ws.WebServiceMessage) BeanFactory(org.springframework.beans.factory.BeanFactory) MessageContext(org.springframework.ws.context.MessageContext) StringResult(org.springframework.xml.transform.StringResult) PoxMessage(org.springframework.ws.pox.PoxMessage) Test(org.junit.Test)

Aggregations

MessageHandlingException (org.springframework.messaging.MessageHandlingException)66 Test (org.junit.Test)34 ErrorMessage (org.springframework.messaging.support.ErrorMessage)14 IOException (java.io.IOException)12 GenericMessage (org.springframework.messaging.support.GenericMessage)12 Message (org.springframework.messaging.Message)10 MessagingException (org.springframework.messaging.MessagingException)10 File (java.io.File)9 Matchers.containsString (org.hamcrest.Matchers.containsString)6 BeanFactory (org.springframework.beans.factory.BeanFactory)6 FileNotFoundException (java.io.FileNotFoundException)4 ArrayList (java.util.ArrayList)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)4 MessageRejectedException (org.springframework.integration.MessageRejectedException)4 QueueChannel (org.springframework.integration.channel.QueueChannel)4 AbstractReplyProducingMessageHandler (org.springframework.integration.handler.AbstractReplyProducingMessageHandler)4 FileOutputStream (java.io.FileOutputStream)3 OutputStream (java.io.OutputStream)3 IntegrationMessageHeaderAccessor (org.springframework.integration.IntegrationMessageHeaderAccessor)3