Tuesday, 25 March 2014

Buy One, Get One Free on all of #Packt’s 2000 eBooks

The fine folks at Packt are having a Buy One, Get One Free sale, to celebrate their 2000th title. Go here and give them your money!

Saturday, 26 January 2013

Coherence Queue Messaging testing with littlegrid

For first time Coherence users, the thing that strikes you is the lack of a testing harness, which usually forces you to create custom jobs for setup; these ,for the most part, tend to be brittle and difficult to maintain. Thankfully, the excellent littlegrid by Jon Hall makes Coherence integration and unit testing a breeze. I recently had a requirement, where I had to implement one of Coherence's Incubator patterns, Messaging. The following is how you go about setting up and testing the Queue implementation of this pattern, with the aid of littlegrid.


Requirements

  • latest littlegrid
  • Coherence 3.6.1
  • Coherence Messaging Pattern 2.8.4.32329
  • Coherence Common 2.3.0.39174


Implementation

We start by setting up the minimal XML configuration files required for Coherence.

<pof-config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.oracle.com/coherence/coherence-pof-config" xsi:schemalocation="http://xmlns.oracle.com/coherence/coherence-pof-config coherence-pof-config.xsd">

    <user-type-list>
        <include>coherence-pof-config.xml</include>
        <include>coherence-common-pof-config.xml</include>
        <include>coherence-messagingpattern-pof-config.xml</include>
    </user-type-list>
</pof-config>

<cache-config
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns="http://xmlns.oracle.com/coherence/coherence-cache-config"
        xsi:schemaLocation="http://xmlns.oracle.com/coherence/coherence-cache-config coherence-cache-config.xsd"
        xmlns:element="class://com.oracle.coherence.environment.extensible.namespaces.XmlElementProcessingNamespaceContentHandler"
        element:introduce-cache-config="coherence-messagingpattern-cache-config.xml">

    <defaults>
        <serializer>
            <instance>
                <class-name>com.tangosol.io.pof.ConfigurablePofContext</class-name>
                <init-params>
                    <init-param>
                        <param-value>src/test/resources/master-pof-config.xml</param-value>
                        <param-type>String</param-type>
                    </init-param>
                </init-params>
            </instance>
        </serializer>
    </defaults>
</cache-config>

Finally the unit test code

import com.oracle.coherence.patterns.messaging.DefaultMessagingSession;
import com.oracle.coherence.patterns.messaging.MessagingSession;
import com.oracle.coherence.patterns.messaging.Subscriber;
import junit.framework.Assert;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.littlegrid.ClusterMemberGroup;
import org.littlegrid.ClusterMemberGroupUtils;


import java.io.IOException;
import java.util.concurrent.*;
import java.util.logging.Logger;

public class ITCoherenceMessaging {


    private static ClusterMemberGroup memberGroup;
    private static final Logger LOG = Logger.getLogger(ITCoherenceMessaging.class.getName());


    @BeforeClass
    public static void beforeTests() {
        memberGroup = ClusterMemberGroupUtils
                .newBuilder()
                .setStorageEnabledCount(2)
                .setCacheConfiguration("messaging-cache-config.xml")
                .setLogLevel(3)
                .buildAndConfigureForStorageEnabledMember();
    }

    @AfterClass
    public static void afterTests() {
        ClusterMemberGroupUtils.shutdownCacheFactoryThenClusterMemberGroups(memberGroup);
    }

    @Test
    public void testMessagingQueue() throws ExecutionException, InterruptedException {

        MessagingSession messagingSession = DefaultMessagingSession.getInstance();
        messagingSession.createQueue("Queue");

        LOG.info("_______ Inserting message _______");
        messagingSession.publishMessage("Queue", "foo");

        ExecutorService executor = Executors.newSingleThreadExecutor();
        Future<String> messageFuture = executor.submit(new Callable<String>() {
            @Override
            public String call() {
                Subscriber subscriber = DefaultMessagingSession.getInstance().subscribe("Queue");
                LOG.info("Waiting for message");
                return (String) subscriber.getMessage();
            }
        });
        Assert.assertEquals("foo", messageFuture.get());
        executor.shutdown();
        try {
            executor.awaitTermination(30, TimeUnit.SECONDS);
        } catch (InterruptedException iex) {
            executor.shutdownNow();
        }
    }
}
Let's take a look at how the test is composed.
    @BeforeClass
    public static void beforeTests() {
        memberGroup = ClusterMemberGroupUtils
                .newBuilder()
                .setStorageEnabledCount(2)
                .setCacheConfiguration("messaging-cache-config.xml")
                .setLogLevel(3)
                .buildAndConfigureForStorageEnabledMember();
    }

    @AfterClass
    public static void afterTests() {
      ClusterMemberGroupUtils.shutdownCacheFactoryThenClusterMemberGroups(memberGroup);
    }
These methods contents are pure littlegrid. What we are doing here is setting up a new Coherence cluster, with 2 storage enabled members, pointing it to the cache configuration, setting the log level to 3, and with the final method invocation starting it up. As you might have noticed with the @BeforeClass annotation present, we do this as a pre-initialisation step. Similarly, the method annotated with @AfterClass tears down our cluster after all our test methods have completed.
 
MessagingSession messagingSession = DefaultMessagingSession.getInstance();
messagingSession.createQueue("Queue");
messagingSession.publishMessage("Queue", "foo");
Subscriber subscriber = DefaultMessagingSession.getInstance().subscribe("Queue");
(String) subscriber.getMessage();

The preceding block is the meat of Coherence Messaging. We get a MessagingSession instance and we create a queue. We then publish the String "foo" to the newly created queue.
Finally, we get a Subscriber instance by binding it to our queue, and invoke getMessage() on it which will block until a message is received.
It's worth noting that the createQueue() invocation returns an Identifier object, which we can use as the first argument to publishMessage() and as the single argument to subscribe(), instead of the String name of the queue.

And there you have it, a simple Coherence Messaging test, thanks to the power of littlegrid.

Tuesday, 23 October 2012

Thanks Apple..

..for feeling you should screw me as a loyal customer with a hardware refresh less than 6 months after I bought my "new" iPad..

Sunday, 21 October 2012

Another PaaS in the wall?

I received a LinkedIn invite recently to try out a new PaaS service called Uhuru (www.uhurucloud.com). I haven't played with cloud VM's and services before, mainly due to time and interest constraints, but hey what the heck, I had an hour or so to kill...

After a painless signup, you are presented with this screen:



It's pretty sparse stuff at the moment. You can push a bunch of pre-packed modules, which are the usual mix of CRM/PHP applications, create/rename/remove your Cloud and define services; the following are available:



And this is as far as you would get from the web based UI. In order to do any real work, you'd need to use their .NET GUI, or a command line client; and this is one of my first gripes. Seeing as they call this bit a "Service" , I would expect a wider mix of types here e.g. a Java web app container. All things being equal, I would prefer if I could deploy from the web and the command line, and dispense with the .NET local GUI.




Using the .NET GUI has been a mixed experience. It's clearly rough around the edges, but mostly functional. It will now and then get mixed up when you delete a deployed App and keep displaying it's status as "DELETING", although it's been removed from the tree at the left hand side.

Deploying is fairly easy. Right click on apps, select "Push" and you are presented with this menu:

You can define your framework or have the GUI detect it for you. It did get my app type wrong though, more on it shortly. Once you've defined your app and tied it to the services it requires, it's time to push and that's that...

Or not. Unfortunately, as of the time of writing this, I've not been able to deploy an albeit non-trivial web app, but with a very standard WAR structure; The command line client was equally cryptic



 and therein lie my gripes:


  • The UI and command line need much better feedback. I simply got a failure to push the app and had no idea why.
  • You cannot push an exploded WAR. I see no real reason why this should not be possible
  • The GUI got my application type wrong; it thought it was a Spring app just by virtue of it having an applicationContext.xml
  • I cannot edit the Tomcat server.xml properties so no DataSource access via JNDI.
  • I cannot create a DB once my instance has spun up, but I can drop the existing one.
These things aside, the whole premise looks promising. Of course, there's the little issue of a pricing structure, especially given the big bull in the corner (hello Amazon) but it's early days.

Saturday, 16 July 2011

New-New beginnings

I've been in a new job for the past few weeks now and I'm enjoying it. I got to play with stuff I haven't touched in ages, and although not my forte, it's fun. The first couple of weeks are quite curious for me, as I felt my new colleagues are sizing me up (maybe that's the paranoia talking, hello!). 

I usually don't want to pipe up too much, in case nonsense comes out of my mouth, as it tends to happen when I am nervous. However, the odd thing that has happened is that I started feeling a heck of a lot more confident as time went by, something which took a long,long,long time in my previous job. There's a refreshing lack of ego from everyone so far and my opinion is actively sought after, which is great. Even though it's a very traditional sort of organisation, in terms of development processes, they are very open-minded and will try new things if the justification is there.

Of course, the shine may eventually wear off and I might sing a different tune, but for the time being, all is good.

Tuesday, 26 April 2011

On interviews

I was recently called up for a couple of job interviews. One of them looks very promising, both are with financial institutions. Now, anyone who knows me (well) will tell you this about me:


I hate interviews, with a passion.


The night before one, I will pace endlessly, sleep fitfully, wake up with my head buzzing like a swarm of bees on acid. I will actually arrive at least 45 mins before, so that I can calm the hell down. Which doesn't really work all that well as I will get agitated again, the moment I start making my way to the doors. 
Once I meet the interviewer and we exchange pleasantries, I await for the moment the dreaded test is wheeled out; at which point I feel like Han Solo on the interrogation slab, before Darth Vader. Quick check at my armpits and yes, I am sweating buckets and the sensation of a deep unpleasant heat develops at the pit of my stomach, even before I've looked at the questions.


I've tried to reason why, many times, and it's a deep seated anxiety. I believe it stems from these three things:

  • I always compare myself with the better person than me, and naturally find I am falling short.
  • I forget things.
  • I hate humiliation.
The last one is a killer. I dont enjoy making a fool of myself, and I always want to have the answers for things I'm supposed to be a damn professional at. So I take it as a major personal failing when I can't fully (or at all) answer something.


So, you can imagine what happened when I was given a written test and asked to comment on a piece of code on the fly. 


You probably think I soiled my pants.


I didn't.


I aced it, and I have no idea how. The information came readily available to me, I was fully answering what I knew, and taking an intelligent guess at what I didn't. Maybe this is a turning point for me, maybe this is a one off, but even if I dont get the job (one more interview to go) for one day, I felt so damn good about myself and my knowledge.

Friday, 8 April 2011

On books..

A recent spate of self-loathing prompted me to post the following on Twitter:
The more I read @joshbloch 's books the more I want to sit in a corner rocking back and forth, mumbling "my code sucks" ad infinitum..

I didn't expect anyone in particular to answer or comment, I was merely railing against my own inadequacies. Yet an hour later  I received this
@idiotmonkeycodr Sorry.  That wasn't my plan.  FWIW, I learned most of those lessons the hard way. 

Holy shit..I peed myself because Josh Bloch deemed my bullshit worthy enough of a response (ok a little bit of hero worship in play here). However, I almost instantly realised, I had actually inadvertently offended him. Here's someone who put a lot of fucking effort in producing books that are concise and well-read and some shit comes along and complains (albeit obliquely) that they point out the major suck-age in his code.

This all comes down to the fact that I feel very defensive about what I code, because I know my initial drafts are going to be bullshit, so to read how things should be done, makes me feel embarrassed to call myself a developer (coupled with my bizarre inability to absorb and retain information, I've read Effective Java probably 5 times now, from cover to cover, and I still cannot recall most of the nuggets of wisdom contained therein).

I guess Google telling me (politely) to fuck off has seriously dented my confidence..