Aggregation strategy does not return body in caggregator

Highlighted
Five Stars

Aggregation strategy does not return body in caggregator

I have a route that call a rest service with a loop and I aggregate the result to get a final json payload. But in the clog, I cann see the body but does not show in postman, any idea about the problem and here the aggregation strategy:

package beans;

import java.util.ArrayList;

import org.apache.camel.Exchange;
import org.apache.camel.processor.aggregate.AggregationStrategy;
import org.eclipse.jetty.util.ajax.JSON;
import org.json.JSONArray;
import org.json.JSONObject;

/*
* user specification: the function's comment should contain keys as follows: 1. write about the function's comment.but
* it must be before the "{talendTypes}" key.
*
* 2. {talendTypes} 's value must be talend Type, it is required . its value should be one of: String, char | Character,
* long | Long, int | Integer, boolean | Boolean, byte | Byte, Date, double | Double, float | Float, Object, short |
* Short
*
* 3. {Category} define a category for the Function. it is required. its value is user-defined .
*
* 4. {param} 's format is: {param} <type>[(<default value or closed list values>)] <name>[ : <comment>]
*
* <type> 's value should be one of: string, int, list, double, object, boolean, long, char, date. <name>'s value is the
* Function's parameter name. the {param} is optional. so if you the Function without the parameters. the {param} don't
* added. you can have many parameters for the Function.
*
* 5. {example} gives a example for the Function. it is optional.
*/
public class AggregateGoogleGeo implements AggregationStrategy{

public Exchange aggregate(Exchange oldEx, Exchange newEx) {
try{
String lattitude;
String longitude;
Integer i = newEx.getProperty("CamelLoopIndex", Integer.class);
String request_ID = ((ArrayList<String>)newEx.getProperty("request_id")).get(i);
//System.out.println(request_ID);

JSONObject googleGeo = (new JSONObject(newEx.getIn().getBody(String.class))).getJSONArray("results").getJSONObject(0)
.getJSONObject("geometry").getJSONObject("location");

//System.out.println(googleGeo.toString());
lattitude = String.valueOf(googleGeo.getDouble("lat"));
longitude = String.valueOf(googleGeo.getDouble("lng"));

JSONArray coordinates = new JSONArray();
coordinates.put(longitude);
coordinates.put(lattitude);


JSONObject geo_location = new JSONObject();
geo_location.put("coordinates",coordinates) ;
geo_location.put("request_id",request_ID) ;

if(oldEx == null){
JSONArray googleGeos = new JSONArray();
googleGeos.put(geo_location);
JSONObject googleResult = new JSONObject();
googleResult.put("googleResult", googleGeos);
newEx.getIn().setBody(googleResult);
//System.out.println(googleResult.toString());
return newEx;
}
JSONObject googleResult = new JSONObject(oldEx.getIn().getBody(String.class));
//System.out.println(googleResult.toString());
JSONArray googleGeos = googleResult.getJSONArray("googleResult");
googleResult.remove("googleResult");
googleGeos.put(geo_location);
googleResult.put("googleResult", googleGeos);
//System.out.println(googleResult.toString());
newEx.getIn().setBody(googleResult, String.class);
//newEx.getIn().setBody(googleResult);
return newEx;

}
catch(Exception ex){
System.out.print(ex.getMessage());
return oldEx;
}
}
}

Route.PNG


Accepted Solutions
Community Manager

Re: Aggregation strategy does not return body in caggregator

I'm sorry, can you explain exactly what is going wrong. The image looks like the route is functioning OK and I can see a message produced by the cLog. I'm struggling to see the issue at the moment.

View solution in original post

Community Manager

Re: Aggregation strategy does not return body in caggregator

Ah I think I see. You need to set the Out message if you want a message returned to the system calling the service. An Exchange has an In and Out message. Your In message will be whatever you send to the service, the Out message can be configured to return whatever you want sent back.

View solution in original post


All Replies
Community Manager

Re: Aggregation strategy does not return body in caggregator

I'm sorry, can you explain exactly what is going wrong. The image looks like the route is functioning OK and I can see a message produced by the cLog. I'm struggling to see the issue at the moment.

View solution in original post

Five Stars

Re: Aggregation strategy does not return body in caggregator

in clog you can see the response, but in postman the response is empty does not show.

Community Manager

Re: Aggregation strategy does not return body in caggregator

Ah I think I see. You need to set the Out message if you want a message returned to the system calling the service. An Exchange has an In and Out message. Your In message will be whatever you send to the service, the Out message can be configured to return whatever you want sent back.

View solution in original post

2019 GARNER MAGIC QUADRANT FOR DATA INTEGRATION TOOL

Talend named a Leader.

Get your copy

OPEN STUDIO FOR DATA INTEGRATION

Kickstart your first data integration and ETL projects.

Download now

What’s New for Talend Summer ’19

Watch the recorded webinar!

Watch Now

Talend API Designer – Technical Overview

Take a look at this technical overview video of Talend API Designer

Watch Now

Getting Started with APIs

Find out how to get started with APIs

Read