Skip to content

Commit 6fb7538

Browse files
author
Emile Joubert
committed
Merged bug24999 into default
2 parents da33f32 + 5b518a8 commit 6fb7538

File tree

1 file changed

+64
-52
lines changed

1 file changed

+64
-52
lines changed

test/src/com/rabbitmq/examples/TracerConcurrencyTest.java

Lines changed: 64 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -20,68 +20,80 @@
2020
import com.rabbitmq.client.Connection;
2121
import com.rabbitmq.client.ConnectionFactory;
2222

23-
/**
24-
* Test that the tracer correctly handles multiple concurrently processing
25-
* channels. If it doesn't then this code will cause the tracer to break
26-
* and will be disconneted.
23+
/**
24+
* Java Application. Repeatedly generate (and get) messages on multiple concurrently processing channels.
25+
* <p/>
26+
* This application connects to localhost port 5673, and is useful for testing {@link com.rabbitmq.tools.Tracer Tracer}.
27+
* @see com.rabbitmq.tools.Tracer Tracer
2728
*/
28-
public class TracerConcurrencyTest{
29+
public class TracerConcurrencyTest {
2930

30-
public String uri = "amqp://localhost:5673";
31-
public int threadCount = 3;
31+
private static final String uri = "amqp://localhost:5673";
32+
private static final int THREADCOUNT = 3;
33+
private static final String EXCHANGE = "tracer-exchange";
34+
private static final String QUEUE = "tracer-queue";
35+
private static final String ROUTING_KEY = "";
3236

33-
private final Object lock = new Object();
37+
/**
38+
* @param args command-line parameters -- all ignored.
39+
* @throws Exception test
40+
*/
41+
public static void main(String[] args) throws Exception {
3442

35-
public static void main(String[] args) throws Exception{
36-
new TracerConcurrencyTest().run();
37-
}
43+
final Object outputSync = new Object();
3844

39-
static String EXCHANGE = "tracer-exchange";
40-
static String QUEUE = "tracer-queue";
45+
final Connection conn = createConnectionAndResources();
4146

42-
public void run(){
47+
for (int i = 0; i < THREADCOUNT; i++) {
48+
new TestThread(conn, outputSync).start();
49+
}
50+
}
4351

44-
final Connection conn;
45-
try {
46-
conn = new ConnectionFactory()
47-
{{setUri(uri);}}.newConnection();
48-
Channel setup = conn.createChannel();
52+
private static class TestThread extends Thread {
53+
private final Connection conn;
54+
private final Object outputSync;
4955

50-
setup.exchangeDeclare(EXCHANGE, "direct");
51-
setup.queueDeclare(QUEUE, false, false, false, null);
52-
setup.queueBind(QUEUE,EXCHANGE, "");
53-
54-
setup.close();
55-
} catch(Exception e){
56-
e.printStackTrace();
57-
System.exit(1);
58-
throw null; // placate the compiler
59-
}
56+
private TestThread(Connection conn, Object outputSync) {
57+
this.conn = conn;
58+
this.outputSync = outputSync;
59+
}
6060

61-
for(int i = 0; i < threadCount; i++){
62-
new Thread(){
63-
@Override public void run(){
64-
try {
65-
Channel ch = conn.createChannel();
66-
while(true){
67-
ch.close();
68-
ch = conn.createChannel();
69-
ch.basicPublish(
70-
EXCHANGE,
71-
"", null,
72-
new byte[1024 * 1024]
73-
);
74-
ch.basicGet(QUEUE, true);
75-
}
76-
} catch(Exception e){
77-
synchronized(lock){
78-
e.printStackTrace();
79-
System.err.println();
61+
@Override
62+
public void run() {
63+
try {
64+
while (true) {
65+
Channel ch = conn.createChannel();
66+
ch.basicPublish(EXCHANGE, ROUTING_KEY, null, new byte[1024 * 1024]);
67+
ch.basicGet(QUEUE, true);
68+
ch.close();
69+
}
70+
} catch (Exception e) {
71+
synchronized (outputSync) {
72+
e.printStackTrace();
73+
System.err.println();
74+
}
75+
System.exit(1);
8076
}
81-
System.exit(1);
82-
}
8377
}
84-
}.start();
78+
79+
}
80+
81+
/**
82+
* Create connection and declare exchange and queue for local use.
83+
*
84+
* @return connection
85+
*/
86+
private static final Connection createConnectionAndResources() throws Exception {
87+
ConnectionFactory cf = new ConnectionFactory();
88+
cf.setUri(uri);
89+
Connection conn = cf.newConnection();
90+
Channel setup = conn.createChannel();
91+
92+
setup.exchangeDeclare(EXCHANGE, "direct");
93+
setup.queueDeclare(QUEUE, false, false, false, null);
94+
setup.queueBind(QUEUE, EXCHANGE, ROUTING_KEY);
95+
96+
setup.close();
97+
return conn;
8598
}
86-
}
8799
}

0 commit comments

Comments
 (0)