1 4 package org.objectweb.celtix.bus.transports.jms; 5 6 import junit.extensions.TestSetup; 7 import junit.framework.TestSuite; 8 9 import org.activemq.broker.impl.BrokerContainerImpl; 10 import org.activemq.store.vm.VMPersistenceAdapter; 11 12 13 class JMSBrokerSetup extends TestSetup { 14 JMSEmbeddedBroker jmsBrokerThread; 15 String jmsBrokerUrl = "tcp://localhost:61616"; 16 String activeMQStorageDir; 17 public JMSBrokerSetup(TestSuite suite, String url) { 18 super(suite); 19 jmsBrokerUrl = url; 20 } 21 22 public JMSBrokerSetup(TestSuite suite) { 23 super(suite); 24 } 25 26 public void setUp() throws Exception { 27 28 jmsBrokerThread = new JMSEmbeddedBroker(jmsBrokerUrl); 29 jmsBrokerThread.startBroker(); 30 } 31 32 public void tearDown() throws Exception { 33 synchronized (this) { 34 jmsBrokerThread.shutdownBroker = true; 35 } 36 if (jmsBrokerThread != null) { 37 jmsBrokerThread.join(); 38 } 39 40 jmsBrokerThread = null; 41 } 42 43 class JMSEmbeddedBroker extends Thread { 44 boolean shutdownBroker; 45 final String brokerUrl; 46 Exception exception; 47 48 49 public JMSEmbeddedBroker(String url) { 50 brokerUrl = url; 51 } 52 53 public void startBroker() throws Exception { 54 synchronized (this) { 55 super.start(); 56 try { 57 wait(); 58 if (exception != null) { 59 throw exception; 60 } 61 } catch (InterruptedException ex) { 62 ex.printStackTrace(); 63 } 64 } 65 } 66 67 public void run() { 68 try { 69 ContainerWapper container; 70 synchronized (this) { 71 container = new ContainerWapper(); 72 container.addConnector(brokerUrl); 73 container.setPersistenceAdapter(new VMPersistenceAdapter()); 74 container.start(); 75 Thread.sleep(200); 76 notifyAll(); 77 } 78 synchronized (this) { 79 while (!shutdownBroker) { 80 wait(1000); 81 } 82 } 83 container.shutdown(); 84 container = null; 85 } catch (Exception e) { 86 exception = e; 87 e.printStackTrace(); 88 } 89 } 90 91 92 } 93 94 class ContainerWapper extends BrokerContainerImpl { 95 96 public void shutdown() { 97 super.containerShutdown(); 98 } 99 } 100 } 101 | Popular Tags |