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.sampler; 19 20 import org.apache.jmeter.samplers.AbstractSampler; 21 import org.apache.jmeter.samplers.Entry; 22 import org.apache.jmeter.samplers.SampleResult; 23 import org.apache.jmeter.testelement.property.IntegerProperty; 24 import org.apache.jmeter.testelement.property.StringProperty; 25 import org.apache.jmeter.threads.JMeterContext; 26 import org.apache.jmeter.threads.JMeterContextService; 27 import org.apache.jorphan.logging.LoggingManager; 28 import org.apache.log.Logger; 29 30 /** 31 * Dummy Sampler used to pause or stop a thread or the test; 32 * intended for use in Conditional Controllers. 33 * 34 */ 35 public class TestAction extends AbstractSampler { 36 37 private static final Logger log = LoggingManager.getLoggerForClass(); 38 39 // Actions 40 public final static int STOP = 0; 41 public final static int PAUSE = 1; 42 public final static int STOP_NOW = 2; 43 44 // Action targets 45 public final static int THREAD = 0; 46 // public final static int THREAD_GROUP = 1; 47 public final static int TEST = 2; 48 49 // Identifiers 50 private final static String TARGET = "ActionProcessor.target"; //$NON-NLS-1$ 51 private final static String ACTION = "ActionProcessor.action"; //$NON-NLS-1$ 52 private final static String DURATION = "ActionProcessor.duration"; //$NON-NLS-1$ 53 54 public TestAction() { 55 super(); 56 } 57 58 /* 59 * (non-Javadoc) 60 * 61 * @see org.apache.jmeter.samplers.Sampler#sample(org.apache.jmeter.samplers.Entry) 62 */ 63 public SampleResult sample(Entry e) { 64 JMeterContext context = JMeterContextService.getContext(); 65 66 int target = getTarget(); 67 int action = getAction(); 68 if (action == PAUSE) { 69 pause(getDurationAsString()); 70 } else if (action == STOP || action == STOP_NOW) { 71 if (target == THREAD) { 72 log.info("Stopping current thread"); 73 context.getThread().stop(); 74 // //Not yet implemented 75 // } else if (target==THREAD_GROUP) { 76 } else if (target == TEST) { 77 if (action == STOP_NOW) { 78 log.info("Stopping all threads now"); 79 context.getEngine().stopTest(); 80 } else { 81 log.info("Stopping all threads"); 82 context.getEngine().askThreadsToStop(); 83 } 84 } 85 } 86 87 return null; // This means no sample is saved 88 } 89 90 private void pause(String mili_s) { 91 int milis; 92 try { 93 milis=Integer.parseInt(mili_s); 94 } catch (NumberFormatException e){ 95 log.warn("Could not create number from "+mili_s); 96 milis=0; 97 } 98 try { 99 Thread.sleep(milis); 100 } catch (InterruptedException e) { 101 } 102 } 103 104 public void setTarget(int target) { 105 setProperty(new IntegerProperty(TARGET, target)); 106 } 107 108 public int getTarget() { 109 return getPropertyAsInt(TARGET); 110 } 111 112 public void setAction(int action) { 113 setProperty(new IntegerProperty(ACTION, action)); 114 } 115 116 public int getAction() { 117 return getPropertyAsInt(ACTION); 118 } 119 120 public void setDuration(String duration) { 121 setProperty(new StringProperty(DURATION, duration)); 122 } 123 124 public String getDurationAsString() { 125 return getPropertyAsString(DURATION); 126 } 127 }