半盏清暑茶 2021-10-15 10:37 采纳率: 100%
浏览 220
已结题

Java应用winrm执行powershell指令(时间比较长),等待接收返回值时报400

项目需要,将压缩文件上传到WINDOWS服务器上,并解压,程序部署在LINUX服务器上;
文件123.zip上传正常进行的,现在执行解压,手动执行指令解压需要3-5分钟(),
部分代码如下:

WinRmClientContext context = null;
try {
    context = WinRmClientContext.newInstance();
    WinRmTool wTool = WinRmTool.Builder.builder("100.xx.xx.xx", "administrator", "123123")
            .authenticationScheme(AuthSchemes.NTLM)
            .port(5985)
            .useHttps(false)
            .context(context)
            .build();
    wTool.setConnectionTimeout(10 * 60 * 1000L);
    wTool.setOperationTimeout(10 * 60 * 1000L);
    WinRmToolResponse wResponse = wTool.executePs("if (Test-Path 'c:\\123.zip'){Expand-Archive -LiteralPath 'c:\\123.zip' -DestinationPath 'c:\\' -Force};Test-Path 'c:\\123'");
    System.out.println(wResponse.getStatusCode());
    System.out.println(wResponse.getStdOut());
        System.out.println(wResponse.getStdErr());
} catch (Exception e) {
    e.printStackTrace();
    System.out.println("exitCode: -1");
} finally {
    if (context != null) {
        context.shutdown();
    }
}

调试执行后,报错如下:

09:53:24.297 [Thread-0] DEBUG io.cloudsoft.winrm4j.client.RetryingProxyHandler - failed task "delete" after 2 attempt(s), rethrowing first exception
java.lang.RuntimeException: failed task "signal" after 2 attempt(s)
    at io.cloudsoft.winrm4j.client.RetryingProxyHandler.invokeWithRetry(RetryingProxyHandler.java:79)
    at io.cloudsoft.winrm4j.client.RetryingProxyHandler.invoke(RetryingProxyHandler.java:31)
    at com.sun.proxy.$Proxy47.signal(Unknown Source)
    at io.cloudsoft.winrm4j.client.ShellCommand.releaseCommand(ShellCommand.java:212)
    at io.cloudsoft.winrm4j.client.ShellCommand.execute(ShellCommand.java:102)
    at io.cloudsoft.winrm4j.winrm.WinRmTool.executeCommand(WinRmTool.java:398)
    at io.cloudsoft.winrm4j.winrm.WinRmTool.executePs(WinRmTool.java:422)
    at io.cloudsoft.winrm4j.winrm.WinRmTool.executePs(WinRmTool.java:413)
    at com.inspur.util.WindowsCommandUtil.lambda$0(WindowsCommandUtil.java:296)
    at java.lang.Thread.run(Thread.java:745)
    Suppressed: java.lang.RuntimeException: failed task "delete" after 2 attempt(s)
        at io.cloudsoft.winrm4j.client.RetryingProxyHandler.invokeWithRetry(RetryingProxyHandler.java:79)
        at io.cloudsoft.winrm4j.client.RetryingProxyHandler.invoke(RetryingProxyHandler.java:31)
        at com.sun.proxy.$Proxy47.delete(Unknown Source)
        at io.cloudsoft.winrm4j.client.ShellCommand.close(ShellCommand.java:219)
        at io.cloudsoft.winrm4j.winrm.WinRmTool.executeCommand(WinRmTool.java:401)
        ... 4 more
    Caused by: javax.xml.ws.WebServiceException: Could not send Message.
        at org.apache.cxf.jaxws.JaxWsClientProxy.mapException(JaxWsClientProxy.java:183)
        at org.apache.cxf.jaxws.JaxWsClientProxy.invoke(JaxWsClientProxy.java:145)
        at com.sun.proxy.$Proxy46.delete(Unknown Source)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:497)
        at io.cloudsoft.winrm4j.client.RetryingProxyHandler.invokeWithRetry(RetryingProxyHandler.java:44)
        ... 8 more
    Caused by: org.apache.cxf.transport.http.HTTPException: HTTP response '400: ' when communicating with http://100.xx.xx.xx:5985/wsman
        at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.doProcessResponseCode(HTTPConduit.java:1619)
        at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.handleResponseInternal(HTTPConduit.java:1626)
        at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.handleResponse(HTTPConduit.java:1571)
        at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.close(HTTPConduit.java:1371)
        at org.apache.cxf.transport.http.asyncclient.AsyncHTTPConduit$AsyncWrappedOutputStream.close(AsyncHTTPConduit.java:429)
        at org.apache.cxf.io.CachedOutputStream.close(CachedOutputStream.java:223)
        at org.apache.cxf.transport.AbstractConduit.close(AbstractConduit.java:56)
        at org.apache.cxf.transport.http.HTTPConduit.close(HTTPConduit.java:671)
        at org.apache.cxf.interceptor.MessageSenderInterceptor$MessageSenderEndingInterceptor.handleMessage(MessageSenderInterceptor.java:63)
        at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:308)
        at org.apache.cxf.endpoint.ClientImpl.doInvoke(ClientImpl.java:530)
        at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:441)
        at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:356)
        at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:314)
        at org.apache.cxf.frontend.ClientProxy.invokeSync(ClientProxy.java:96)
        at org.apache.cxf.jaxws.JaxWsClientProxy.invoke(JaxWsClientProxy.java:140)
        ... 14 more
Caused by: javax.xml.ws.WebServiceException: Could not send Message.
    at org.apache.cxf.jaxws.JaxWsClientProxy.mapException(JaxWsClientProxy.java:183)
    at org.apache.cxf.jaxws.JaxWsClientProxy.invoke(JaxWsClientProxy.java:145)
    at com.sun.proxy.$Proxy46.signal(Unknown Source)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at io.cloudsoft.winrm4j.client.RetryingProxyHandler.invokeWithRetry(RetryingProxyHandler.java:44)
    ... 9 more
Caused by: org.apache.cxf.transport.http.HTTPException: HTTP response '400: ' when communicating with http://100.xx.xx.xx:5985/wsman
    at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.doProcessResponseCode(HTTPConduit.java:1619)
    at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.handleResponseInternal(HTTPConduit.java:1626)
    at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.handleResponse(HTTPConduit.java:1571)
    at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.close(HTTPConduit.java:1371)
    at org.apache.cxf.transport.http.asyncclient.AsyncHTTPConduit$AsyncWrappedOutputStream.close(AsyncHTTPConduit.java:429)
    at org.apache.cxf.io.CachedOutputStream.close(CachedOutputStream.java:223)
    at org.apache.cxf.transport.AbstractConduit.close(AbstractConduit.java:56)
    at org.apache.cxf.transport.http.HTTPConduit.close(HTTPConduit.java:671)
    at org.apache.cxf.interceptor.MessageSenderInterceptor$MessageSenderEndingInterceptor.handleMessage(MessageSenderInterceptor.java:63)
    at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:308)
    at org.apache.cxf.endpoint.ClientImpl.doInvoke(ClientImpl.java:530)
    at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:441)
    at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:356)
    at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:314)
    at org.apache.cxf.frontend.ClientProxy.invokeSync(ClientProxy.java:96)
    at org.apache.cxf.jaxws.JaxWsClientProxy.invoke(JaxWsClientProxy.java:140)
    ... 15 more

目前尝试的方向:
1.创建Start-Job后台执行,返回值Job创建成功,但实际并没有执行解压;
2.自定义winrm4j的RetryingProxyHandler,还在探索中;
请教各位,有没有什么好的解决方法?

  • 写回答

1条回答 默认 最新

  • 半盏清暑茶 2021-10-18 09:48
    关注

    自问自答了:
    1.原先pom.xml引用的是winrm4j是0.12.1版本,后面引入了0.12.3版本;
    2.执行指令前增加了重试策略,并改为Job的方式,代码如下:

    WinRmTool wTool = WinRmTool.Builder.builder("100.xx.xx.xx", "administrator", "123123")
                        .authenticationScheme(AuthSchemes.NTLM)
                        .port(5985)
                        .useHttps(false)
                        .context(context)
                        .build();
                wTool.setConnectionTimeout(10 * 60 * 1000L);
                wTool.setOperationTimeout(10 * 60 * 1000L);
                // 最大尝试100,每隔3s一次
                wTool.setFailureRetryPolicy(100, 3, TimeUnit.SECONDS);
                WinRmToolResponse wResponse = wTool.executePs("$job=Start-Job -ScriptBlock {if (Test-Path 'c:\\123.zip'){Expand-Archive -LiteralPath 'c:\\123.zip' -DestinationPath 'c:\\' -Force};Test-Path 'c:\\123'}\r\n"
                        + "$job|Wait-Job Timeout 300000\r\n"
                        + "if ($job.State -ne 'Completed'){\r\n"
                        + "    $job|Stop-Job|Remove-Job\r\n"
                        + "    $null\r\n"
                        + "} else {\r\n"
                        + "    $job|Receive-Job\r\n"
                        + "}");
            System.out.println(wResponse.getStatusCode());
            System.out.println(wResponse.getStdOut());
            System.out.println(wResponse.getStdErr());
    

    其中300000为5 * 60 * 1000L,设定了5min,可以根据文件不同进行修正;
    参考:https://www.pstips.net/invoke-command-timeout.html

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 10月26日
  • 已采纳回答 10月18日
  • 创建了问题 10月15日

悬赏问题

  • ¥15 C# 调用Bartender打印机打印
  • ¥100 华为ensp只要2-9实验运行结果能做的来加我QQ
  • ¥15 我这个代码哪里有问题 acm 平台上显示错误 90%,我自己运行好像没什么问题
  • ¥50 C#编程中使用printDocument类实现文字排版打印问题
  • ¥15 找会编程的帅哥美女 可以用MATLAB里面的simulink编程,用Keil5编也可以。
  • ¥15 已知隐函数其中一个变量τ的具体值,求另一个变量
  • ¥15 r语言Hurst指数
  • ¥15 Acrn IVSHMEM doorbell问题
  • ¥15 yolov5中的val测试集训练时数量变小问题
  • ¥15 MPLS/VPN实验中MPLS的配置问题