|
20 | 20 | import com.rabbitmq.client.Connection;
|
21 | 21 | import com.rabbitmq.client.ConnectionFactory;
|
22 | 22 |
|
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 |
27 | 28 | */
|
28 |
| -public class TracerConcurrencyTest{ |
| 29 | +public class TracerConcurrencyTest { |
29 | 30 |
|
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 = ""; |
32 | 36 |
|
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 { |
34 | 42 |
|
35 |
| - public static void main(String[] args) throws Exception{ |
36 |
| - new TracerConcurrencyTest().run(); |
37 |
| - } |
| 43 | + final Object outputSync = new Object(); |
38 | 44 |
|
39 |
| - static String EXCHANGE = "tracer-exchange"; |
40 |
| - static String QUEUE = "tracer-queue"; |
| 45 | + final Connection conn = createConnectionAndResources(); |
41 | 46 |
|
42 |
| - public void run(){ |
| 47 | + for (int i = 0; i < THREADCOUNT; i++) { |
| 48 | + new TestThread(conn, outputSync).start(); |
| 49 | + } |
| 50 | + } |
43 | 51 |
|
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; |
49 | 55 |
|
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 | + } |
60 | 60 |
|
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); |
80 | 76 | }
|
81 |
| - System.exit(1); |
82 |
| - } |
83 | 77 | }
|
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; |
85 | 98 | }
|
86 |
| - } |
87 | 99 | }
|
0 commit comments