Saturday, January 30, 2010

Generating Web Service using Axis2Java

Define a Java class for the Web Service

package com.mycompany.myproduct.um.subscriber.ws.profile;

public class SubscriberProfileWS {
public String updateSubscriber (String username, String password, SubscriberProfile profile) {
System.out.println ("USername is : " + username + " password :" + password);
return "messageis:1244";
}

public String createSubscriber (String username, String password, SubscriberProfile profile) {
System.out.println ("USername is : " + username + " password :" + password);
return "messageis:1244";
}

public String deleteSubscriber (String username, String password, String msisdn) {
System.out.println ("USername is : " + username + " password :" + password + " Msisdn :" + msisdn);
return "messageis:1244";
}

}

Please Note: SubscriberProfile class is a container .All the variables that you want to access from generated code must have setter and getters.


Execute the following commans:
[serkans@serkanslnx bin] java org.apache.ws.java2wsdl.Java2WSDL -cp ../java/classes -cn com.mycompany.myproduct.um.subscriber.ws.profile.SubscriberProfileWS -of ../resources/SubscriberProfileWS.wsdl

[serkans@serkanslnx bin] java org.apache.axis2.wsdl.WSDL2Java -uri ../resources/SubscriberProfileWS.wsdl -p com.mycompany.myproduct.um.subscriber.ws.profile.SubscriberProfileWS.adb -d adb -s -ss -sd -ssi -o sourceOut

note: source files are generated under sourceOut directory


How To Invoke From SoapUI
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsd="http://profile.ws.subscriber.um.myproduct.mycompany.com/xsd">
<soap:Header/>
<soap:Body>
<xsd:createSubscriber>
<xsd:username>funny</xsd:username>
<xsd:password>funny</xsd:password>
<xsd:profile>
<xsd:birthdate>2009-06-17T11:49:52.986-07:00</xsd:birthdate>
<xsd:chargingType>0</xsd:chargingType>
<xsd:company>mycompany</xsd:company>
<xsd:department>coe</xsd:department>
<xsd:emailAddress>mutlu.sen@gmail.com</xsd:emailAddress>
<xsd:faxNumber>3901234567</xsd:faxNumber>
<xsd:forceChangePassword>true</xsd:forceChangePassword>
<xsd:gsmPhoneNumber>3901234567</xsd:gsmPhoneNumber>
<xsd:homePhoneNumber>3901234567</xsd:homePhoneNumber>
<xsd:languageCode>en</xsd:languageCode>
<xsd:msisdn>3901234567</xsd:msisdn>
<xsd:password>1234</xsd:password>
<xsd:passwordSecurityEnabled>true</xsd:passwordSecurityEnabled>
<xsd:personalName>mutlu</xsd:personalName>
<xsd:secretQuestionAnswer>sakaci</xsd:secretQuestionAnswer>
<xsd:streetAddress>street</xsd:streetAddress>
<xsd:subcriberPolicyName>voice</xsd:subcriberPolicyName>
<xsd:subscriptionCosPolicyName>voice</xsd:subscriptionCosPolicyName>
<xsd:subscriptionPolicyName>voice</xsd:subscriptionPolicyName>
<xsd:subscriptionStatus>0</xsd:subscriptionStatus>
<xsd:title>mr</xsd:title>
<xsd:umEmailAddress>3901234567@um.life.com</xsd:umEmailAddress>
<xsd:username>3901234567</xsd:username>
<xsd:webPage>http://sunels.com</xsd:webPage>
<xsd:workPhoneNumber>3901234567</xsd:workPhoneNumber>
</xsd:profile>
</xsd:createSubscriber>
</soap:Body>
</soap:Envelope>

Thursday, January 21, 2010

How to analyse Out of Memory Error, fix java memory leak


Add the following JVM arguments for your java process to analyse and fix memory leaks.
XX:+HeapDumpOnOutOfMemoryError -verbose:gc
Download MAT (memory analyser tool) http://www.eclipse.org/mat/
Open the head dump file with tool.

Ex: root@serkanslnx:/space/tools/mat# ./MemoryAnalyzer /tmp/java_pid2774.hprof

Note: If you are using java 1.6 you can use the built in tools to get heapDump at runtime also like below example

[root@sunelslnx lib]#/usr/java/jdk1.6.0_17/bin/jmap -dump:file=/space/myHeapDump.dmp 27400

27400 is the process id here :)

A useful page

Another historical page

Thursday, January 14, 2010

Ldap Operations

LDAP ADD
ldapadd -D "cn=root,dc=mycompany,dc=com" -w secret1 -f allLdif.ldif

LDAP SEARCH
ldapsearch -h localhost -p 389 -D cn='Directory Manager' -w tneu34 -b dc=mycompany,dc=com 'tnaPhoneNumber=380332340400'

INDEX USAGE TEST
ldapsearch -h localhost -p 389 -D cn='Directory Manager' -w tneu34 -b dc=mycompany,dc=com 'tnaPhoneNumber=380933998099' debugsearchindex

LDAP MODIFY
ldapmodify -h localhost -p 389 -D cn=root,dc=mycompany,dc=com -w secret1 -f okanGrep.ldif

LDAP EXPORT
Openldap:ldapsearch -h localhost -p 389 -D cn=root,dc=mycompany,dc=com -w secret1 -b dc=mycompany,dc=com objectClass=* > /tmp/all.ldif
Opends:/export-ldif -h localhost -p 4444 -D "cn=Directory Manager" -w secret1 -X --includeBranch "dc=mycompany,dc=com" --backendID userRoot --ldifFile /space/primaryOpendsExport.ldif

LDAP EXPORT OF SPECIFIC ATTRIBUTE
Openldap exm-1:ldapsearch -x -h localhost -p 389 -D cn=root,dc=mycompany,dc=com -w secret1 -b ou=subscriptions,dc=mycompany,dc=com tnaTotalMessageCountQuota=* dn tnaSubscriptionStatus tnaTotalMessageCountQuota > quotaExport.ldif

Openldap exm-2:ldapsearch -h localhost -p 389 -D cn=root,dc=mycompany,dc=com -w secret1 -b ou=subscriptions,dc=mycompany,dc=com objectClass=* > /tmp/allSubscriptions.ldif

FOUND SUBCOUNT COUNT UNDER A NODE
Opends:ldapsearch -h localhost -p 1389 -D cn='Directory Manager' -w secret1 -b "ou=subscribers,dc=mycompany,dc=com" -s base "objectclass=*" numsubordinates

Getting exact count with scripting

ldapsearch -h localhost -p 1389 -D cn='Directory Manager' -w secret1 -b "ou=subscribers,dc=mycompany,dc=com" -s base "objectclass=*" numsubordinates| grep "numsubordinates:"| cut -c "18-"

FOUNDING OPEN CONNECTIONS
Opends:ldapsearch -h tas5 -p 4444 -D "cn=directory manager" -w secret1 --useSSL --trustAll -s base -b "cn=Client Connections,cn=LDAP Connection Handler 0.0.0.0 port 1389,cn=monitor" "(objectclass=*)"| wc -l
160

EXAMPLE OF BATCH SEARCH ON OPENDS UNDER LINUX
[ennio@serkanslnx ~]# while read line ; do ldapsearch -h localhost -p 389 -D cn=root,dc=mycompany,dc=com -w secret1 -b dc=mycompany,dc=com tnaPhoneNumber=$line; done < unique_in_ldap.txt | grep "dn" >dns.txt

LDAP IMPORT
Opends:import-ldif -n userRoot --skipSchemaValidation -l /space/migration/huge.ldif >import.log &

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