Created
April 21, 2021 12:56
-
-
Save kunupat/4a6ff3618183ad26a96f8ca52bf357c8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.apache.camel.Exchange; | |
import org.apache.camel.processor.aggregate.AggregationStrategy; | |
import org.codehaus.jettison.json.JSONArray; | |
import org.springframework.stereotype.Component; | |
@Component | |
public class FileAggregationStrategy implements AggregationStrategy { | |
@Override | |
public Exchange aggregate(Exchange oldExchange, Exchange newExchange) { | |
if (oldExchange == null) { | |
newExchange.setProperty(Exchange.AGGREGATION_COMPLETE_ALL_GROUPS, true); | |
return newExchange; | |
} | |
try { | |
String body1 = oldExchange.getIn().getBody(String.class); | |
String body2 = newExchange.getIn().getBody(String.class); | |
JSONArray sourceArray = new JSONArray(body2); | |
JSONArray destinationArray = new JSONArray(body1); | |
for (int i = 0; i < sourceArray.length(); i++) { | |
destinationArray.put(sourceArray.getJSONObject(i)); | |
} | |
String result = destinationArray.toString(); | |
oldExchange.getIn().setBody(result); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
return oldExchange; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment