package sunels;
import java.io.*;
public class LinuxCommandExecutor {
public static void main (String args[]) {
try {
ProcessBuilder pb = new ProcessBuilder ("/bin/grep", "phoneNumber", "/space/subscribersDBdump.txt");
Process proc = pb.start ();
BufferedInputStream in = new BufferedInputStream (proc.getInputStream ());
FileOutputStream fout = new FileOutputStream ("/space/phoneNumbersOnly.txt");
byte[] buffer = new byte[2048];
while (true) {
int nRead = in.read (buffer);
if (nRead == -1)
break;
fout.write (buffer, 0, nRead);
System.out.print (".");
}
fout.close ();
proc.waitFor ();
}
catch (Exception e) {
e.printStackTrace ();
}
}
}
Monday, February 15, 2010
Sample Jmeter Java Test
package sunels;
import java.io.Serializable;
import org.apache.jmeter.protocol.java.sampler.*;
import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext;
import org.apache.jmeter.samplers.SampleResult;
import java.util.List;
import java.util.Properties;
import java.io.IOException;
import java.util.ArrayList;
import java.io.FileInputStream;
import com.mycompany.myproduct.um.msgstore.MessagestoreFactory;
import com.mycompany.myproduct.um.profile.ProfileFactory;
import java.util.Date;
import com.mycompany.myproduct.um.common.Language;
import com.mycompany.myproduct.um.profile.Subscriber;
import java.util.Random;
public class JmeterTest implements JavaSamplerClient , Serializable {
private static List msisdns = new ArrayList();
private static int totalRecordCount = 0;
private static java.util.Date startDate = new java.util.Date();
private static final String umModuleName = "UM";
private ProfileFactory profilefactory = ProfileFactory.getInstance (umModuleName);
private MessagestoreFactory msgstoreFactory = MessagestoreFactory.getInstance (umModuleName);
private Random randomGenerator = new Random();
private static int testType = 1;
static {
try {
Properties props = new Properties();
props.load (new FileInputStream ("/tmp/phoneNumbers.txt"));
msisdns = new ArrayList (props.keySet());
totalRecordCount = msisdns.size();
System.out.println (" -- Msisdns are loaded ....");
}
catch (IOException e) {
e.printStackTrace ();
System.exit (1);
}
}
public void setupTest(JavaSamplerContext context) {
}
public Arguments getDefaultParameters() {
Arguments params = new Arguments();
return params;
}
public synchronized String getMsisdn (){
String subscriberMsisdn = (String) msisdns.get(randomGenerator.nextInt(totalRecordCount));
return subscriberMsisdn;
}
public Subscriber playWithSubscriber (String subscriberPhoneNumber) throws Exception {
Subscriber subscriber = null;
subscriber = (Subscriber) profilefactory.querySubscriberByPhoneNumber (profilefactory.getCommonFactory ().createPhoneNumber (subscriberPhoneNumber));
if (subscriber != null) {
if(testType != 1){
Language subscriberLanguage = subscriber.getLanguage ();
if (subscriberLanguage == Language.ENGLISH) {
subscriber.setLanguage (Language.UKRAINIAN);
}
else {
subscriber.setLanguage (Language.ENGLISH);
}
Date currDate = new java.util.Date ();
subscriber.getSubscription ().setLastLoginDate (currDate);
subscriber.getSubscription ().setLastMessageActivityDate (currDate);
subscriber.save ();
}
}
else {
System.out.println (" -- Unable to find subscriber with msisdn :" + subscriberPhoneNumber);
}
return subscriber;
}
public SampleResult runTest(JavaSamplerContext context) {
SampleResult results = new SampleResult();
results.sampleStart();
String msisdn = getMsisdn();
try{
playWithSubscriber (msisdn);
results.setSuccessful(true);
results.setResponseCodeOK();
results.setResponseMessage("Subscriber found:" + msisdn);
} catch (Exception ex){
ex.printStackTrace();
results.setSuccessful(true);
results.setResponseCode("Exception*");
results.setResponseMessage("Subscriber not found:" + msisdn);
}
results.sampleEnd();
return results;
}
public void teardownTest(JavaSamplerContext context) {
}
}
import java.io.Serializable;
import org.apache.jmeter.protocol.java.sampler.*;
import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext;
import org.apache.jmeter.samplers.SampleResult;
import java.util.List;
import java.util.Properties;
import java.io.IOException;
import java.util.ArrayList;
import java.io.FileInputStream;
import com.mycompany.myproduct.um.msgstore.MessagestoreFactory;
import com.mycompany.myproduct.um.profile.ProfileFactory;
import java.util.Date;
import com.mycompany.myproduct.um.common.Language;
import com.mycompany.myproduct.um.profile.Subscriber;
import java.util.Random;
public class JmeterTest implements JavaSamplerClient , Serializable {
private static List msisdns = new ArrayList();
private static int totalRecordCount = 0;
private static java.util.Date startDate = new java.util.Date();
private static final String umModuleName = "UM";
private ProfileFactory profilefactory = ProfileFactory.getInstance (umModuleName);
private MessagestoreFactory msgstoreFactory = MessagestoreFactory.getInstance (umModuleName);
private Random randomGenerator = new Random();
private static int testType = 1;
static {
try {
Properties props = new Properties();
props.load (new FileInputStream ("/tmp/phoneNumbers.txt"));
msisdns = new ArrayList (props.keySet());
totalRecordCount = msisdns.size();
System.out.println (" -- Msisdns are loaded ....");
}
catch (IOException e) {
e.printStackTrace ();
System.exit (1);
}
}
public void setupTest(JavaSamplerContext context) {
}
public Arguments getDefaultParameters() {
Arguments params = new Arguments();
return params;
}
public synchronized String getMsisdn (){
String subscriberMsisdn = (String) msisdns.get(randomGenerator.nextInt(totalRecordCount));
return subscriberMsisdn;
}
public Subscriber playWithSubscriber (String subscriberPhoneNumber) throws Exception {
Subscriber subscriber = null;
subscriber = (Subscriber) profilefactory.querySubscriberByPhoneNumber (profilefactory.getCommonFactory ().createPhoneNumber (subscriberPhoneNumber));
if (subscriber != null) {
if(testType != 1){
Language subscriberLanguage = subscriber.getLanguage ();
if (subscriberLanguage == Language.ENGLISH) {
subscriber.setLanguage (Language.UKRAINIAN);
}
else {
subscriber.setLanguage (Language.ENGLISH);
}
Date currDate = new java.util.Date ();
subscriber.getSubscription ().setLastLoginDate (currDate);
subscriber.getSubscription ().setLastMessageActivityDate (currDate);
subscriber.save ();
}
}
else {
System.out.println (" -- Unable to find subscriber with msisdn :" + subscriberPhoneNumber);
}
return subscriber;
}
public SampleResult runTest(JavaSamplerContext context) {
SampleResult results = new SampleResult();
results.sampleStart();
String msisdn = getMsisdn();
try{
playWithSubscriber (msisdn);
results.setSuccessful(true);
results.setResponseCodeOK();
results.setResponseMessage("Subscriber found:" + msisdn);
} catch (Exception ex){
ex.printStackTrace();
results.setSuccessful(true);
results.setResponseCode("Exception*");
results.setResponseMessage("Subscriber not found:" + msisdn);
}
results.sampleEnd();
return results;
}
public void teardownTest(JavaSamplerContext context) {
}
}
Locate any java class file in your filesystem in runtime
package sunels;
import java.security.*;
import java.net.URL;
public class ClasspathUtil {
public static void main (String[] args) {
ClasspathUtil classpathutil = new ClasspathUtil ();
ProtectionDomain pDomain = classpathutil.getClass().getProtectionDomain();
CodeSource cSource = pDomain.getCodeSource();
URL loc = cSource.getLocation();
System.out.println (" -- Class< " + classpathutil.getClass().getCanonicalName() + "> Found in : {" + classpathutil.getClass().getCanonicalName() + " :" + loc+"}");
System.exit (0);
}
}
note: This example is finding itself
import java.security.*;
import java.net.URL;
public class ClasspathUtil {
public static void main (String[] args) {
ClasspathUtil classpathutil = new ClasspathUtil ();
ProtectionDomain pDomain = classpathutil.getClass().getProtectionDomain();
CodeSource cSource = pDomain.getCodeSource();
URL loc = cSource.getLocation();
System.out.println (" -- Class< " + classpathutil.getClass().getCanonicalName() + "> Found in : {" + classpathutil.getClass().getCanonicalName() + " :" + loc+"}");
System.exit (0);
}
}
note: This example is finding itself
Paging on Records with JPA
JPA Paging
int maxRecords = 250;
int startPosition = 0;
String queryString = "SELECT u FROM test.VmUsers u where u.userCli LIKE :pattern ";
while (true){
try {
Query selectQuery = subscriberEntityManager.createQuery (queryString);
selectQuery.setParameter ("pattern", "%"+pattern);
selectQuery.setFirstResult(startPosition);
selectQuery.setMaxResults (maxRecords);
List users = selectQuery.getResultList ();
if (users.isEmpty ()) {
break;
}
}catch (Exception ex1) {
ex1.printStackTrace();
}
}
int maxRecords = 250;
int startPosition = 0;
String queryString = "SELECT u FROM test.VmUsers u where u.userCli LIKE :pattern ";
while (true){
try {
Query selectQuery = subscriberEntityManager.createQuery (queryString);
selectQuery.setParameter ("pattern", "%"+pattern);
selectQuery.setFirstResult(startPosition);
selectQuery.setMaxResults (maxRecords);
List users = selectQuery.getResultList ();
if (users.isEmpty ()) {
break;
}
}catch (Exception ex1) {
ex1.printStackTrace();
}
}
Executing some jobs in parallel
import java.util.*;
import java.io.*;
public class Test {
public static void main (String[] args) {
Runnable r = new Runnable () {
public void run () {
while (true) {
try {
System.out.println ("-- this lines are being printed in another thread.."+Thread.currentThread().getName());
Thread.currentThread().sleep(130);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
};
new Thread (r).start();
while (true) {
System.out.println (" -- this lines are bing printed in main thread.."+Thread.currentThread().getName());
try {
Thread.currentThread ().sleep (60);
} catch (InterruptedException ex) {
//noop
}
}
}
}
-- this lines are being printed in another thread..Thread-0
-- this lines are bing printed in main thread..main
-- this lines are bing printed in main thread..main
-- this lines are bing printed in main thread..main
-- this lines are being printed in another thread..Thread-0
-- this lines are bing printed in main thread..main
-- this lines are bing printed in main thread..main
-- this lines are being printed in another thread..Thread-0
-- this lines are bing printed in main thread..main
-- this lines are bing printed in main thread..main
-- this lines are being printed in another thread..Thread-0
-- this lines are bing printed in main thread..main
-- this lines are bing printed in main thread..main
-- this lines are being printed in another thread..Thread-0
import java.io.*;
public class Test {
public static void main (String[] args) {
Runnable r = new Runnable () {
public void run () {
while (true) {
try {
System.out.println ("-- this lines are being printed in another thread.."+Thread.currentThread().getName());
Thread.currentThread().sleep(130);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
};
new Thread (r).start();
while (true) {
System.out.println (" -- this lines are bing printed in main thread.."+Thread.currentThread().getName());
try {
Thread.currentThread ().sleep (60);
} catch (InterruptedException ex) {
//noop
}
}
}
}
-- this lines are being printed in another thread..Thread-0
-- this lines are bing printed in main thread..main
-- this lines are bing printed in main thread..main
-- this lines are bing printed in main thread..main
-- this lines are being printed in another thread..Thread-0
-- this lines are bing printed in main thread..main
-- this lines are bing printed in main thread..main
-- this lines are being printed in another thread..Thread-0
-- this lines are bing printed in main thread..main
-- this lines are bing printed in main thread..main
-- this lines are being printed in another thread..Thread-0
-- this lines are bing printed in main thread..main
-- this lines are bing printed in main thread..main
-- this lines are being printed in another thread..Thread-0
Subscribe to:
Posts (Atom)