Beispiel #1
0
  /** 定期的実行のメイン・ルーチン */
  public void run() {

    try {
      Thread.sleep(5000);
    } catch (Exception e) {
    }

    init();

    Storage2Admin admin = Storage2Admin.getInstance();
    admin.log(
        "fiap.client.writecopy.run.begin",
        "fiapURI="
            + m_FIAPURI
            + "; frequency="
            + m_Frequency
            + "; mode="
            + m_Mode
            + "; offset="
            + m_OffSet,
        false);

    long time = System.currentTimeMillis() / 1000;
    long lastWRITEtime = 0;
    while (true) {
      try {
        if (time < System.currentTimeMillis() / 1000) {
          time++;
        }
        if ((time + m_OffSet) % m_Frequency == 0 && time >= lastWRITEtime) {
          lastWRITEtime = time;

          // Find points to retrieve by FETCH
          java.util.ArrayList<String> idArray =
              new java.util.ArrayList<String>(); // idArray -- 取得するべき point idのみをリストアップする				
          String[] ids = new String[0];
          ids = m_PointMap.keySet().toArray(ids);
          for (int i = 0; i < ids.length; i++) {
            if (m_PointMap.get(ids[i]).hasPoint()) {
              idArray.add(ids[i]);
            }
          }
          long startTime = (time - m_Frequency + m_OffSet) * 1000;
          long endTime = (time + m_OffSet) * 1000;
          String gt =
              org.fiap.util.W3CTimestamp.toString(startTime, java.util.TimeZone.getDefault());
          String lteq =
              org.fiap.util.W3CTimestamp.toString(endTime, java.util.TimeZone.getDefault());

          // Generate Query
          Query query = new Query();
          String id = java.util.UUID.randomUUID().toString();
          Uuid uuid = new Uuid();
          uuid.setUuid(id);
          query.setId(uuid);
          query.setType(QueryType.storage);
          query.setAcceptableSize(new org.apache.axis2.databinding.types.PositiveInteger("1000"));
          for (int i = 0; i < idArray.size(); i++) {
            Key key = new Key();
            key.setId(new org.apache.axis2.databinding.types.URI(idArray.get(i)));
            key.setAttrName(AttrNameType.time);
            if (m_Mode.equals("diff")) {
              key.setGteq(gt);
              key.setLt(lteq);
            }
            if (m_Mode.equals("latest")) {
              key.setSelect(SelectType.maximum);
            }
            query.addKey(key);
          }

          // FETCH repeatedly from the local storage and WRITE to a remote server specified by
          // fiapURI
          FIAPWS srv = new FIAPWSStub(m_FIAPURI);
          boolean eof = false;
          while (!eof) {
            Transport request = new Transport();
            Header hreq = new Header();

            hreq.setQuery(query);
            request.setHeader(hreq);

            Transport response = null;
            admin.log(
                "fiap.client.writecopy.run.query.begin",
                "fiapURI="
                    + m_FIAPURI
                    + "; frequency="
                    + m_Frequency
                    + "; mode="
                    + m_Mode
                    + "; offset="
                    + m_OffSet,
                false);
            try {
              response = Storage2.m_GUTUnit.query(request, "127.0.0.1");
              Header hres = response.getHeader();

              if (hres.getOK() != null) {
                Query qres = hres.getQuery();
                Uuid cursor = qres.getCursor();
                if (cursor == null) {
                  eof = true;
                } else {
                  query = qres;
                }
                Body body = response.getBody();

                PointSet[] ps = body.getPointSet();
                Point[] p = body.getPoint();

                // Create request to WRITE data to a remote fiapURI.
                Transport remote_transport = new Transport();
                Body remote_body = new Body();
                remote_transport.setBody(remote_body);
                if (ps != null && ps.length != 0) {
                  remote_body.setPointSet(ps);
                }
                if (p != null && p.length != 0) {
                  remote_body.setPoint(p);
                }

                DataRQ dataRQ = new DataRQ();
                dataRQ.setTransport(remote_transport);

                DataRS dataRS = null;
                admin.log(
                    "fiap.client.writecopy.run.write.begin",
                    "fiapURI="
                        + m_FIAPURI
                        + "; frequency="
                        + m_Frequency
                        + "; mode="
                        + m_Mode
                        + "; offset="
                        + m_OffSet,
                    true);
                try {
                  dataRS = srv.data(dataRQ);
                  remote_transport = dataRS.getTransport();
                  Header remote_header = remote_transport.getHeader();

                  if (remote_header.getOK() != null) {
                    // Do nothing.
                  } else if (remote_header.getError() != null) {
                    org.fiap.types.Error error = remote_header.getError();
                    admin.log(
                        "fiap.client.writecopy.run.write.error",
                        "fiapURI="
                            + m_FIAPURI
                            + "; frequency="
                            + m_Frequency
                            + "; mode="
                            + m_Mode
                            + "; offset="
                            + m_OffSet
                            + "; errorType="
                            + error.getType()
                            + "; errorMessage="
                            + error.getString(),
                        true);
                  }
                } catch (Exception e) {
                  admin.log(
                      "fiap.client.writecopy.run.write.error",
                      "fiapURI="
                          + m_FIAPURI
                          + "; frequency="
                          + m_Frequency
                          + "; mode="
                          + m_Mode
                          + "; offset="
                          + m_OffSet
                          + "; error="
                          + e.getMessage(),
                      true);
                } finally {
                  admin.log(
                      "fiap.client.writecopy.run.write.end",
                      "fiapURI="
                          + m_FIAPURI
                          + "; frequency="
                          + m_Frequency
                          + "; mode="
                          + m_Mode
                          + "; offset="
                          + m_OffSet,
                      false);
                }

              } else if (hres.getError() != null) {
                org.fiap.types.Error error = hres.getError();
                admin.log(
                    "fiap.client.writecopy.run.query.error",
                    "fiapURI="
                        + m_FIAPURI
                        + "; frequency="
                        + m_Frequency
                        + "; mode="
                        + m_Mode
                        + "; offset="
                        + m_OffSet
                        + "; errorType="
                        + error.getType()
                        + "; errorMessage="
                        + error.getString(),
                    true);
              }
            } catch (Exception e) {
              admin.log(
                  "fiap.client.writecopy.run.query.error",
                  "fiapURI="
                      + m_FIAPURI
                      + "; frequency="
                      + m_Frequency
                      + "; mode="
                      + m_Mode
                      + "; offset="
                      + m_OffSet
                      + "; error="
                      + e.getMessage(),
                  true);
            } finally {
              admin.log(
                  "fiap.client.writecopy.run.query.end",
                  "fiapURI="
                      + m_FIAPURI
                      + "; frequency="
                      + m_Frequency
                      + "; mode="
                      + m_Mode
                      + "; offset="
                      + m_OffSet,
                  false);
            }
          }
        }
      } catch (Exception e) {
        // e.printStackTrace();
        admin.log(
            "fiap.client.writecopy.run.error",
            "fiapURI="
                + m_FIAPURI
                + "; frequency="
                + m_Frequency
                + "; mode="
                + m_Mode
                + "; offset="
                + m_OffSet
                + "; fatalerror="
                + e.getMessage(),
            true);
      }

      try {
        sleep(500);
      } catch (Exception e) {
      }
    }
  }
Beispiel #2
0
  /** PointSet木を ローカルのStorageに作成する */
  public void init() {

    Storage2Admin admin = Storage2Admin.getInstance();
    admin.log(
        "fiap.client.writecopy.init.begin",
        "fiapURI="
            + m_FIAPURI
            + "; frequency="
            + m_Frequency
            + "; mode="
            + m_Mode
            + "; offset="
            + m_OffSet,
        false);

    java.util.Map<String, PointData> roots = new java.util.Hashtable<String, PointData>();
    java.util.Map<String, PointData> points = new java.util.Hashtable<String, PointData>();
    String[] keys = new String[0];
    keys = m_PointMap.keySet().toArray(keys);
    for (int i = 0; i < keys.length; i++) {
      PointData obj = m_PointMap.get(keys[i]);
      PointData clone = null;
      if (obj.hasPointSet()) {
        PointSet clonePS = new PointSet();
        clonePS.setId(obj.getPointSet().getId());
        clone = new PointData(clonePS);
      } else if (obj.hasPoint()) {
        Point cloneP = new Point();
        cloneP.setId(obj.getPoint().getId());
        clone = new PointData(cloneP);
      }
      if (m_PointTreeRoots.containsKey(keys[i])) {
        roots.put(keys[i], clone);
      }
      points.put(keys[i], clone);
    }
    keys = new String[0];
    keys = m_ChildToParentMap.keySet().toArray(keys);
    for (int i = 0; i < keys.length; i++) {
      String id = keys[i];
      String parent = m_ChildToParentMap.get(id);
      PointData child = points.get(id);
      if (child.hasPointSet()) {
        points.get(parent).getPointSet().addPointSet(child.getPointSet());
      } else if (child.hasPoint()) {
        points.get(parent).getPointSet().addPoint(child.getPoint());
      }
    }

    keys = new String[0];
    keys = roots.keySet().toArray(keys);
    for (int i = 0; i < keys.length; i++) {
      PointData obj = roots.get(keys[i]);
      //	System.out.println(keys[i]);
      m_Parent.sendUpdate(obj);
    }

    admin.log(
        "fiap.client.writecopy.init.end",
        "fiapURI="
            + m_FIAPURI
            + "; frequency="
            + m_Frequency
            + "; mode="
            + m_Mode
            + "; offset="
            + m_OffSet,
        false);
  }