如何为cxfEndpoint设置receiveTimeout和连接超时

时间:2014-12-23 09:07:13

标签: cxf apache-camel

我正在尝试在下面的代码中设置cxfEndpoint的receiveTimeout和连接超时。我得到了很多spring dsl相关的答案,但我特意使用了camel dsl。

我正在尝试在下面的代码中为cxfEndpoint设置receiveTimeout和连接超时。我得到了很多spring dsl相关的答案,但我特意使用camel dsl。 我试图在下面的代码中为cxfEndpoint设置receiveTimeout和连接超时..我得到了这么多的春天dsl相关的答案,但我特别使用骆驼dsl。 我试图在下面的代码中为cxfEndpoint设置receiveTimeout和连接超时..我得到了这么多的春天dsl相关的答案,但我特意使用骆驼dsl。

void configure() throws Exception {
    super.configure()

    CamelContext context=getContext()

    String version=context.resolvePropertyPlaceholders('{{'+ CommonConstants.VERSION_PROPERTY+ '}}')
    String region=context.resolvePropertyPlaceholders('{{'+ CommonConstants.REGION_PROPERTY + '}}')
    String getContextRoot=context.resolvePropertyPlaceholders('{{' + CommonConstants.CONTEXT_ROOT_PROPERTY + '}}')
    boolean validateResponse=getContextRoot

    //main route exposing a GET
    rest("/$version/$region/")
            .get("/$getContextRoot")
            .produces('application/json')\
            .to('direct:validate')

    from('direct:validate')
            .routeId('validate')
            .bean(ValidatorSubRouteHelper.class,'validate')
            .to('direct:get-deviceIdentification')

    from('direct:get-deviceIdentification')
            .routeId('get-deviceIdentification')
            //pre-processing closure
            .process {
                it.out.body = [ it.properties[MessageReferenceConstants.USER_AGENT_HEADER], new CallContext() ]
                it.in.headers[CxfConstants.OPERATION_NAME] = context.resolvePropertyPlaceholders('{{'+MessageReferenceConstants.PROPERTY_OPERATION_NAME+'}}')
                it.in.headers[Exchange.SOAP_ACTION] = context.resolvePropertyPlaceholders('{{'+MessageReferenceConstants.PROPERTY_SOAP_ACTION+'}}')

                Map<String, Object> reqCtx = new HashMap<String, Object>();
                HTTPClientPolicy clientHttpPolicy = new HTTPClientPolicy();
                clientHttpPolicy.setReceiveTimeout(10000);
                reqCtx.put(HTTPClientPolicy.class.getName(), clientHttpPolicy)
                it.in.headers[Client.REQUEST_CONTEXT]=reqCtx


            }
            .to(getEndpointURL())
            //In case of SOAPFault from device, handling the exception in processSOAPResponse
            .onException(SoapFault.class)
            .bean(ProcessResponseExceptionHelper.class,"processSOAPResponse")
            .end()
            //post-processing closure
            .process {
                log.info("processing the response retrieved from device service")
                MessageContentsList li = it.in.getBody(MessageContentsList.class)
                DeviceFamily deviceFamily = (DeviceFamily) li.get(0)
                log.debug('device type is '+deviceFamily.deviceType.value)
                it.properties[MessageReferenceConstants.PROPERTY_RESPONSE_BODY] = deviceFamily.deviceType.value
            }.to('direct:transform')

    from('direct:transform')
            .routeId('transform')
            //transform closure
            .process {
                log.info("Entering the FilterTransformSubRoute(transform)")
                Device device=new Device()
                log.debug('device type '+it.properties[MessageReferenceConstants.PROPERTY_RESPONSE_BODY])
                device.familyName = it.properties[MessageReferenceConstants.PROPERTY_RESPONSE_BODY]
                it.out.body=device
            }
            .choice()
            .when(simple('{{validateResponse}}'))
            .to('direct:validateResponse')

    if(validateResponse) {
        from('direct:validateResponse')
                .bean(DataValidator.getInstance('device.json'))
    }

}

/**
 * Constructs the endpoint url.
 * Formatting end point URL for device identification service call
 * @return the endpoint url
 */
private String getEndpointURL() {
    CamelContext context=getContext()
    def serviceURL=context.resolvePropertyPlaceholders('{{'+MessageReferenceConstants.SERVICE_URL+'}}')
    def wsdlURL=context.resolvePropertyPlaceholders('{{'+MessageReferenceConstants.WSDL_URL+'}}')
    boolean isGZipEnable=CommonConstants.TRUE.equalsIgnoreCase(context.resolvePropertyPlaceholders('{{'+MessageReferenceConstants.GZIP_ENABLED+'}}'))
    def serviceClass = context.resolvePropertyPlaceholders('{{'+MessageReferenceConstants.PROPERTY_SERVICE_CLASS+'}}')
    def serviceName = context.resolvePropertyPlaceholders('{{'+MessageReferenceConstants.PROPERTY_SERVICE_NAME+'}}')

    def url="cxf:$serviceURL?"+
            "wsdlURL=$wsdlURL"+
            "&serviceClass=$serviceClass"+
            "&serviceName=$serviceName"

    if(isGZipEnable) {
        url+= "&cxfEndpointConfigurer=#deviceIdentificationServiceCxfConfigurer"
    }

    log.debug("endpoint url is " + url)

    url


}

1 个答案:

答案 0 :(得分:4)

您已经为它找到了cxfEndpointConfigurer选项。 现在您只需要实现配置器接口,如下所示:

public static class MyCxfEndpointConfigurer implements CxfEndpointConfigurer {

        @Override
        public void configure(AbstractWSDLBasedEndpointFactory factoryBean) {
            // Do nothing here
        }

        @Override
        public void configureClient(Client client) {
            // reset the timeout option to override the spring configuration one
            HTTPConduit conduit = (HTTPConduit) client.getConduit();
            HTTPClientPolicy policy = new HTTPClientPolicy();
            // You can setup the timeout option here
            policy.setReceiveTimeout(60000);
            policy.setConnectionTimeout(30000);
            conduit.setClient(policy);

        }

        @Override
        public void configureServer(Server server) {
            // Do nothing here

        }

    }