Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

Wednesday, March 28, 2012

Eclipse java decompiler plugin


http://java.decompiler.free.fr/jd-eclipse/update










-------------------------------------------------

Monday, March 5, 2012

Deriving class from generic

A blog post note about

Type t;
t = ( (ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];

Monday, December 26, 2011

Import maven project into eclipse with sources and javadocs

mvn eclipse:clean eclipse:eclipse -DdownloadSources -DdownloadJavadocs

Friday, November 4, 2011

The "Java Life" Rap Music Video

Monday, October 10, 2011

JSON parse in java

google-json (1.7.1) is well designed and very fast with preinitializited new Gson() instance.


Gson gson = new Gson();


time in ms in 10 iterations


Data data = gson.fromJson(json, Data.class);



>>> alexey: GoogleJsonParse.main time =0= 62
>>> alexey: GoogleJsonParse.main time =1= 3
>>> alexey: GoogleJsonParse.main time =2= 6
>>> alexey: GoogleJsonParse.main time =3= 1
>>> alexey: GoogleJsonParse.main time =4= 1
>>> alexey: GoogleJsonParse.main time =5= 9
>>> alexey: GoogleJsonParse.main time =6= 1
>>> alexey: GoogleJsonParse.main time =7= 1
>>> alexey: GoogleJsonParse.main time =8= 2
>>> alexey: GoogleJsonParse.main time =9= 1


If you haven't object to cast the parsed data - you can use Collection or Map


Type typeOfT = new TypeToken<Map<String, String>>(){}.getType();
Map<String, String> data = gson.fromJson(json, typeOfT);

Java date convert: long milliseconds to string iso 8601

1305637849064 to 2011-10-10T07:46:56.394+0000
Time to covert is 1ms


import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;


public class MyDateUtilStuff {

private static DateFormat ISO_8601_DATE_TIME = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ");
static {
ISO_8601_DATE_TIME.setTimeZone(TimeZone.getTimeZone("UTC"));
}

public static void main(String[] args) {

String string = "1305637849064";

long startTime = System.currentTimeMillis();

Date date = new Date();
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(Long.parseLong(string));
String outDate = ISO_8601_DATE_TIME.format(date);

long time = System.currentTimeMillis() - startTime;

System.out.println(">>> alexey: MyDateUtilStuff.main ii = " + time);

System.out.println(">>> alexey: MyDateUtilStuff.main outDate = "
+ outDate);
}

}




Saturday, October 1, 2011

Solved two mysql+tomcat problems

Solved two mysql+tomcat problems:

1. The web application registered the JDBC driver [com.mysql.jdbc.Driver] but failed to unregister it when the web.
2. The web application appears to have started a thread named [MySQL Statement Cancellation Timer] but has failed.


1. The web application registered the JDBC driver [com.mysql.jdbc.Driver] but failed to unregister it when the web.

PROBLEM:
org.apache.catalina.loader.WebappClassLoader clearReferencesJdbc
SEVERE: The web application [/rest] registered the JDBC driver [com.mysql.jdbc.Driver] but failed to unregister it when the web


SOLUTION:
http://stackoverflow.com/questions/3320400/jdbc-driver-unregisted-when-the-web-application-stops

import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Enumeration;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

public class JdbcListener implements ServletContextListener
{
private static final Logger LOG = Logger.getLogger(JdbcListener.class);

public void contextDestroyed(ServletContextEvent sce)
{
deregisterJdbcDriver();
}

public void contextInitialized(ServletContextEvent sce)
{

}

private void deregisterJdbcDriver()
{
// This manually deregisters JDBC driver, which prevents Tomcat 7 from complaining about memory leaks
Enumeration drivers = DriverManager.getDrivers();
while (drivers.hasMoreElements())
{
Driver driver = drivers.nextElement();
try
{
DriverManager.deregisterDriver(driver);
LOG.info(String.format("Deregistering jdbc driver: %s", driver));
}
catch (SQLException e)
{
LOG.info(String.format("Error deregistering driver %s", driver), e);
}
}
}

}




2. The web application appears to have started a thread named [MySQL Statement Cancellation Timer] but has failed.

PROBLEM:
org.apache.catalina.loader.WebappClassLoader clearReferencesThreads
SEVERE: The web application [/rest] appears to have started a thread named [MySQL Statement Cancellation Timer] but has failed


SOLUTION:
http://bugs.mysql.com/bug.php?id=36565
To upgrage mysql connector jar.
http://dev.mysql.com/downloads/connector/j/

     
mysql
mysql-connector-java
5.1.17

Friday, September 30, 2011

JMX jconsole

Configure Tomcat
Add those params to the server JVM options :
-Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=3456 -Dcom.sun.management.jmxremote.authenticate=false 


Run jconsole
Go to JAVA_HOME/bin
run ./jconsole
Connect to the console

Wednesday, August 3, 2011

Test Google Datastore with Places REST service

https://code.google.com/p/app-engine-datastore/source/browse/#svn%2Ftrunk




The REST java class


URLS:
http://places-test-datastore.appspot.com/places/count


http://places-test-datastore.appspot.com/places/querycount?fromLat=49.39&toLat=49.41&fromLng=32.02&toLng=32.07&sort=hash
ERROR: can't have inequality filters on different properties: lat and lng

http://places-test-datastore.appspot.com/places/querycount?fromLat=49.39&toLat=49.41&sort=hash
ERROR: inequality filter property and first sort order must be the same: lat and hash

http://places-test-datastore.appspot.com/places/querycount?fromLat=49.39&toLat=49.41&sort=lat



Results: In long time (1min and 1000 PUT iteration) multi threading datastore slows and gets connection timout error.

Thursday, July 28, 2011

Java concurrent - ExecutorService

http://www.xinotes.org/notes/note/1221/

Eclipse -> Code Templates

Setting templates:
Java -> Editor -> Templates

1: "Edit..." for sysout:
System.out.println(">>> ${user}: ${enclosing_type}.${enclosing_method} ${word_selection} = " + ${word_selection});${cursor}

2: "Edit..." for systrace:
System.out.println(">>> ${user}: ${enclosing_type}.${enclosing_method}()");

Thursday, May 26, 2011

You must use a 32-bit Java runtime to run GWT Hosted Mode.

Fix the error

You must use a 32-bit Java runtime to run GWT Hosted Mode.

Download the 32 bit JDK (http://download.oracle.com/otn-pub/java/jdk/6u25-b06/jdk-6u25-linux-i586.bin) and add executable, and execute. This is unpacked it.

Add the unpacked JDK to the Eclipse at menu Window -> Preferences -> Java -> Installed JREs Click "Add..." and select Standard VM - > Next -> select Directory... inside unpacked JDR with /jre folder.

Monday, April 18, 2011

generics are not supported in -source 1.3

PROBLEM
mvn compile test
mvn clean install

generics are not supported in -source 1.3
(use -source 5 or higher to enable generics)


SOLUTION

mvn compile test -Dmaven.compiler.source=1.5 -Dmaven.compiler.target=1.5

Tuesday, February 1, 2011

2012 Java зависает на 2.2250738585072012e-308 Fun


2.2250738585072012

222 milliseconds 50 seconds 7:38:58 5-07-2012

Thursday, January 20, 2011

org.hibernate.MappingException: Could not read mapping document from URL: null

If you use tag @hibernate.class for generate hibernate mapping

Don't use commented lines between java doc block and you class declaration.



/**
* Author : Alexey Zavizionov
*
* @hibernate.class table="MY_CLASS"
*/
// <- here it is
public class MyClass {


Regards,
Alexey

Friday, December 3, 2010

Repost from Kohsuke: deadlock that you cant avoid

http://kohsuke.org/2010/09/01/deadlock-that-you-cant-avoid/

Saturday, November 27, 2010

Simple servlet to print parameters and attributes, headers and cookies


public class MyServlet extends GenericServlet
{

public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException
{
System.out.println("\n\n >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
System.out.println(">>> alexey: MyServlet.service 1 = " + 1);

HttpServletRequest hReq = (HttpServletRequest)req;
HttpServletResponse hRes = (HttpServletResponse)res;

printParsAndAttrs(hReq);
printHeaders(hReq);
printCookies(hReq);

}

private void printParsAndAttrs(ServletRequest req)
{
Enumeration parameterNames = req.getParameterNames();
while (parameterNames.hasMoreElements())
{
String name = (String)parameterNames.nextElement();
System.out.println(">>> alexey: MyServlet.logParsAndAttrs P name = " + name);
System.out.println(">>> alexey: MyServlet.logParsAndAttrs = " + req.getParameter(name));
}
Enumeration attributeNames = req.getAttributeNames();
while (attributeNames.hasMoreElements())
{
String name = (String)attributeNames.nextElement();
System.out.println(">>> alexey: MyServlet.logParsAndAttrs A name = " + name);
System.out.println(">>> alexey: MyServlet.logParsAndAttrs = " + req.getAttribute(name));
}
}

private void printHeaders(HttpServletRequest hReq)
{
Enumeration headerNames = hReq.getHeaderNames();
if (headerNames != null)
{
while (headerNames.hasMoreElements())
{
String name = (String)headerNames.nextElement();
System.out.println(">>> alexey: MyServlet.printHeaders H name = " + name);
System.out.println(">>> alexey: MyServlet.printHeaders = " + hReq.getHeader(name));
}
}
}

private void printCookies(HttpServletRequest hReq)
{
Cookie[] cookies = hReq.getCookies();
if (cookies != null)
{
for (Cookie cookie : cookies)
{
System.out.println(">>> alexey: MyServlet.printCookies C name = " + cookie.getName());
System.out.println(">>> alexey: MyServlet.printCookies = " + cookie.getValue());
}
}
}

}


Convert the byte to hex format


public static void main(String[] args)
{
String toHash =
"expire=1290777974mid=2865745secret=blablasid=1fa2773cd5451a627ebe63a6b3f1341b14ab7c5e866b01d808fee30ba2uQpzRxC856eUH5N8viXt";

MessageDigest md = null;
try
{
md = MessageDigest.getInstance("MD5");
}
catch (NoSuchAlgorithmException e)
{
e.printStackTrace();
}

byte[] mdbytes = md.digest(toHash.getBytes());

long currentTimeMillis = System.currentTimeMillis();
for (int k = 0; k < 10000; k++)
{
//convert the byte to hex format method 1
StringBuffer sb2 = new StringBuffer();
for (int i = 0; i < mdbytes.length; i++)
{
sb2.append(Integer.toString((mdbytes[i] & 0xff) + 0x100, 16).substring(1));
}
String string = sb2.toString();
// System.out.println("Digest(in hex format) 1 :: " + sb2.toString());
}
currentTimeMillis = System.currentTimeMillis() - currentTimeMillis;
System.out.println(">>> alexey: .main currentTimeMillis = " + currentTimeMillis);

currentTimeMillis = System.currentTimeMillis();
for (int k = 0; k < 10000; k++)
{
//convert the byte to hex format method 2
StringBuffer hexString = new StringBuffer();
for (int i = 0; i < mdbytes.length; i++)
{
String hex = Integer.toHexString(0xff & mdbytes[i]);
if (hex.length() == 1)
hexString.append('0');
hexString.append(hex);
}
String string = hexString.toString();
// System.out.println("Digest(in hex format) 2 :: " + hexString.toString());
}
currentTimeMillis = System.currentTimeMillis() - currentTimeMillis;
System.out.println(">>> alexey: .main currentTimeMillis = " + currentTimeMillis);
}




The result is
>>> alexey: .main currentTimeMillis = 223
>>> alexey: .main currentTimeMillis = 76