骆驼:聚集的元帅

时间:2014-12-30 10:24:11

标签: java apache-camel dsl fluent

我的路线有以下定义。

public class HttpRouteBuilder extends RouteBuilder { 
   ... 

    @Override 
    public void configure() throws Exception { 

            from(ICallbackServiceManager.ORIGIN_ENDPOINT) 
                    .filter(PredicateBuilder.and( 
                            header(ICallbackEvent.EVENT_HEADER_IS_TEST).isEqualTo(allowTestEvent), 
                            header(ICallbackEvent.EVENT_HEADER_EVENT_TYPE).isEqualTo(eventType.name()), 
                            PredicateBuilder.or( 
                                    header(ICallbackEvent.EVENT_HEADER_SVC_ID).in(httpEndpoint._getKeywords().toArray()), 
                                    header(ICallbackEvent.EVENT_HEADER_SVC_ID).regex(httpEndpoint._getKeywords().get(0)) 
                            ) 
                    )) 
                    .routeId((allowTestEvent) ? httpEndpoint._getTestRouteId() : httpEndpoint.getRouteId()) 
                    .aggregate(constant(true), new BasicBodyListAggregator())
                    .completionSize(Config.getInt("callbacks.route.aggregate.size", 2)) 
                    .completionInterval(Config.getInt("callbacks.route.aggregate.interval", 1000)) 
                    .setHeader(Exchange.HTTP_METHOD, constant("POST")) 
                    .setHeader(Exchange.CONTENT_TYPE, constant(dataFormat.getClass().equals(XStreamDataFormat.class) ? "application/xml" : "application/json")) 
                    .setHeader(Exchange.HTTP_PROTOCOL_VERSION, constant("HTTP/1.0")) 
                    .marshal(dataFormat) 
                    .to(httpEndpoint.endpointUri()); 

    } 
} 

DataFormat定义为:

dataFormat = CustomDataFormat.getDataFormat(SerializationType.XSTREAM | SerializationType.JSON);

public class CustomDataFormat{
    private static final Class[] annotatedClasses = new Class[]{GupshupEvent.class, CommEvent.class};

    public static DataFormat getDataFormat(SerializationType dataFormat) {

        XStream _xStream = new XStream();
        _xStream.processAnnotations(annotatedClasses);

        if((0 == dataFormat.compareTo(SerializationType.XSTREAM)))
            return new XStreamDataFormat(_xStream);

        JsonDataFormat jsonFormat = new JsonDataFormat();
        jsonFormat.setXStream(_xStream);
        return jsonFormat;
    }
}

GupshupEvent | CommEvent是pojo(s),即

@XStreamAlias("ExternalEvent")
public class GupshupEvent extends CallbackEvent implements ICallbackEvent{

    public GupshupEvent() {}

    public GupshupEvent(Map<String, String> eventMap) throws CException {
        super(eventMap);
    }

    @Override
    public Map<String, Object> _getHeaders(boolean isTestEvent) {

        Map<String, Object> headers = new HashMap<String, Object>();
        headers.put(ICallbackEvent.EVENT_HEADER_EVENT_TYPE, CallbackEventType.EXTERNAL);
        headers.put(ICallbackEvent.EVENT_HEADER_SVC_ID, _getKeyword());
        headers.put(ICallbackEvent.EVENT_HEADER_IS_TEST, isTestEvent);

        return headers;
    }

    /* incl. of public getters / setters */

    private String phoneNo;
    private String content;
    private String carrier;
    private String location;
    private String event;
}

聚合策略为:

public class BasicBodyListAggregator implements AggregationStrategy { 
    @Override 
    public Exchange aggregate(Exchange oldExchange, Exchange newExchange) { 

        Object newBody = newExchange.getIn().getBody(); 
        ArrayList list = null; 
        if (oldExchange == null) { 

            list = new ArrayList(); 
            list.add(newBody); 
            newExchange.getIn().setBody(list); 
            return newExchange; 
        } else { 

            list = oldExchange.getIn().getBody(ArrayList.class); 
            list.add(newBody); 
            return oldExchange; 
        } 
    } 
} 

问题是,当在1的completionInterval内只处理一个交换时,序列化输出不包括列表(包含1个单个消息),而是包含单个消息本身。

{ 
    "list": { 
        "ExternalEvent": { 
            "eventTime": 1408900445, 
            "phoneNo": 9739772042, 
            "content": "FK OFFERS something", 
            "carrier": "vodafone", 
            "location": "Bangalore" 
        }
    } 
} 

应该是:

{ 
    "list": { 
        "ExternalEvent": [ 
            { 
                "eventTime": 1408900445, 
                "phoneNo": 9739772042, 
                "content": "FK OFFERS something", 
                "carrier": "vodafone", 
                "location": "Bangalore" 
            } 
        ]
    } 
} 

但是,当在completionInterval中处理多个交换时,它工作正常。

{ 
    "list": { 
        "ExternalEvent": [ 
            { 
                "eventTime": 1408900445, 
                "phoneNo": 9739772042, 
                "content": "FK OFFERS something", 
                "carrier": "vodafone", 
                "location": "Bangalore" 
            }, 
            { 
                "eventTime": 1408900448, 
                "phoneNo": 9232977242, 
                "content": "FK OFFERS something", 
                "carrier": "vodafone", 
                "location": "Bangalore" 
            } 
        ] 
    } 
} 

任何帮助/指针将不胜感激。 骆驼版:2.13.2

谢谢,

0 个答案:

没有答案