|
18 | 18 | import io.atomix.catalyst.transport.Address; |
19 | 19 | import io.atomix.catalyst.transport.netty.NettyTransport; |
20 | 20 | import io.atomix.copycat.client.CopycatClient; |
| 21 | +import org.tron.core.consensus.common.GetQuery; |
| 22 | +import org.tron.core.consensus.common.PutCommand; |
| 23 | + |
21 | 24 | import java.net.InetAddress; |
22 | 25 | import java.net.UnknownHostException; |
23 | 26 | import java.util.Arrays; |
24 | 27 | import java.util.Collection; |
25 | 28 | import java.util.concurrent.CompletableFuture; |
26 | | -import org.tron.consensus.common.GetQuery; |
27 | | -import org.tron.consensus.common.PutCommand; |
28 | 29 |
|
29 | 30 | public class ClientTest { |
30 | | - public static void main(String[] args) { |
31 | | - CopycatClient.Builder builder = CopycatClient.builder(); |
| 31 | + public static void main(String[] args) { |
| 32 | + CopycatClient.Builder builder = CopycatClient.builder(); |
32 | 33 |
|
33 | | - builder.withTransport(NettyTransport.builder() |
34 | | - .withThreads(2) |
35 | | - .build()); |
| 34 | + builder.withTransport(NettyTransport.builder() |
| 35 | + .withThreads(2) |
| 36 | + .build()); |
36 | 37 |
|
37 | | - CopycatClient client = builder.build(); |
| 38 | + CopycatClient client = builder.build(); |
38 | 39 |
|
39 | | - client.serializer().register(PutCommand.class); |
40 | | - client.serializer().register(GetQuery.class); |
| 40 | + client.serializer().register(PutCommand.class); |
| 41 | + client.serializer().register(GetQuery.class); |
41 | 42 |
|
42 | | - InetAddress localhost = null; |
43 | | - try { |
44 | | - localhost = InetAddress.getLocalHost(); |
45 | | - System.out.println(localhost); |
46 | | - Collection<Address> cluster = Arrays.asList( |
47 | | - new Address(localhost.getHostAddress(), 5000) |
48 | | - ); |
49 | | - CompletableFuture<CopycatClient> future = client.connect(cluster); |
50 | | - future.join(); |
51 | | - } catch (UnknownHostException e) { |
52 | | - e.printStackTrace(); |
53 | | - } |
54 | | - client.submit(new GetQuery("block")).thenAccept(result1 -> { |
55 | | - System.out.println("foo is: " + result1.toString()); |
56 | | - }); |
57 | | - int i = 1; |
58 | | - //客户端提交查询 |
59 | | - boolean f = true; |
60 | | - while (f) { |
61 | | - String key = "block" + i; |
62 | | - Object result = client.submit(new GetQuery(key)).join(); |
63 | | - try { |
64 | | - if (!(result == null)) { |
65 | | - System.out.println("空指针异常没有发生,为null"); |
66 | | - System.out.println("Consensus " + key + " is: " + result); |
67 | | - f = true; |
68 | | - i = i + 1; |
69 | | - } else { |
70 | | - f = false; |
| 43 | + InetAddress localhost = null; |
| 44 | + try { |
| 45 | + localhost = InetAddress.getLocalHost(); |
| 46 | + System.out.println(localhost); |
| 47 | + Collection<Address> cluster = Arrays.asList( |
| 48 | + new Address(localhost.getHostAddress(), 5000) |
| 49 | + ); |
| 50 | + CompletableFuture<CopycatClient> future = client.connect(cluster); |
| 51 | + future.join(); |
| 52 | + } catch (UnknownHostException e) { |
| 53 | + e.printStackTrace(); |
71 | 54 | } |
| 55 | + client.submit(new GetQuery("block")).thenAccept(result1 -> { |
| 56 | + System.out.println("foo is: " + result1.toString()); |
| 57 | + }); |
| 58 | + int i = 1; |
| 59 | + //客户端提交查询 |
| 60 | + boolean f = true; |
| 61 | + while (f) { |
| 62 | + String key = "block" + i; |
| 63 | + Object result = client.submit(new GetQuery(key)).join(); |
| 64 | + try { |
| 65 | + if (!(result == null)) { |
| 66 | + System.out.println("空指针异常没有发生,为null"); |
| 67 | + System.out.println("Consensus " + key + " is: " + result); |
| 68 | + f = true; |
| 69 | + i = i + 1; |
| 70 | + } else { |
| 71 | + f = false; |
| 72 | + } |
72 | 73 |
|
73 | | - } catch (NullPointerException e) { |
74 | | - System.out.println("object == null不会导致空指针异常发生"); |
75 | | - f = false; |
76 | | - } |
| 74 | + } catch (NullPointerException e) { |
| 75 | + System.out.println("object == null不会导致空指针异常发生"); |
| 76 | + f = false; |
| 77 | + } |
77 | 78 |
|
| 79 | + } |
78 | 80 | } |
79 | | - } |
80 | 81 | } |
0 commit comments