KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > pool > ConnectionFailureEvictsFromPool


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.activemq.pool;
19
20 import javax.jms.Connection JavaDoc;
21 import javax.jms.JMSException JavaDoc;
22 import javax.jms.MessageProducer JavaDoc;
23 import javax.jms.Session JavaDoc;
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 JavaDoc {
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 JavaDoc {
49         Connection JavaDoc connection = pooledFactory.createConnection();
50         sendMessage(connection);
51         createConnectionFailure(connection);
52         try {
53             sendMessage(connection);
54             fail("Expected Error");
55         } catch ( JMSException JavaDoc e) {
56         }
57         
58         // If we get another connection now it should be a new connection that works.
59
Connection JavaDoc connection2 = pooledFactory.createConnection();
60         sendMessage(connection2);
61     }
62     
63     private void createConnectionFailure(Connection JavaDoc connection) throws Exception JavaDoc {
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 JavaDoc connection) throws JMSException JavaDoc {
70         Session JavaDoc session = connection.createSession(false, 0);
71         MessageProducer JavaDoc producer = session.createProducer(new ActiveMQQueue("FOO"));
72         producer.send(session.createTextMessage("Test"));
73         session.close();
74     }
75
76     protected void tearDown() throws Exception JavaDoc {
77         broker.stop();
78     }
79 }
80
Popular Tags