1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package org.apache.jmeter.protocol.jms.client; 19 20 import java.util.ArrayList; 21 import java.util.HashMap; 22 import java.util.Iterator; 23 24 /** 25 * 26 * ClientPool holds the client instances in an ArrayList. The main purpose of 27 * this is to make it easier to clean up all the instances at the end of a test. 28 * If we didn't do this, threads might become zombie. 29 * 30 * N.B. This class needs to be fully synchronized as it is called from sample threads 31 * and the thread that runs testEnded() methods. 32 */ 33 public class ClientPool { 34 35 private static final ArrayList clients = new ArrayList(); 36 37 private static final HashMap client_map = new HashMap(); 38 39 /** 40 * Add a ReceiveClient to the ClientPool. This is so that we can make sure 41 * to close all clients and make sure all threads are destroyed. 42 * 43 * @param client 44 */ 45 public static synchronized void addClient(ReceiveSubscriber client) { 46 clients.add(client); 47 } 48 49 /** 50 * Add a OnMessageClient to the ClientPool. This is so that we can make sure 51 * to close all clients and make sure all threads are destroyed. 52 * 53 * @param client 54 */ 55 public static synchronized void addClient(OnMessageSubscriber client) { 56 clients.add(client); 57 } 58 59 /** 60 * Add a Publisher to the ClientPool. This is so that we can make sure to 61 * close all clients and make sure all threads are destroyed. 62 * 63 * @param client 64 */ 65 public static synchronized void addClient(Publisher client) { 66 clients.add(client); 67 } 68 69 /** 70 * Clear all the clients created by either Publish or Subscribe sampler. We 71 * need to do this to make sure all the threads creatd during the test are 72 * destroyed and cleaned up. In some cases, the client provided by the 73 * manufacturer of the JMS server may have bugs and some threads may become 74 * zombie. In those cases, it is not the responsibility of JMeter for those 75 * bugs. 76 */ 77 public static synchronized void clearClient() { 78 Iterator itr = clients.iterator(); 79 while (itr.hasNext()) { 80 Object client = itr.next(); 81 if (client instanceof ReceiveSubscriber) { 82 ReceiveSubscriber sub = (ReceiveSubscriber) client; 83 sub.close(); 84 sub = null; 85 } else if (client instanceof Publisher) { 86 Publisher pub = (Publisher) client; 87 pub.close(); 88 pub = null; 89 } else if (client instanceof OnMessageSubscriber) { 90 OnMessageSubscriber sub = (OnMessageSubscriber) client; 91 sub.close(); 92 sub = null; 93 } 94 } 95 clients.clear(); 96 client_map.clear(); 97 } 98 99 public static synchronized void put(Object key, OnMessageSubscriber client) { 100 client_map.put(key, client); 101 } 102 103 public static synchronized void put(Object key, Publisher client) { 104 client_map.put(key, client); 105 } 106 107 public static synchronized Object get(Object key) { 108 return client_map.get(key); 109 } 110 }