Sunday, December 7, 2014

Custom ThreadPoolExecutor based on spinlock approach.

      In one project we used ActiveMQ(JMS API) to communicate between business services and custom engine that worked asynchronously behind the scene to process financial data.
The flow to add/remove entity from ui was done as it usually organized in context of three-tier architecture: call to ui controller, route appropriate method, use service and then DAO to add/delete entity and then notify active mq corresponding consumers to process add/delete actions.

     But the issue was in the following: entity might be created in database, but not fully processed by engine while user tries to delete it from web interface.
It's not the issue on the  database side, this is multithreading synchronization question.

    I decided to use spin lock approach to sync two add/delete threads that did their work when the corresponding messages came to ActiveMQ queue.

    This is how JMS handler looks like. When message comes to queue it's then appropriate handled by  tickerEngineRequestProcessor, that is simply container for ThreadPoolExecutor performing tasks execution(add or delete one).

     Below is definition for tickerEngineRequestProcessor:
Let's now describe TickerTask, it just implemented Runnable interface to be processed within custom spin lock thread pool which I will describe further.

   The main idea in TickerEngineAddRemoveSyncThreadPoolExecutor is to perform spin iterations on thread that performs removing ticker while thread which adds ticker is not finished yet. This logic is placed in beforeExecute action. In afterExecute we just remove corresponding tickerId key from tickerIdsToAddInProgressState set.
   One more word about tickerIdsToAddInProgressState. This is ConcurrentHashSet that I choosed for storing state as per given tickerId, because we need to maintain sync add/remove operations based on the given tickerId and don't affect other tickers that might be processed at the same time. Thread.yield() is signal for threads scheduler to switch its work to other threads but the yielding thread can be scheduled back in one interrupt period later.






Tuesday, December 2, 2014

[mobile] How to overcome 'adb devices empty list' issue on Linux Mint 17

Recently I needed to install small  phonegap application with custom oauth2 authentication to my old htc desire.
It's not so big deal to run such applications on emulator devices via
phonegap run android,
But when I switched to the real device this caused some difficulties.
I work in Linux mint 17 and as it turned out adb devices is empty on this case.
After a little bit hacking and digging through internet I've found working solution.

1) type lsusb in shell and know vendor id: 
Device 007: ID 0bb4:0cb0 HTC (High Tech Computer Corp.)

So I see my old htc)
2) create 51-android.rules in /etc/udev/rules.d folder:
SUBSYSTEM=="usb", ATTR{idVendor}=="0bb4", MODE="0666", GROUP="plugdev"
3) Edited the adb_usb.ini file, added the 0x0bb4 
4) Enabled debugging on htc device(settings->developers)
And now list of devices is not empty!
List of devices attached HT15XTR04859 device
So we can run app on htc after that:

phonegap run android --device=HT15XTR04859






Monday, December 1, 2014

How To Fix NTFS Mount Issue In Linux Mint And Windows 8

After installing new Linux mint cinanmon 17 in dual bot with Windows 8 I'have faced with boring issue when mint could not mount disks belonging to Windows 8. The answer is this is because of Fast boot feature and it's might be simply resolved with turning off it from within Win 8. How?
Just follow this link for example :
http://itsfoss.com/solve-ntfs-mount-problem-ubuntu-windows-8-dual-boot/

Wednesday, October 29, 2014

Wednesday, July 9, 2014

Real world Java examples of GoF design patterns

http://balusc.blogspot.com/2010/04/examples-of-gof-design-patterns.html

Tuesday, June 10, 2014

How to download file using netty

https://github.com/Atmosphere/nettosphere/blob/master/server/src/main/java/org/atmosphere/nettosphere/HttpStaticFileServerHandler.java

Tuesday, March 25, 2014

How to shutdown 'play' programmaticly based on the corresponding conditions.

      I have got the task to stop 'play'(controller and the whole server) according to some requirements. To be more clear I need to check whether the сurrent request to the given method of the given controller is the last allowed one and then shutdown play.

     The main point that Calling System.exit() is enough to shut down the JVM I have read here: https://groups.google.com/forum/#!searchin/play-framework/system.exit/play-framework/xWAi7tPIEhY/2Ainrixfp8IJ

Cause even  Play.stop(); does not work as expected as per link above from
the 'play-framework' user group.

    But then I faced with the issue on how to return answer with JSON data and only then stopping the 'play' server. ScheduledExecutorService сame to help me). I've read a lot of documentation/articles about built-in play's approaches like async jobs, await and etc, but no one of them can guarantee me then play server will be stopped after returning of JSON answer.

   With ScheduledExecutorService I wait 5 sec after the short json answer and then shut down the 'play' server. Moreover if we assume that the json result will not be back to time of shutdown (which is very very unlikely) then my logic inside deviceConfigurationLocator service kills play server at the next request.

   I've tested this logic with the different scenarios and it works! Play server dies when I want this :-) Below is the my implementation of this task:


P.S. If you have any other ideas on how to stop/shutdown play server in context of my task feel free to tell about this in the comments.