35
35
import java .nio .charset .StandardCharsets ;
36
36
import java .util .concurrent .*;
37
37
import java .util .concurrent .atomic .AtomicBoolean ;
38
+ import org .slf4j .Logger ;
39
+ import org .slf4j .LoggerFactory ;
38
40
39
41
public class AmqpPerfTest {
40
42
43
+ private static final Logger LOGGER = LoggerFactory .getLogger (AmqpPerfTest .class );
44
+
41
45
/*
42
46
./mvnw -q clean test-compile exec:java \
43
47
-Dexec.mainClass=com.rabbitmq.client.amqp.perf.AmqpPerfTest \
@@ -86,6 +90,12 @@ public static void main(String[] args) throws IOException {
86
90
87
91
connection
88
92
.consumerBuilder ()
93
+ .listeners (
94
+ context -> {
95
+ if (context .currentState () == Resource .State .RECOVERING ) {
96
+ LOGGER .info ("Consumer is recovering..." );
97
+ }
98
+ })
89
99
.queue (q )
90
100
.initialCredits (1000 )
91
101
.messageHandler (
@@ -103,7 +113,24 @@ public static void main(String[] args) throws IOException {
103
113
104
114
executorService .submit (
105
115
() -> {
106
- Publisher publisher = connection .publisherBuilder ().exchange (e ).key (rk ).build ();
116
+ AtomicBoolean shouldPublish = new AtomicBoolean (false );
117
+ Publisher publisher =
118
+ connection
119
+ .publisherBuilder ()
120
+ .exchange (e )
121
+ .key (rk )
122
+ .listeners (
123
+ context -> {
124
+ if (context .currentState () == Resource .State .OPEN ) {
125
+ shouldPublish .set (true );
126
+ } else {
127
+ if (context .currentState () == Resource .State .RECOVERING ) {
128
+ LOGGER .info ("Publisher is recovering..." );
129
+ }
130
+ shouldPublish .set (false );
131
+ }
132
+ })
133
+ .build ();
107
134
Publisher .Callback callback =
108
135
context -> {
109
136
try {
@@ -116,11 +143,23 @@ public static void main(String[] args) throws IOException {
116
143
};
117
144
int msgSize = 10 ;
118
145
while (!Thread .currentThread ().isInterrupted ()) {
119
- long creationTime = System .currentTimeMillis ();
120
- byte [] payload = new byte [msgSize ];
121
- writeLong (payload , creationTime );
122
- Message message = publisher .message (payload );
123
- publisher .publish (message , callback );
146
+ if (shouldPublish .get ()) {
147
+ long creationTime = System .currentTimeMillis ();
148
+ byte [] payload = new byte [msgSize ];
149
+ writeLong (payload , creationTime );
150
+ try {
151
+ Message message = publisher .message (payload );
152
+ publisher .publish (message , callback );
153
+ } catch (Exception ex ) {
154
+ LOGGER .info ("Error while trying to publish: {}" , ex .getMessage ());
155
+ }
156
+ } else {
157
+ try {
158
+ Thread .sleep (1000L );
159
+ } catch (InterruptedException ex ) {
160
+ Thread .interrupted ();
161
+ }
162
+ }
124
163
}
125
164
});
126
165
out .println ("Prometheus endpoint started on http://localhost:" + monitoringPort + "/metrics" );
0 commit comments