4
4
import java .net .URL ;
5
5
import java .util .Objects ;
6
6
import java .util .concurrent .ArrayBlockingQueue ;
7
+ import java .util .concurrent .ConcurrentHashMap ;
7
8
import java .util .concurrent .ThreadPoolExecutor ;
8
9
import java .util .concurrent .TimeUnit ;
10
+ import java .util .concurrent .atomic .AtomicInteger ;
9
11
import java .util .stream .Collectors ;
10
- import org .fisco .bcos .sdk .tars .Callback ;
12
+ import org .fisco .bcos .sdk .tars .ConcurrentQueue ;
13
+ import org .fisco .bcos .sdk .tars .ConcurrentQueueCallback ;
11
14
import org .fisco .bcos .sdk .tars .Config ;
12
15
import org .fisco .bcos .sdk .tars .CryptoSuite ;
13
16
import org .fisco .bcos .sdk .tars .LogEntry ;
@@ -32,10 +35,39 @@ public class TarsClient extends ClientImpl implements Client {
32
35
private static Logger logger = LoggerFactory .getLogger (TarsClient .class );
33
36
private RPCClient tarsRPCClient ;
34
37
private TransactionFactoryImpl transactionFactory ;
38
+ private Thread queueThread ;
35
39
private ThreadPoolExecutor asyncThreadPool ;
40
+
36
41
static final int queueSize = 10 * 10000 ;
37
42
static final String libFileName = System .mapLibraryName ("bcos_swig_java" );
38
43
44
+ private class CallbackContent {
45
+ public SendTransaction sendTransaction ;
46
+ TransactionCallback callback ;
47
+ Transaction transaction ;
48
+ };
49
+
50
+ ConcurrentQueue concurrentQueue = new ConcurrentQueue ();
51
+ ConcurrentHashMap <Integer , CallbackContent > callbackMap =
52
+ new ConcurrentHashMap <Integer , CallbackContent >();
53
+ AtomicInteger callbackSeq = new AtomicInteger (0 );
54
+
55
+ public RPCClient getTarsRPCClient () {
56
+ return tarsRPCClient ;
57
+ }
58
+
59
+ public void setTarsRPCClient (RPCClient tarsRPCClient ) {
60
+ this .tarsRPCClient = tarsRPCClient ;
61
+ }
62
+
63
+ public TransactionFactoryImpl getTransactionFactory () {
64
+ return transactionFactory ;
65
+ }
66
+
67
+ public void setTransactionFactory (TransactionFactoryImpl transactionFactory ) {
68
+ this .transactionFactory = transactionFactory ;
69
+ }
70
+
39
71
protected TarsClient (String groupID , ConfigOption configOption , long nativePointer ) {
40
72
super (groupID , configOption , nativePointer );
41
73
String connectionString =
@@ -52,6 +84,25 @@ protected TarsClient(String groupID, ConfigOption configOption, long nativePoint
52
84
CryptoSuite cryptoSuite =
53
85
bcos .newCryptoSuite (configOption .getCryptoMaterialConfig ().getUseSmCrypto ());
54
86
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
+ });
55
106
asyncThreadPool =
56
107
new ThreadPoolExecutor (
57
108
1 ,
@@ -72,7 +123,7 @@ public static void loadLibrary(String libPath) {
72
123
73
124
public static TarsClient build (String groupId , ConfigOption configOption , long nativePointer ) {
74
125
logger .info (
75
- "build, groupID: {}, configOption: {}, nativePointer: {}" ,
126
+ "TarsClient build, groupID: {}, configOption: {}, nativePointer: {}" ,
76
127
groupId ,
77
128
configOption ,
78
129
nativePointer );
@@ -101,25 +152,27 @@ public void sendTransactionAsync(
101
152
String signedTransactionData ,
102
153
boolean withProof ,
103
154
TransactionCallback callback ) {
155
+ logger .debug ("sendTransactionAsync..." , node , withProof );
104
156
if (withProof ) {
105
157
super .sendTransactionAsync (node , signedTransactionData , withProof , callback );
106
158
return ;
107
159
}
108
160
node = Objects .isNull (node ) ? "" : node ;
109
161
Transaction transaction = toTransaction (signedTransactionData );
162
+ sendTransactionAsync (transaction , callback );
163
+ }
164
+
165
+ public void sendTransactionAsync (Transaction transaction , TransactionCallback callback ) {
110
166
SendTransaction sendTransaction = new SendTransaction (tarsRPCClient );
111
167
112
- sendTransaction .setCallback (
113
- new Callback () {
114
- public void onMessage () {
115
- asyncThreadPool .submit (
116
- () -> {
117
- TransactionReceipt receipt = sendTransaction .get ();
118
- callback .onResponse (
119
- toJSONTransactionReceipt (receipt , transaction ));
120
- });
121
- }
122
- });
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 ));
175
+
123
176
sendTransaction .send (transaction );
124
177
}
125
178
0 commit comments