|
| 1 | +package com.rabbitmq.examples; |
| 2 | + |
| 3 | +import com.rabbitmq.client.*; |
| 4 | +import java.util.concurrent.*; |
| 5 | +import java.util.Random; |
| 6 | + |
| 7 | +/** |
| 8 | + * Test that the tracer correctly handles multiple concurrently processing |
| 9 | + * channels. If it doesn't then this code will cause the tracer to break |
| 10 | + * and will be disconneted. |
| 11 | + */ |
| 12 | +public class TracerConcurrencyTest{ |
| 13 | + |
| 14 | + public int port = 5673; |
| 15 | + public String host = "localhost"; |
| 16 | + public int threadCount = 3; |
| 17 | + |
| 18 | + private final Object lock = new Object(); |
| 19 | + |
| 20 | + public static void main(String[] args) throws Exception{ |
| 21 | + new TracerConcurrencyTest().run(); |
| 22 | + } |
| 23 | + |
| 24 | + static String EXCHANGE = "tracer-exchange"; |
| 25 | + static String QUEUE = "tracer-queue"; |
| 26 | + |
| 27 | + public void run(){ |
| 28 | + |
| 29 | + final Connection conn; |
| 30 | + try { |
| 31 | + conn = new ConnectionFactory().newConnection(host, port); |
| 32 | + Channel setup = conn.createChannel(); |
| 33 | + |
| 34 | + setup.exchangeDeclare(EXCHANGE, "direct"); |
| 35 | + setup.queueDeclare(QUEUE); |
| 36 | + setup.queueBind(QUEUE,EXCHANGE, ""); |
| 37 | + |
| 38 | + setup.close(); |
| 39 | + } catch(Exception e){ |
| 40 | + e.printStackTrace(); |
| 41 | + System.exit(1); |
| 42 | + throw null; // placate the compiler |
| 43 | + } |
| 44 | + |
| 45 | + for(int i = 0; i < threadCount; i++){ |
| 46 | + final int j = i; |
| 47 | + new Thread(){ |
| 48 | + @Override public void run(){ |
| 49 | + try { |
| 50 | + Random rnd = new Random(); |
| 51 | + Channel ch = conn.createChannel(); |
| 52 | + while(true){ |
| 53 | + Channel old = ch; |
| 54 | + ch.close(); |
| 55 | + ch = conn.createChannel(); |
| 56 | + ch.basicPublish( |
| 57 | + EXCHANGE, |
| 58 | + "", null, |
| 59 | + new byte[1024 * 1024] |
| 60 | + ); |
| 61 | + ch.basicGet(QUEUE, true); |
| 62 | + } |
| 63 | + } catch(Exception e){ |
| 64 | + synchronized(lock){ |
| 65 | + e.printStackTrace(); |
| 66 | + System.err.println(); |
| 67 | + } |
| 68 | + System.exit(1); |
| 69 | + } |
| 70 | + } |
| 71 | + }.start(); |
| 72 | + } |
| 73 | + } |
| 74 | +} |
0 commit comments