1 18 package org.apache.activemq; 19 20 import java.io.File ; 21 import java.io.IOException ; 22 import java.net.URI ; 23 import java.util.ArrayList ; 24 import java.util.Collections ; 25 import java.util.Iterator ; 26 import java.util.List ; 27 28 import javax.jms.Connection ; 29 import javax.jms.ConnectionFactory ; 30 import javax.jms.Destination ; 31 import javax.jms.JMSException ; 32 import javax.jms.MessageConsumer ; 33 import javax.jms.MessageProducer ; 34 import javax.jms.Session ; 35 36 import org.apache.activemq.broker.BrokerFactory; 37 import org.apache.activemq.broker.BrokerService; 38 import org.apache.activemq.command.ActiveMQDestination; 39 40 45 public class JmsTestSupport extends CombinationTestSupport { 46 47 public String userName; 48 public String password; 49 50 protected ConnectionFactory factory; 51 protected ActiveMQConnection connection; 52 protected BrokerService broker; 53 54 protected List connections = Collections.synchronizedList(new ArrayList ()); 55 56 protected ActiveMQDestination createDestination(Session session, byte type) throws JMSException { 62 switch (type) { 63 case ActiveMQDestination.QUEUE_TYPE: 64 return (ActiveMQDestination) session.createQueue("TEST"); 65 case ActiveMQDestination.TOPIC_TYPE: 66 return (ActiveMQDestination) session.createTopic("TEST"); 67 case ActiveMQDestination.TEMP_QUEUE_TYPE: 68 return (ActiveMQDestination) session.createTemporaryQueue(); 69 case ActiveMQDestination.TEMP_TOPIC_TYPE: 70 return (ActiveMQDestination) session.createTemporaryTopic(); 71 } 72 throw new IllegalArgumentException ("type: " + type); 73 } 74 75 protected void sendMessages(Destination destination, int count) throws Exception { 76 ConnectionFactory factory = createConnectionFactory(); 77 Connection connection = factory.createConnection(); 78 connection.start(); 79 sendMessages(connection, destination, count); 80 connection.close(); 81 } 82 83 protected void sendMessages(Connection connection, Destination destination, int count) throws JMSException { 84 Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); 85 sendMessages(session, destination, count); 86 session.close(); 87 } 88 89 protected void sendMessages(Session session, Destination destination, int count) throws JMSException { 90 MessageProducer producer = session.createProducer(destination); 91 for (int i = 0; i < count; i++) { 92 producer.send(session.createTextMessage(""+i)); 93 } 94 producer.close(); 95 } 96 97 protected ConnectionFactory createConnectionFactory() throws Exception { 98 return new ActiveMQConnectionFactory("vm://localhost"); 99 } 100 101 protected BrokerService createBroker() throws Exception { 102 return BrokerFactory.createBroker(new URI ("broker://()/localhost?persistent=false")); 103 } 104 105 protected void setUp() throws Exception { 106 super.setUp(); 107 108 if(System.getProperty("basedir")==null){ 109 File file=new File ("."); 110 System.setProperty("basedir",file.getAbsolutePath()); 111 } 112 113 broker = createBroker(); 114 broker.start(); 115 factory = createConnectionFactory(); 116 connection = (ActiveMQConnection) factory.createConnection(userName, password); 117 connections.add(connection); 118 } 119 120 protected void tearDown() throws Exception { 121 for (Iterator iter = connections.iterator(); iter.hasNext();) { 122 Connection conn= (Connection ) iter.next(); 123 try { 124 conn.close(); 125 } catch (Throwable e) { 126 } 127 } 128 broker.stop(); 129 super.tearDown(); 130 } 131 132 protected void safeClose(Connection c) { 133 try { 134 c.close(); 135 } catch (Throwable e) { 136 } 137 } 138 139 protected void safeClose(Session s) { 140 try { 141 s.close(); 142 } catch (Throwable e) { 143 } 144 } 145 146 protected void safeClose(MessageConsumer c) { 147 try { 148 c.close(); 149 } catch (Throwable e) { 150 } 151 } 152 153 protected void safeClose(MessageProducer p) { 154 try { 155 p.close(); 156 } catch (Throwable e) { 157 } 158 } 159 160 protected void profilerPause(String prompt) throws IOException { 161 if( System.getProperty("profiler")!=null ) { 162 pause(prompt); 163 } 164 } 165 166 protected void pause(String prompt) throws IOException { 167 System.out.println(); 168 System.out.println(prompt+"> Press enter to continue: "); 169 while( System.in.read()!='\n' ) { 170 } 171 } 172 173 } 174 | Popular Tags |