Page 1 of 3 123 LastLast
Results 1 to 10 of 21

Thread: Error Handling

  1. #1
    Join Date
    Aug 2006
    Posts
    236

    Default Error Handling

    Hi

    I am currently trying to work on an error handling mechanism using Spring Integration. I have defined the following error handling and I was wondering if I could get some input on whether it is the correct way of doing it. Essentially when an error occurs I would like to send an email. Any help would be appreciated:

    Code:
     	
     	<channel id="errorChannel">
         <queue capacity="500"/>
     	</channel>
    	
    	<beans:bean id="errorTransformer" class="com.amin.app.index.integration.transformer.ErrorTransformer"/>
    	
    	<chain input-channel="errorChannel">
    		<transformer ref="errorTransformer" method="transformError" />
    		<service-activator ref="errorHandler" method="sendEmail" />
    	</chain>
    	
    	<beans:bean id="errorHandler" class="com.amin.app.index.integration.error.handling.ErrorHandler">
    		<beans:constructor-arg ref="mailSender" />
    	</beans:bean>
     	
    	<!-- Mail sending error -->
    
    	<beans:bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
      		<beans:property name="host" value="localhost" />
      		<beans:property name="port" value="22" />
    	</beans:bean>
    Also I was wondering whether I should use the mail support from Spring Integration rather than creating a service activator. I wasn't sure how to configure the error handling with the mail support (I was using the sample from the distribution).
    Last edited by amin; Aug 4th, 2009 at 04:14 PM.

  2. #2
    Join Date
    Oct 2005
    Location
    Boston, MA
    Posts
    2,840

    Default

    I think what you're doing makes sense. You might consider using a synchronous hand-off for the errorChannel (i.e. no queue) but there's a tradeoff in that case: the original thread that created the error will block for the mail-sending, but less likely to result in lost error messages.

    You could use the outbound-channel-adapter in the Spring Integration mail module. It basically expects a MailMessage or String as its payload. In the simplest case, you can just pass a String and then add some headers as necessary with the mail namespace's 'header-enricher' element.

  3. #3
    Join Date
    Aug 2006
    Posts
    236

    Default

    Thanks Mark for your response. I have updated accordingly and have the following (which works):
    Code:
    <channel id="errorChannel" />
     	
     	<channel id="errorOutChannel" />
    	
    	<beans:bean id="errorTransformer" class="com.amin.app.index.integration.transformer.ErrorTransformer"/>
    	
    	<chain input-channel="errorChannel" output-channel="errorOutChannel">
    		<transformer ref="errorTransformer" method="transformError" />
    	</chain>
    	
    	<!-- Mail sending error -->
    	<mail:outbound-channel-adapter channel="errorOutChannel" mail-sender="mailSender" />
    Cheers

  4. #4
    Join Date
    Aug 2006
    Posts
    236

    Default

    Hi again

    I've been looking back at what i've done and decided to make some improvements to the error mail handling. I decided to add a header-enricher rather than adding to the mail creation class. However the error handling does not seem to work. I was wondering if someone could tell me whether I am on the right path:

    Code:
    <chain input-channel="errorChannel" output-channel="errorOutChannel">
    		<transformer ref="errorTransformer" method="transformError" />
            <mail:header-enricher from="${mailFrom}" to="{mailTo}" subject="${mail.subject}" overwrite="false" />
    
    	</chain>
    Should it be:

    Code:
    <channel id="errorChannel" />
     	
     	<channel id="errorOutChannel" />
    
        <channel id="errorMailOutChannel" />
    	
    	<beans:bean id="errorTransformer" class="com.amin.luceneindexsi.index.integration.transformer.ErrorTransformer" />
            
    	
    	<chain input-channel="errorChannel" output-channel="errorOutChannel">
    		<transformer ref="errorTransformer" method="transformError" />
    
    	</chain>
    
         <mail:header-enricher from="${mailFrom}" to="{mailTo}" subject="${mail.subject}" overwrite="false" input-channel="errorOutChannel" output-channel="errorMailOutChannel"/>
    
    	<!-- Mail sending error -->
    	<mail:outbound-channel-adapter channel="errorMailOutChannel" mail-sender="mailSender" />
    Any help would be appreciated.

    Cheers

  5. #5
    Join Date
    Oct 2005
    Location
    Boston, MA
    Posts
    2,840

    Default

    If you copied this directly, it looks like you are missing the $ for your "mailTo" value.

  6. #6
    Join Date
    Aug 2006
    Posts
    236

    Default

    Hi Mark

    oh boy so sorry for posting that mistake! Thanks for pointing out user error.

    Cheers

    amin

  7. #7
    Join Date
    Oct 2005
    Location
    Boston, MA
    Posts
    2,840

    Default

    No problem. If that's all that was causing you trouble, then I'm happy.

    Cheers,
    Mark

  8. #8
    Join Date
    Aug 2006
    Posts
    236

    Default

    Hi Mark

    Unfortunately it is still not working. I have done:

    Code:
    <chain input-channel="errorChannel" output-channel="errorOutChannel">
    		<transformer ref="errorTransformer" method="transformError" />
    
         </chain>
    
           <mail:header-enricher from="${mailFrom}" to="${mailTo}" subject="${mail.subject}" overwrite="false" input-channel="errorOutChannel" output-channel="errorMailOutChannel"/>
    	<!-- Mail sending error -->
    	<mail:outbound-channel-adapter channel="errorMailOutChannel" mail-sender="mailSender" />
    and tried

    Code:
    	<beans:bean id="errorTransformer" class="com.amin.luceneindexsi.index.integration.transformer.ErrorTransformer" />
            
    	
    	<chain input-channel="errorChannel" output-channel="errorOutChannel">
    		<transformer ref="errorTransformer" method="transformError" />
            <mail:header-enricher from="${mailFrom}" to="${mailTo}" subject="${mail.subject}" overwrite="false"/>
         </chain>
    But both do not work. I'm not sure which one is the best approach to be honest. My error to mail transformer has the following:

    Code:
    public class ErrorTransformer {
    	
    	public MailMessage transformError(ErrorMessage errorMessage) {
    		Message<?> failedMessage = ((MessagingException) errorMessage.getPayload()).getFailedMessage();
    		Throwable cause = ((MessagingException) errorMessage.getPayload()).getCause();
    		IndexingException indexingException = new IndexingException(cause,failedMessage.getPayload());
    		SimpleMailMessage mailMessage = new SimpleMailMessage();
    		message.append("An exception occured while performing an index operation").append(", the following provides more details:\n");
    		message.append("Exception message:\n").append(indexingException.getExceptionThrown().getMessage() + "\n");
    		message.append("\n\n");
    		message.append("Exception occured:\n");
    		message.append(indexingException.getExceptionThrown().getStackTrace()[0].toString());
    		message.append("\n\n");
    		message.append("Message failed:\n").append(indexingException.getPayLoad().toString());
    		mailMessage.setText(message.toString());
    		return mailMessage;
    	}
    I presume the enricher should add the to and from settings to the mailMessage.

    The process only works when I add the "to" and "from" the mail message itself.

    Any help would be appreciated.

    Cheers

  9. #9
    Join Date
    Oct 2005
    Location
    Boston, MA
    Posts
    2,840

    Default

    Where are you configuring the mailFrom and mailTo properties? I assume you have a property-placeholder-configurer element as well?

  10. #10
    Join Date
    Aug 2006
    Posts
    236

    Default

    Yep. I even tried hard coding the values as well. The properties file is in the classpath as well. The only time it works is when I had the "to" and "from" in the mail message in the transformer and remove the mail enricher from the config.

    Cheers
    Amin

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •