【问题标题】:reading variable value in play WS response scope在播放 WS 响应范围中读取变量值
【发布时间】:2016-05-26 13:12:42
【问题描述】:
void makePdfPage(String url, PdfContentByte contentByte){
    com.itextpdf.text.Font sans = UtilityMethods.getSansSerifFont(14);
    sans.setColor(80,147,225);
    ColumnText ct = new ColumnText(contentByte);
    ct.setSimpleColumn("Hello", 0, 780, 595, 830, 10, Element.ALIGN_CENTER);
    try {
        ct.go();
    } catch (DocumentException e) {
        System.out.println(e);
        // TODO Auto-generated catch block
        e.printStackTrace();
    }



  Promise<WSResponse> out = notification.call(url);
    out.map(resp->{
        Map<String,Object> mapp= Json.fromJson(resp.asJson().get("list"), Map.class);
        PdfService.designPdf(mapp, contentByte);
        return resp;
    });
}

contentByte 将变为空 desginPdf

它是异步的,所以它没有 contentByte 的值,可以任何其他方式,以便我可以同步使用或任何其他方式来解决我的问题。

WSResponse resp = out.get(10000);

获取失败

【问题讨论】:

  • contentByte 是从哪里来的?它是一个全局变量吗?还是在响应块中可用?

标签: java playframework java-8 promise playframework-2.3


【解决方案1】:

您应该将 contentByte 变量声明为 final

void makePdfPage(String url, final PdfContentByte contentByte){

此外,您应该为失败情况添加恢复代码

 map(...)
 .recover(new Function<Throwable,JsonNode>() {
   public JsonNode apply(Throwable ex) {
        if(Logger.isErrorEnabled()){
            Logger.error("retrieving api info",ex);
        }
        return null; //TODO
    }
  });

【讨论】:

    【解决方案2】:

    我没有使用 Java Promise 的经验,但根据我在 Scala 方面的经验,我会尝试这样的事情:

    Promise<WSResponse> out = notification.call(url);
    WSResponse res = out.map(resp->{
      Map<String,Object> mapp= Json.fromJson(resp.asJson().get("list"), Map.class);
      PdfService.designPdf(mapp, contentByte);
      return resp;
    });
    //Do something with res
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-12-16
      • 1970-01-01
      • 2016-11-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多