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

  • emailHandlingService

    Hello,
    on pages 187-189 of "Spring Integration in Action", the authors give an example of an inbound-channel-adapter and a chain whose service activator is an email handling service, called "emailHandlingService". Unfortunately, they don't provide the service's program in the book's source code. Could someone please provide an example of such a service?

    Many thanks.

    Philroc

    <int-mail:inbound-channel-adapter id="mailAdapter"
    store-uri="pop3:..."
    java-mail-properties="javaMailProperties"
    channel="emails"
    auto-startup="true">
    <intoller max-messages-per-poll="1" fixed-rate="5000" />
    </int-mail:inbound-channel-adapter>

    <int:chain input-channel="emails" output-channel="handled-emails">
    <int-mail:mail-to-string-transformer />
    <int:transformer expression="payload.content"/>
    <int:service-activator ref="emailHandlingService"/>
    </int:chain>

  • #2
    That's just a hypothetical service that "does something" with the email. In the example on page 189, the emailHandlingService might have a method

    Code:
     public Foo handleEmail(String email) {...}
    In the example on page 190, the custom transformer might transform the 'javax.mail.Message' to a Bar object, in which case, the method might look like

    Code:
     public Foo handleTransformedEmail(Bar bar) {...}
    Bottom line is you can do whatever you want with the results of the transformation.

    You are not required to use the transformer either, the service method can simply take a 'javax.mail.Message' and act on it directly.

    The service method can even return 'void' if it's the end of the flow.
    Last edited by Gary Russell; Dec 2nd, 2012, 05:06 PM.

    Comment


    • #3
      Thanks for you reply Gary.

      Let's say that in my spring-integration-context-file.xml, I have the following code:

      <?xml version="1.0" encoding="UTF-8"?>
      <beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:int="http://www.springframework.org/schema/integration"
      xmlns:int-mail="http://www.springframework.org/schema/integration/mail"
      xmlns:util="http://www.springframework.org/schema/util"
      xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schem...ring-beans.xsd
      http://www.springframework.org/schema/integration
      http://www.springframework.org/schem...ntegration.xsd
      http://www.springframework.org/schema/integration/mail
      http://www.springframework.org/schem...ation-mail.xsd
      http://www.springframework.org/schema/util
      http://www.springframework.org/schem...pring-util.xsd

      "
      >

      <bean id="emailHandlingService"
      class="com.abc.integrat2.EmailHandlingService" />

      <utilroperties id="javaMailProperties">
      <prop key="mail.store.protocol">pop3</prop>
      <prop key="mail.debug">true</prop>
      </utilroperties>

      <!-- See http://static.springsource.org/sprin...mlsingle/#mail -->

      <int-mail:inbound-channel-adapter id="mailAdapter"
      store-uri="pop3://.../INBOX"
      java-mail-properties="javaMailProperties"
      channel="emails"
      auto-startup="true">
      <intoller max-messages-per-poll="1" fixed-rate="5000" />
      </int-mail:inbound-channel-adapter>

      <int:chain input-channel="emails" output-channel="handled-emails">
      <int-mail:mail-to-string-transformer />
      <int:transformer expression="payload.content"/>
      <int:service-activator ref="emailHandlingService" method="sayHello"/>
      </int:chain>

      </beans>

      and that my Service class looks like this:

      package com.abc.integrat2;

      import java.util.Map;

      import org.springframework.integration.Message;
      import org.springframework.integration.support.MessageBui lder;
      import org.springframework.stereotype.Component;

      @Component
      public class EmailHandlingService {

      public Message<String> sayHello( Message<String> inboundMessage) {
      Map<String,Object> headers = inboundMessage.getHeaders();
      String payload = inboundMessage.getPayload();
      System.out.println(
      "In the sayHello method, printing out the "
      + "payload of the inbound message: "
      + payload
      + ". Also, there are "
      + headers.size() + " headers.");
      return MessageBuilder.withPayload(inboundMessage.getPaylo ad())
      .copyHeadersIfAbsent(inboundMessage.getHeaders()). build();
      }

      }

      Do I need to write a Main class to run this Spring Integration application? If so, what should I put in it?

      Many thanks.

      Philroc

      Comment


      • #4
        Please use [ code ] ... [ /code ] tags around code/config (no spaces in brackets).

        You would use something like...

        Code:
        public static void main(String[] args) throws Exception {
        
            ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-integration-context-file.xml", this.getClass());
            System.out.println("Hit enter to terminate");
            System.in.read();
            ctx.destroy();
            System.exit(0);
        }
        There are lots of sample apps here...

        https://github.com/SpringSource/spri...ration-samples

        Comment

        Working...
        X