如何在Spring Integration自定义消息处理程序中自动装配bean?

时间:2016-08-23 22:11:36

标签: spring spring-integration

我希望创建一个自定义消息处理程序来在流中使用检查点。此外,这些检查点将存储在 ElasticSearch

我创建了一个类检查点

public class CheckpointMessageHandlerSpec extends MessageHandlerSpec<CheckpointMessageHandlerSpec, CheckpointHandler> {

    public CheckpointMessageHandlerSpec(CheckpointHandler checkpointHandler) {
        this.target = checkpointHandler;
    }

    public CheckpointMessageHandlerSpec apply(Message<?> message) {
        this.target.handleMessage(message);
        return _this();
    }

    @Override
    protected CheckpointHandler doGet() {
        throw new UnsupportedOperationException();
    }
}

接下来,我创建了 CheckpointMessageHandlerSpec

public class CheckpointHandler extends IntegrationObjectSupport implements MessageHandler {

    private String status;
    private String message;

    // I want inject services or repositories here

    public CheckpointHandler(String status, String message) {
        this.status = status;
        this.message = message;
    }

    @Override
    public void handleMessage(Message<?> message) {
        // Test to watch if I have the bean factory. It is always null
        this.getBeanFactory();

        Expression expression = EXPRESSION_PARSER.parseExpression("'" + this.message + "'");

        // Here I intend to persist information of payload/headers with spring-data-elasticSearch repository previously injected   
        Object obj = expression.getValue(message);
    }
}

CheckpointHandler ,在这个类中,我希望从Spring Data中注入服务或存储库:

@Bean
public IntegrationFlow checkpointFlow(Checkpoint checkpoint) {
    return IntegrationFlows.from(Http.inboundChannelAdapter("/checkpointFlow"))
            .enrichHeaders(Collections.singletonMap(Checkpoint.TASK_HEADER_KEY, taskName))
            .handle(new AppendMessageHandler())
            .wireTap(c -> c.handle(m -> checkpoint.warn("SOMETHING IS HAPPENING HERE. MY PAYLOAD: ' + payload.toString() + '").apply(m)))
            .handle(m -> log.info("[LOGGING DEMO] {}" , m.getPayload()))
            .get();
}

private class AppendMessageHandler implements GenericHandler {

    @Override
    public String handle(Object payload, Map headers) {
        return new StringBuilder().append(testMessage).toString();
    }
}

最后,在流程中使用的一个例子:

set: example1, "/random/string"

我想念的是什么?有可能吗?我有这个想法How to create custom component and add it to flow in spring java dsl?

谢谢!

1 个答案:

答案 0 :(得分:1)

Bean可以自动装配,如果它们是,那么,正是豆类。

让我们再看看你的代码吧!

c.handle(m -> checkpoint.warn("SOMETHING IS HAPPENING HERE. MY PAYLOAD: ' + payload.toString() + '").apply(m))

这里真正的bean正是Lambda :)。当然,很遗憾,但不是您的自定义工厂,后续apply()。您的自定义代码将在每个传入邮件的目标Lambda中完全调用,但不知道BeanFactory

要解决您的问题,您应该按原样使用工厂:

.wireTap(c -> c.handle(checkpoint.warn("SOMETHING IS HAPPENING HERE. MY PAYLOAD: ' + payload.toString() + '")))

框架将注意您将CheckpointHandler注册为bean,因此需要自动装配。

您可能已经猜到,您不需要apply()方法。仅仅因为当Java DSL为bean填充树时需要区分汇编阶段。初始化和注册阶段,当框架解析该树并且bean在应用程序上下文中注册时。最后,还有一个运行时阶段,当消息通过所有那些消息处理程序,变换器等从一个通道传播到另一个通道时。