Announcement Announcement Module
Collapse
No announcement yet.
Error Handling Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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, 04:14 PM.

  • #2
    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.

    Comment


    • #3
      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

      Comment


      • #4
        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

        Comment


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

          Comment


          • #6
            Hi Mark

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

            Cheers

            amin

            Comment


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

              Cheers,
              Mark

              Comment


              • #8
                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

                Comment


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

                  Comment


                  • #10
                    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

                    Comment


                    • #11
                      Okay, I finally had a chance to look at your example in more detail, and what is happening is the header values are not applied if you pass an actual MailMessage to the outbound adapter. In that case, it assumes you are in control (however, I can see how that could be viewed as a bug).

                      I noticed that you are only setting the text on that MailMessage anyways. So, can you try to just returning a String from your 'transformError' method? It should then apply the headers when it creates a new MailMessage in the outbound adapter.

                      By the way, I am not sure where your 'message' variable is actually defined (when you build the String), but it looks suspiciously like a thread-safety issue.

                      -Mark

                      Comment


                      • #12
                        Hi Mark

                        thanks for response. I can change the transform error to a string. There is a string builder that is a local variable I may have commented it out in the post. The only other thing I guess is do I put the mail enricher def inside the chain config or should it be outside?

                        Thanks

                        Comment


                        • #13
                          Hi Mark

                          I updated my code based on your recommendations and it works. I have added the enricher within the chain config:
                          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>
                          Is this ok?

                          Cheers
                          Amin

                          Comment


                          • #14
                            Cool. I'm glad you were able to get that working, and it looks good to me.

                            In fact, now that your transformer is returning a simple String, it should be easier for you to unit test that component.

                            I am still interested in your opinion on the applicability of those headers to a preexisting MailMessage object though. As I mentioned, our rationale was that an explicitly constructed MailMessage should have been configured completely. The headers only make sense for the non-MailMessage payloads that are passed in to the outbound adapter. In hindsight, does that make sense?

                            At the very least, I think we should clarify this in the JavaDoc as well as the reference manual. Could you please open a JIRA issue for that and link to this forum thread?

                            Thanks,
                            Mark

                            Comment


                            • #15
                              Hi Mark

                              I guess when I read the documentation I naturally thought if my transformer returned a MailMessage then the enricher would add the header to it. I would defintiely recommend adding something to the documentation (I could've missed it) or may be add a sample project to show it's use. From a user point of view I would like to have the mail header enricher be applied to the MailMessage as well as the string payload (maybe too much!). To me it made sense for it to be applied to the MailMessage to be honest.

                              Unit testing the errortransformer was still very easy whether i returned a string or the MailMessage. I can raise a JIRA for the docs to be updated.

                              Cheers
                              Amin

                              Comment

                              Working...
                              X