コード例 #1
0
  @Override
  public void doWork() {
    try {
      ConsumerRecords<byte[], byte[]> records = consumer.poll(Long.MAX_VALUE);
      for (ConsumerRecord<byte[], byte[]> record : records) {
        K messageKey = null;
        try {
          messageKey = this.serializer.deserializeKey(record.key());
        } catch (SerializationException e) {
          log.error("Failed to deserialize the schema or config key", e);
          continue;
        }

        if (messageKey.equals(noopKey)) {
          // If it's a noop, update local offset counter and do nothing else
          try {
            offsetUpdateLock.lock();
            offsetInSchemasTopic = record.offset();
            offsetReachedThreshold.signalAll();
          } finally {
            offsetUpdateLock.unlock();
          }
        } else {
          V message = null;
          try {
            message =
                record.value() == null
                    ? null
                    : serializer.deserializeValue(messageKey, record.value());
          } catch (SerializationException e) {
            log.error("Failed to deserialize a schema or config update", e);
            continue;
          }
          try {
            log.trace(
                "Applying update (" + messageKey + "," + message + ") to the local " + "store");
            if (message == null) {
              localStore.delete(messageKey);
            } else {
              localStore.put(messageKey, message);
            }
            this.storeUpdateHandler.handleUpdate(messageKey, message);
            try {
              offsetUpdateLock.lock();
              offsetInSchemasTopic = record.offset();
              offsetReachedThreshold.signalAll();
            } finally {
              offsetUpdateLock.unlock();
            }
          } catch (StoreException se) {
            log.error("Failed to add record from the Kafka topic" + topic + " the local store");
          }
        }
      }
    } catch (WakeupException we) {
      // do nothing because the thread is closing -- see shutdown()
    } catch (RecordTooLargeException rtle) {
      throw new IllegalStateException(
          "Consumer threw RecordTooLargeException. A schema has been written that "
              + "exceeds the default maximum fetch size.",
          rtle);
    } catch (RuntimeException e) {
      log.error("KafkaStoreReader thread has died for an unknown reason.");
      throw new RuntimeException(e);
    }
  }
コード例 #2
0
ファイル: App.java プロジェクト: csiems/ShoeStore
  public static void main(String[] args) {
    staticFileLocation("/public");
    String layout = "templates/layout.vtl";

    get(
        "/",
        (request, response) -> {
          response.redirect("/stores");
          return null;
        });

    get(
        "/stores",
        (request, response) -> {
          HashMap<String, Object> model = new HashMap<String, Object>();
          model.put("stores", Store.all());
          model.put("template", "templates/stores.vtl");

          return new ModelAndView(model, layout);
        },
        new VelocityTemplateEngine());

    get(
        "/stores/:id",
        (request, response) -> {
          HashMap<String, Object> model = new HashMap<String, Object>();
          model.put("store", Store.find(Integer.parseInt(request.params("id"))));
          model.put("brands", Brand.all());
          model.put("template", "templates/store.vtl");
          return new ModelAndView(model, layout);
        },
        new VelocityTemplateEngine());

    post(
        "/stores/addstore",
        (request, response) -> {
          Store store = new Store(request.queryParams("store-name"));
          store.save();
          response.redirect("/stores/" + store.getId());
          return null;
        });

    post(
        "/stores/:id/addbrand",
        (request, response) -> {
          Store store = Store.find(Integer.parseInt(request.queryParams("addbrand-store-id")));
          store.add(Integer.parseInt(request.queryParams("addbrand-brand-id")));
          response.redirect("/stores/" + store.getId());
          return null;
        });

    post(
        "/stores/:id/unlinkbrand",
        (request, response) -> {
          Store store = Store.find(Integer.parseInt(request.params("id")));
          store.unlink(Integer.parseInt(request.queryParams("unlink-brand")));
          response.redirect("/stores/" + store.getId());
          return null;
        });

    post(
        "/stores/:id/update",
        (request, response) -> {
          Store store = Store.find(Integer.parseInt(request.params("id")));
          store.update(request.queryParams("update-store-name"));
          response.redirect("/stores/" + store.getId());
          return null;
        });

    post(
        "/stores/deletestore",
        (request, response) -> {
          Store store = Store.find(Integer.parseInt(request.queryParams("store-id")));
          store.delete();
          response.redirect("/stores");
          return null;
        });

    get(
        "/brands",
        (request, response) -> {
          HashMap<String, Object> model = new HashMap<String, Object>();
          model.put("template", "templates/brands.vtl");
          model.put("brands", Brand.all());
          return new ModelAndView(model, layout);
        },
        new VelocityTemplateEngine());

    get(
        "/brands/:id",
        (request, response) -> {
          HashMap<String, Object> model = new HashMap<String, Object>();
          model.put("brand", Brand.find(Integer.parseInt(request.params("id"))));
          model.put("stores", Store.all());
          model.put("template", "templates/brand.vtl");
          return new ModelAndView(model, layout);
        },
        new VelocityTemplateEngine());

    post(
        "/brands/addbrand",
        (request, response) -> {
          Brand brand = new Brand(request.queryParams("brand-name"));
          brand.save();
          response.redirect("/brands/" + brand.getId());
          return null;
        });

    post(
        "/brands/:id/addstore",
        (request, response) -> {
          Brand brand = Brand.find(Integer.parseInt(request.queryParams("addstore-brand-id")));
          brand.add(Integer.parseInt(request.queryParams("addstore-store-id")));
          response.redirect("/brands/" + brand.getId());
          return null;
        });

    post(
        "/brands/:id/unlinkstore",
        (request, response) -> {
          Brand brand = Brand.find(Integer.parseInt(request.params("id")));
          brand.unlink(Integer.parseInt(request.queryParams("unlink-store")));
          response.redirect("/brands/" + brand.getId());
          return null;
        });

    post(
        "/brands/:id/update",
        (request, response) -> {
          Brand brand = Brand.find(Integer.parseInt(request.params("id")));
          brand.update(request.queryParams("update-brand-name"));
          response.redirect("/brands/" + brand.getId());
          return null;
        });

    post(
        "/brands/deletebrand",
        (request, response) -> {
          Brand brand = Brand.find(Integer.parseInt(request.queryParams("brand-id")));
          brand.delete();
          response.redirect("/brands");
          return null;
        });
  }