public static void main(String[] args) throws Exception {
try (DaprClient client = new DaprClientBuilder().build()) {
System.out.println("Waiting for Dapr sidecar ...");
client.waitForSidecar(3000).block();
System.out.println("Dapr sidecar is ready.");
String message = args.length == 0 ? "wang test string " : args[0];
MyClass myClass = new MyClass();
myClass.message = message;
MyClass secondState = new MyClass();
secondState.message = "test message";
client.saveState(STATE_STORE_NAME, FIRST_KEY_NAME, myClass).block();
System.out.println("Saving class with message 用消息保存类: " + message);
Mono<State<MyClass>> retrievedMessageMono = client.getState(STATE_STORE_NAME, FIRST_KEY_NAME, MyClass.class);
System.out.println("Retrieved class message from state 从状态检索到类消息: " + (retrievedMessageMono.block().getValue()).message);
System.out.println("Updating previous state and adding another state 'test state 更新以前的状态并添加另一个状态的测试状态'... ");
myClass.message = message + " updated";
System.out.println("Saving updated class with message 使用消息保存更新的类: " + myClass.message);
// execute transaction
List<TransactionalStateOperation<?>> operationList = new ArrayList<>();
operationList.add(new TransactionalStateOperation<>(TransactionalStateOperation.OperationType.UPSERT,
new State<>(FIRST_KEY_NAME, myClass, "")));
operationList.add(new TransactionalStateOperation<>(TransactionalStateOperation.OperationType.UPSERT,
new State<>(SECOND_KEY_NAME, secondState, "")));
client.executeStateTransaction(STATE_STORE_NAME, operationList).block();
// get multiple states
Mono<List<State<MyClass>>> retrievedMessagesMono = client.getBulkState(STATE_STORE_NAME,
Arrays.asList(FIRST_KEY_NAME, SECOND_KEY_NAME), MyClass.class);
System.out.println("Retrieved messages using bulk get 使用批量获取检索到的消息:");
retrievedMessagesMono.block().forEach(System.out::println);
System.out.println("Deleting states 删除状态...");
System.out.println("Verify delete key request is aborted if an etag different from stored is passed 如果传递的etag不同于存储的etag,则验证删除密钥请求是否中止.");
// delete state API
try {
client.deleteState(STATE_STORE_NAME, FIRST_KEY_NAME, "100", null).block();
} catch (DaprException ex) {
if (ex.getErrorCode().equals(Status.Code.ABORTED.toString())) {
// Expected error due to etag mismatch.
System.out.println(String.format("Expected failure 预期的失败。. %s", ex.getErrorCode()));
} else {
System.out.println("Unexpected exception 意外异常.");
throw ex;
}
}
Exception in thread "main" java.lang.NoSuchFieldError: Companion
at io.dapr.client.DaprHttp.(DaprHttp.java:117)
at io.dapr.client.DaprHttpBuilder.buildDaprHttp(DaprHttpBuilder.java:55)
at io.dapr.client.DaprHttpBuilder.build(DaprHttpBuilder.java:35)
at io.dapr.client.DaprClientBuilder.buildDaprClientHttp(DaprClientBuilder.java:159)
at io.dapr.client.DaprClientBuilder.buildDaprClient(DaprClientBuilder.java:126)
at io.dapr.client.DaprClientBuilder.build(DaprClientBuilder.java:106)
at com.dapeng.demodapr1.testone.TestOneController.main(TestOneController.java:44)
Disconnected from the target VM, address: '127.0.0.1:65365', transport: 'socket'