Do not input private or sensitive data. View Qlik Privacy & Cookie Policy.
Skip to main content

Announcements
See why IDC MarketScape names Qlik a 2025 Leader! Read more
cancel
Showing results for 
Search instead for 
Did you mean: 
Anonymous
Not applicable

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;
}
}
}

0683p000009M4f1.png

Labels (1)
  • Other

1 Solution

Accepted Solutions
Anonymous
Not applicable
Author

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

5 Replies
Anonymous
Not applicable
Author

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.

Anonymous
Not applicable
Author

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

Anonymous
Not applicable
Author

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.

PDIOUF1632816531
Partner - Contributor
Partner - Contributor

Hi, have you found how to set the payload with the aggregation ? 'cause I have the same issue. Even a cSetBody after the caggregate doesn't work

bevvet
Contributor
Contributor

This is a good idea, but if OP for whatever reason doesn’t want to go that route, I’d just put the query on the model and do one trip to the database with a join/avg query and put the method on the Restaurant model