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

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 01: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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
    半盏清暑茶 2021-10-18 01:53

    代码块ScriptBlock也适用于其他执行耗时长的指令、脚本文件(bat ps1)之类的,自定义要完成的逻辑;

    回复
编辑
预览

报告相同问题?

问题事件

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

悬赏问题

  • ¥15 以AT89C51单片机芯片为核心来制作一个简易计算器,外部由4*4矩阵键盘和一个LCD1602字符型液晶显示屏构成,内部由一块AT89C51单片机构成,通过软件编程可实现简单加减乘除。
  • ¥15 某东JD算法逆向算法
  • ¥15 求GCMS辅导数据分析
  • ¥30 SD中的一段Unet下采样代码其中的resnet是谁跟谁进行残差连接
  • ¥15 Unet采样阶段的res_samples问题
  • ¥60 Python+pygame坦克大战游戏开发实验报告
  • ¥15 R语言regionNames()和demomap()无法选中中文地区的问题
  • ¥15 Open GL ES 的使用
  • ¥15 我如果只想表示节点的结构信息,使用GCN方法不进行训练可以吗
  • ¥15 QT6将音频采样数据转PCM
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部