LibraryToggle FramesPrintFeedback

To use the CXFConnectionFactory your application needs to:

  1. Look up a CXFConnectionFactory object in the application server's JNDI registry.

  2. Use the CXFConnectionFactory.getConnection() method to get a CXFConnection object.

    The CXFConnectionFactory.getConnection() method takes one parameter, a CXFConnectionSpec object, which the has following fields:

  3. Use the CXFConnection.getService() method to obtain a Web service client.

  4. Close the CXFConnection object.

  5. Invoke on the service.

    The Web service client can still be used after the CXFConnection object is closed.

The code shown in Example 3.1 shows how to use the Apache CXF JCA Connector connection management API.

Example 3.1. HelloWorldServlet—Outbound Connections

1Context ctx = new InitialContext();
CXFConnectionFactory factory = (CXFConnectionFactory)ctx.lookup(EIS_JNDI_NAME);

2CXFConnectionSpec spec = new CXFConnectionSpec();
    spec.setServiceClass(Greeter.class);
    spec.setServiceName(new QName("http://apache.org/hello_world_soap_http", "SOAPService"));
    spec.setEndpointName(new QName("http://apache.org/hello_world_soap_http", "SoapPort"));
    spec.setWsdlURL(getClass().getResource("/wsdl/hello_world.wsdl"));
    CXFConnection connection = null;
    try {
          connection = getConnection(spec);
           
          3Greeter greeter = connection.getService(Greeter.class);
          
          4connection.close();
          
          5greeter.sayHi();
...      
         }

The code shown in Example 3.1 does the following:

1

Retrieves the connection factory from JNDI.

2

Creates the connection and uses a CXFConnectionSpec object to specify:

  • The service class.

  • A QName that identifies which service in the WSDL file to use.

  • A QName that identifies which port in the WSDL file to use

  • The WSDL file URL.

3

Obtains a Web service client.

4

Closes the connection to the service and return to the application server connection pool.

Remember you can close the connection and continue using the client.

5

Invokes methods on the service.

Comments powered by Disqus