Disrupt java threads.

Hello,

First, keep calm and keep reading :), there is no c/c++ code there, only pure and clear Java code πŸ˜€
By disrupting java threads, i mean the way we use them to communicate with each others.

Basically, when you want to enable communication between threads, the first idea is to use a queue : Thread-producer create a workload, put it in a queue to be consumed by Thread-consumer.

Easy to understand, but not efficient if we want to :

    • Have multiple consumers : like a multicast queue
    • To build a topologie (dependecy graph) can be a nightmare of complexity
    • Lock-free : Queues in java are ‘Blocking’ because the use locks, so we can have performance issues there.

So, in this article i will show you how to build a complex inter-thread messaging architecture in an EASY way πŸ™‚

Let me introduce Disruptor framework from LMAX (a trading platform)

Disruptor is a very low-latency and high-throughput inter-thread messaging framework based on standard java framework (no ext lib ^_^) and on the RingBuffer pattern (Circular buffer from wikipedia or more human explanation from Trisha Gee here).

So the Disruptor is more than an implementation of Queue in java, it provides :

  • Multicast events to consumers, with consumer dependency graph.
  • Pre-allocate memory for events.
  • Optionally lock-free.

Here we will use it to implement 3 real work use cases :

Prerequisites

To run the article’s code, you need to have:

– JDK 6 or greater – examples here are in Java 8.
– Disruptor dependency from maven

or from LMAX repository here

1) Ordered building strategy

We want to have a Strategy that build a car, then set color on it, then set power, then set wheels and finally print it, all that stuff in order.

Ordered steps
Ordered steps

 

Easy with the Disruptor DSL : check the code below:

See method DisruptorTests.buildCarInOrder() for more information.

Here a fully working example with all init:

See Class ExampleMain for more information.

2) Parallel building strategy

In this strategy we want our car to be Colored, Powered, Wheeled in parallel, and then Print it.

With this strategy, the Same object “Event-A” will be consumed by 3 threads at the same time.

Parallel
Parallel

 

Let see how with the code looks:

See method DisruptorTests.buildCarInParallel() for complete example.

3) Dispatching car strategy

After building our car, we want to deliver it by using one delivery type at time (we can’t deliver the same car using multiple ways πŸ˜‰ ).

 

dispatcher
dispatcher

For this strategy, we use a WorkPool and define our WorkHandler(business logic executed by each thread) and then build a ringbufferΒ  :

See method DisruptorTests.dispatchEachCarByOnlyOneWarAtOnce() for complete example.

That it, now you can configure a powerful producer/consumer pattern in a easy way using Disruptor api.

Get the code

The code is available on GitHub.

Hope you enjoyed this post, and don’t forget to share!

About the author

Hichame EL KHALFI is a consultant and a senior software architect on financial Front-to-Back software solution.

You can follow him on twitter @Helkhalfi

Links

Disruptor api home page :Β http://lmax-exchange.github.io/disruptor/

Launch deamon process with Jenkins

Hello,
A quick post to share with you how to launch a deamon process – like a webserver πŸ™‚ – with jenkins on linux.
Using

or

inside jenkins script will not work, trust me, jenkins will start your app and stop it.

If you want to do this you must force jenkins to ‘forget’ your process by overriding the BUILD_ID variable before launching your app.

Otherwise you process will be killed when the jenkins script finish.

Cheers.

Hichame

Free ssh client for blackberry OS 10 and above

Hello,
Few months ago i was looking for a ssh client for my Blackberry Q10.

Infortunalty nothing that fits my needs was on the Blackberry app store (Blackberry World).

Then i remembered that i have a Blackberry with an OS version higher than 10.2.1 so i can run Android applications πŸ™‚

Let me introduce to you JuiceSSH which can :

  • Save your credentials of connections (server/user/password).
  • With a well designed virtual keyboard with strategic keys like ‘|’, ‘escape’, etc…

It become a pleasure to write command and script with the blackberry physical keyboard πŸ˜‰

JuiceSSH interface
JuiceSSH interface

image

Login to a server panel

image

How much RAM is used on linux

Hello all,

Last time i was RAM-cleaning a VM linux server by kill -9 bunch of processes, then i was curious to know how much RAM is available.
Naturally, i thinked of top command:

[hichame@server app]$ top
top - 16:16:59 up 19 days, 2:05, 2 users, load average: 0.00, 0.00, 0.00
Tasks: 186 total, 1 running, 185 sleeping, 0 stopped, 0 zombie
Cpu(s): 11.4%us, 1.4%sy, 0.0%ni, 86.8%id, 0.3%wa, 0.0%hi, 0.0%si, 0.0%st
Mem: 16336460k total, 11442068k used, 4894392k free, 516964k buffers
Swap: 0k total, 0k used, 0k free, 8449812k cached

PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
10639 hichame 20 0 19288 1292 916 R 1.9 0.0 0:00.01 top
1 root 20 0 21444 1224 888 S 0.0 0.0 0:01.49 init
.......

Naturally, i screamed WTF!!! 11442068k of 16336460k is used, near 70% of all my beautiful RAM is used after my wonderfull RAM-cleaning.

Calm down and let’s focus on this line Mem: 16336460k total, 11442068k used, 4894392k free, 516964k buffers and get some help with free linux command :

[hichame@server app]$ free
total used free shared buffers cached
Mem: 16336460 11442068 4894392 0 516964 8449812
-/+ buffers/cache: 2475292 13861168
Swap: 0 0 0

Ahaa, the real available RAM is 13861168 ^_^

If you want more information about free output command checkout this link

Hope this post helps.

Hichame