关于cxf客户端的线程安全性

Yes CXF is thread safe, you can use single instance/singleton for Weather and WeatherSoap, you can think of cxf as similar to servlet engine which handles all the infrastructure for you such as transport, databinding for you. I had similar usecase, where I had a front end presentation layer and number of network servers, to interact between these I had a rest for presentation and SOAP which implements business logic as well as interacts with servers. Hence I implemented a soap client in rest layer. I had requirement were I needed split rest request and invoke parallel soap calls which had time delays 800ms. I performance tested the entire setup and did not run-up into any thread issues.

So coming into to client implementation

Pure Java

public class MySoapClient{

  private static WeatherSoap weatherSoap;

  private MySoapClient(){
  }

  public static WeatherSoap getClient(){

    if(weatherSoap==null){
        Weather weatherService = new Weather();
        weatherSoap= weatherService.getWeatherSoap();
    }
    return weatherSoap;
  }

}

And I would modify the Weather class to get SOAP url from properties file.

@WebServiceClient(name = "Weather", 
                  wsdlLocation = "classpath:weather.wsdl",
                  targetNamespace = "http://ws.cdyne.com/WeatherWS/") 
public class Weather extends Service {

    private static final Logger LOG = LoggerFactory.getLogger(Weather.class);
    public final static URL WSDL_LOCATION;
    public final static QName SERVICE = new QName("http://ws.cdyne.com/WeatherWS/", "Weather");
    public final static QName WeatherHttpPost = new QName("http://ws.cdyne.com/WeatherWS/", "WeatherHttpPost");
    public final static QName WeatherHttpGet = new QName("http://ws.cdyne.com/WeatherWS/", "WeatherHttpGet");
    public final static QName WeatherSoap12 = new QName("http://ws.cdyne.com/WeatherWS/", "WeatherSoap12");
    public final static QName WeatherSoap = new QName("http://ws.cdyne.com/WeatherWS/", "WeatherSoap");
    static {
        URL url = null;
        try {
            url = new URL(MyPropertiesUtil.getProperty("app.weather.url"));
        } catch (MalformedURLException e) {
            LOG.error(e.getMessage(), e);
        }
        if (url == null) {
            LOG.error("an issue with your url");
        }       
        WSDL_LOCATION = url;   
    }

    public Weather(URL wsdlLocation) {
        super(wsdlLocation, SERVICE);
    }

    public Weather(URL wsdlLocation, QName serviceName) {
        super(wsdlLocation, serviceName);
    }

    public Weather() {
        super(WSDL_LOCATION, SERVICE);
    }


    //All the other interface methods

}

Using Spring

if you are using spring you can make things even simpler, you can eliminate Weather.java class by using configuration file as shown below and let cxf generate proxy for you.

<jaxws:client id="weatherSoap" serviceClass="com.cdyne.ws.weatherws.WeatherSoap" address="${app.weather.url}" />

And Business Class would look like below.

@Component
MyBusinessLogic{

  @Autowired
  private WeatherSoap weatherSoap;

  public ArrayOfWeatherDescription getOutput(){
    return weatherSoap.getWeatherInformation();
  }

}
add a comment
您好!关于使用Spring Boot和Apache CXF开发客户端的问题,我可以为您提供一些指导。 首先,您需要在您的Spring Boot项目中添加CXF的依赖。可以在您的pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-spring-boot-starter-jaxws</artifactId> <version>3.4.1</version> </dependency> ``` 接下来,您可以创建一个CXF客户端来调用远程WebService。您可以使用`@WebServiceClient`注解来生成客户端代码。例如,假设您要调用一个名为"HelloWorldService"的WebService: ```java @WebServiceClient(name = "HelloWorldService", targetNamespace = "http://example.com/", wsdlLocation = "http://example.com/HelloWorldService?wsdl") public class HelloWorldServiceClient extends Service { public HelloWorldServiceClient(URL wsdlLocation, QName serviceName) { super(wsdlLocation, serviceName); } public HelloWorldService getHelloWorldServicePort() { return super.getPort(HelloWorldService.class); } } ``` 在上面的代码中,`wsdlLocation`参数指定了远程WebService的WSDL地址。`getHelloWorldServicePort()`方法返回了一个用于调用WebService方法的接口。 接下来,您可以在您的Spring Boot应用程序中使用这个客户端。您可以将它注入到您的服务或控制器中,并使用它来调用远程WebService方法。例如: ```java @Service public class MyService { @Autowired private HelloWorldServiceClient helloWorldServiceClient; public void invokeRemoteService() { HelloWorldService helloWorldService = helloWorldServiceClient.getHelloWorldServicePort(); String result = helloWorldService.sayHello("World"); System.out.println(result); } } ``` 在上面的代码中,通过`helloWorldServiceClient.getHelloWorldServicePort()`方法获取到了远程WebService的接口实例,然后可以调用其中的方法。 这就是使用Spring Boot和CXF开发客户端的基本步骤。您可以根据您的具体需求进一步进行配置和调整。希望对您有所帮助!如果您有更多问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值