1 18 package org.apache.activemq.pool; 19 20 import javax.jms.Connection ; 21 import javax.jms.JMSException ; 22 import javax.jms.MessageProducer ; 23 import javax.jms.Session ; 24 25 import org.apache.activemq.ActiveMQConnection; 26 import org.apache.activemq.ActiveMQConnectionFactory; 27 import org.apache.activemq.broker.BrokerService; 28 import org.apache.activemq.broker.TransportConnector; 29 import org.apache.activemq.command.ActiveMQQueue; 30 import org.apache.activemq.test.TestSupport; 31 import org.apache.activemq.transport.mock.MockTransport; 32 33 public class ConnectionFailureEvictsFromPool extends TestSupport { 34 35 private BrokerService broker; 36 private ActiveMQConnectionFactory factory; 37 private PooledConnectionFactory pooledFactory; 38 39 protected void setUp() throws Exception { 40 broker = new BrokerService(); 41 broker.setPersistent(false); 42 TransportConnector connector = broker.addConnector("tcp://localhost:0"); 43 broker.start(); 44 factory = new ActiveMQConnectionFactory("mock:"+connector.getConnectUri()); 45 pooledFactory = new PooledConnectionFactory(factory); 46 } 47 48 public void testEviction() throws Exception { 49 Connection connection = pooledFactory.createConnection(); 50 sendMessage(connection); 51 createConnectionFailure(connection); 52 try { 53 sendMessage(connection); 54 fail("Expected Error"); 55 } catch ( JMSException e) { 56 } 57 58 Connection connection2 = pooledFactory.createConnection(); 60 sendMessage(connection2); 61 } 62 63 private void createConnectionFailure(Connection connection) throws Exception { 64 ActiveMQConnection c = ((PooledConnection)connection).getConnection(); 65 MockTransport t = (MockTransport) c.getTransportChannel().narrow(MockTransport.class); 66 t.stop(); 67 } 68 69 private void sendMessage(Connection connection) throws JMSException { 70 Session session = connection.createSession(false, 0); 71 MessageProducer producer = session.createProducer(new ActiveMQQueue("FOO")); 72 producer.send(session.createTextMessage("Test")); 73 session.close(); 74 } 75 76 protected void tearDown() throws Exception { 77 broker.stop(); 78 } 79 } 80 | Popular Tags |