Camel Splitter分配器以及Aggregate聚合器使用实例

作用:splite分批  aggregate聚合  单线程处理通过splite变成多线程并行处理。

使用场景:一个消息如果它包含多个元素,其中每一个元素可能要以不同的方式去处理,那么我们该如何处理?

分析:需要将不同类型的消息发给别的bundle去做处理,可以根据类型进行分类,然后根据不同类型聚合起来,最后再根据聚合后的对应类型去到不同的bundle做处理。

<dependency>
			<groupId>org.apache.camel</groupId>
			<artifactId>camel-core</artifactId>
			<version>2.15.2</version>
		</dependency>
		<dependency>
			<groupId>org.osgi</groupId>
			<artifactId>org.osgi</artifactId>
			<version>3.0.0</version>
		</dependency>


1.在blueprint中配置

<bean id="TaskAggregator" class="com.jmust.lklk.TaskAggregator">
</bean>

<bean id="ResponseAggregator" class="com.jmust.lklk.ResponseAggregator">
</bean>

<camelContext id="SpliteCamelContext" xmlns="http://camel.apache.org/schema/blueprint" xsi:schemaLocation="http://camel.apache.org/schema/blueprint http://camel.apache.org/schema/blueprint/camel-blueprint.xsd">
        <route>
            <from uri="direct-vm:_PayTaskSplit"/><!--跨bundle之间调用 -->
            <split strategyRef="ResponseAggregate"> <!--返回结果  把Exchange进行聚合-->
                <simple>${body}</simple>
                <to uri="bean:BatchSplitProcessorBean"/>
                <to uri="log:xxxxxxxxxxy"/>
                <choice>
                    <when>
                        <simple>${header.CamelSplitComplete}</simple>
                        <setHeader headerName="CamelAggregationCompleteAllGroupsInclusive" >
                        <constant>true</constant>
                        </setHeader>
                    </when>
                </choice>
                <aggregate strategyRef="TaskAggregator" completionSize="3" completionFromBatchConsumer="true"> <!-- 把分拆后的结果按照类型聚合 ,如果是List对象,那么会自动将里面的小对象拆分-->
                    <correlationExpression>
                        <simple>${in.header.group}</simple><!-- 按照规则聚合,比如 in.header.group == 100 ,那么就按照100进行聚合-->
                    </correlationExpression>
                    <completionPredicate>
                        <simple>${in.header.CamelSplitComplete} == true </simple>
                    </completionPredicate> <!-- 分拆结束标识-->
                    <to uri="log:yyyyyyyyyyx"/>
                    <to uri="log:ddddd?showAll=true" />
                    <choice>
                        <when>
                            <simple>${in.header.group} != '01' and ${in.header.group} != '04'</simple>
                            <!--<wireTap uri="vm:_choosePayPolicy" />-->
                            <wireTap uri="log:_choosePayPolicy" />
                        </when>
                    </choice>

                </aggregate>
            </split>
            <wireTap uri="vm:_UpdateRecordProcessor"/><!-- 将此放在外面才能拿全部的内容,否则拿到按类型聚合后的结果,需要具体业务具体操作-->
            <to uri="direct-vm:_ValidateRecodProcessor"/>
        </route>
    </camelContext>

2.创建TaskAggregate聚合类以及ResponseAggregate聚合类

public class TaskAggregate implements AggregationStrategy {
     private static final Logger log = LoggerFactory.getLogger(TaskAggregate.class);

    @Override
    public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
        List<PaymentRequest> responseList = new ArrayList<>();
        PaymentRequest paymentRequest = newExchange.getIn().getBody(PaymentRequest.class);
        if (null == oldExchange) {
            responseList.add(paymentRequest);
            newExchange.getIn().setBody(responseList);
            return newExchange;
        } else {
            responseList = oldExchange.getIn().getBody(ArrayList.class);
            responseList.add(paymentRequest);
            oldExchange.getIn().setBody(responseList);
            return oldExchange;
        }
    }

}


public class ResponseAggregate implements AggregationStrategy {
     private static final Logger log = LoggerFactory.getLogger(ResponseAggregate.class);

    @Override
    public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
        List<PaymentRequest> responseList = new ArrayList<>();
        PaymentRequest paymentRequest = newExchange.getIn().getBody(PaymentRequest.class);
        if (null == oldExchange) {
            responseList.add(paymentRequest);
            newExchange.getIn().setBody(responseList);
            return newExchange;
        } else {
            responseList = oldExchange.getIn().getBody(ArrayList.class);
            responseList.add(paymentRequest);
            oldExchange.getIn().setBody(responseList);
            return oldExchange;
        }
    }

}




你可能感兴趣的:(Camel Splitter分配器以及Aggregate聚合器使用实例)