Monday, June 16, 2014
A Good Resource about Java Generics
I just hit a good resource about generic classes and methods in java, fun to read : http://math.hws.edu/javanotes/c10/s5.html
Stinging Lessons Learned Today
- Don't use static SimpleDateFormat as class member. SimpleDateFormat is not thread safe and multiple threads reaching to a single instance of SimpleDateFormat at the same time may result in strange time formatting results. Use it inside of synchronized block or even better use apache date utils and apache date format those are thread safe and already tested and quality passed solutions :)
- Know that elements inherited from interface are static although they are not declared as static inside interface.
import java.text.SimpleDateFormat; public interface MyIntf { public SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy"); }
public class MyImpl implements MyIntf { public static void main(String[] args) { System.out.println(new MyImpl().dateFormat); } }
Each MyImpl instance will inherit same dateFormat static instance. It is better not to use object with an interface, just leave it as an interface.
- To get unique identity of an object, use System.identityHashCode(anObject);
Monday, April 21, 2014
Small Simple Safe (SSS Rule)
I have to devote this blog entry to my teacher in Marmara University Prof. Akif Eyler, I heard small-simple-safe from him first time. This entry is an opportunity to thank him for all valuable teachings he gave us professionals or non-professionals, "tesekkurler hocam".
I have used this principle "small-simple-safe" in my technical career frequently since I heard from him and tried to look problem through it. Here is web page of SSS and here is a great cartoon picturing the term.
I have used this principle "small-simple-safe" in my technical career frequently since I heard from him and tried to look problem through it. Here is web page of SSS and here is a great cartoon picturing the term.
Thursday, February 13, 2014
Google Guava RateLimiter
Uncontrolled power is not power. Here how throttling can be done easily using Google Guava RateLimiter
create a RateLimiter with TPS value at line 15.
Acquire RateLimiter at line 21 before submitting a job into an ExecutorService. Thats all :)
create a RateLimiter with TPS value at line 15.
Acquire RateLimiter at line 21 before submitting a job into an ExecutorService. Thats all :)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.atomic.AtomicInteger; import com.google.common.util.concurrent.RateLimiter; public class Test { static long start; static AtomicInteger doneCounter = new AtomicInteger(0); static int joblimit = 10000; static int TPS = 1000; public static void main(String[] args) { RateLimiter rateLimiter = RateLimiter.create(TPS); ExecutorService executorService = Executors.newCachedThreadPool();//newFixedThreadPool(10); start = System.currentTimeMillis(); for (int i = 0; i < joblimit; i++){ rateLimiter.acquire(); // may wait executorService.execute(new Runnable() { @Override public void run() { System.out.println("doing by " + Thread.currentThread().getName()); int index = doneCounter.incrementAndGet(); if (index == joblimit){ System.out.println(joblimit + " job is completed in " + (System.currentTimeMillis() - start) + " msec with " + TPS + " tps"); } } }); } } } |
Wednesday, October 23, 2013
AJAX Demo
Here is a sample AJAX demo deployed on google app engine. When link is clicked, request is sent to a backend service (http://kulferhat1.appspot.com/time) and result is displayed asynchronously without posting whole page. An ajax loading image is displayed until response is received.
Html code is below
References:
Html code is below
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> </head> <script type="text/javascript"> function createXMLHttpRequest(){ // See http://en.wikipedia.org/wiki/XMLHttpRequest // Provide the XMLHttpRequest class for IE 5.x-6.x: if( typeof XMLHttpRequest == "undefined" ) XMLHttpRequest = function() { try { return new ActiveXObject("Msxml2.XMLHTTP.6.0") } catch(e) {} try { return new ActiveXObject("Msxml2.XMLHTTP.3.0") } catch(e) {} try { return new ActiveXObject("Msxml2.XMLHTTP") } catch(e) {} try { return new ActiveXObject("Microsoft.XMLHTTP") } catch(e) {} throw new Error( "This browser does not support XMLHttpRequest." ) }; return new XMLHttpRequest(); } var AJAX = createXMLHttpRequest(); function handler() { if(AJAX.readyState == 4 && AJAX.status == 200) { var json = eval('(' + AJAX.responseText +')'); document.getElementById("resultdata").innerHTML='Success. Result: time => ' + json.time; }else if (AJAX.readyState == 4 && AJAX.status != 200) { document.getElementById("resultdata").innerHTML = 'Something went wrong...'; } } function show(){ AJAX.onreadystatechange = handler; AJAX.open("GET", "/time"); AJAX.send(""); document.getElementById("resultdata").innerHTML = '<img src="img/Ajax-loader.gif" />'; }; function clearResultData(){ document.getElementById("resultdata").innerHTML = ''; } </script> <body> <a href="#" onclick="javascript:show();"> Click here to get time data from the server side</a> <a href="#" onclick="javascript:clearResultData();"> Clear </a> <div id="resultdata"></div> </html> |
References:
Sunday, September 29, 2013
Apache Camel Prototype
I have just uploaded apache camel prototype application into github. Basically, you can prepare camel routes at runtime and execute them. You can check documentation about details. I should say, it is a prototype and might miss some validation and error handling issues :)
Wednesday, September 18, 2013
Good Tips about JQuery
Saturday, September 07, 2013
MongoDB as a NoSQL, when to use or not
Here is a great stackoverflow about when to use MongoDb versus RDBMS
http://stackoverflow.com/questions/1476295/when-to-use-mongodb-or-other-document-oriented-database-systems
The most crucial sentence is that:
NoSQL is a great tool, but it’s certainly not going to be your competitive edge, it’s not going to make your app hot, and most of all, your users won’t give a shit about any of this.
After that killing sentence :), lets go with some advantage and disadvantage of NoSQL, specifically MongoDB
MongoDB does not support transactions, so it is not suitable where it is needed. You can imagine money transfer or other complex operations.
Well suited for the following situations
Ref:
http://blog.iprofs.nl/2011/11/25/is-mongodb-a-good-alternative-to-rdbms-databases-like-oracle-and-mysql/
http://stackoverflow.com/questions/1476295/when-to-use-mongodb-or-other-document-oriented-database-systems
http://kkovacs.eu/cassandra-vs-mongodb-vs-couchdb-vs-redis
http://ethangunderson.com/blog/two-reasons-to-not-use-mongodb/
http://nosql.mypopescu.com/post/703837964/when-should-i-use-mongodb
http://ilya40umov.wordpress.com/2013/03/05/considerations-for-choosing-or-not-choosing-mongodb/
http://ryanangilly.com/post/1091884265/3-reasons-to-use-mongodb
http://stackoverflow.com/questions/1476295/when-to-use-mongodb-or-other-document-oriented-database-systems
The most crucial sentence is that:
NoSQL is a great tool, but it’s certainly not going to be your competitive edge, it’s not going to make your app hot, and most of all, your users won’t give a shit about any of this.
After that killing sentence :), lets go with some advantage and disadvantage of NoSQL, specifically MongoDB
Advantages:
Document
oriented and schemaless
This is the most prominent advantage of using NoSQL databases. They are good to store unstructured data,
Unlike relational DBs, MongoDB stores all your data in
collections of BSON documents and has no schema
It worths for rapid software development where you basically can’t
afford to spend too much time doing schema design
Horizontal Scalability and
High Availability
MongoDB allows to build a clustered topology with
replication and sharding, where the former provides fail-over and eventually
consistent read scaling and the latter facilitates scaling out writes(and reads
as well).
Just add up nodes and it can scale horizontally quite well.
Fast writes in the
fire-and-forget mode. Which is quite useful for collecting
various statistics where possible loss of some data is acceptable in favor of a
shorter response time.
It’s Free and OpenSource.
Disadvantages:
No SQL = No Joins.
You end up writing jobs to do things like joining data from different
tables/collections, something that an RDBMS would do for you
automatically.
MongoDb queries can retrieve data from only one collection and take advantage of only one index. n many scenarios, this means more round-trips to the server to find
related records. And then you start de-normalizing data - which means
background jobs.
No foreign key constraint to ensure data consistency
MongoDB does not support transactions, so it is not suitable where it is needed. You can imagine money transfer or other complex operations.
Well suited for the following situations
- Scaling out
- Caching
- High volume traffic
- High transactional applications
- Ad-hoc business intelligence
- Problems which require a sql solution
In Summary
- Need fast? consider
Need horizontal scaling? consider
Need fast programmability / agile? consider
Need some atomicity? consider
Need complex transactional semantics? no
Need SQL? no
Lots of reporting? a little reporting: fine. real time stats: great. lots of traditional reports: SQL GROUP BY is very powerful. but see also scale.
Ref:
http://blog.iprofs.nl/2011/11/25/is-mongodb-a-good-alternative-to-rdbms-databases-like-oracle-and-mysql/
http://stackoverflow.com/questions/1476295/when-to-use-mongodb-or-other-document-oriented-database-systems
http://kkovacs.eu/cassandra-vs-mongodb-vs-couchdb-vs-redis
http://ethangunderson.com/blog/two-reasons-to-not-use-mongodb/
http://nosql.mypopescu.com/post/703837964/when-should-i-use-mongodb
http://ilya40umov.wordpress.com/2013/03/05/considerations-for-choosing-or-not-choosing-mongodb/
http://ryanangilly.com/post/1091884265/3-reasons-to-use-mongodb
Wednesday, August 28, 2013
Flexibility - Usability Trade off
Always consider flexibility-usability trade off in daily life and at work
as the flexibility of a system increases, its usability decreases
as the flexibility of a system increases, its usability decreases
Wednesday, August 21, 2013
Sunday, August 18, 2013
Subscribe to:
Posts (Atom)