Using Jedis

Jedis is one of Redis clients written in Java, though I have not tried using any other client. It is very easy to configure Jedis and use it as a client. Lets quickly jump over to the code part and understand things.
Using Maven dependency:
<dependency>
  <groupId>redis.clients</groupId>
  <artifactId>jedis</artifactId>
  <version>2.6.0</version>
</dependency>
Problem Statement: – We have to add items to a sorted set on basis of some random scores attached to the values. Also there has to be maximum of 10 items present in the sorted set. We will use the functions zadd and zremrandeByScore.
Some Java code:
 Initializing the Redis Client – Our server is up on localhost:6379 (default). For multiple servers running we can add them to pool.
public RedisCacheManager() {
    List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();
    for (String host : HOSTNAMES) {
        JedisShardInfo si = new JedisShardInfo(host, Constants.CACHE_PORT);
        shards.add(si);
        LOGGER.info("Added host to shard: " + host);
    }
    GenericObjectPoolConfig jedisConfig = new GenericObjectPoolConfig();
    JEDIS_POOL = new ShardedJedisPool(jedisConfig, shards);
}
Pushing items in a sorted set –
public boolean pushActivity(String activity, String activityId, double score, int ttl) {
    ShardedJedis redis = JEDIS_POOL.getResource();
    String redisKey = null;
    try {
        redisKey = activityId;
        redis.zadd(redisKey, score, activity);
        if (ttl > 0) {
            redis.expire(redisKey, ttl);
        }
        Long count = redis.zcount(redisKey, Double.MIN_VALUE, Double.MAX_VALUE);
        if (count > 10) {
            redis.zremrangeByScore(redisKey, Double.MIN_VALUE, count - 10);
        }
        return true;
    } catch (Exception e) {
        LOGGER.error("Unable to enter data into redis for - " + redisKey + " with activity - " + activity, e);
    } finally {
        JEDIS_POOL.returnResource(redis);
    }
    return false;
}
Retrieving item from Redis –
public Set<String> retrieveItem(int activityId, int maxIndex) {
    ShardedJedis redis = JEDIS_POOL.getResource();
    String redisKey = activityId;
    Set<String> redisValue = Collections.emptySet();
    try {
        redisValue = redis.zrange(redisKey, 0, maxIndex);
    } catch (Exception e) {
        LOGGER.error("Unable to fetch data from redis for - " + redisKey, e);
    } finally {
        JEDIS_POOL.returnResource(redis);
    }
    return redisValue;
}

Using Spring with Hibernate and c3p0 Connection Pool.

C3P0 is a very nice tool to manage database connections. I had hard time configuring Apache DBCP/2 so tried c3p0. There are many config options to set and the setting has to be done carefully so that we do not end up choking our database. Let us understand some of the config options.
  • testConnectionOnCheckin validates the connection when it is returned to the pool.
  • testConnectionOnCheckOut would ensure active connections before use, would be too expensive to do.
  • idleConnectionTestPeriod sets a limit to how long a connection will stay idle before testing it. Without preferredTestQuery, the default is DatabaseMetaData.getTables() – which is database agnostic, and although a relatively expensive call, is probably fine for a relatively small database. If you’re paranoid about performance use a query specific to your database (i.e. preferredTestQuery="SELECT 1")
  • maxIdleTimeExcessConnections will bring back the connectionCount back down to minPoolSize after a spike in activity, the connection is removed from the pool and returned back to db.
  • numHelperThreads it will help c3p0 spawns helper threads to manage the connections and returning them back

My spring configuration goes as –

<bean id="dataSource"  class="com.mchange.v2.c3p0.ComboPooledDataSource">
    <property name="driverClass">
        <value>com.mysql.jdbc.Driver</value>
    </property>
    <property name="jdbcUrl">
        <value>jdbc:mysql://localhost:3306/api</value>
    </property>
    <property name="user">
        <value>root</value>
    </property>
    <property name="password">
        <value></value>
    </property>
    <property name="idleConnectionTestPeriod">
        <value>300</value>
    </property>
    <property name="maxIdleTimeExcessConnections" value="180"/>
    <property name="maxPoolSize">
        <value>100</value>
    </property>
    <property name="acquireIncrement">
        <value>1</value>
    </property>
    <property name="maxStatements">
        <value>0</value>
    </property>
    <property name="minPoolSize">
        <value>10</value>
    </property>
    <property name="unreturnedConnectionTimeout">
        <value>3600</value>
    </property>
    <property name="preferredTestQuery">
        <value>SELECT 1</value>
    </property>
    <property name="initialPoolSize">
        <value>10</value>
    </property>
</bean>
Things to keep in mind:
  • To get an idea, please try to check the number of connection the app has hooked up with the database. For MySql try –  SHOW STATUS WHERE variable_name = ‘Threads_connected’;
  • When using Hibernate try to take care of opening and closing of sessions. If sessions are not properly closed, the connections are not freed and eventually it will choke the database.
Further Read - How to configure c3p0

Jackson JSON parser – Writing Custom Deserializer

One of major difference between Jackson and GSON is how they handle json parsing and mapping the json to a class object. While doing so Jackson uses strict checking which means the object from which the json was created and the class to which the json has to be mapped should be in strict sync.

Consider the following example –

class Foo {
private String value;
}
class Bar {
private String value;
}

and

String json = "{\"value\" : \"whatever\"}";
new Gson().fromJson(json, Foo.class);
new Gson().fromJson(json, Bar.class);

Gson is setup to perform a best effort to deserialize the given JSON into an instance of the given class. It will map as many fields as it finds. If none are found, that’s too bad, mapping will not be done.

Coming to the custom deserializer thing. We may need to write one in few cases as – changing date format of the json string to our custom one. For writing our custom deserializer we need to write a class which extends JsonDeserializer<T>. In this class we implement  the method deserialize(…) which contains our custom conversion code.

public class CustomJsonDateDeserializer extends JsonDeserializer<Date> {
@Override
public Date deserialize(JsonParser jsonParser, DeserializationContext context)
throws IOException, JsonProcessingException {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm a z");
String date = jsonParser.getText();
try {
return format.parse(date);
} catch (ParseException e) {
throw new RuntimeException(e);
}
}
}

Now annotate the field in the POJO which has to be deserialized using the custom deserializer.

@JsonDeserialize(using = CustomJsonDateDeserializer.class) private Date createdAt;

That’s it. Pretty simple thing.

RestEasy 3.0

We were using RESTEasy 2.0 in on of our earlier projects. RESTEasy is a JBoss project that provides various frameworks to help you build RESTful Web Services and RESTful Java applications. The 2.0 version was quite simple to use. I decided to use version 3.0 in one of recent projects and the migration was not that tough. A lot of chages have been done in the newer version. I am going to mention the ones which I found out.

RESTEasy Client 

In version 2.0

      String url = "http://resetservice.com/api/testService";
      ClientRequest request = new ClientRequest(url);
        request.body("application/json", json);
        response = request.post();
        int status = response.getStatus();
        if (status == 200) {
            String data = (String) response.getEntity(String.class);
            Boolean value = new Gson().fromJson(data, Boolean.class);
            return value;
        }
        return false;

The same thing  in 3.0 now changes to –

     ResteasyClient client = new ResteasyClientBuilder().build();
     String result = getResponseString("http://resetservice.com/api/testService");
     Boolean returnVal = clientUtil.getObjectFromJson(Boolean.class, result);
     return returnVal;
Authentication

I also had to use some in-house authentication technic.  I used it in very basic way. I checked for a particular request header (authorizationString) for authenticating the request. If that header is not present the request gets aborted. Below is the code snippet for the same.

@Provider
@ServerInterceptor
public class AuthorizationRequestFilter implements ContainerRequestFilter {
@Override
public void filter(ContainerRequestContext requestContext) throws IOException {
    final SecurityContext securityContext =requestContext.getSecurityContext();
    String header = requestContext.getHeaderString("authorizationString");
    try {
         if (securityContext == null || header == null || header == "" ||
                                (header != null && !header.equals(
                                 ConfigReader.getInstance().getAuthToken()))) {
             requestContext.abortWith(
             Response.status(Response.Status.UNAUTHORIZED).entity("User cannot access the resource.").build());
          }
        } catch (ConfigurationException e) {
          e.printStackTrace();
     }
   }
}

Amazon RDS with MySql – How to create an instance!

This post will guide through how to use Amazon RDS, the steps of creating an instance and other related things.

Before going further one can take a look on few links to get a hang of what RDS is all about and how does it perform against other available solutions.
Lets get started.
1. Launch DB instance – AWS Services are relatively cheaper in USA region, so you can consider about choosing the region. Go to Amazon AWS console and select RDS from there. You will land up on the below page. Click “launch a DB Instance”.

2. Select mysql engine.
3. Production- Choose Multi-AZ Deployment.
4. Specify DB Details – On the basis of your requirements you can choose DB Instance Class. In my case I used db.t2.medium which was of 5GB capacity. Specify user and password for the db from here.
5. Configure Advanced Settings – You can define your custom parameter group so you can simply choose default parameter group here. If you have a pre-defined security group, you should choose that one here. 
6. DB Instance Created- The database has now been created. It may take couple of minutes for the db to set up completely.
7. The newly created DB instance will start reflecting in the dashboard.

Changing the default configuration variables of MySql

Now after the db instance has been created we need to change a bit of configurations and monitor the db.

I struggled a bit when I had to change the default configuration values. To change the config values (equivalent to changing values in my.cnf file) we have these following steps.
1.  Go to RDS dashboard and select the database which you have created. Choose “Parameter Group” from the left pane.
2. Click on “Create DB Parameter Group” to create a custom Parameter Group. Choose some suitable name and description.
3. Now choose “Edit” to change the config. Some important variables as – max_connections, query_cache_size etc can be changed from here. The changes will be applied and reflected.

Monitoring

One of the coolest features of RDS is its monitoring. You can check out for n different metrics. – CPU Utilizations, Number of connections etc.
Simple, isn’t it? Feel free to comment.

A Bong Land Away From Bengal

Chittaranjan Park (Bengali: চিত্তরঞ্জন পার্ক), aka C.R. Park is a neighborhood in South Delhi, and home to a large Bengali community in Delhi. That’s a very known fact. People say, if you want to get a feeling of how Durga Puja is celebrated in West Bengal, then go to this place. I visited this place on the last day of Puja and witnessed the show and it was totally worth it. 


The Bengalis go little insane during Puja period. They have their own way of celebrating it. Seems like there is a sudden induction of too much happiness in these people. And thanks to whatever it is, the place turns into an electric one during the Puja. The streets were decorated with light bulbs – not too bright.  Too many people walking the streets. Kids shouting, girls giggling and ladies blabbering. Too much of chit-chat and I could not hear myself at times. Initially I was little disturbed by the noise, but after I walked enough along with the crowd I start enjoying it. And then there were things around which kept me engaged. The sound of drums, flock of girls in ethnic dresses dancing on a stage nearby the Pandal, a beautiful woman in red embroidered sari singing some classic Bangla song, young girls chatting shyly with boys near the food stalls, smell of fish in and around the Pandal. Everything was oozing out of itself some kind of Bengali class. 

Though I am not a great fan of Bengal/i, but one thing I must mention that I like is – they carry their culture wherever they go. You must be having some Bengali family living near by and they having your mind blown off with some great foods or at least some great variety of sweets. They make you feel the impact of rich culture own and they belong to. A single Bengali family can form community of its own and then slowly engulfing the people near by. A small place secluded from the typical charm which Delhi carries was what the place appeared. The streets reminded me of  a typical Kolkata streets minus the crowd part. It was indeed a nice experience. 

It Was Dreddfully Fantastic



Cast: Karl Urban, Olivia Thirlby, Lena Headey, Wood Harris
Director: Pete Travis

For a long time we all have been waiting for a perfect action movie where there is no Super-hero but a bad-ass toughie. Lessons, protagonism and philosophy ruin the essence of action. A perfect action flick is the one where we get some gory stuffs, gunshots, some dark humor, rushing sequences and  a music to elevate all these up to a different level. With Dredd you are guaranteed to get everything. A perfect violence crime thriller with every element presented in pristine form. Right from the start, the movie assures you that you will be fed a great dose of insanity and in the end you have with you a head swirling on the tune of it.

The movie, set in future,  revolves around busting of a ghetto gang homed in a multi-storeyed led by an unattractive blood crazed lady called Ma-Ma. This gang deals in production and selling of drugs and their main product- Slo Mo – which slows the perception of time of the addicts. The slow motioned effects of this drug has been so wonderfully portrayed. Everything stops and you feel yourself to be the subject. Judge Dredd, character led by Karl Urban, is a ruthless executioner who believes in law and exterminates every one who goes against it. He teams up with a rookie who is a mind-reader too and very strategically uproots the gang.

The plot may not be the greatest, but it does not have to be. What matters the most is the presentation. And for that the movie wins a standing ovation. Cinematography, I must mention, is breathtaking – as good as it should be. The futuristic city – chaotic, gloomy and plagued by social malaise – speaks off its story. The slow motion acts are one of many great things about the movie. Who needs a description on acting now? Karl Urban is such a perfect fit for the lead role. With laconic dialogues in husky voice, the helmeted actor will surely win your heart. The character(of a rookie) played by Olivia Thirlby is very powerful. She is on a par with Dredd – witty and equally intelligent. The music is just divine. It keeps you on your toes every time. The thrilling sequences coupled with great background music will make your heart slip at times.

Dredd is what we have been looking for so long. It has been crafted very nicely. A perfect juxtaposition of brutality and violence! Leave everything behind and behold to those bloody sequences. It would very foolish mentioning what 3D effects have done to the movie. For those who enjoyed Kill Bill and District 13, here is Dredd satisfying the hunger of both.

The Greatest Fear Of All – Fear Of Death


No one knows whether death may not be the greatest of all blessings for a man, yet men fear it as if they knew that it is the greatest of evils.”  – Plato.

I am not fearful of death – of my death – but I get too much scared when someone dies – someone close. I am fearful of the fear and silence that it propels in the  surroundings. I am fearful of the all the other scared faces around me, of the mournful smell of the air. I am fearful of the darkness with which it overshadows the brightest sunlight. The tears of all the disturbed souls that soon would flood the universe haunt me. The  swelled-dry-lips and red-wet-eyes of all who are suffering the loss weaken me. How cruel is the fact –  that this astronomically huge span of life would be destroyed in a blink of an eye and would leave everyone behind in pain -it tortures me a lot. Death, by far the most stupid and preposterous creation of God is the greatest fear of all. 

A couple of days back I lost someone. A person who was not there with me for the most of the time of my life, but yes there was this noble and magnanimous image of that lady which was alive in my mind,  my heart and my thoughts for so long – it was all shattered. The image which was just so alive and so vibrant – talking to me smilingly – was all missing in the very next moment.  It was like a black-hole of silence and fear and pain created somewhere in my heart and ’twas drowning me in. I was scared and was never scared this much. I cried a lot – alone – many a times – but that was even drowning me more. I was crying and thinking about the condition of those who were closely related to her, those who had spent their entire life with her.  How pitiful their conditions would be – unimaginable ! And then I was cursing God for  he was the only one who scripted all this drama and was watching it being enacted. Preposterous.

I do not want to leave people behind me tormented and crying in pain. I now know how it feels. But I can not help it. I know they will cry when I am gone, miss me more than ever. But why? Why God has created such nonsense circle of Life and Death? Why on one hand does he put so much strength and positivity in Life and then despise his own creation and spread fear with Death? And why does he not bestow the humans with the power to overcome that fear? Why does he let them suffer? These unanswerable questions will haunt everyone – everyone who has felt death so closely. One can not escape this only irrefutable reality. But then why is this truth so untrue that no one want to face it, to believe it? Death is  evil and the God himself created this evil.

Rest in peace the pious soul of the lovely and beautiful old lady; you shall always be there in my heart. 

Killing Of A Hope

I followed the news channels and newspapers over 3-4 days and there were numerous of news which left me miffed. I do not generally involve myself in going through what’s all happening in my country because my heart is afraid of and can’t take anymore these descriptions of rapes, murders, fraud and vandalism. But they come to you anyhow to annoy you, disturb you, haunt you and leave your mind in a state of paranoia. No one knows whats happening out here. You are never sure about the things which may happen to you.  Look around yourself and see what this world has turned into. Never a peaceful place was this world, it has gone more clamorous.

I remember this instance from my childhood. My father used to read newspaper very minutely. He would read every peace of news every time. And when he would finish, I would grab it and check what was so important out there which kept him so indulged. I would find only useless rubbish things –  partly because I kept myself off from politics and big write-ups and partly because I used to find the small news interesting. One day I asked him –
“What do you read the newspaper for?”
“To know whats all happening.”
“But I only find news of theft and fraud and land disputes. They disturb me a lot. Don’t you get disturbed? What else do you find there which I do not?”
“Did you check out about the new Dam Project? The central government has approved it and soon it is going to do wonders to many people. Won’t they be happy? Do not you think it’s a great news?”
“No. I think I missed it. I shall check it again.”
“I knew you would never read those things or let me rephrase my words – you would never find such things interesting. Son, there are always some bad things happening around you, creating frictions in your mind, deviating you from real pleasures of this world. But that does not mean you start loosing hope. Hope is what drives this world, will always be the most important factor for the mankind to sustain. Whenever you are surrounded by the clouds of grief and dismay, hope is the only thing you can count on.”
“But does not hope weaken you?”
“How can it do so?”
“Let’s say you only hope and hope for something to get better, for example – this society to get better. And you know it never is going to be. In that case your hope is bringing you nothing. Is it?”
“What would say about those poor villagers who got today the news about Dam Project approval? They were also hoping. They were hoping that if something be done and the floods never strike. They were hoping if they were not turned to homeless refugees by the flood. Hoping that their crops were never destroyed. Hoping that they would even for once get to see what electricity looks like. Now did this hope weaken them? “
“No. Not at all.”
“You know many people take this hope thing wrongly. Hope is not presuming that a thing is going to happen for sure. It is rather convincing the heart that the thing may happen and be prepared for it. You may have to celebrate or may have to feel bad. But you never win or lose when you hope. Do you try doing something when you hope for something? No, because you have tried doing all you could and now it’s the time to just sit and wait. So there stands no chances of winning or losing. You write an exam and then you hope that you get good grades. You do not appear for the same exam for many a times to get good grades, that would be a such a flop case.”
“Yes, I get it.”

Do I hope for anything now? Do I ever convince my heart that things are going to get better? Not at all. This country of mine has turned into a rat-hole. But it used to be something different till when I was a child. Now when I compare today’s condition with that of 12-13 years old, I find myself very tormented. This ain’t the same world which my father had then promised me it would be. Everything is totally preposterous. There is this air of angst in which I am suffocating. Nothing can be done now. No reform, no rebel, no riot nothing can help. You can not do anything but just sit back and rue the day you were born and let this whole craziness eat you up.

Morning’s Mine !!

One morning, which is usual as all the mornings,I hear a hard knock on my door. “Let’s go to the mess,its breakfast time”. I hear that knock.What the hell!Why is this happening? My minds instructing me-dont you dare open your eyes,otherwise the eyeballs are going to pop-out.I need my pillow to put on my ear-God damn it, where’s it?? I spread my hands to every part of my bed I can, but my body’s not getting slightest of movement. My pillow’s missing. Fuck it – bastard’s missing. Suddenly I feel my leg’s feeling it somewhere.” Ahh, thank God – I got it – you just saved my morning from getting screwed”. I grab the pillow, put it on my ear and make sure that even the most tumultuous thunder doesn’t disturb me. But, I can feel that the knock is growing louder as the person’s getting no response from me. He reminds me what I had said to him last night. “Friend, I’ve been missing my breakfast for last 4 days due to oversleeping. I dont want this to happen tomorrow again. So, please wake me up in the morning anyhow. Do whatever comes in your mind.But, please!! “, he says. I am listening all these shits.”What a stupid he’s,he takes everything so seriously”.I am thinking this and cursing myself for whatever i said to him last night.

Eventually, he gives up. “Fuck off. Dont you dare say me to wake you up any day. Its all wastage of time. I am leaving. “I am hearing all this with all my ears,he leaves and I take a great sigh of relief. “Good, you are gone, now I can have a sound sleep. “Pressing hard the saviour pillow on my face I push my self to a deep sleep, deeper than ever.

After about 1 hour I feel its time to get up. I try very hard to lift my body off the bed,seems that the bed has clutched me tightly and it doesn’t want me to leave it. A commitment that I make to it just before I sleep is to break,so it tries to savour every last moment that I am spending with it. Finally I detach my body, mind and soul from my sweet and lovely darling, promising that i will be back again with a lot of love and the next night will be more entertaining for it.