Apache Camel and web services

前端 未结 2 2009
一向
一向 2021-01-19 18:22

I am trying to understand how to integrate Apache Camel with any web service that provides a WSDL to generate its classes to afterward call his methods to return some reques

2条回答
  •  自闭症患者
    2021-01-19 18:43

    One of my main responsibilities is to consume different web services using camel as a client. This is the approach I generally use, never ran into any problems it works like a charm everytime.

    1) Define the endpoint in your camel-config.xml file:

    < bean id="service_name_CXFEndpoint" class="org.apache.camel.component.cxf.CxfEndpoint" />

    2) Use the Endpoint Defined in the config file in the route for web service consumption:

    private String CXF_SERVICE_ENDPOINT = "cxf:bean:service_name_CXFEndpoint?address=wsdl_uri_location&serviceClass=service_name_from_the_stubs&loggingFeatureEnabled=true";

    For the service name, you need to copy the contents of the wsdl, paste it in a .wsdl file and generate webservice client. To do this, you need to right click on the wsdl file>webservices>Generate Client. Then you need to select JbossWS for the runtime( You have to set it up first in the preferences). Once the stubs, have been generated, look for the main service class. Then copy its entire location.(for example, for a class called WebServiceClass located in com.test would be serviceClass=com.test.WebService.class)

    3) Define the routing for consumption:

    from("direct:web_service")
    .routeId("service_name")
    .bean(ServiceWSProcessor,"processRequest")
    .to(CXF_SERVICE_ENDPOINT)
    .bean(ServiceWSProcessor,"processResponse")
    .end();
    

    Now, you send a request to this processor, which will go to the web service endpoint and give you a response.

    4) Write a processor for the request response(in this case serviceWS processor).

    @Component(value="serviceWSProcessor")
    public class ServiceWSProcessor {
    
      private static final Logger LOGGER = LoggerFactory.getLogger(ServiceWSProcessor.class);
    
      public void processRequest(Exchange exchange) throws Exception {
    
        Message in = exchange.getIn();
        Message out = exchange.getOut();
    
        try {
    
          LOGGER.info("Service - START");
    
          Request Request = in.getBody(Request.class);
    
          //This depends on your WSDL File. Test the process in SOAP UI and see how the request looks like and code
          //accordingly.
    
          List list = new ArrayList();
          list.add(header);
          list.add(body);
          //for this, you need to check the service for what are the parameters for that method and what response its expecting.
    
          out.setHeader(CxfConstants.OPERATION_NAME, "method_name");
          out.setBody(list);
    
        } 
        catch (Exception e) {
          e.printStackTrace();
        }
    
      }
    
      public void processResponse(Exchange exchange) throws Exception {
    
        Message in = exchange.getIn();
        Message out = exchange.getOut();
    
        try {
          Response response = null; //Code this based on what response you should set to the final body. check the message
          //contents list in debug mode to see what response you are getting.
    
          try {
            MessageContentsList result = (MessageContentsList) in.getBody();
            if (result != null) {
              response = (Response)result.get(0);
    
              out.setHeader(Constants.HEADER_SERVICE_RESPONSE_SUCCESS, Constants.SERVICE_RESPONSE_SUCCESS_Y);
            } else {
              out.setHeader(Constants.HEADER_SERVICE_RESPONSE_SUCCESS, Constants.SERVICE_RESPONSE_SUCCESS_N);
            }
    
          } catch (Exception e) {
            e.printStackTrace();
            out.setHeader(Constants.HEADER_SERVICE_RESPONSE_SUCCESS, Constants.SERVICE_RESPONSE_SUCCESS_N);
          }
          LOGGER.info("Service - END");
          out.setBody(response));
        } 
        catch (Exception e) {
          LOGGER.error("Service - ERROR");
          out.setBody(e.getMessage());
        } 
    
      }
    
    }
    
    
    

    For the list of dependencies required for camel and spring-ws, check out this page.

    https://github.com/gauthamhs/Java/blob/master/JavaEE/Dependencies-Important-Camel-Spring.xml

    Let me know if you need additional help or if you have any concerns.

    Regards, Gautham

    提交回复
    热议问题