Announcement Announcement Module
Collapse
No announcement yet.
Twitter example with mail oubound channel Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Twitter example with mail oubound channel

    Hi,

    I took the twitter integration example from the spring integration project template and just added an outbound mail channel to achieve that the TwitterMessages are sent via email.

    Code:
    		<int-mail:outbound-channel-adapter id="mailChannel"	host="localhost"  />
    
        <!-- See also:
            http://static.springsource.org/spring-integration/reference/htmlsingle/#twitter -->
        <int-twitter:search-inbound-channel-adapter id="twitter" query="#springsource"
                                                    channel="twitterChannel" auto-startup="true">
            <int:poller fixed-rate="5000" max-messages-per-poll="10" />
        </int-twitter:search-inbound-channel-adapter>
    
        <!-- See also:
            http://static.springsource.org/spring-integration/reference/htmlsingle/#service-activator
            http://www.eaipatterns.com/MessagingAdapter.html -->
        <int:service-activator input-channel="twitterChannel" ref="twitterService" method="addTwitterMessages"
        output-channel="mailChannel" />
    
        <bean id="twitterService" class="com.labbyon.integration.service.impl.DefaultTwitterService"/>
    
        <int:logging-channel-adapter id="logger" level="INFO"
                                     expression="'Id:' + payload.id + '; Date:' + payload.createdAt + '; FromUser: ' + payload.fromUser" />
    
        <!-- See also:
            http://static.springsource.org/spring-integration/reference/htmlsingle/#channel
            http://www.eaipatterns.com/MessageChannel.html -->
        <int:channel id="twitterChannel">
            <int:interceptors>
                 <int:wire-tap channel="logger" />
           </int:interceptors>
        </int:channel>
    
        <int:channel id="controlBusChannel"/>
    
        <!-- See also:
            http://static.springsource.org/spring-integration/reference/htmlsingle/#control-bus
            http://www.eaipatterns.com/ControlBus.html -->
        <int:control-bus input-channel="controlBusChannel"/>
    The problem is - no mail is sent. I think I have to configure a ToString transformer somehow but I do not know how.
    Tips are welcome

    Thank you

  • #2
    Hi!
    I think I have to configure a ToString transformer somehow but I do not know how.
    What do you do in the 'twitterService'?
    <int-twitter:search-inbound-channel-adapter> produces Messages with org.springframework.social.twitter.api.Tweet as payload.
    I think there is no any problem to convert this Simple POJO to any mail-appropriate type: MimeMessage, String, byte[]

    Take care,
    Artem

    Comment


    • #3
      The TwitterService is the impl from the example. There is a getTwitterMessages method which is used by the HomeController.
      But I have no clue how to configure it that the messages are sent by mail...

      Code:
      /*
       * Copyright 2002-2010 the original author or authors
       *
       *     Licensed under the Apache License, Version 2.0 (the "License");
       *     you may not use this file except in compliance with the License.
       *     You may obtain a copy of the License at
       *
       *         http://www.apache.org/licenses/LICENSE-2.0
       *
       *     Unless required by applicable law or agreed to in writing, software
       *     distributed under the License is distributed on an "AS IS" BASIS,
       *     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
       *     See the License for the specific language governing permissions and
       *     limitations under the License.
       */
      package com.labbyon.integration.service.impl;
      
      import java.util.Collection;
      import java.util.LinkedHashMap;
      import java.util.Map;
      
      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.beans.factory.annotation.Qualifier;
      import org.springframework.integration.Message;
      import org.springframework.integration.channel.DirectChannel;
      import org.springframework.integration.core.MessagingTemplate;
      import org.springframework.integration.support.MessageBuilder;
      import org.springframework.integration.twitter.core.Tweet;
      import org.springframework.stereotype.Service;
      
      import com.labbyon.integration.model.TwitterMessage;
      import com.labbyon.integration.service.TwitterService;
      
      /**
       * Implementation of the TwitterService interface.
       */
      @Service
      public class DefaultTwitterService implements TwitterService {
      
          /** Holds a collection of polled Twitter messages */
          private Map<Long, TwitterMessage> twitterMessages;
      
          @Autowired
          @Qualifier("controlBusChannel")
          private DirectChannel channel;
      
          /**
           * Constructor that initializes the 'twitterMessages' Map as a simple LRU
           * cache. @See http://blogs.oracle.com/swinger/entry/collections_trick_i_lru_cache
           */
          public DefaultTwitterService() {
      
              twitterMessages = new LinkedHashMap<Long, TwitterMessage>( 10, 0.75f, true ) {
      
                  private static final long serialVersionUID = 1L;
      
                  protected boolean removeEldestEntry( java.util.Map.Entry<Long, TwitterMessage> entry ) {
                      return size() > 10;
                  }
      
              };
      
          }
      
          /** {@inheritDoc} */
          @Override
          public Collection<TwitterMessage> getTwitterMessages() {
              return twitterMessages.values();
          }
      
          /** {@inheritDoc} */
          @Override
          public void startTwitterAdapter() {
      
              final MessagingTemplate m = new MessagingTemplate();
              final Message<String> operation = MessageBuilder.withPayload("@twitter.start()").build();
      
              m.send(channel, operation);
      
          }
      
          /** {@inheritDoc} */
          @Override
          public void stopTwitterAdapter() {
      
              final MessagingTemplate m = new MessagingTemplate();
              final Message<String> operation = MessageBuilder.withPayload("@twitter.stop()").build();
      
              m.send(channel, operation);
      
          }
      
      
          /**
           * Called by Spring Integration to populate a simple LRU cache.
           *
           * @param tweet - The Spring Integration tweet object.
           */
          public void addTwitterMessages(Tweet tweet) {
              this.twitterMessages.put(tweet.getCreatedAt().getTime(), new TwitterMessage(tweet.getCreatedAt(),
                      tweet.getText(),
                      tweet.getFromUser(),
                      tweet.getProfileImageUrl()));
          }
      
      }

      Comment


      • #4
        Well.
        Code:
         public void addTwitterMessages(Tweet tweet)
        HTML Code:
         <int:service-activator input-channel="twitterChannel" ref="twitterService" method="addTwitterMessages"
            output-channel="mailChannel" />
        You want here to add a Tweet to the internal service's twitterMessages Map, but don't return anything from this service-method.
        From other side you want to send a result of this method to the 'mailChannel'.
        So, just make it here! E.g. return some String based on Tweet.
        I recomend you read more about Spring Integration:
        http://static.springsource.org/sprin...nce/htmlsingle
        http://www.manning.com/fisher
        and more digging in the examples: https://github.com/SpringSource/spri...ration-samples

        Comment

        Working...
        X