Monday, November 4, 2013

Eclipse Tomcat unable to start within 30 seconds



Have you encountered the following error when you start up your tomcat in eclipse?


Server Tomcat v4.1 Server at localhost was unable to start within 30 seconds. If the server requires more time, try increasing the timeout in the server editor.







FATAL ERROR in native method: No transports initialized
err:: No such file or directory
Error [2] in connect() call!
Socket transport failed to init.
Transport dt_socket failed to initialize, rc = -1.




I have no idea what caused this error. But what I did was changed the following setting in my eclipse tomcat server and it work now.

Under the “Timeouts > Specify the time limit to complete server operations.” Change the “Start (in seconds)” from 30 to 45.
 

Thursday, October 31, 2013

How to send batch email using Gmail in Java

import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class BatchEmailer {

      public static void main(String[] args) {
            Properties props = System.getProperties();
        props.put("mail.smtp.user", "abc@gmail.com");
        props.put("mail.smtp.password", "12345678");
            props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.port", "587");
        props.put("mail.smtp.auth", "true");
        props.put("mail.stmp.sendpartial", "true");
       
        Session session = Session.getDefaultInstance(props);
            Message message = new MimeMessage(session);
           
            try {
                  message.setFrom(new InternetAddress("abc@gmail.com"));

                  String[] toAddress = new String[] { "aaa@aaa.com", "bbb@bbb.com", "ccc@ccc.com" };
                  for (int i = 0; i < toAddress.length; i++) {
                        InternetAddress address = new InternetAddress(toAddress[i]);
                        message.addRecipient(Message.RecipientType.TO, address);
                  }

                  message.setSubject("Batch Email Testing");
                  message.setText("Batch Email Testing 1 2 3");

                  Transport.send(message);
            } catch (MessagingException e) {
                  e.printStackTrace();
            }
      }
}

Tuesday, July 2, 2013

Generate Large Excel Report by Using Apache POI Performance Tuning

If you are using Apache POI to generate large excel file, please take note the sheet.autoSizeColumn((short) p); line because this will impact the performance.


import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

public class CSVToExcelConverter {

       public static void main(String args[]) throws IOException {
              ArrayList arList = null;
              ArrayList al = null;
              String fName = "test.csv";
              String thisLine;
              int count = 0;
              FileInputStream fis = new FileInputStream(fName);
              DataInputStream myInput = new DataInputStream(fis);
              int i = 0;
              arList = new ArrayList();
              while ((thisLine = myInput.readLine()) != null) {
                     al = new ArrayList();
                     String strar[] = thisLine.split(",");
                     for (int j = 0; j < strar.length; j++) {
                           al.add(strar[j]);
                     }
                     arList.add(al);
                     i++;
              }

              try {
                     HSSFWorkbook hwb = new HSSFWorkbook();
                     HSSFSheet sheet = hwb.createSheet("new sheet");
                     for (int k = 0; k < arList.size(); k++) {
                           ArrayList ardata = (ArrayList) arList.get(k);
                           HSSFRow row = sheet.createRow((short) 0 + k);
                           for (int p = 0; p < ardata.size(); p++) {
                                  HSSFCell cell = row.createCell((short) p);
                                  sheet.autoSizeColumn((short) p); // this will slow down the performance
                                  String data = ardata.get(p).toString();
                                  if (data.startsWith("=")) {
                                         cell.setCellType(HSSFCell.CELL_TYPE_STRING);
                                         data = data.replaceAll("\"", "");
                                         data = data.replaceAll("=", "");
                                         cell.setCellValue(data);
                                  } else if (data.startsWith("\"")) {
                                         data = data.replaceAll("\"", "");
                                         cell.setCellType(HSSFCell.CELL_TYPE_STRING);
                                         cell.setCellValue(data);
                                  } else {
                                         data = data.replaceAll("\"", "");
                                         cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
                                         cell.setCellValue(data);
                                  }
                                  // */
                                  // cell.setCellValue(ardata.get(p).toString());
                           }
                           System.out.println();
                     }
                     FileOutputStream fileOut = new FileOutputStream("test.xls");
                     hwb.write(fileOut);
                     fileOut.close();
                     System.out.println("Your excel file has been generated");
              } catch (Exception ex) {
                     ex.printStackTrace();
              } // main method ends
       }
}



Hope this can save you time.