9
9
import java .util .concurrent .TimeUnit ;
10
10
import java .util .concurrent .atomic .AtomicInteger ;
11
11
import java .util .stream .Collectors ;
12
- import org .fisco .bcos .sdk .tars .ConcurrentQueue ;
13
- import org .fisco .bcos .sdk .tars .ConcurrentQueueCallback ;
12
+ import org .fisco .bcos .sdk .tars .Callback ;
14
13
import org .fisco .bcos .sdk .tars .Config ;
15
14
import org .fisco .bcos .sdk .tars .CryptoSuite ;
16
15
import org .fisco .bcos .sdk .tars .LogEntry ;
@@ -35,22 +34,45 @@ public class TarsClient extends ClientImpl implements Client {
35
34
private static Logger logger = LoggerFactory .getLogger (TarsClient .class );
36
35
private RPCClient tarsRPCClient ;
37
36
private TransactionFactoryImpl transactionFactory ;
38
- private Thread queueThread ;
39
37
private ThreadPoolExecutor asyncThreadPool ;
38
+ private Callback callback ;
40
39
41
- static final int queueSize = 10 * 10000 ;
42
- static final String libFileName = System .mapLibraryName ("bcos_swig_java" );
40
+ private static final int queueSize = 10 * 10000 ;
41
+ private static final String libFileName = System .mapLibraryName ("bcos_swig_java" );
43
42
44
- private class CallbackContent {
45
- public SendTransaction sendTransaction ;
46
- TransactionCallback callback ;
47
- Transaction transaction ;
43
+ private static class Content {
44
+ private SendTransaction sendTransaction ;
45
+ private Transaction transaction ;
46
+ private TransactionCallback callback ;
47
+
48
+ public SendTransaction getSendTransaction () {
49
+ return sendTransaction ;
50
+ }
51
+
52
+ public void setSendTransaction (SendTransaction sendTransaction ) {
53
+ this .sendTransaction = sendTransaction ;
54
+ }
55
+
56
+ public Transaction getTransaction () {
57
+ return transaction ;
58
+ }
59
+
60
+ public void setTransaction (Transaction transaction ) {
61
+ this .transaction = transaction ;
62
+ }
63
+
64
+ public TransactionCallback getCallback () {
65
+ return callback ;
66
+ }
67
+
68
+ public void setCallback (TransactionCallback callback ) {
69
+ this .callback = callback ;
70
+ }
48
71
};
49
72
50
- ConcurrentQueue concurrentQueue = new ConcurrentQueue ();
51
- ConcurrentHashMap <Integer , CallbackContent > callbackMap =
52
- new ConcurrentHashMap <Integer , CallbackContent >();
53
- AtomicInteger callbackSeq = new AtomicInteger (0 );
73
+ ConcurrentHashMap <Integer , Content > callbackMap =
74
+ new ConcurrentHashMap <Integer , TarsClient .Content >();
75
+ AtomicInteger currentSeq = new AtomicInteger ();
54
76
55
77
public RPCClient getTarsRPCClient () {
56
78
return tarsRPCClient ;
@@ -78,38 +100,37 @@ protected TarsClient(String groupID, ConfigOption configOption, long nativePoint
78
100
Config config = new Config ();
79
101
config .setConnectionString (connectionString );
80
102
config .setSendQueueSize (queueSize );
81
- config .setTimeoutMs (configOption . getNetworkConfig (). getTimeout () * 1000 );
103
+ config .setTimeoutMs (60 * 1000 );
82
104
tarsRPCClient = new RPCClient (config );
83
105
84
106
CryptoSuite cryptoSuite =
85
107
bcos .newCryptoSuite (configOption .getCryptoMaterialConfig ().getUseSmCrypto ());
86
108
transactionFactory = new TransactionFactoryImpl (cryptoSuite );
87
- queueThread =
88
- new Thread (
89
- () -> {
90
- while (true ) {
91
- int seq = concurrentQueue .pop ();
92
- logger .debug ("Receive queue message..." , seq );
93
- asyncThreadPool .submit (
94
- () -> {
95
- CallbackContent content = callbackMap .remove (seq );
96
- if (content != null ) {
97
- TransactionReceipt receipt =
98
- content .sendTransaction .get ();
99
- content .callback .onResponse (
100
- toJSONTransactionReceipt (
101
- receipt , content .transaction ));
102
- }
103
- });
104
- }
105
- });
106
109
asyncThreadPool =
107
110
new ThreadPoolExecutor (
108
111
1 ,
109
112
configOption .getThreadPoolConfig ().getThreadPoolSize (),
110
113
0 ,
111
114
TimeUnit .SECONDS ,
112
115
new ArrayBlockingQueue <Runnable >(queueSize ));
116
+ callback =
117
+ new Callback () {
118
+ public void onMessage (int seq ) {
119
+ asyncThreadPool .submit (
120
+ () -> {
121
+ logger .debug ("Receive seq: {}" , seq );
122
+ Content content = callbackMap .remove (seq );
123
+ if (content != null ) {
124
+ TransactionReceipt receipt =
125
+ content .getSendTransaction ().get ();
126
+ content .getCallback ()
127
+ .onResponse (
128
+ toJSONTransactionReceipt (
129
+ receipt , content .getTransaction ()));
130
+ }
131
+ });
132
+ }
133
+ };
113
134
}
114
135
115
136
public static void loadLibrary () {
@@ -152,7 +173,7 @@ public void sendTransactionAsync(
152
173
String signedTransactionData ,
153
174
boolean withProof ,
154
175
TransactionCallback callback ) {
155
- logger .debug ("sendTransactionAsync..." , node , withProof );
176
+ logger .debug ("sendTransactionAsync... {} {} " , node , withProof );
156
177
if (withProof ) {
157
178
super .sendTransactionAsync (node , signedTransactionData , withProof , callback );
158
179
return ;
@@ -163,15 +184,17 @@ public void sendTransactionAsync(
163
184
}
164
185
165
186
public void sendTransactionAsync (Transaction transaction , TransactionCallback callback ) {
166
- SendTransaction sendTransaction = new SendTransaction ( tarsRPCClient );
187
+ int seq = currentSeq . addAndGet ( 1 );
167
188
168
- int seq = callbackSeq .addAndGet (1 );
169
- CallbackContent callbackContent = new CallbackContent ();
170
- callbackContent .sendTransaction = sendTransaction ;
171
- callbackContent .callback = callback ;
172
- callbackContent .transaction = transaction ;
173
- callbackMap .put (seq , callbackContent );
174
- sendTransaction .setCallback (new ConcurrentQueueCallback (concurrentQueue , seq ));
189
+ SendTransaction sendTransaction = new SendTransaction (tarsRPCClient );
190
+ sendTransaction .setCallback (this .callback );
191
+ sendTransaction .setSeq (seq );
192
+
193
+ Content content = new Content ();
194
+ content .setSendTransaction (sendTransaction );
195
+ content .setTransaction (transaction );
196
+ content .setCallback (callback );
197
+ callbackMap .put (seq , content );
175
198
176
199
sendTransaction .send (transaction );
177
200
}
0 commit comments