22
22
import com .rabbitmq .model .Consumer ;
23
23
import com .rabbitmq .model .ModelException ;
24
24
import com .rabbitmq .model .metrics .MetricsCollector ;
25
- import java .lang .reflect .Field ;
26
- import java .lang .reflect .InvocationTargetException ;
27
- import java .lang .reflect .Method ;
28
- import java .util .Arrays ;
29
25
import java .util .concurrent .*;
30
26
import java .util .concurrent .atomic .AtomicBoolean ;
31
27
import java .util .concurrent .atomic .AtomicLong ;
32
28
import java .util .concurrent .atomic .AtomicReference ;
33
29
import org .apache .qpid .protonj2 .client .*;
34
30
import org .apache .qpid .protonj2 .client .exceptions .*;
35
- import org .apache .qpid .protonj2 .client .impl .ClientLinkType ;
36
- import org .apache .qpid .protonj2 .client .impl .ClientReceiverLinkType ;
31
+ import org .apache .qpid .protonj2 .client .impl .ClientReceiver ;
37
32
import org .apache .qpid .protonj2 .client .util .DeliveryQueue ;
38
33
import org .apache .qpid .protonj2 .engine .EventHandler ;
39
34
import org .apache .qpid .protonj2 .engine .Scheduler ;
40
- import org .apache .qpid .protonj2 .engine .impl .ProtonLink ;
41
35
import org .apache .qpid .protonj2 .engine .impl .ProtonLinkCreditState ;
42
36
import org .apache .qpid .protonj2 .engine .impl .ProtonReceiver ;
43
37
import org .apache .qpid .protonj2 .engine .impl .ProtonSessionIncomingWindow ;
@@ -50,7 +44,7 @@ final class AmqpConsumer extends ResourceBase implements Consumer {
50
44
51
45
private static final Logger LOGGER = LoggerFactory .getLogger (AmqpConsumer .class );
52
46
53
- private volatile Receiver nativeReceiver ;
47
+ private volatile ClientReceiver nativeReceiver ;
54
48
private final AtomicBoolean closed = new AtomicBoolean (false );
55
49
private volatile Future <?> receiveLoop ;
56
50
private final int initialCredits ;
@@ -94,15 +88,16 @@ public void close() {
94
88
95
89
// internal API
96
90
97
- private Receiver createNativeReceiver (Session nativeSession , String address ) {
91
+ private ClientReceiver createNativeReceiver (Session nativeSession , String address ) {
98
92
try {
99
- return nativeSession .openReceiver (
100
- address ,
101
- new ReceiverOptions ()
102
- .deliveryMode (DeliveryMode .AT_LEAST_ONCE )
103
- .autoAccept (false )
104
- .autoSettle (false )
105
- .creditWindow (0 ));
93
+ return (ClientReceiver )
94
+ nativeSession .openReceiver (
95
+ address ,
96
+ new ReceiverOptions ()
97
+ .deliveryMode (DeliveryMode .AT_LEAST_ONCE )
98
+ .autoAccept (false )
99
+ .autoSettle (false )
100
+ .creditWindow (0 ));
106
101
} catch (ClientException e ) {
107
102
throw ExceptionUtils .convert (e , "Error while creating receiver from '%s'" , address );
108
103
}
@@ -229,46 +224,19 @@ String address() {
229
224
return this .address ;
230
225
}
231
226
232
- static <T > T field (String name , Object obj ) {
233
- return field (obj .getClass (), name , obj );
234
- }
235
-
236
- @ SuppressWarnings ("unchecked" )
237
- static <T > T field (Class <?> lookupClass , String name , Object obj ) {
238
- try {
239
- Field field = lookupClass .getDeclaredField (name );
240
- field .setAccessible (true );
241
- return (T ) field .get (obj );
242
- } catch (NoSuchFieldException | IllegalAccessException e ) {
243
- throw new ModelException ("Error during Java reflection operation" , e );
244
- }
245
- }
246
-
247
- @ SuppressWarnings ("unchecked" )
248
- static <T > T invoke (Class <?> lookupClass , String name , Object obj , Object ... args ) {
249
- try {
250
- Class <?>[] argTypes = Arrays .stream (args ).map (Object ::getClass ).toArray (Class []::new );
251
- Method method = lookupClass .getDeclaredMethod (name , argTypes );
252
- method .setAccessible (true );
253
- return (T ) method .invoke (obj , args );
254
- } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e ) {
255
- throw new ModelException ("Error during Java reflection operation" , e );
256
- }
257
- }
258
-
259
- private void initStateFromNativeReceiver (Receiver receiver ) {
227
+ private void initStateFromNativeReceiver (ClientReceiver receiver ) {
260
228
try {
261
- Scheduler protonExecutor = field ( ClientLinkType . class , " executor" , receiver );
229
+ Scheduler protonExecutor = receiver . executor ( );
262
230
CountDownLatch fieldsSetLatch = new CountDownLatch (1 );
263
231
protonExecutor .execute (
264
232
() -> {
265
- this .protonReceiver = field ( ClientReceiverLinkType . class , "protonReceiver" , receiver );
266
- this .creditState = invoke ( ProtonLink . class , "getCreditState" , this .protonReceiver );
267
- this .sessionWindow = field ( "sessionWindow" , this .protonReceiver );
268
- this .protonDeliveryQueue = field ( "deliveryQueue" , receiver );
233
+ this .protonReceiver = ( ProtonReceiver ) receiver . protonReceiver ( );
234
+ this .creditState = this .protonReceiver . getCreditState ( );
235
+ this .sessionWindow = this .protonReceiver . sessionWindow ( );
236
+ this .protonDeliveryQueue = receiver . deliveryQueue ( );
269
237
270
238
EventHandler <org .apache .qpid .protonj2 .engine .Receiver > eventHandler =
271
- field ( "linkCreditUpdatedHandler" , this .protonReceiver );
239
+ this .protonReceiver . linkCreditUpdatedHandler ( );
272
240
EventHandler <org .apache .qpid .protonj2 .engine .Receiver > decorator =
273
241
target -> {
274
242
eventHandler .handle (target );
@@ -326,7 +294,7 @@ void pause() {
326
294
private void doPause () {
327
295
this .creditState .updateCredit (0 );
328
296
this .creditState .updateEcho (true );
329
- invoke ( this .sessionWindow .getClass (), " writeFlow" , this . sessionWindow , this .protonReceiver );
297
+ this .sessionWindow .writeFlow ( this .protonReceiver );
330
298
}
331
299
332
300
void unpause () {
0 commit comments