Thursday 31 January 2013

Tech: A Rough Guide to Atmosphere with Spring, Tomcat & Jersey: Avoiding the Pitfalls

Welcome to the second  installment of the NerdAbility tech guide series. This post looks at the Atmosphere framework! Enjoy....

Edit: I Previously forgot to include the  project sources and people have rightly asked in the comments about it. I couldn't find the example I wrote this blog post from, but I found a similar example on my github. You can find it here

More and more it's becoming a common requirement to support some sort of bi-directional (asynchronous) channel in modern web applications. Atmosphere is a great library which does much of the heavy lifting if you're running on a Java, servlet based stack. It offers support for WebSockets, Server Side Events (SSE), Long-Polling, HTTP Streaming (Forever frame) and JSONP. There are, however, some pitfalls and, if you've not got a lot of experience in the area, it can be frustrating trying to work out whats going wrong.

Having run through the process once I thought it'd be nice to cover my implementation in some detail. Maybe you'll see something which can help you with your own configuration or implementation problems.

Overview of the stack

This is what I had in place before integrating atmosphere.
  • Application server: Tomcat 7.0.32 (Must be greater than 7.0.30 for atmosphere to work correctly).
  • Container: Spring 3.2 RC1 ( though anything post 3 is fine )
  • Build Enginer/ Dependency Manager: Maven3
  • Front-End: Javascript / JQuery

    Include the Dependencies

    You'll want to add the following entries to your POM :

    Properties:

    Dependencies:

    Just a quick note about the cors-filter dependancy, you'll only need it if you want Cross-Origin-Resource-Sharing support. I thought about covering it in this article, but maybe i'll write it up as a follow up

    The web.xml

    If you're not using servlet 3 already, time to upgrade. Change your web-app config to look like this:

    The next step is to include the standard spring dispatcher etc to your configuration. You will probably you'll already have this anyway.


    The Atmosphere Controller 

    Using The Atmosphere Servlet

    I fully accept that everyone has their own pet way of setting up servlets, this is just one possible mutation. You want to add the following to your web.xml:

    Pitfall 1: The broadcasterLifeCyclePolicy tells atmosphere to destroy any connections that been closed by the client, enabling it will prevent OOM (Out of Memory) issues in your application.
    Pitfall 2: The recoverFromDestroyedBroadcaster prevents an exception being thrown when you try to reuse a broadcaster that you have recently destroyed ( useful if you want your client to subscribe using its own personal channel ).
    So now you're able to use spring MVC alongside your atmosphere code without them stepping on each others' toes. Lets move on.

    The Controller Class

    Mine was simple enough. It basically passes off the broadcasters that it creates to another service which whatever listeners you have configured ( Mine was RabbitMQ ) will call when they have an appropriate message.

    
    @Configurable: Using @Configurable is a great way to use aop classweaving to inject spring dependencies into non-managed spring classes. There is extensive documentation on how to set this up in spring 

    Pitfall 3: Make sure you set the suspend period, or you're going to be dealing with a java.net.SocketException: Too many open files in pretty much no time.

    The Service Class

    Also very simple. Using a map to store the in use channel ids -> broadcasters, the service is simply responsible for looping through the registered broadcasters and broadcasting a generic message.

    The Async Class

    Probably you'll have some asynchronous process which is waiting for messages, there are many possible implementations that this may take so i'll just show one of the popular ones RabbitMQ.
    You'll need to make sure you add the following dependency to your pom:

    RabbitMQ has a pretty clean java integration which is nice and quick to set up. The first component is a context configuration file where you bind your queues, connection factory, admin, exchanges and listeners.


    Notice the reference to broadcastQueueConsumer we used there ? Lets write that now.

    I've omitted the declaration of the Simple Message Converter in the context file but please dont forget about it. Once you're done you've got a pretty clean implementation of the serverside and should be ready to move onto the frontend.

    JQuery frontend implementation

    Really its quite simple to build a small subscription handler. I've added an example of mine below :
    Pitfall 4: Make sure you set enableXDR to true if you want to support cross domain requests.

    Pitfall 5: To send custom request params it seems like you need to pass via url (maybe jfrancard has fixed now but not sure)

    Thanks for reading, and feel free to ask any questions via a comment below or our twitter @nerd_ability

    Wednesday 16 January 2013

    Tech: How to Fix 'SSLPeerUnverifiedException: peer not authenticated' Exception in Groovy / Java

    This is the first in a series of tech posts on the NerdAbility blog. We are aiming to blog any useful tips / gotchas we come across when developing with various technologies. We will still be posting tech recruitment insights and NerdAbility news, so stay tuned!

    How to Fix 'SSLPeerUnverifiedException: peer not authenticated' Exception in Java / Groovy


    When developing with web services in Java you may come across the need to connect to a HTTPS URL, for example when creating a REST client. In some cases there will be an issue with the type of certificate the web server is using, resulting in a SSLPeerUnverifiedException.

    To solve this you could previously export the servers SSL certificate via firefox / chrome and load this directly into the cacerts keystore (jvm's default trusted keystore). In recent versions of firefox / chrome this feature seems to have disappeared.  In this post we will show you how to grab the certificate using command line tools and then load it into the cacerts keystore. Finally we give an example of connecting to a HTTPS URL with Groovy using RESTClient.

    Please note this guide is for Linux / Mac users. Windows users may be able to follow along using cygwin, but we have not tested this. If you are using an alternative trusted keystore in your application, use this instead of cacerts in the examples.

    Prerequisites: Before loading any key into your cacerts keystore, please verify you are happy with the certificate and its authenticity, and issuer. You can do this by using a tool like this one.

    Disclaimer: Follow this guide at your own risk, we can not be held liable / accountable for any damage or issues caused to you or your systems.

    Step 1: Download and Store the Certificate


    To download and store the certificate run the following command, changing $ADDRESS for the sites address. For example https://www.facebook.com would become facebook.com:

    echo -n | openssl s_client -connect $ADDRESS:443 | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' > /tmp/$ADDRESS.cert

    To check the certificate was grabbed, you can run:

    cat /tmp/$ADDRESS.cert

    This will output the certificate and you should see something like:

    -----BEGIN CERTIFICATE-----
    *DATA*
    -----END CERTIFICATE-----


    Step 2: Load this into the default keystore for the JVM CACERTS


    First of all you need to locate the cacerts keystore for the JRE you are using. To find out the version of java run the following command:

    java -version

    This should give you something similar to:

    java version "1.6.0_11" Java(TM) SE Runtime Environment (build 1.6.0_11-b03) Java HotSpot(TM) 64-Bit Server VM (build 11.0-b16, mixed mode)

    Next take the Java version number from the previous output, in this case 1.6.0_11 and use locate to find the cacerts keystore for this Java install:

    locate cacerts | grep "1.6.0_11"

    The output should give you something similar to:

    /usr/lib/jvm/jre1.6.0_11/lib/security/cacerts

    Now you have enough information to import the key into the keystore. Run the following command, replacing the $ADDRESS with the address variable you used earlier, the $ALIAS with a name for the certificate i.e. facebook. Replace the $PATH variable with the path to the cacert (the output from the locate command we just ran). Also we have added the -storepass argument, passing the default password for the cacerts keystore. You will want to change this, if you have not already, and should be prompted to do so.

    sudo keytool -importcert -alias "$ALIAS" -file /tmp/$ADDRESS.cert -keystore $PATH/cacerts -storepass changeit

    Once you run this you will be shown the certificate and prompted to confirm you want to import the certificate:

    Trust this certificate? [no]:  yes
    Certificate was added to keystore

    Now you should have the certificate ready for use in your application, providing it is configured to use the default keystore and runs on the JVM we configured the certificate for!

    Step 3: Test It!


    Here is some example Groovy code using RESTClient:



    Thursday 10 January 2013

    TechBritain Launch Interactive UK Tech Startup Community Map

    Tech Britain has just launched an interactive map of the UK tech startup community! This great service allows you to explore the tech startup scene around you, including hangouts, investors, companies and communities. 

    Tech Britain Interactive Map
    TechBritain.com UK Interactive Startup Map
    If you are working in a startup why not explore who is around you, or where you can grab a coffee and meet up with other startup folk? Maybe you are looking to move to a startup or just get involved, you will find all this and more!

    Tech Britain is also taking submissions for anything that they have missed, so if your company isn't featured you can sign up and submit a listing here.

    Remember if you are wanting to get involved with the UK tech startup scene also come check out NerdAbility.com and create a free profile to show how awesome you are!

    Wednesday 9 January 2013

    2013: A Programmer's Resolutions

    This time last year an article did the rounds that featured 12 month-long resolutions aimed at developers - it was very interesting and insightful with some good ideas and it generated a lot of discussion and interest around the web.

    So this year I thought it would be fun to set out some NerdAbility New Year Resolutions, I hope you find them interesting/inspiring and feel free to adopt as few or many as suits you! (and of course, the month names are there for fun, they can be done in any month/time period you like)  

    (I have intentionally not gone back and re-read last years list, as they were good points that would probably just get stuck in my head and would result in this list being a clone of those ideas - but don't shout if there is still some overlap!)

    Get Real
    Month 1 - January
    2012 was very much the year of the Raspberry Pi, at just ~£25 it has made working with the physical a very real possibility for many developers previously used to working purely with software. Step away from the software for a month and make something real. Even if it's just a cat toy that sends tweets, or a home made spice rack, making something physical will give you a different perspective, a different set of challenges to building software, and will likely help you tackle software problems with a different mindset.

    Get Healthy
    Month 2 - February
    This is probably an obvious one, and probably one that should last for more than a month. As professional developers we spend a lot of time sitting down, and when you spend some of your free-time programming as well it leaves little time for physical exercise. More and more reports show these days that sitting down more than 8 hours a day is detrimental to your health, so we all really need to keep on top of this one! Livestrong.com is a great resource for health and fitness and you can get most of the information for free. If you are new to fitness try following a program like this one which can be done at home just using your body weight.

    Get Aesthetic
    Month 3 - March
    For some of us, this will come natural (the designers among us anyhow), but for lots of developers aesthetics and visual designs are not something that need to be considered. However, understanding aesthetics and good UI design can be good on the CV but can also help developers understand customer rational, User Experience (UX) and the more visual elements of building an effective product. Plus with more and more developers building mobile apps, it is often down to them to make them visually pleasing and to ensure a good UX. If you are gunning for a job in a small startup then design skills (or at least understanding of design issues) are an enviable string to your bow!

    Get Involved
    Month 4 - April
    There are loads of active technology communities these days, both online and in the real world. Get involved in a community! Whether this be an online forum or q'n'a site (you know we are always preaching the many benefits of Stack Overflow) or in the real world in the form of a local meetup (not heard of any local meetups? head over to meetup.com - you'll be surprised how many you find!).  Being active in any kind of community can be beneficial in so many ways - you can help junior members in a mentor role, you can learn from more experienced, you can network, you can find out about new jobs and you can make friends!

    Get Introspective
    Month 5 - May
    How long have you been at your current job? are you happy there? even if you are, it is always to be aware of what the job market is like, what roles you might be able to do, what you might like to do. Even if it is just thinking about your career plan and where you see yourself in a few years (even if its still at your current company). The tech job market in the UK is really busy and its a really great time to be a developer right now, so its good to know your options.

    Get Creative
    Month 6 - June
    Create something! Start a personal project, put it up on GitHub and spend a month working on it. Whether it be something you work on alone or with friends, get it started and see where it takes you! Personal projects are great for your CV (demonstrates actual ability and lets employers see your code plus shows you to be pro-active and motivated in learning and working on projects), they also let you brush up on your skills and maybe use technologies you wouldn't otherwise get the chance to play with (stuck using Java5 in work because of policies? Your personal project can be Java7, latest Spring nightly builds, Groovy 2.x, anything you want!).  If at the end of the month its not going anywhere, then at least you have brushed up your skills, have a great piece for your CV and spent time well.

    Get Trendy
    Month 7 - July
    In tech one thing you can always bank on and that is trends and fads, there is always something in trend in tech, and it inevitably later starts getting a hard time (see the stick MongoDb got after shooting to fame). But whilst some tech trends may be a flash in the pan, many of them do see out the time to become established technology - The current trend for Javascript being a great example with the likes of NodeJS offering complete JS solutions. So why not spend a month learning what they are about? If nothing else you will be able to contribute something more to the latest water fountain banter.

    Get Mobile
    Month 8 - August
    Its fairly safe to say that mobile isn't going away, and with iPads outselling Windows laptops this last year, its also safe to say that if you start learning to develop apps for iOS or Android you are in a pretty stable market place. Get involved and write an app for your phone. Android is free to do, with awesome SDK and plugins for several (free) IDEs, go ahead and get involved. Another great demonstrable piece for the CV and presents different challenges such as catering for reduced power/memory, different device sizes, UI design, etc.

    Get Wordy
    Month 9 - September
    Start a blog. Blogging can be quite therapeutic and is also a great way to give back to the tech community with insights or tutorials of how you solved a problem. With free platforms like Google's Blogger and WordPress.com its so easy to get up and blogging in seconds. But don't feel as though just because you work in tech you have to blog about tech - write about anything you ate passionate or opinionated about, be it movies, food, art, comics, or home decor. It will improve your writing skills and another way to show off your skills, knowledge and passion on your CV.

    Get Out
    Month 10 - October
    So yeah. Get outside. Spend some time to things you love (other than tech stuff), go to the cinema, go on holiday, visit interesting places you've never been, go to the pub. Just have some fun. After a month spent in the pub or on holiday, maybe skip to Decembers activity? It sure sounds good to us!

    Get Schooled
    Month 11 - November
    Learn something new, take a new course, learn a language, take a cake decorating course. Expand your mind. We will be doing a post soon about becoming a life long learner and sharing some awesome resources for continual learning. There's loads of interesting stuff out there to learn about.

    If you are interested in picking up some new coding skills there are plenty of cool courses at codeschool.com or codeacademy.com!

    Get Lazy
    Month 12 - December
    The cynical among you may think this is just because we ran out of ideas. Or just couldn't be bothered ourselves. But really, take some time off, spend it with family, friends etc. After all, that's what life is all about.


    Sunday 6 January 2013

    Why it's Great to be a Developer in 2013

    At NerdAbility we have been chatting about how great it will be to be a developer in 2013 and wanted to share some of our predictions, to help keep you one, two or even three steps ahead of the pack!

    Startup Life


    With the London startup scene still booming we think that the hunt for the best developers will really intensify and the need to stand out from the masses will continue to be a key factor in joining the hottest tech startups and SME's. Also (surprisingly) we expect to see companies moving away from CV's and resumes and relying more on GitHub and StackOverflow for screening candidates. There are also companies offering to test coders for you (at a cost of course), one that we think looks interesting is Codility. They offer online coding tests that you can customise and support a range of languages. Codility also offers the ability for developers to self certify by completing a monthly challenge.

    We expect increased collaboration between startups to maximise their visibility as the UK economy improves in 2013. Also don't be shocked to see more collaborative / integrated services launched between startups and the SME's (maybe even enterprise?!). Take a look at the Argos and Shutl (delivery startup) collaboration for products ordered on www.argos.co.uk as an example of a big business working with a startup / SME. Hopefully this will lead to lots of exciting opportunities for developers!

    Argos Shutl Collaboration Example

    Call Me Maybe? 


    Will 2013 be the year of the meetup? We think so! Well tech meetups have been round for a long time, but we think 2013 will see their traction deepen as sites like meetup.com increase the accessibility and visibility of the great events being hosted around the UK. We hope to see companies encouraging their staff to get involved, present and share insights into their technical operations and innovations! Many events also have slots for new presenters in what are being called lightning talks. These are short presentations, demo's or talks that are the perfect way to share your thoughts, cool things you have made and get experience with an audience. So get involved and give a talk in 2013!

    Is 2013 the year the world learns to code? 


    Well the site CodeYear sure tried to make it happen in 2012! CodeYear received big support from around the web and even got NYC's Mayor Michael Bloomberg signed up! We think the best thing to come out of this initiative was the Codecademy platform. Here you can follow a range of programming courses for free and record your progress. This is a great alternative to sites like Code School that charge a monthly fee. It's worth noting with code school you do get access to great video content and the courses cover the latest technologies in depth. We think that the technical education space will continue to expand in 2013 as interactive learning technologies mature, which is great news for developers and students alike. We hope to see more people and groups (open source contributors, enterprises, developers) getting involved creating material for these platforms!

    Engagement!


    We predict your online and offline technology engagement will matter even more to companies this year, so now really is the time to start building your online portfolio and growing your engagement with the community. We thoroughly recommend joining StackOverflow and getting involved with the Q&A, uploading / open sourcing your interesting hobby projects to GitHub or BitBucket and blogging about any of the tricky problems you have solved. Also go along to interesting meetups and talks, with many of the best listed on meetup.com. If you are in London why not check out the London Java Community for a range of great events.

    Remember a great place to show all that you are involved with is NerdAbility.com. We integrate with GitHub, StackOverflow, Geeklist, BitBucket, Google Code and all RSS / Atom blogs, so come on over and create your free profile now!

    Thursday 3 January 2013

    Technologies You Can't and Won't Miss in 2013

    With the new year welcomed in around the world, here at Nerdability we wanted to highlight some of the tools and frameworks we have followed / used in 2012, which we think will continue to grow and have a strong 2013.

    Meteorjs Logo

    The Meteorjs framework / application platform made a big splash in the technology ocean this year when the group behind it received a ton of funding ($11.2m) to focus on developing the open source project. Meteor is great for building responsive web applications and components that run in a modern distributed environment. Meteor is based around the concept of smart packages, offering bundles of functionality that can be added to your application. These smart packages  help keep things nice and lightweight and also offer functionality that you would spend way to long making yourself. However Meteorjs is in what they are calling early preview, so things will be changing and if you want to run the latest version your application will require updates and changes to keep up with new Meteor releases. Also we would say Meteor is an advanced framework, and you will need to have a grasp of the underlying technologies (Node.js / HTML 5) to really get the most out of it. We think 2013 will see Meteor make a deep impact, so why not give it a try by following their quick start guide and looking through some examples here?

    jClarity Logo

    jClarity have just launched their first JVM performance tuning tool focused on garbage collection log analysis called Censum. Censum tries to solve your GC and memory nightmares, which we all know can be a very long winded process! From what we have seen so far jClarity seem to be on top of the needs of developers wanting easy to interpret information about what is happening under the hood of the JVM. What we really like about Censum is the fact it can be used in any organisation and comes in at a very reasonable price point. Also jClarity is run by three Java heavy hitters, Ben Evans, Kirk Pepperdine and Martijn Verburg, so we are sure that there will be much more to look forward to over 2013.

    Twitter Bootstrap Logo


    The NerdAbility team have really enjoyed using the Twitter Bootstrap framework over the past year while working on NerdAbility and other projects. The Twitter Bootstrap framework has really grown in popularity in 2012, and there are now so many kick ass add-ons that supplement the already vast Bootstrap features. A great list is provided by @bootstraphero here. If you haven't tried the framework yet then check out some examples

    BitBucket Logo
    In December (2012) we moved over our Git hosting to BitBucket from CloudBees. We made this decision due to the fact there is now a very similar interface to GitHub, with issue tracking, code reviews and a smart desktop client (SourceTree). Even though many are saying BitBucket has simply cloned GitHubs features, BitBucket is free for the first five users with unlimited repos and has a reasonable price plan starting at $10 for 10 users. For a small team this is a great freebie! While we have a small team and a small number of repositories we are quite happy with using BitBucket. It seems that when choosing between GitHub and BitBucket it will be down to brand or price plan preference. GitHub charges by the number of private repo's, BitBucket by the number of users. We expect BitBucket's popularity to grow among small teams looking to keep costs down while still getting some great features.

    Grails Logo Play Logo

    While neither of these frameworks are new in 2012, the last year has seen some big releases for both Grails and Play frameworks. They have both passed the 2.0 milestones and continue to go from strength to strength.

    We are excited by what Typesafe are doing with the Play framework and the move towards Scala. We have seen some great examples of how you can use the framework and Scala to write precise and elegant code that is just not possible with Java. Also Typesafe have had a sizeable amount ($14m) of investment this year, and the Spring legend Rod Johnson has joined as a director. We like the approach Typesafe are taking with the 'Typesafe Stack' which should help keep the various technologies focused.

    The team at SpringSource have also been very busy with Grails. Behind the scenes Grails 2.2 is running Groovy 2.0 and is still built on Spring. Additionally the plugin repository is ever growing and we are now seeing many well established add-ons. This makes Grails an attractive framework, and we expect it to continue to grow in usage in enterprises and startups.

    NerdAbility.com Logo

    Here at NerdAbility we will be working hard in 2013 to keep improving the site. We are working on many new integrations for your profile, so in 2013 you will be able to show off even more of the cool tech things you do on-line. Help NerdAbility grow by sharing your profile URL on forums, Twitter, LinkedIn and especially when you are applying for jobs. There is no better way to stand out than showing that you are an awesome developer in and outside of work!

    Come check us out today and signup for a free profile. You can then connect to sites like StackOverflow, GitHub, BitBucket, Google Code, Geeklist and have all the awesome stuff you do shown on your NerdAbility profile.