2015-02-06 74 views
1

如何從過濾器註釋方法切換到Spring集成java DSL過濾器。我怎樣才能調用過濾方法?彈簧集成dsl過濾器改爲過濾器方法註釋

IntegrationFlows.from("removeSession") 
       // remove chat session from user sessions map 
       .handle("sessionLogService", "removeChatSession") 
       // continue and remove user from ehcache only if user have no more opened sessions. 
       .filter(/* what's going here? */) 
       .get(); 

改爲過濾器註釋。

@Filter(inputChannel = "userGoOfflineFilter", outputChannel = "userGoOffline") 
    public boolean notifyOnlyIfLastConnectionClosed(SecureUser secureUser) { 
     ChatUser user = sessionUtils.getChatUser(secureUser.getId()); 
     if(user == null || user.getChatSessionIds() == null || user.getChatSessionIds().isEmpty()) 
      return true; 
     LOGGER.debug(secureUser.getFirstName()+": Offline message not sent yet"); 
     return false; 
    } 

回答

1

IntegrationFlowDefinition有幾種超載.filter()方法。看看javadocs,但是

filter("expression"); 

需要一個SpEL表達式。這可能是一個bean引用,如

.filter("@myFilter.notifyOnlyIfLastConnectionClosed('payload')") 

,或者你可以使用一個GenericSelector ...

.filter(SecureUser.class, u -> u == null || u.getChatSessionIds() == null || u.getChatSessionIds().isEmpty()) 

(java的8拉姆達)或

.filter(new GenericSelector<SecureUser>() { 
        @Override 
        public boolean accept(SecureUser u) { 
         return u == null || u.getChatSessionIds() == null || u.getChatSessionIds().isEmpty(); 
        } 
       }) 

(java的6/7 )。

+0

非常感謝你這.filter( 「@ myFilter.notifyOnlyIfLastConnectionClosed( '有效載荷')」)工作的完美,我喜歡它。 :) – lubo08 2015-02-06 20:59:01