Search in sources :

Example 61 with MessageHandlingException

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

the class InboundOneWayErrorTests method errorChannel.

@Test
public void errorChannel() throws Exception {
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("InboundOneWayErrorTests-context.xml", getClass());
    JmsTemplate jmsTemplate = new JmsTemplate(context.getBean("jmsConnectionFactory", ConnectionFactory.class));
    Destination queue = context.getBean("queueB", Destination.class);
    jmsTemplate.send(queue, (MessageCreator) session -> session.createTextMessage("test-B"));
    PollableChannel errorChannel = context.getBean("testErrorChannel", PollableChannel.class);
    Message<?> errorMessage = errorChannel.receive(3000);
    assertNotNull(errorMessage);
    assertEquals(MessageHandlingException.class, errorMessage.getPayload().getClass());
    MessageHandlingException exception = (MessageHandlingException) errorMessage.getPayload();
    assertNotNull(exception.getCause());
    assertEquals(TestException.class, exception.getCause().getClass());
    assertEquals("failed to process: test-B", exception.getCause().getMessage());
    TestErrorHandler errorHandler = context.getBean("testErrorHandler", TestErrorHandler.class);
    assertNull(errorHandler.lastError);
    context.close();
}
Also used : Assert.assertNotNull(org.junit.Assert.assertNotNull) Test(org.junit.Test) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) ErrorHandler(org.springframework.util.ErrorHandler) MessageCreator(org.springframework.jms.core.MessageCreator) Assert.assertNull(org.junit.Assert.assertNull) Destination(javax.jms.Destination) MessageHandlingException(org.springframework.messaging.MessageHandlingException) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) JmsTemplate(org.springframework.jms.core.JmsTemplate) Message(org.springframework.messaging.Message) PollableChannel(org.springframework.messaging.PollableChannel) Assert.assertEquals(org.junit.Assert.assertEquals) ConnectionFactory(javax.jms.ConnectionFactory) Destination(javax.jms.Destination) ConnectionFactory(javax.jms.ConnectionFactory) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) PollableChannel(org.springframework.messaging.PollableChannel) JmsTemplate(org.springframework.jms.core.JmsTemplate) MessageHandlingException(org.springframework.messaging.MessageHandlingException) Test(org.junit.Test)

Example 62 with MessageHandlingException

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

the class JmsOutboundGateway method handleRequestMessage.

@Override
protected Object handleRequestMessage(final Message<?> requestMessage) {
    if (!this.initialized) {
        afterPropertiesSet();
    }
    try {
        Object reply;
        if (this.replyContainer == null) {
            reply = sendAndReceiveWithoutContainer(requestMessage);
        } else {
            if (this.idleReplyContainerTimeout > 0) {
                synchronized (this.lifeCycleMonitor) {
                    this.lastSend = System.currentTimeMillis();
                    if (!this.replyContainer.isRunning()) {
                        if (logger.isDebugEnabled()) {
                            logger.debug(this.getComponentName() + ": Starting reply container.");
                        }
                        this.replyContainer.start();
                        this.idleTask = getTaskScheduler().scheduleAtFixedRate(new IdleContainerStopper(), this.idleReplyContainerTimeout / 2);
                    }
                }
            }
            reply = this.sendAndReceiveWithContainer(requestMessage);
        }
        if (reply == null) {
            if (this.requiresReply) {
                throw new MessageTimeoutException(requestMessage, "failed to receive JMS response within timeout of: " + this.receiveTimeout + "ms");
            } else {
                return null;
            }
        }
        if (reply instanceof javax.jms.Message) {
            return buildReply((javax.jms.Message) reply);
        } else {
            return reply;
        }
    } catch (JMSException e) {
        throw new MessageHandlingException(requestMessage, e);
    }
}
Also used : Message(org.springframework.messaging.Message) JMSException(javax.jms.JMSException) MessageTimeoutException(org.springframework.integration.MessageTimeoutException) MessageHandlingException(org.springframework.messaging.MessageHandlingException)

Example 63 with MessageHandlingException

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

the class StatusUpdatingMessageHandler method handleMessageInternal.

@Override
protected void handleMessageInternal(Message<?> message) throws Exception {
    Object value;
    if (this.tweetDataExpression != null) {
        value = this.tweetDataExpression.getValue(this.evaluationContext, message);
    } else {
        value = message.getPayload();
    }
    Assert.notNull(value, "The tweetData cannot evaluate to 'null'.");
    TweetData tweetData = null;
    if (value instanceof TweetData) {
        tweetData = (TweetData) value;
    } else if (value instanceof Tweet) {
        tweetData = new TweetData(((Tweet) value).getText());
    } else if (value instanceof String) {
        tweetData = new TweetData((String) value);
    } else {
        throw new MessageHandlingException(message, "Unsupported tweetData: " + value);
    }
    this.twitter.timelineOperations().updateStatus(tweetData);
}
Also used : Tweet(org.springframework.social.twitter.api.Tweet) TweetData(org.springframework.social.twitter.api.TweetData) MessageHandlingException(org.springframework.messaging.MessageHandlingException)

Example 64 with MessageHandlingException

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

the class AbstractScriptExecutingMessageProcessor method processMessage.

/**
 * Executes the script and returns the result.
 */
@Override
public final T processMessage(Message<?> message) {
    try {
        ScriptSource source = this.getScriptSource(message);
        Map<String, Object> variables = this.scriptVariableGenerator.generateScriptVariables(message);
        return this.executeScript(source, variables);
    } catch (Exception e) {
        throw new MessageHandlingException(message, "failed to execute script", e);
    }
}
Also used : ScriptSource(org.springframework.scripting.ScriptSource) MessageHandlingException(org.springframework.messaging.MessageHandlingException) BeansException(org.springframework.beans.BeansException) MessageHandlingException(org.springframework.messaging.MessageHandlingException)

Example 65 with MessageHandlingException

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

the class S3MessageHandler method upload.

private Transfer upload(Message<?> requestMessage) {
    Object payload = requestMessage.getPayload();
    String bucketName = obtainBucket(requestMessage);
    String key = null;
    if (this.keyExpression != null) {
        key = this.keyExpression.getValue(this.evaluationContext, requestMessage, String.class);
    }
    if (payload instanceof File && ((File) payload).isDirectory()) {
        File fileToUpload = (File) payload;
        if (key == null) {
            key = fileToUpload.getName();
        }
        return this.transferManager.uploadDirectory(bucketName, key, fileToUpload, true, new MessageHeadersObjectMetadataProvider(requestMessage.getHeaders()));
    } else {
        ObjectMetadata metadata = new ObjectMetadata();
        if (this.uploadMetadataProvider != null) {
            this.uploadMetadataProvider.populateMetadata(metadata, requestMessage);
        }
        PutObjectRequest putObjectRequest = null;
        try {
            if (payload instanceof InputStream) {
                InputStream inputStream = (InputStream) payload;
                if (metadata.getContentMD5() == null) {
                    Assert.state(inputStream.markSupported(), "For an upload InputStream with no MD5 digest metadata, the " + "markSupported() method must evaluate to true. ");
                    String contentMd5 = Md5Utils.md5AsBase64(inputStream);
                    metadata.setContentMD5(contentMd5);
                    inputStream.reset();
                }
                putObjectRequest = new PutObjectRequest(bucketName, key, inputStream, metadata);
            } else if (payload instanceof File) {
                File fileToUpload = (File) payload;
                if (key == null) {
                    key = fileToUpload.getName();
                }
                if (metadata.getContentMD5() == null) {
                    String contentMd5 = Md5Utils.md5AsBase64(fileToUpload);
                    metadata.setContentMD5(contentMd5);
                }
                if (metadata.getContentLength() == 0) {
                    metadata.setContentLength(fileToUpload.length());
                }
                if (metadata.getContentType() == null) {
                    metadata.setContentType(Mimetypes.getInstance().getMimetype(fileToUpload));
                }
                putObjectRequest = new PutObjectRequest(bucketName, key, fileToUpload).withMetadata(metadata);
            } else if (payload instanceof byte[]) {
                byte[] payloadBytes = (byte[]) payload;
                InputStream inputStream = new ByteArrayInputStream(payloadBytes);
                if (metadata.getContentMD5() == null) {
                    String contentMd5 = Md5Utils.md5AsBase64(inputStream);
                    metadata.setContentMD5(contentMd5);
                    inputStream.reset();
                }
                if (metadata.getContentLength() == 0) {
                    metadata.setContentLength(payloadBytes.length);
                }
                putObjectRequest = new PutObjectRequest(bucketName, key, inputStream, metadata);
            } else {
                throw new IllegalArgumentException("Unsupported payload type: [" + payload.getClass() + "]. The only supported payloads for the upload request are " + "java.io.File, java.io.InputStream, byte[] and PutObjectRequest.");
            }
        } catch (IOException e) {
            throw new MessageHandlingException(requestMessage, e);
        }
        if (key == null) {
            if (this.keyExpression != null) {
                throw new IllegalStateException("The 'keyExpression' [" + this.keyExpression.getExpressionString() + "] must not evaluate to null. Root object is: " + requestMessage);
            } else {
                throw new IllegalStateException("Specify a 'keyExpression' for non-java.io.File payloads");
            }
        }
        S3ProgressListener progressListener = this.s3ProgressListener;
        if (this.objectAclExpression != null) {
            Object acl = this.objectAclExpression.getValue(this.evaluationContext, requestMessage);
            Assert.state(acl instanceof AccessControlList || acl instanceof CannedAccessControlList, "The 'objectAclExpression' [" + this.objectAclExpression.getExpressionString() + "] must evaluate to com.amazonaws.services.s3.model.AccessControlList " + "or must evaluate to com.amazonaws.services.s3.model.CannedAccessControlList. " + "Gotten: [" + acl + "]");
            SetObjectAclRequest aclRequest;
            if (acl instanceof AccessControlList) {
                aclRequest = new SetObjectAclRequest(bucketName, key, (AccessControlList) acl);
            } else {
                aclRequest = new SetObjectAclRequest(bucketName, key, (CannedAccessControlList) acl);
            }
            final SetObjectAclRequest theAclRequest = aclRequest;
            progressListener = new S3ProgressListener() {

                @Override
                public void onPersistableTransfer(PersistableTransfer persistableTransfer) {
                }

                @Override
                public void progressChanged(ProgressEvent progressEvent) {
                    if (ProgressEventType.TRANSFER_COMPLETED_EVENT.equals(progressEvent.getEventType())) {
                        S3MessageHandler.this.transferManager.getAmazonS3Client().setObjectAcl(theAclRequest);
                    }
                }
            };
            if (this.s3ProgressListener != null) {
                progressListener = new S3ProgressListenerChain(this.s3ProgressListener, progressListener);
            }
        }
        if (progressListener != null) {
            return this.transferManager.upload(putObjectRequest, progressListener);
        } else {
            return this.transferManager.upload(putObjectRequest);
        }
    }
}
Also used : CannedAccessControlList(com.amazonaws.services.s3.model.CannedAccessControlList) AccessControlList(com.amazonaws.services.s3.model.AccessControlList) SetObjectAclRequest(com.amazonaws.services.s3.model.SetObjectAclRequest) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) CannedAccessControlList(com.amazonaws.services.s3.model.CannedAccessControlList) ProgressEvent(com.amazonaws.event.ProgressEvent) MessageHandlingException(org.springframework.messaging.MessageHandlingException) S3ProgressListenerChain(com.amazonaws.services.s3.transfer.internal.S3ProgressListenerChain) ByteArrayInputStream(java.io.ByteArrayInputStream) S3ProgressListener(com.amazonaws.services.s3.transfer.internal.S3ProgressListener) PersistableTransfer(com.amazonaws.services.s3.transfer.PersistableTransfer) File(java.io.File) ObjectMetadata(com.amazonaws.services.s3.model.ObjectMetadata) PutObjectRequest(com.amazonaws.services.s3.model.PutObjectRequest)

Aggregations

MessageHandlingException (org.springframework.messaging.MessageHandlingException)65 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 File (java.io.File)9 MessagingException (org.springframework.messaging.MessagingException)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