BOM添加Item的UpdateTable事件与性能分析

问题描述

客户在为BOM添加Item过程中自定义了一个UpdateTable Event事件,该事件需要对新加入的Item修改某些属性值,但发现每次添加一个Item,耗费好几秒才能完成,其中某一次性加入10个Item,共耗时40秒,性能极差。

分析

Agile PLM 9.3版本中为SDK开发引入了Event事件机制,可以让用户对具体的操作进行基于Java或Groovy的自定义的二次开发,大量拓展各种特殊的业务需求。该客户使用Java,基于Agile API的代码片段如下。
public class UpdateBOMAttr implements IEventAction {
  private static Integer ATT_READER = 2000008065;
  private static Integer ATT_EDITOR = 2000008066;
  public EventActionResult doAction(IAgileSession session, 
      INode actionNode, IEventInfo request) {
   try {
     IObjectEventInfo objectEventInfo = (IObjectEventInfo) request;     
     IDataObject affectedObject = objectEventInfo.getDataObject();  
     IItem item = (IItem)affectedObject;
     Object reader = item.getValue(ATT_READER);
     Object editor = item.getValue(ATT_EDITOR);
     ITable bom = item.getTable(ItemConstants.TABLE_BOM);
     Iterator cl = bom.getReferentIterator();
     
     String nowTime=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new java.util.Date());
     System.out.println(nowTime + " -- begin into while loop");
     while (cl.hasNext()) {
         IItem bomItem = (IItem)cl.next();
         bomItem.setValue(ATT_READER, reader);
         bomItem.setValue(ATT_EDITOR, editor);
         nowTime=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new java.util.Date());
         System.out.println(nowTime + " finished one Item --------------");
     }
     nowTime=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new java.util.Date());
     System.out.println(nowTime + " -- done all");
     ActionResult actionResult = new ActionResult(ActionResult.STRING, "ok_update attribute");
     return new EventActionResult(request, actionResult);
   } catch (Throwable th) {
     ActionResult actionResult = new ActionResult(ActionResult.STRING, th.getMessage());
     return new EventActionResult(request, null);
   }
 }
}

在while循环中,该程序需要对当前遍历的item设置两个属性bomItem.setValue。在客户的数据中中做测试后,查看打印信息

09/12/31 07:03:46 2009-12-31 07:03:46 -- begin into while loop
09/12/31 07:04:02 2009-12-31 07:04:02 finished one Item --------------
09/12/31 07:05:11 2009-12-31 07:05:11 finished one Item --------------
09/12/31 07:06:42 2009-12-31 07:06:42 finished one Item --------------
09/12/31 07:08:15 2009-12-31 07:08:15 finished one Item --------------
09/12/31 07:09:47 2009-12-31 07:09:47 finished one Item --------------
09/12/31 07:15:22 2009-12-31 07:15:22 finished one Item --------------

每个循环耗时1分30秒多。观察客户数据,原先BOM已有大量的Item。分析上述代码,会发现每次添加一个Item,代码均要扫描整个BOM表,为所有的Item都要重新设置一遍属性。代码逻辑冗余。

解决方法

在Agle API中,定义了专门针对新加入(或需要编辑的)的单独一行数据的类IEventDirtyRowUpdate,而引用此接口必须确保SDK得到的table也必须是实现了IEventDirtyTable接口的具体类。因此修改代码如下:
public class UpdateBOMAttr implements IEventAction {
  private static Integer ATT_READER = 2000008065;
  private static Integer ATT_EDITOR = 2000008066;


  public EventActionResult doAction(IAgileSession session, 
      INode actionNode, IEventInfo request) {
    try {


      // Get current Item's General Info attributes
      IObjectEventInfo objectEventInfo = (IObjectEventInfo) request;
      IDataObject affectedObject = objectEventInfo.getDataObject();
      IItem item = (IItem) affectedObject;
      Object reader = item.getValue(ATT_READER);
      Object editor = item.getValue(ATT_EDITOR);


      // get BOM table
      IUpdateTableEventInfo eventInfo = (IUpdateTableEventInfo) request;
      IEventDirtyTable table = eventInfo.getTable();
      Iterator it = table.iterator();


      String nowTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new java.util.Date());
      System.out.println(nowTime + " -- begin into while loop");
      // update the new BOM items
      while (it.hasNext()) {
        IEventDirtyRowUpdate row = (IEventDirtyRowUpdate) it.next();
        IItem bomItem = (IItem)row.getReferent();


        bomItem.setValue(ATT_READER, reader);
        bomItem.setValue(ATT_EDITOR, editor);
        nowTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new java.util.Date());
        System.out.println(nowTime + " finished one Item --------------");
      }
      nowTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new java.util.Date());
      System.out.println(nowTime + " -- done all");
      ActionResult actionResult = new ActionResult(ActionResult.STRING, "ok_update attribute");
      return new EventActionResult(request, actionResult);
    } catch (Throwable th) {
      ActionResult actionResult = new ActionResult(ActionResult.STRING, th.getMessage());
      return new EventActionResult(request, null);
    }
  }
}
重新查看输出的信息:
10/02/26 02:34:02 2010-02-26 02:34:02 -- begin into while loop
10/02/26 02:34:02 2010-02-26 02:34:02 finished one Item --------------
10/02/26 02:34:03 2010-02-26 02:34:03 finished one Item --------------
10/02/26 02:34:03 2010-02-26 02:34:03 -- done all

经测试,一次性加入90个Item,只需要9秒即可执行完整个自定义的Event代码。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值