带有ProducerTemplate的Apache Camel处理器

时间:2016-10-17 09:25:29

标签: apache-camel apache-servicemix

我尝试编写一个自定义处理器,它在循环中发送多个消息,如:

public class ListAllAttributes implements Processor {
    static final transient Logger LOG = LoggerFactory.getLogger(ListAllAttributes.class);
    ProducerTemplate producer;

    public void setProducer(ProducerTemplate producer) {
        this.producer = producer;
    }

    @Override
    public void process(Exchange exchange) throws Exception {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();  
        DocumentBuilder builder; 
        Document doc =  exchange.getIn().getBody(Document.class);
        NodeList entries = doc.getElementsByTagNameNS("*", "ItemId");
        int length = entries.getLength();
        for (int i = 0; i < length; ++i) {
        .........
        producer.requestBodyAndHeaders(newdoc,headers);
   }
}

ProducerTemplate的端点,我在xml中配置:

<template id="myTemplate" defaultEndpoint="https4..."/>
<bean class="ru.devprg.ruspace.bus.messages.ListAllAttributes" id="ListAllAttributes">
    <property name="producer" ref="myTemplate"/>
</bean>

在我看到的日志中,该请求已执行:

 HttpProducer                     | 296 - org.apache.camel.camel-http4 - 2.16.3 | Executing http POST method: https://172.17.10.105/ews/exchange.asmx

但交付失败了:

Caused by: org.apache.http.client.ClientProtocolException
  at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:186)[294:org.apache.httpcomponents.httpclient:4.5.1]
  at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)[294:org.apache.httpcomponents.httpclient:4.5.1]
  at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:55)[294:org.apache.httpcomponents.httpclient:4.5.1]
  at org.apache.camel.component.http4.HttpProducer.executeMethod(HttpProducer.java:278)[296:org.apache.camel.camel-http4:2.16.3]
  at org.apache.camel.component.http4.HttpProducer.process(HttpProducer.java:164)[296:org.apache.camel.camel-http4:2.16.3]
  at org.apache.camel.util.AsyncProcessorConverterHelper$ProcessorToAsyncProcessorBridge.process(AsyncProcessorConverterHelper.java:61)[126:org.apache.camel.camel-core:2.16.3]
  at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:190)[126:org.apache.camel.camel-core:2.16.3]
  at org.apache.camel.util.AsyncProcessorHelper.process(AsyncProcessorHelper.java:109)[126:org.apache.camel.camel-core:2.16.3]
  at org.apache.camel.processor.UnitOfWorkProducer.process(UnitOfWorkProducer.java:68)[126:org.apache.camel.camel-core:2.16.3]
  at org.apache.camel.impl.ProducerCache$2.doInProducer(ProducerCache.java:412)[126:org.apache.camel.camel-core:2.16.3]
  at org.apache.camel.impl.ProducerCache$2.doInProducer(ProducerCache.java:380)[126:org.apache.camel.camel-core:2.16.3]
  at org.apache.camel.impl.ProducerCache.doInProducer(ProducerCache.java:270)[126:org.apache.camel.camel-core:2.16.3]
  at org.apache.camel.impl.ProducerCache.sendExchange(ProducerCache.java:380)[126:org.apache.camel.camel-core:2.16.3]
  at org.apache.camel.impl.ProducerCache.send(ProducerCache.java:238)[126:org.apache.camel.camel-core:2.16.3]
  at org.apache.camel.impl.DefaultProducerTemplate.send(DefaultProducerTemplate.java:128)[126:org.apache.camel.camel-core:2.16.3]
  at org.apache.camel.impl.DefaultProducerTemplate.sendBodyAndHeaders(DefaultProducerTemplate.java:253)[126:org.apache.camel.camel-core:2.16.3]
  ... 24 more
Caused by: org.apache.http.client.NonRepeatableRequestException: Cannot retry request with a non-repeatable request entity.
  at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:225)[294:org.apache.httpcomponents.httpclient:4.5.1]
  at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:184)[294:org.apache.httpcomponents.httpclient:4.5.1]
  at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:88)[294:org.apache.httpcomponents.httpclient:4.5.1]
  at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110)[294:org.apache.httpcomponents.httpclient:4.5.1]
  at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184)[294:org.apache.httpcomponents.httpclient:4.5.1]

有什么不对?

1 个答案:

答案 0 :(得分:0)

<camelContext id="ARMRoscosmos" trace="true" xmlns="http://camel.apache.org/schema/blueprint">
        <properties>
            <property key="CamelLogDebugBodyMaxChars" value="2000"/>
            <property key="CamelLogDebugBodyStreams" value="true"/>
        </properties> 
        <template id="myTemplate" defaultEndpoint="https4:{{Exchange}}/ews/exchange.asmx?httpClientConfigurer=hostnameVerifier&amp;authUsername={{DomainUsername}}&amp;authPassword={{DomainPassword}}&amp;authDomain={{Domain}}"/>
        <route id="requesttoexchange">
            <from id="_from1" uri="activemq:queue:IN.EXCHANGE"/>
            <setProperty id="_setProperty1" propertyName="DateStart">
                <groovy>new java.text.SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss").format(request.body.getStartDate())</groovy>
            </setProperty>
            <setProperty id="_setProperty2" propertyName="EndDate">
                <groovy>new java.text.SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss").format(request.body.getEndDate())</groovy>
            </setProperty>
            <log id="_log1" message="Dates:${body.getStartDate()}->${property.DateStart}  ${body.getEndDate()}->${property.EndDate}"/>
            <setBody id="_setBody1">
                <simple>

       &lt;soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
       xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages" 
       xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types" 
       xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"&gt;
  &lt;soap:Header&gt;
    &lt;t:RequestServerVersion Version="Exchange2007_SP1" /&gt;
  &lt;/soap:Header&gt;
  &lt;soap:Body&gt;
    &lt;m:GetFolder&gt;
      &lt;m:FolderShape&gt;
        &lt;t:BaseShape&gt;IdOnly&lt;/t:BaseShape&gt;
      &lt;/m:FolderShape&gt;
      &lt;m:FolderIds&gt;
        &lt;t:DistinguishedFolderId Id="calendar" /&gt;
      &lt;/m:FolderIds&gt;
    &lt;/m:GetFolder&gt;
  &lt;/soap:Body&gt;
&lt;/soap:Envelope&gt;

 </simple>
            </setBody>
            <removeHeaders id="_removeHeaders1" pattern="*"/>
            <setHeader headerName="CamelHttpMethod" id="_setHeader1">
                <constant>POST</constant>
            </setHeader>
            <setHeader headerName="Content-Type" id="_setHeader2">
                <constant>text/xml</constant>
            </setHeader>
            <to id="_to1" uri="https4:{{Exchange}}/ews/exchange.asmx?httpClientConfigurer=hostnameVerifier&amp;authUsername={{DomainUsername}}&amp;authPassword={{DomainPassword}}&amp;authDomain={{Domain}}"/>
            <process id="_cprocess1" ref="MyProcessor"/>
            <setBody id="_setBody2">
                <simple>

                </simple>
            </setBody>
            <removeHeaders id="_removeHeaders2" pattern="*"/>
            <setHeader headerName="CamelHttpMethod" id="_setHeader3">
                <constant>POST</constant>
            </setHeader>
            <setHeader headerName="Content-Type" id="_setHeader4">
                <constant>text/xml</constant>
            </setHeader>
            <log id="_log2" message="authUsername {{DomainUsername}}"/>
            <to id="_to2" uri="https4:{{Exchange}}/ews/exchange.asmx?httpClientConfigurer=hostnameVerifier&amp;authUsername={{DomainUsername}}&amp;authPassword={{DomainPassword}}&amp;authDomain={{Domain}}"/>
            <removeHeaders id="_removeHeaders3" pattern="*"/>
            <setHeader headerName="Content-Type" id="_setHeader5">
                <constant>text/xml</constant>
            </setHeader>
            <process id="_cprocess2" ref="ListAllAttributes"/>
            <log id="_log3" message="message ${body}"/>

            <to id="_to3" uri="activemq:queue:OUT.EXCHANGE?jmsMessageType=Object"/>
        </route>