Wednesday, January 13, 2010

my favorite java tips & tricks and notes

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;
    }

    startPosition = startPosition + users.size ();
 
}catch (Exception e) {
    e.printStackTrace();
}
}

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

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) {
}
}
Executing linux commands from Java


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 ();
}
}
}

No comments:

Post a Comment