spring boot懒加载webservice服务bean并设置超时时间

在spring boot项目中使用第三方的webservice,由于是第三方的webservice,要考虑其无法访问的情况。

需要解决以下两个问题:

1. spring boot由于第三方webservice无法访问导致报create bean error错误

2. spring中使用JaxWsPortProxyFactoryBean 设置读超时

解决方案:

1. spring中使用JaxWsPortProxyFactoryBean 设置读超时

// 经过测试 当对端web service挂掉 这两个参数其作用

customProperty.put("com.sun.xml.internal.ws.request.timeout", 5000);

customProperty.put("com.sun.xml.internal.ws.connect.timeout", 5000);

 

 

2. spring boot中WebService服务bean 懒加载

@Bean

public JaxWsPortProxyFactoryBean wsService() {

 

在spring boot启动时,如果对端webservice无法访问,那么spring boot会直接报create bean error,从而启动不起来。

 

@Lazy

@Bean

public JaxWsPortProxyFactoryBean wsService() {



    try {

        JaxWsPortProxyFactoryBean proxy = new JaxWsPortProxyFactoryBean();

        proxy.setWsdlDocumentUrl(new URL(appealUrl));

        proxy.setServiceInterface(AppealWebService.class);



        Map<String, Object> customProperty = new HashMap<>(6);

        // 经过测试 当对端web service挂掉 这两个参数其作用

        customProperty.put("com.sun.xml.internal.ws.request.timeout", 5000);

        customProperty.put("com.sun.xml.internal.ws.connect.timeout", 5000);



        customProperty.put("com.sun.xml.ws.request.timeout", 5000);

        customProperty.put("com.sun.xml.ws.connect.timeout", 5000);

        customProperty.put("javax.xml.ws.client.receiveTimeout", 5000);

        customProperty.put("javax.xml.ws.client.connectionTimeout", 5000);

        proxy.setCustomProperties(customProperty);

        return proxy;

    } catch (Exception ex) {

        throw new BeanCreationException("创建wsService失败");

    }

}

 

 

解决方法就是比如说你在controller的类中,service的类中,@Autowired 这个webservicebean都加上@Lazy注解

@Autowired(required = false)

@Lazy

private AppealWebService appealWebService;

 

不要忘记外部的config类也要加上@Lazy注解

@Lazy

@Configuration

public class Ws12345Config {

 

 

你可能感兴趣的:(web,service)