Search in sources :

Example 71 with MessagingException

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

the class ChannelPublishingJmsMessageListener method onMessage.

@Override
public void onMessage(javax.jms.Message jmsMessage, Session session) throws JMSException {
    Object result = jmsMessage;
    Message<?> requestMessage = null;
    boolean errors = false;
    try {
        if (this.extractRequestPayload) {
            result = this.messageConverter.fromMessage(jmsMessage);
            if (this.logger.isDebugEnabled()) {
                this.logger.debug("converted JMS Message [" + jmsMessage + "] to integration Message payload [" + result + "]");
            }
        }
        Map<String, Object> headers = this.headerMapper.toHeaders(jmsMessage);
        requestMessage = (result instanceof Message<?>) ? this.messageBuilderFactory.fromMessage((Message<?>) result).copyHeaders(headers).build() : this.messageBuilderFactory.withPayload(result).copyHeaders(headers).build();
    } catch (RuntimeException e) {
        MessageChannel errorChannel = this.gatewayDelegate.getErrorChannel();
        if (errorChannel == null) {
            throw e;
        }
        this.gatewayDelegate.getMessagingTemplate().send(errorChannel, this.gatewayDelegate.buildErrorMessage(new MessagingException("Inbound conversion failed for: " + jmsMessage, e)));
        errors = true;
    }
    if (!errors) {
        if (!this.expectReply) {
            this.gatewayDelegate.send(requestMessage);
        } else {
            Message<?> replyMessage = this.gatewayDelegate.sendAndReceiveMessage(requestMessage);
            if (replyMessage != null) {
                Destination destination = this.getReplyDestination(jmsMessage, session);
                if (this.logger.isDebugEnabled()) {
                    this.logger.debug("Reply destination: " + destination);
                }
                if (destination != null) {
                    // convert SI Message to JMS Message
                    Object replyResult = replyMessage;
                    if (this.extractReplyPayload) {
                        replyResult = replyMessage.getPayload();
                    }
                    try {
                        javax.jms.Message jmsReply = this.messageConverter.toMessage(replyResult, session);
                        // map SI Message Headers to JMS Message Properties/Headers
                        this.headerMapper.fromHeaders(replyMessage.getHeaders(), jmsReply);
                        this.copyCorrelationIdFromRequestToReply(jmsMessage, jmsReply);
                        this.sendReply(jmsReply, destination, session);
                    } catch (RuntimeException e) {
                        this.logger.error("Failed to generate JMS Reply Message from: " + replyResult, e);
                        throw e;
                    }
                }
            } else if (this.logger.isDebugEnabled()) {
                this.logger.debug("expected a reply but none was received");
            }
        }
    }
}
Also used : Destination(javax.jms.Destination) MessageChannel(org.springframework.messaging.MessageChannel) MessagingException(org.springframework.messaging.MessagingException)

Example 72 with MessagingException

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

the class AttributePollingMessageSource method doReceive.

/**
 * Retrieves the JMX attribute value.
 */
@Override
protected Object doReceive() {
    Assert.notNull(this.server, "MBeanServer is required");
    Assert.notNull(this.objectName, "object name is required");
    Assert.notNull(this.attributeName, "attribute name is required");
    try {
        return this.server.getAttribute(this.objectName, this.attributeName);
    } catch (Exception e) {
        throw new MessagingException("failed to retrieve JMX attribute '" + this.attributeName + "' on MBean [" + this.objectName + "]", e);
    }
}
Also used : MessagingException(org.springframework.messaging.MessagingException) MalformedObjectNameException(javax.management.MalformedObjectNameException) MessagingException(org.springframework.messaging.MessagingException)

Example 73 with MessagingException

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

the class OperationInvokingMessageHandler method handleRequestMessage.

@Override
protected Object handleRequestMessage(Message<?> requestMessage) {
    ObjectName objectName = this.resolveObjectName(requestMessage);
    String operationName = this.resolveOperationName(requestMessage);
    Map<String, Object> paramsFromMessage = this.resolveParameters(requestMessage);
    try {
        MBeanInfo mbeanInfo = this.server.getMBeanInfo(objectName);
        MBeanOperationInfo[] opInfoArray = mbeanInfo.getOperations();
        boolean hasNoArgOption = false;
        for (MBeanOperationInfo opInfo : opInfoArray) {
            if (operationName.equals(opInfo.getName())) {
                MBeanParameterInfo[] paramInfoArray = opInfo.getSignature();
                if (paramInfoArray.length == 0) {
                    hasNoArgOption = true;
                }
                if (paramInfoArray.length == paramsFromMessage.size()) {
                    int index = 0;
                    Object[] values = new Object[paramInfoArray.length];
                    String[] signature = new String[paramInfoArray.length];
                    for (MBeanParameterInfo paramInfo : paramInfoArray) {
                        Object value = paramsFromMessage.get(paramInfo.getName());
                        if (value == null) {
                            /*
								 * With Spring 3.2.3 and greater, the parameter names are
								 * registered instead of the JVM's default p1, p2 etc.
								 * Fall back to that naming style if not found.
								 */
                            value = paramsFromMessage.get("p" + (index + 1));
                        }
                        if (value != null && valueTypeMatchesParameterType(value, paramInfo)) {
                            values[index] = value;
                            signature[index] = paramInfo.getType();
                            index++;
                        }
                    }
                    if (index == paramInfoArray.length) {
                        return this.server.invoke(objectName, operationName, values, signature);
                    }
                }
            }
        }
        if (hasNoArgOption) {
            return this.server.invoke(objectName, operationName, null, null);
        }
        throw new MessagingException(requestMessage, "failed to find JMX operation '" + operationName + "' on MBean [" + objectName + "] of type [" + mbeanInfo.getClassName() + "] with " + paramsFromMessage.size() + " parameters: " + paramsFromMessage);
    } catch (JMException e) {
        throw new MessageHandlingException(requestMessage, "failed to invoke JMX operation '" + operationName + "' on MBean [" + objectName + "]" + " with " + paramsFromMessage.size() + " parameters: " + paramsFromMessage, e);
    } catch (IOException e) {
        throw new MessageHandlingException(requestMessage, "IOException on MBeanServerConnection", e);
    }
}
Also used : MBeanInfo(javax.management.MBeanInfo) MessagingException(org.springframework.messaging.MessagingException) MBeanOperationInfo(javax.management.MBeanOperationInfo) IOException(java.io.IOException) MessageHandlingException(org.springframework.messaging.MessageHandlingException) ObjectName(javax.management.ObjectName) JMException(javax.management.JMException) MBeanParameterInfo(javax.management.MBeanParameterInfo)

Example 74 with MessagingException

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

the class PresenceListeningEndpointTests method testWithErrorChannel.

@Test
public void testWithErrorChannel() {
    DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
    XMPPConnection connection = mock(XMPPConnection.class);
    bf.registerSingleton(XmppContextUtils.XMPP_CONNECTION_BEAN_NAME, connection);
    PresenceListeningEndpoint endpoint = new PresenceListeningEndpoint();
    DirectChannel outChannel = new DirectChannel();
    outChannel.subscribe(message -> {
        throw new RuntimeException("ooops");
    });
    PollableChannel errorChannel = new QueueChannel();
    endpoint.setBeanFactory(bf);
    endpoint.setOutputChannel(outChannel);
    endpoint.setErrorChannel(errorChannel);
    endpoint.afterPropertiesSet();
    RosterListener listener = (RosterListener) TestUtils.getPropertyValue(endpoint, "rosterListener");
    Presence presence = new Presence(Type.available);
    listener.presenceChanged(presence);
    ErrorMessage msg = (ErrorMessage) errorChannel.receive();
    assertSame(presence, ((MessagingException) msg.getPayload()).getFailedMessage().getPayload());
}
Also used : QueueChannel(org.springframework.integration.channel.QueueChannel) DirectChannel(org.springframework.integration.channel.DirectChannel) MessagingException(org.springframework.messaging.MessagingException) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) PollableChannel(org.springframework.messaging.PollableChannel) Presence(org.jivesoftware.smack.packet.Presence) XMPPConnection(org.jivesoftware.smack.XMPPConnection) ErrorMessage(org.springframework.messaging.support.ErrorMessage) RosterListener(org.jivesoftware.smack.roster.RosterListener) Test(org.junit.Test)

Example 75 with MessagingException

use of org.springframework.messaging.MessagingException in project spring-cloud-stream by spring-cloud.

the class StreamListenerHandlerMethodTests method testMethodWithMultipleOutputParameters.

@Test
public void testMethodWithMultipleOutputParameters() throws Exception {
    ConfigurableApplicationContext context = SpringApplication.run(TestMethodWithMultipleOutputParameters.class, "--server.port=0", "--spring.jmx.enabled=false");
    Processor processor = context.getBean(Processor.class);
    StreamListenerTestUtils.FooOutboundChannel1 source2 = context.getBean(StreamListenerTestUtils.FooOutboundChannel1.class);
    final CountDownLatch latch = new CountDownLatch(2);
    ((SubscribableChannel) processor.output()).subscribe(new MessageHandler() {

        @Override
        public void handleMessage(Message<?> message) throws MessagingException {
            Assert.isTrue(message.getPayload().equals("testing"), "Assert failed");
            Assert.isTrue(message.getHeaders().get("output").equals("output2"), "Assert failed");
            latch.countDown();
        }
    });
    ((SubscribableChannel) source2.output()).subscribe(new MessageHandler() {

        @Override
        public void handleMessage(Message<?> message) throws MessagingException {
            Assert.isTrue(message.getPayload().equals("TESTING"), "Assert failed");
            Assert.isTrue(message.getHeaders().get("output").equals("output1"), "Assert failed");
            latch.countDown();
        }
    });
    processor.input().send(MessageBuilder.withPayload("testING").setHeader("output", "output1").build());
    processor.input().send(MessageBuilder.withPayload("TESTing").setHeader("output", "output2").build());
    assertThat(latch.await(1, TimeUnit.SECONDS));
    context.close();
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) Processor(org.springframework.cloud.stream.messaging.Processor) MessageHandler(org.springframework.messaging.MessageHandler) MessagingException(org.springframework.messaging.MessagingException) CountDownLatch(java.util.concurrent.CountDownLatch) SubscribableChannel(org.springframework.messaging.SubscribableChannel) Test(org.junit.Test)

Aggregations

MessagingException (org.springframework.messaging.MessagingException)143 Test (org.junit.Test)60 IOException (java.io.IOException)25 Message (org.springframework.messaging.Message)23 QueueChannel (org.springframework.integration.channel.QueueChannel)22 ErrorMessage (org.springframework.messaging.support.ErrorMessage)21 CountDownLatch (java.util.concurrent.CountDownLatch)17 BeanFactory (org.springframework.beans.factory.BeanFactory)15 MessageChannel (org.springframework.messaging.MessageChannel)15 MessageHandlingException (org.springframework.messaging.MessageHandlingException)15 GenericMessage (org.springframework.messaging.support.GenericMessage)15 MessageHandler (org.springframework.messaging.MessageHandler)14 File (java.io.File)12 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)12 Matchers.containsString (org.hamcrest.Matchers.containsString)12 DirectChannel (org.springframework.integration.channel.DirectChannel)12 ArrayList (java.util.ArrayList)11 PollableChannel (org.springframework.messaging.PollableChannel)11 Lock (java.util.concurrent.locks.Lock)8 MessageDeliveryException (org.springframework.messaging.MessageDeliveryException)8