简体   繁体   中英

How to make a header value router in spring integration java dsl and assign it a default output channel?

I have it like this currently:

.route("headers.STATE", new Consumer<RouterSpec<ExpressionEvaluatingRouter>>() {
        @Override
        public void accept(RouterSpec<ExpressionEvaluatingRouter> spec) {
            spec
                .channelMapping(ProcStatus.NORMAL_OPERATION.toString(), "primaryChannel")
                .channelMapping(ProcStatus.FAILED_OVER.toString(), "secondaryChannel")
                .channelMapping(ProcStatus.UNKNOWN.toString(), "stateRetrievalChannel");
                }
            })

But it's not really a header value router per se right? I can't seem to set HeaderValueRouter as the routing spec and just give the name of the header on the first param.

Plus i couldn't find a default channel mapping on the spec. Thanks for the help!

To be honest the <header-value-router> does not make sense since introduction of SpEL router, where you can simply configure it like expression="headers.STATE" , like in your config for Java DSL.

Everything else is the same for any kind of Router implementation. See more in the reference manual .

And, yes, you can use HeaderValueRouter directly as well:

.route(new HeaderValueRouter("STATE"), new Consumer<RouterSpec<ExpressionEvaluatingRouter>>() {
    @Override
    public void accept(RouterSpec<ExpressionEvaluatingRouter> spec) {
        spec
            .channelMapping(ProcStatus.NORMAL_OPERATION.toString(), "primaryChannel")
            .channelMapping(ProcStatus.FAILED_OVER.toString(), "secondaryChannel")
            .channelMapping(ProcStatus.UNKNOWN.toString(), "stateRetrievalChannel");
            }
        })

But as you see the .channelMapping() remains the same.

As for "default channel mapping". I think you just mean default-output-channel , which we have in the XML configuration.

If you noticed no one component in the SI Java DSL has an output-channel option (the default-output-channel plays the same role). We just propagate the next .channel() definition in the IntegrationFlow to the current outputChannel -aware component. So, to map the default-output-channel for the .route() you should just go ahead in the method-chain with the IntegrationFlow definition. Like this:

.route()
.handle()

So, if routing condition doesn't meet any .channelMapping() and resolutionRequired == false , the message will be send to the next .handle() through the implicit DirectChannel between them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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