• java webservice超时时间设置方法


    一、前言

    之前遇到过rabbitmq队列卡死的问题,有消费者,但是就不消费队列里的消息;

    后来发现是消费者执行webservice方法时,没有设置超时时间,默认永不超时,然后赶上对端系统出问题,方法就一直执行不完,队列消息也就一直卡住不动。(加try-catch是没有用的,catch不到,就是没有执行完一直卡住,因为超时时间默认永不超时)

    在此总结下java webservice设置超时时间的方法。

    二、代码

    		try {
    			JaxWsDynamicClientFactory dcf=JaxWsDynamicClientFactory.newInstance();
    			
    			//这里有2种方法,不确定到底是哪种,就都写上了
                dcf.getJaxbContextProperties().put("com.sun.xml.ws.request.timeout", 10000);
                dcf.getJaxbContextProperties().put("com.sun.xml.ws.connect.timeout", 10000);
                dcf.getJaxbContextProperties().put("com.sun.xml.internal.ws.connection.timeout", 10000);//建立连接的超时时间为10秒
                dcf.getJaxbContextProperties().put("com.sun.xml.internal.ws.request.timeout", 10000);//指定请求的响应超时时间为10秒
    
    			LOGGER.info("设置超时时间");
    			Client client= dcf.createClient("http://128.0.0.1/A/services/BService?wsdl");
    
                //这里再设置超时时间好像也行
    			//client.getRequestContext().put("com.sun.xml.ws.request.timeout", 10000);
    			//client.getRequestContext().put("com.sun.xml.ws.connect.timeout", 10000);
                //client.getRequestContext().put("com.sun.xml.internal.ws.connection.timeout", 10000);//建立连接的超时时间为10秒
                //client.getRequestContext().put("com.sun.xml.internal.ws.request.timeout", 10000);//指定请求的响应超时时间为10秒
    
    
    			//如果不设置超时时间,那么如果连接不通,就会卡在这一步
    			Object[] resultObj = client.invoke("createOrenableAccount", new Object[] { accountToXML("abc") });
    			String retXML = resultObj[0].toString();
    			StringReader read = new StringReader(retXML);
    			InputSource source = new InputSource(read);
    			SAXBuilder sb = new SAXBuilder();
    			Document doc = sb.build(source);
    			Element root = doc.getRootElement();
    		    LOGGER.info(root.getChildText("code"));// 0  成功
    			LOGGER.info(root.getChildText("code") + ", " + root.getChildText("message"));
    		} catch (Exception e) {
    			LOGGER.error((new StringBuilder("invokeWS Exception:")).append(e).toString(),e);
    		}
    		
    ------------------
    	public static String accountToXML(String userName) {
    		Document document = null;
    		Element et = new Element("account");
    		document = new Document(et);
    		document = addNode(document, "appname", "AI");
    		document = addNode(document, "uid", userName);
    		document = addNode(document, "eruid", userName);
    		return documentStr(document);
    	}
    
    -------------------
    	public static String documentStr(Document document) {
    		XMLOutputter xop = new XMLOutputter();
    		String xmlStr = xop.outputString(document);
    		return xmlStr;
    	}	
    
    ------------------------
    pom.xml是这样:
    		
    		
    			org.apache.cxf
    			cxf-rt-rs-client
    			3.0.0
    		
    		
    		  org.apache.cxf
    		  cxf-rt-frontend-jaxws
    		  3.0.0
    		
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
  • 相关阅读:
    代码质量提升之注解一
    K8S集群中部署服务之应用环境配置
    Linux下Couchbase的安装&升级&维护最佳指南
    安装facebook/wdt备忘
    沈阳陪诊系统|沈阳陪诊系统开发|沈阳陪诊系统功能和优势
    带你玩转Redis 的String 数据类型
    【Python&语义分割】Segment Anything(SAM)模型交互式分割+掩膜保存(三)
    SpringCloud入门 1.Eureka Server安装 2.基础跨进程调用
    C++高频面试题总结
    【简单粗暴】Python导入cv2包
  • 原文地址:https://blog.csdn.net/BHSZZY/article/details/133792737