spring 集成 dsl 过滤器而不是过滤器方法注释

spring integration dsl filter instead filter method annotation

如何从过滤器注释方法切换到 Spring 集成 java 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;
    }

IntegrationFlowDefinition 上有几个重载的 .filter() 方法。查看 java 文档,但是

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).

等等