Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
"Try-catch"
It is recommended to use a try----catch block so that the log is clearer when there is a problem with the Java statement;
Try { //Java code }catch (Throwable ex) { log.error (" Something wrong in BeanShell postprocessor ", ex); Throw ex;}
"Gets the current month and minute of the day and seconds"
The following example: Gets the current year and the minute milliseconds and constructs the form as "2017-08-16t15:35:28.456z";
ImportJava.text.SimpleDateFormat;Importjava.util.Date;ImportJava.util.Calendar; Calendar Cal=calendar.getinstance ();intYY =Cal.get (calendar.year);intMM = Cal.get (calendar.month) + 1; intDD =Cal.get (calendar.date);intHH =Cal.get (calendar.hour_of_day);intMM =Cal.get (calendar.minute);intSS =Cal.get (calendar.second);intMI =Cal.get (Calendar.millisecond); Vars.put ("Nowdate", dd+ "T" +hh+ ":" +mm+ ":" +ss+ "." +MI);//construct the time format required in the request
"Mathematical operations on parameter values"
The following example: is a fragment of a dialog script, if condition controller uses "${__javascript (" ${messagenum} ">0)}" as a condition, each sending a message, MessageNum-1, a total of 10 times after the end of the Send, The following uses the BeanShell processor, after the request to send a message using the BeanShell processor messagenum processing;
The Java statement to use:
int tmpmessagenum = Integer.parseint (stringvalue); string to int
String messagenum = integer.tostring (tmpmessagenum); int to String
Try{String tmp= Vars.get ("Messagenum");//gets the current Messagenum value intTmpmessagenum = Integer.parseint (TMP);//string to int//As right statementusing Error in JMeter,---integer.valueof (tmp) is not solved. Intvalue ();Tmpmessagenum= TmpMessageNum-1;//One less piece of processing is leftString Messagenum= Integer.tostring (Tmpmessagenum);//int to string//Log.info (messagenum);Vars.put ("Messagenum", Messagenum);}Catch(Throwable ex) {Log.error ("Something Wrong", ex); Throwex;}
BeanShell Processor_ using Java to process scripts