如何使用POLLENRICH读取JSON文件内容

问题描述

我正在使用Apache camel版本2.24.3,这是我的JSON文件内容

{
  "p-1":"smh_5000","p-2":"smh_6000"
}

因此,我想使用花粉读取此内容文件:当我给他p-1时,他返回smh_5000。 可能有人请在此示例中提供有关如何使用花粉刺的建议。

解决方法

<camelContext> 
<pollEnrich id="readFile" timeout="5000"> 
                <constant>file://data?fileName=test.json</constant> 
            </pollEnrich> 
     <log id="jsonData" message="The message contains ${body}"/> 
     <process id="readingparams" ref="ReadingP"/> 
</camelContext>

我的流程是:

public class ReadingP implements Processor { 
    @Override 
    public void process(Exchange exchange) throws Exception,JSONException {
                JSONObject json = (new JSONObject(exchange.getIn().getBody(String.class))).getJSONArray("catalogue").getJSONObject(0);
                Map<String,Object> ctx= exchange.getProperties();
                ctx.put("product-p",json.get("p-1"));
                exchange.getOut().setHeaders(exchange.getIn().getHeaders());
         }
}