Getting TIME ELAPSED on date

select created,updated,
trunc((86400*(updated-created ))/60)
from MYTABLE where and trunc((86400*(updated-created ))/60)>10


Generate oAuth key in ..

Java

public String computeHmac(String baseString, String key)     throws NoSuchAlgorithmException, InvalidKeyException, IllegalStateException, UnsupportedEncodingException {     Mac mac = Mac.getInstance("HmacSHA1");     SecretKeySpec secret = new SecretKeySpec(key.getBytes(), mac.getAlgorithm());     mac.init(secret);     byte[] digest = mac.doFinal(baseString.getBytes());     return Base64.encode(digest); }
or

MessageDigest digest = MessageDigest.getInstance("SHA-1");
			digest.reset();
			byte[] hash = digest.digest(body.getBytes("UTF-8"));
			String encodedHash = Base64.encode(hash);


MySQL 99.9% CPU , 1.4%Memory , help ….

Before I jumped into my findings , here I share my problem of my SMS system

  • > 200 request per minutes
  • 20 persistent connection <- I used OJB framework, now it is in the ATTIC
  • 99.9% MySQL CPU
  • 150Kb/s traffic to MySQL
  • I panic
  • MySQL 5.1.35 running on Redhat Linux Release 9 (Shrike)
  • No my.cnf , running on default installation
Since i have no my.cnf, i have to set the SERVER VARIABLES manually each time the mysql instance was restarted. I’ve checked so many websites , in order to improve performance , you are advised to enable query caching, and yes it works.
This is how to do it, since i don’t have my.cnf anywhere in the server
1. create empty my.cnf at /etc/my.cnf . This will overwrite any my.cnf exists in the system (if you run default mysql)
2. Below are the content of my.cnf
will be continued

MySQL – no my.cnf when installing in Linux

I just found out here on how to have the file. By default, no my.cnf will be created during installation. Therefore some SERVER VARIABLES may be set up during session initialized.
Before creating the file, make sure check if any my.cnf exists in the system. If we want to overide existing my.cnf which we don’t know which one is using, simply create my.cnf in /etc/my.cnf


Tomcat 5.5 keeps on reloading context and cause logs full

this new things i found that Tomcat keeps on reloading context, still cannot find the answer


Generating SSL key in Mac

This info I found from

http://superuser.com/questions/73979/how-to-easily-create-a-ssl-certificate-and-configure-it-in-apache2-in-mac-os-x

Question:

I’d like to use my Mac OS X with https for local development tests. How can I easily make Apache2 respond to ssl, just for test proposes – I don’t want a real certificate, just a fake to make local https work ?

Answer:

Generating the private key:

openssl genrsa -des3 -out server.key 1024

output:

Generating RSA private key, 1024 bit long modulus …………………………………………………++++++ ……..++++++ e is 65537 (0×10001) Enter PEM pass phrase: Verifying password – Enter PEM pass phrase:

enter a passphrase for your private key.
Generating the CSR (certificate signing request):

openssl req -new -key server.key -out server.csr

It will request details like this:

Country Name (2 letter code) [GB]:
State or Province Name (full name) [Berkshire]:
Locality Name (eg, city) [Newbury]:
Organization Name (eg, company) [My Company Ltd]:
Organizational Unit Name (eg, section) []:
Common Name (eg, your name or your server’s hostname) []:
Email Address []:
Please enter the following ‘extra’ attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:

it’s fairly straightforward, the common name is your server’s hostname as it says in brackets.

Generating the self signed certificate:

openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt

Configuring SSL in httpd.conf for Apache:

SSLEngine on
SSLCertificateFile /path/to/generated/server.crt
SSLCertificateKeyFile /path/to/generated/server.key

(replace path appropriately with the path to your certificate and key)

Restart Apache:

apachectl restart

Apache will ask you for the passphrase to your key. If you think you will be shutting the server down a lot, you may want to remove the passphrase from the key so you can avoid entering it each time. If not, don’t worry about it. If so, complete this step after step 2 (Generating the CSR):

cp server.key server.key.copy
openssl rsa -in server.key.copy -out server.key

Oracle high CPU usage

Below is sql statement to check CPU usage on database. You can login with any DB user and run it.

select 	nvl(ss.USERNAME,'ORACLE PROC') username,
	se.SID,ss.logon_time,
	VALUE cpu_usage,osuser
from 	v$session ss,
	v$sesstat se,
	v$statname sn
where  	se.STATISTIC# = sn.STATISTIC#
and  	NAME like '%CPU used by this session%'
and  	se.SID = ss.SID
order  	by VALUE desc
------------------
SELECT s.sid, p.spid "OS Pid", s.module, s.process, s.schemaname "Schema", s.username "Username",
s.osuser "OS User", s.program "Program", substr(a.sql_text,1,550) "SQL Text"
FROM v$session s, v$sqlarea a, v$process p
WHERE s.sql_hash_value = a.hash_value (+)
AND s.sql_address = a.address (+)
AND s.paddr = p.addr
and nvl(a.sql_text,'-')!='-'
--and s.sid in (418)
and p.spid in (<your OS process id on the db server by running command prstat>)

Fastest to way to calculate SMS text length and divided message

(length / 160) + (length%160 > 0 ? 1 : 0)


Manipulating BIG file in JAVA

This was discussed back in 2002 during the early of Java, reading and writing big files has been an issue since and til now. Found this article at

http://www.techrepublic.com/article/handling-large-data-files-efficiently-with-java/1046714
/**
   *  Reads a file storing intermediate data into a list. Fast method.
   *  @param file the file to be read
   *  @return a file data
   */
   public byte[] read2list(String file) throws Exception {
      InputStream in = null;
      byte[] buf             = null; // output buffer
      int    bufLen          = 20000*1024;
      try{
         in = new BufferedInputStream(new FileInputStream(file));
         buf = new byte[bufLen];
         byte[] tmp = null;
         int len    = 0;
         List data  = new ArrayList(24); // keeps peaces of data
         while((len = in.read(buf,0,bufLen)) != -1){
            tmp = new byte[len];
            System.arraycopy(buf,0,tmp,0,len); // still need to do copy
            data.add(tmp);
         }
         /*
            This part os optional. This method could return a List data
            for further processing, etc.
         */
         len = 0;
         if (data.size() == 1) return (byte[]) data.get(0);
         for (int i=0;i<data.size();i++) len += ((byte[]) data.get(i)).length;
         buf = new byte[len]; // final output buffer
         len = 0;
         for (int i=0;i<data.size();i++){ // fill with data
           tmp = (byte[]) data.get(i);
            System.arraycopy(tmp,0,buf,len,tmp.length);
            len += tmp.length;
         }
      }finally{
         if (in != null) try{ in.close();}catch (Exception e){}
      }
      return buf;
   }

 


How to read very BIG text files in Java

I found this on

http://code.hammerpig.com/how-to-read-really-large-files-in-java.html

import java.util.*;
import java.io.*;

public class BigFile implements Iterable<String>
{
    private BufferedReader _reader;

    public BigFile(String filePath) throws Exception
    {
	_reader = new BufferedReader(new FileReader(filePath));
    }

    public void Close()
    {
	try
	{
	    _reader.close();
	}
	catch (Exception ex) {}
    }

    public Iterator<String> iterator()
    {
	return new FileIterator();
    }

    private class FileIterator implements Iterator<String>
    {
	private String _currentLine;

	public boolean hasNext()
	{
	    try
	    {
		_currentLine = _reader.readLine();
	    }
	    catch (Exception ex)
	    {
		_currentLine = null;
		ex.printStackTrace();
	    }

	    return _currentLine != null;
	}

	public String next()
	{
	    return _currentLine;
	}

	public void remove()
	{
	}
    }
}

 

BigFile file = new BigFile("C:\Temp\BigFile.txt");

for (String line : file)
    System.out.println(line)

 


Follow

Get every new post delivered to your Inbox.