Search for files containing a text on linux

I found this command to be very useful. Often we would like to search for a file containing a text.

$ find /path-here -type f -exec fgrep -l ‘text-to-find-here’ {} \;


$ find /home/venkat -type f -exec fgrep -l ‘foo’ {} \;

Posted in Linux | Leave a comment

Handling non-english characters in .java files

You should save the .java file as “Encode in UTF-8 without BOM” using an editor like Notepad++.
Do not use Notepad. Notepad introduces some special characters because of which the jvm will throw compiling error because of the special characters.

This applies to .properties files also, which will have non-english characters.

Posted in Java | Leave a comment

Handling OutOfMemory problems in Java

I am now dealing with “big data” applications.
I built applications which process large volumes of data and store them in different data stores like Database, Solr etc.

One of the ways to handle OutOfMemory problem would be to specify the min. and max memory that the JVM can use in your program.

By default a jvm of a Java application uses only 64 MB. We can specify the min and max memory that the JVM can use by using following command line options:
1. -Xms specifies the minimum memory
2. -Xmx specifies the maximum memory

You have to specify the memory values in terms of Megabytes as shown in the following Example:

$ java -Xms1024m -Xmx2048m HelloWorld

You can check the total memory available for the JVM in your program by using the following statement:
This will return the total memory in terms of bytes.

You can check the free memory available for the JVM in your program by using the following statement:

Posted in Java | Leave a comment

Couchbase as properties repository

Couchbase is a NOSQL persistent data store. You can store Serializable Java objects in couchbase.
I have used Couchbase extensively to save all the configurations properties. I have saved all the properties as JSON objects. These objects can be created and edited directly using the admin console provided by Couchbase.

You can perform asynchronous get/set operations with couchbase.

I have observed that the get/set operations fail at certain times. Hence, I have also implemented retrying mechanism. In the retry mechanism I add the the failed tasks to a List of pending tasks. I perform the get/set operations of these pending tasks by means of a separate Timer task which is scheduled at a regular interval.

Posted in Java | Leave a comment

Logging to Loggly – Cloud based logging

Loggly is a platform which acts as a store house of log messages. The advantage of using Loggly is that we will be able to view the logs simply by using a browser. There is an ability to filter the log messages based on the time, category etc.

There is a multi-threaded java implementation of logging messages to Loggly which I customized and used in my project.
You can download the java code from the url –

Posted in Java | Leave a comment

Send email using simple-java-mail API

package com.venkat;

import org.apache.log4j.ConsoleAppender;
import org.apache.log4j.Logger;
import org.apache.log4j.SimpleLayout;
import org.apache.velocity.VelocityContext;
import org.codemonkey.simplejavamail.Email;
import org.codemonkey.simplejavamail.Mailer;
import org.codemonkey.simplejavamail.TransportStrategy;

import java.util.Date;
import java.util.Properties;

import javax.mail.Message.RecipientType;

import javax.mail.Session;

public class SendMailExample {
public static void main(String args[]) throws Exception {
// normally you would do this in the log4j.xml
Logger rootLogger = Logger.getRootLogger();
rootLogger.addAppender(new ConsoleAppender(new SimpleLayout()));


VelocityContext context = new VelocityContext();
context.put(“name”, “Mark”);
context.put(“invoiceNumber”, “42123”);
context.put(“dueDate”, “June 6, 2009″);
context.put(“currentdate”, new Date().toString());

String template = “Hello $name. Please find attached invoice” +
” $invoiceNumber which is due on $dueDate. The current date is $currentdate”;
StringWriter writer = new StringWriter();
Velocity.evaluate(context, writer, “TemplateName”, template);

// Send email logic
final Email email = new Email();
email.addHeader(“X-Priority”, 2);
//email.setFromAddress(“Venkat Ram T at”, “”);
email.setFromAddress(“Venkat Ram T at”, “”);
email.setSubject(“test mail from venkat”);
//email.addRecipient(“Venkat Ram T at”, “”, RecipientType.TO);
email.addRecipient(“Venkat Ram T at”, “”, RecipientType.TO);
//email.addRecipient(“C. Bo”, “”, RecipientType.BCC);
email.setText(“How are you doing?”);
email.setTextHTML(“Hope this mail reaches you!“);

// embed images and include downloadable attachments
//email.addEmbeddedImage(“wink1″, imageByteArray, “image/png”);
//email.addEmbeddedImage(“wink2″, imageDatesource);
//email.addAttachment(“invitation”, pdfByteArray, “application/pdf”);
//email.addAttachment(“dresscode”, odfDatasource);

// Change outgoing server (SMTP) to 587 and use TLS as the encrypted connection.
Properties props = System.getProperties();
props.setProperty(“mail.transport.protocol”, “smtp”);
//props.put(“”, “”);
props.put(“”, “”);
props.put(“mail.smtp.port”, “25”);
props.put(“mail.smtp.auth”, “false”);
Session session = Session.getDefaultInstance(props, null);
new Mailer(session).sendMail(email);
//new Mailer(“”, 465, “”, “tvrsp071104″, TransportStrategy.SMTP_SSL).sendMail(email);
//new Mailer(“”, 25, “venkatramt”, “password-2″, TransportStrategy.SMTP_TLS).sendMail(email);
System.out.println(“email sent successfully!”);


  1. List of jars used for this example:

    • activation.jar
    • log4j-1.2.15.jar
    • mailapi-1.4.4.jar
    • simple-java-mail-v2.1.jar
    • smtp-1.4.4.jar
    • velocity-1.7.jar
  2. I used glossary terms in the email template. I used velocity framework to substitute these glossary terms with the dynamic values.
Posted in Java | 1 Comment

Large String manipulations with

We usually have a tendency to use java.lang.StringBuilder to build a String. StringBuilder is fine if number of manipulations are limited. It is also fine when we are dealing with small strings.

StringBuilder is not the right fit when it comes to building very large Strings. comes to our rescue. To build large strings we can use StringWriter decorated with BufferedWriter. Usage of BufferedWriter ensures that we do not end up with “out of memory” issues when dealing with very large strings.

The following illustrates the usage of StringWriter:

StringWriter stringWriter = new StringWriter();
BufferedWriter bufferedWriter = new BufferedWriter(stringWriter);
// perform write operations
// Ready to get the huge string that we built with StringWriter
String hugeString = stringWriter.toString();

The above code is “out of memory” error proof.

Posted in Java | 3 Comments