【问题标题】:How to gracefully end test client如何优雅地结束测试客户端
【发布时间】:2018-06-07 20:39:39
【问题描述】:

我有一个简单的积聚测试客户端。代码运行良好,但代码完成时 jvm 不会停止。 一些线程仍在运行:MultiThreadedHttpConnectionManager。

如何优雅地关闭由以下人员分配的打开连接:

InventoryApi inventory = platform.getInventoryApi() ?

_

...
platform = new PlatformImpl(App.C8Y_URL, new CumulocityCredentials(App.C8Y_USER, App.C8Y_PWD));
InventoryApi inventory = platform.getInventoryApi();

GId testId = new GId("123456");
ManagedObjectRepresentation testDevice = inventory.get(testId);
MeasurementApi mApi = platform.getMeasurementApi();

MeasurementRepresentation measurement = new MeasurementRepresentation();
measurement.setType("c8y_SampleRate");
measurement.setSource(testDevice);

DateTime time = new DateTime();
System.out.println("time " + time.toString());
measurement.setDateTime(time);
Map<String, Object> flowRateFragment = App.createFlowRateFragment(new BigDecimal(20.5));
measurement.set(flowRateFragment, "c8y_SampleRate");

MeasurementRepresentation measurementCreation = mApi.create(measurement);
...

【问题讨论】:

    标签: java cumulocity


    【解决方案1】:

    优雅地关闭您的 Cumulocity 客户端调用platform.close()。

    【讨论】:

    • 这会导致异常Exception in thread "pool-1-thread-1" java.lang.RuntimeException: at com.cumulocity.sdk.client.buffering.MemoryBasedPersistentProvider.poll(MemoryBasedPersistentProvider.java:43) at com.cumulocity.sdk.client.buffering.BufferProcessor$1.run(BufferProcessor.java:42) Caused by: java.lang.InterruptedException at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.reportInterruptAfterWait(AbstractQueuedSynchronizer.java:2014)
    【解决方案2】:

    不,代码完成后您不能退出。 Cumulocity 代理是连续运行的,所以它们不会停止线程。 如果你想退出,那么你可以使用

    System.exit(0); //Change the status code accordingly.

    【讨论】:

      猜你喜欢
      • 2014-04-29
      • 1970-01-01
      • 2022-11-17
      • 1970-01-01
      • 1970-01-01
      • 2021-05-03
      • 2021-04-21
      • 1970-01-01
      • 2012-05-08
      相关资源
      最近更新 更多