简体   繁体   English

Apache骆驼豆state丢了

[英]Apache camel bean state is lost

I have a route我有路线

    from("timer://poller?fixedRate=true&period=1500")
            .routeId("db-poller")
            .filter(method(StateBean.class, "doProcess"))
            .to("direct:ready-to-process").end();

     from("direct:ready-to-process")
            .bean(StateBean.class, "setDoProcess(false)");

my StateBean looks like this我的 StateBean 看起来像这样

@Component
public class StateBean {

    private final AtomicBoolean process =
            new AtomicBoolean(true);

    public boolean doProcess() {
        return process.get();
    }

    public void setDoProcess(boolean state) {
        process.set(state);
    }

}

My expectations are for bean to preserve process state as false, but on the second loop for some reason the state comes back to true.我的期望是 bean 将进程 state 保留为假,但在第二个循环中,由于某种原因,state 恢复为真。 What I'm doing wrong?我做错了什么?

NOTES笔记

this route, however works as intended if I make process variable static但是,如果我制作process变量 static ,这条路线将按预期工作

is there any way to make it work without a static field process , or without an external source (ie DB)有没有办法让它在没有 static 现场process或没有外部源(即 DB)的情况下工作

OTHER NOTES其他说明

I'm using我在用着

Apache Camel 3.10.0 Apache 骆驼 3.10.0

spring-boot 2.5 spring-boot 2.5

UPDATE NO.1更新 NO.1

For the sake of full solution, thanks to Luca Burgazzoli I did something like this为了完整的解决方案,感谢 Luca Burgazzoli,我做了这样的事情

@Component
public class MyRoute extends RouteBuilder {

    private StateBean state;

    @Autowired
    public MyRoute(StateBean state) {
        this.state = state;
    }

    @Override
    public void configure() {
    
    from("timer://poller?fixedRate=true&period=1500")
            .routeId("db-poller")
            .filter(exchange -> state.doProcess())
            .to("direct:ready-to-process").end();

     from("direct:ready-to-process")
            .process(exchange -> state.setDoProcess(false))
            .process(...)
            .process(...)
            .split(...)
            .streaming()
            .to(...)
            .process(exchange ->  state.setDoProcess(true))
       }
    }

The problem is that you are not sharing a specific bean instance as the call bean(StateBean.class, ...) are supposed to create a new instance of the bean of the required class问题是您没有共享特定的 bean 实例,因为调用 bean(StateBean.class, ...) 应该创建所需 class 的 bean 的新实例

You should use something like.beanRef("name-of-the-bean", ....)您应该使用类似 .beanRef("name-of-the-bean", ....)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM