<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Yohan Liyanage on Medium]]></title>
        <description><![CDATA[Stories by Yohan Liyanage on Medium]]></description>
        <link>https://medium.com/@yohan.liyanage?source=rss-af30b9623712------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*aEglcmk1m9vWUCD4F3tJVA.jpeg</url>
            <title>Stories by Yohan Liyanage on Medium</title>
            <link>https://medium.com/@yohan.liyanage?source=rss-af30b9623712------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 24 Nov 2026 04:44:05 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@yohan.liyanage/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Debugging Angular 4 Routing]]></title>
            <link>https://medium.com/yohan-liyanage/note-to-self-debugging-angular-4-routing-4d1909db841f?source=rss-af30b9623712------2</link>
            <guid isPermaLink="false">https://medium.com/p/4d1909db841f</guid>
            <category><![CDATA[notes-to-self]]></category>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[coding]]></category>
            <dc:creator><![CDATA[Yohan Liyanage]]></dc:creator>
            <pubDate>Wed, 03 May 2017 03:11:25 GMT</pubDate>
            <atom:updated>2018-01-29T11:33:23.212Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/250/1*nbJ41jD1-r2Oe6FsLjKaOg.png" /></figure><p>There’s a neat way to debug how routing works in Angular 4. The router module has in-built tracing support which can be activated by passing a flag when it’s added to the app routes, like so:</p><p>@NgModule({<br>imports: [ RouterModule.forRoot(routes, { enableTracing: true }) ],<br>exports: [ RouterModule ]<br>})<br>export class AppRoutingModule {}</p><p>Now, angular will log it’s routing decisions in the console.</p><p>Router Event: NavigationStart<br>platform-browser.es5.js:1028 NavigationStart(id: 1, url: &#39;/&#39;)<br>platform-browser.es5.js:1028 NavigationStart {id: 1, url: &quot;/&quot;}<br>core.es5.js:3025 Angular is running in the development mode. Call enableProdMode() to enable the production mode.<br>platform-browser.es5.js:1037 Router Event: RoutesRecognized<br>platform-browser.es5.js:1028 RoutesRecognized(id: 1, url: &#39;/&#39;, urlAfterRedirects: &#39;/&#39;, state: Route(url:&#39;&#39;, path:&#39;&#39;) { Route(url:&#39;&#39;, path:&#39;&#39;) } )<br>platform-browser.es5.js:1028 RoutesRecognized {id: 1, url: &quot;/&quot;, urlAfterRedirects: &quot;/&quot;, state: RouterStateSnapshot}<br>platform-browser.es5.js:1037 Router Event: NavigationEnd<br>platform-browser.es5.js:1028 NavigationEnd(id: 1, url: &#39;/&#39;, urlAfterRedirects: &#39;/&#39;)<br>platform-browser.es5.js:1028 NavigationEnd {id: 1, url: &quot;/&quot;, urlAfterRedirects: &quot;/&quot;}</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4d1909db841f" width="1" height="1" alt=""><hr><p><a href="https://medium.com/yohan-liyanage/note-to-self-debugging-angular-4-routing-4d1909db841f">Debugging Angular 4 Routing</a> was originally published in <a href="https://medium.com/yohan-liyanage">Yohan Liyanage</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Docker Machine — moby: Name or service not known]]></title>
            <link>https://medium.com/yohan-liyanage/docker-machine-moby-name-or-service-not-known-bd2a7c74abc2?source=rss-af30b9623712------2</link>
            <guid isPermaLink="false">https://medium.com/p/bd2a7c74abc2</guid>
            <category><![CDATA[docker]]></category>
            <category><![CDATA[mac]]></category>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[infrastructure]]></category>
            <dc:creator><![CDATA[Yohan Liyanage]]></dc:creator>
            <pubDate>Mon, 05 Sep 2016 06:33:46 GMT</pubDate>
            <atom:updated>2018-01-29T11:13:09.532Z</atom:updated>
            <content:encoded><![CDATA[<p>I have been running Docker on OS X for quite a while now and the I switched to Docker Machine for Mac Beta few months back. It has been a great experience so far, but not without occasional hiccups. I run some of my containers in host networking mode, and I have faced the following problem when I start some of my containers.</p><pre>java.net.UnknownHostException: moby: moby: Name or service not known<br>at java.net.InetAddress.getLocalHost(InetAddress.java:1505) ~[na:1.8.0_102]<br>at com.netflix.eureka.transport.JerseyReplicationClient.createReplicationClient(JerseyReplicationClient.java:170) ~[eureka-core-1.4.9.jar!/:1.4.9]<br>at com.netflix.eureka.cluster.PeerEurekaNodes.createPeerEurekaNode(PeerEurekaNodes.java:194) [eureka-core-1.4.9.jar!/:1.4.9]<br>....<br>Caused by: java.net.UnknownHostException: moby: Name or service not known<br>at java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method) ~[na:1.8.0_102]<br>at java.net.InetAddress$2.lookupAllHostAddr(InetAddress.java:928) ~[na:1.8.0_102]<br>at java.net.InetAddress.getAddressesFromNameService(InetAddress.java:1323) ~[na:1.8.0_102]<br>at java.net.InetAddress.getLocalHost(InetAddress.java:1500) ~[na:1.8.0_102]<br>... 67 common frames omitted</pre><p>‘Moby’ is the name given to the host system that runs behind the scenes with Docker Machine for Mac (and Windows). Docker Machine for Mac runs MobyLinux VM, and the hostname file refers to itself as ‘moby’. But this entry is missing in the /etc/hosts file and my services fail to start up because it cannot resolve ‘moby’ to an IP. The workaround is simple. Just add the ‘add-host’ flag to your Docker run command telling it that ‘moby’ is ‘127.0.0.1’, like so:</p><pre>docker run --net=host --add-host=moby:127.0.0.1 yourid/yourimage</pre><p>This will automatically add an entry to the /etc/hosts file saying that moby in fact is 127.0.0.1.</p><p>Happy Dockerizing!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bd2a7c74abc2" width="1" height="1" alt=""><hr><p><a href="https://medium.com/yohan-liyanage/docker-machine-moby-name-or-service-not-known-bd2a7c74abc2">Docker Machine — moby: Name or service not known</a> was originally published in <a href="https://medium.com/yohan-liyanage">Yohan Liyanage</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Docker — Clean Up After Yourself!]]></title>
            <link>https://medium.com/yohan-liyanage/docker-clean-up-after-yourself-8f39bfbd2000?source=rss-af30b9623712------2</link>
            <guid isPermaLink="false">https://medium.com/p/8f39bfbd2000</guid>
            <category><![CDATA[docker]]></category>
            <category><![CDATA[infrastructure]]></category>
            <category><![CDATA[linux]]></category>
            <dc:creator><![CDATA[Yohan Liyanage]]></dc:creator>
            <pubDate>Fri, 29 May 2015 05:22:52 GMT</pubDate>
            <atom:updated>2018-01-29T11:13:24.436Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>UPDATE:</strong></p><p>As pointed out by several readers, newer version of Docker Engine ( &gt; 1.13) now provides the following single command to do all this.</p><p><strong>docker system prune -a</strong></p><p>Please be careful when you execute this command because this will delete and remove any stopped containers / unused images.</p><p>We started dockerizing some of our applications recently, and I got to say, that I am in love with Docker! It’s an awesome piece of engineering, and on AWS EC2, it made our lives much easier. However, one problem that we came across when we used Docker is the insane disk usage of it. We run Docker on Amazon Linux, and we have a build server that builds docker images as part of our build pipeline. Once built, the image is then pushed to our servers through an Ansible playbook. Probably I will blog more about it when I get a chance.</p><p>What we noticed was that over time, docker seem to eat up the disk space of the host. A quick df -h showed that /var/lib/docker was growing to the point where it pretty much covered the entire disk. So we looked around for a solution and came across the following.</p><p><strong>A Word of Caution :</strong> commands below will delete stopped containers and wipe out their data. So make sure that you don’t run these if you have any containers / valuable data that you need to backup.</p><h4>1. Make sure that exited containers are deleted.</h4><p>When a docker container exits, the container is not deleted automatically. You can see all of the containers with ‘docker ps -a’ command. To clean up the exited containers, the command to use is as follows.</p><pre>docker rm -v $(docker ps -a -q -f status=exited)</pre><p>This will remove the exited containers. The -v flag is there to remove any containers that will no longer be needed. If there are no such containers you will see an error message like below.</p><p>docker: “rm” requires a minimum of 1 argument. See ‘docker rm — help’.</p><p>It just means that there’s nothing to delete and you are good to go.</p><h4>2. Remove unwanted ‘dangling’ images.</h4><p>Docker keeps all of the images that you have used in the disk, even if those are not actively running. This is done so that it has the necessary images in the local ‘cache’. This is awesome because when you want to pull an image that depends on those, or when you are building an image, all of these are locally available. The bad news is that this eats up disk space!</p><p>The command to remove these unwanted images is:</p><pre>docker rmi $(docker images -f &quot;dangling=true&quot; -q)</pre><p>Again, you might get an error message that the rmi commands needs an argument if you don’t have any such images.</p><h4>3. Still not enough space? What is this ‘vfs’ directory?</h4><p>If your docker directory still takes up space, that probably means that you have unwanted volumes filling up in your disk. The -v flag we passed to rm command usually takes care of this. But sometimes, if your way of shutting down containers do not auto remove the containers, the vfs directory will grow pretty fast. We can reclaim this space by deleting the unwanted volumes. To do this, there’s a docker image that you can use!</p><p><strong>Note:</strong> If you are using a Docker version that is newer than 1.9, you don’t have to use the below container. Instead, use the following in built docker command.</p><pre>docker volume rm $(docker volume ls -qf dangling=true)</pre><p>Here’s how to run it.</p><pre>docker run -v /var/run/docker.sock:/var/run/docker.sock -v /var/lib/docker:/var/lib/docker --rm martin/docker-cleanup-volumes</pre><p>If you want to be safe and see what it’s going to delete, just pass — dry-run as an argument.</p><p>When that program runs, it will do the needful to delete all unwanted volumes and you should have your disk space back.</p><h4>4. That’s all good. Do I have to do this everytime?</h4><p>Then next question we had was while all that is good, we had to manually run it whenever our servers get filled up. So we decided to automate it. And it is a breeze with crontabs. Just drop all of the above commands into a file under /etc/cron.daily/ directory. We created a file named docker-clean in that directory with execute rights. The file contains the following.</p><pre>docker rm -v $(docker ps -a -q -f status=exited)</pre><pre>docker rmi $(docker images -f &quot;dangling=true&quot; -q)</pre><pre>docker run -v /var/run/docker.sock:/var/run/docker.sock -v /var/lib/docker:/var/lib/docker --rm martin/docker-cleanup-volumes<br>Linux will run this job every day automatically and clean up after Docker. I personally think that this should be baked into the docker daemon as a housekeeping feature. Nevertheless, kudos to docker team for building such an awesome tool.</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8f39bfbd2000" width="1" height="1" alt=""><hr><p><a href="https://medium.com/yohan-liyanage/docker-clean-up-after-yourself-8f39bfbd2000">Docker — Clean Up After Yourself!</a> was originally published in <a href="https://medium.com/yohan-liyanage">Yohan Liyanage</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[[Note To Self] Spring Security Default Target URL]]></title>
            <link>https://medium.com/yohan-liyanage/note-to-self-spring-security-default-target-url-77dafdb7aa59?source=rss-af30b9623712------2</link>
            <guid isPermaLink="false">https://medium.com/p/77dafdb7aa59</guid>
            <category><![CDATA[java]]></category>
            <category><![CDATA[authentication]]></category>
            <category><![CDATA[spring]]></category>
            <dc:creator><![CDATA[Yohan Liyanage]]></dc:creator>
            <pubDate>Mon, 14 Jul 2014 07:53:15 GMT</pubDate>
            <atom:updated>2018-01-29T11:13:34.443Z</atom:updated>
            <content:encoded><![CDATA[<p>Just a quick note to self on Spring Security Default Target URLs. In one of my recent projects, I noticed that suddenly my Spring Security based login does not use the specified default target url in the configuration. Instead, it was hitting the root of the application always. This application was working perfectly fine until recently, and default target URL has not been changed since.</p><p>The Spring Security definition was:</p><pre>&lt;security:http auto-config=&quot;true&quot;&gt;<br>   &lt;security:intercept-url <br>      pattern=&quot;/!/signin&quot; <br>      access=&quot;IS_AUTHENTICATED_ANONYMOUSLY&quot; /&gt;<br>   &lt;security:intercept-url <br>      pattern=&quot;/!/**&quot; <br>      access=&quot;ROLE_LOGIN&quot; /&gt;<br>   &lt;security:form-login <br>      login-page=&quot;/!/signin&quot;<br>      default-target-url=&quot;/!/&quot;<br>      login-processing-url=&quot;/!/authenticate&quot; <br>      authentication-failure-url=&quot;/!/signin#failed&quot;<br>      authentication-success-handler-ref=&quot;authenticationSuccessHandler&quot; /&gt;<br>   &lt;security:logout logout-url=&quot;/!/signout&quot; logout-success-url=&quot;/!/signin&quot; /&gt;<br>&lt;/security:http&gt;</pre><p>After debugging through Spring Security code, I noticed that the defaultTargetURL of AbstractAuthenticationTargetUrlRequestHandler is not set to my value, but it uses the default ‘/’. Then after some digging up, it turned out that I’ve added a new Authentication Success Handler to my definition for a different purpose, and when an authentication-success-handler-ref is present in the configuration, the ‘default-target-url’ element in XML configuration is not used.</p><p>To fix this, the solution was to specify the default target URL on my authentication success handler bean as follows.</p><pre>&lt;bean id=&quot;authenticationSuccessHandler&quot; <br>   class=&quot;com.xyz.PlatformAuthenticationSuccessHandler&quot;&gt;<br>   &lt;property name=&quot;defaultTargetUrl&quot; value=&quot;/!/&quot; /&gt;<br>&lt;/bean&gt;</pre><p>The reason behind this is, the value we provide on the XML configuration goes to the default authentication success handler only. When we define our own, that value goes no where, so we need to specify it manually on the bean itself. This ate up about 15 mins of my time, before luckily noticing that the success handler change was the reason.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=77dafdb7aa59" width="1" height="1" alt=""><hr><p><a href="https://medium.com/yohan-liyanage/note-to-self-spring-security-default-target-url-77dafdb7aa59">[Note To Self] Spring Security Default Target URL</a> was originally published in <a href="https://medium.com/yohan-liyanage">Yohan Liyanage</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Mac Book Air & WiFi Routers]]></title>
            <link>https://medium.com/yohan-liyanage/mac-book-air-wifi-routers-dc790411f62b?source=rss-af30b9623712------2</link>
            <guid isPermaLink="false">https://medium.com/p/dc790411f62b</guid>
            <category><![CDATA[other]]></category>
            <dc:creator><![CDATA[Yohan Liyanage]]></dc:creator>
            <pubDate>Sun, 20 Apr 2014 06:15:40 GMT</pubDate>
            <atom:updated>2018-01-29T11:13:33.479Z</atom:updated>
            <content:encoded><![CDATA[<p>Two days back, I got my ADSL router fried due to a near field lightening strike, and I had to replace the router. I bought a D-Link DSL 2750u and configured it, my MBA happily connected, and everything was working fine.</p><p>This morning, when I woke up, I was in for a surprise, because suddenly my MBA stopped recognising the WiFi Network. It sees all other WiFi networks, connects without any hiccups to those, but not this one. I tried the typical first aids — restarting router, restarting my laptop, no luck. My mobile and other devices can connect to the WiFi network without any problems, but not the Mac Book Air. At random times, MBA sees the WiFi network, but when I try to connect it says ‘A Connection Timeout Has Occurred’ (WTH?).</p><p>After few minutes spent on Google, saw that Apple devices are known to have some problems with particular WiFi Channels. I checked the WiFi Channel on my router and saw that it was set to AUTO. I changed it to Channel 6, and voila — the MBA see’s and connects to the WiFi smoothly.</p><p>So if you are facing any WiFi issues on your Mac devices, try changing the Wireless Channel. On my D-Link Router, it was found in the Administration Console under Wireless -&gt; Basic Settings.</p><p><strong>[2016/11/27] Update on TP-Link Routers:</strong></p><p>My D-Link router got fried due to lightening, and I had to fallback on the TP-Link router that is provided by my ISP. When I started using that connection with my Mac Book Air, my WiFi connection started to disconnect every now and then. This was very annoying and the solution for this was to set the WiFi standard to 802.11g instead of ’n’. Seems like ’n’ doesn’t play well between Macs and TP-Link.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=dc790411f62b" width="1" height="1" alt=""><hr><p><a href="https://medium.com/yohan-liyanage/mac-book-air-wifi-routers-dc790411f62b">Mac Book Air &amp; WiFi Routers</a> was originally published in <a href="https://medium.com/yohan-liyanage">Yohan Liyanage</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[DustJS — Getting ‘Duster’ to work on OS X / Linux]]></title>
            <link>https://medium.com/yohan-liyanage/dustjs-getting-duster-to-work-on-os-x-linux-9e0862800694?source=rss-af30b9623712------2</link>
            <guid isPermaLink="false">https://medium.com/p/9e0862800694</guid>
            <category><![CDATA[coding]]></category>
            <dc:creator><![CDATA[Yohan Liyanage]]></dc:creator>
            <pubDate>Sun, 16 Feb 2014 08:40:24 GMT</pubDate>
            <atom:updated>2018-01-29T11:13:13.373Z</atom:updated>
            <content:encoded><![CDATA[<p>This is going to be a short post on one of the issues I came across when I was trying to use LinkedIn DustJS on my Mac Book. I was trying out DustJS for one of the new projects I’m working on, and I came across ‘DusterJS’ (<a href="https://github.com/dmix/dusterjs/">https://github.com/dmix/dusterjs/</a>), which is a watcher that could monitor your .dust files and convert them on-the-fly to compiled dust templates. This is a life saver when it comes to developing dust templates, and kudos to the author for writing such a great tool.</p><p>DusterJS is based on Node, and it’s available in the NPM repos. So it was easy to install this on my Mac with just one command.</p><p>npm install -g dusterjs</p><p>Once installed, we can run DusterJS in the command line by running the command ‘duster’. However, when I tried this on the Mac, it failed with following error.</p><p>Yohan:bin Yohan$ duster<br>env: node\r: No such file or directory</p><p>After a quick Google, I found that this was because of the ‘Windows’ style line breaks in the /usr/local/lib/node_modules/dusterjs/bin/cli.js, which is the executable for duster. To fix this, there were different approaches, and below is what I used.</p><p>First off, I used the following command to convert line breaks.</p><p>cat cli.js | col -b &gt;cli2.js</p><p>Then I backed up the original cli.js, and renamed the cli2.js as cli.js, and granted execute permissions on the new cli.js.</p><p>mv cli.js cli-old.js<br>mv cli2.js cli.js<br>chmod a+x cli.js</p><p>And gave it a spin,</p><p>Yohan:bin Yohan$ duster<br>Watches a directory tree and compiles templates when changed.<br>Usage: duster [options] input [input...] output</p><p>Voila, it works. I raised an issue in GitHub project page for this one so that this will be fixed in a future release (<a href="https://github.com/dmix/dusterjs/issues/15">https://github.com/dmix/dusterjs/issues/15</a>). On the interim, hope this helps if someone stumbles upon this situation.</p><p><strong>UPDATE :</strong> The issue in DusterJS NPM Package has been fixed by the developer, I believe this is no longer be needed. It works out of the box now. Kudos to <a href="https://github.com/lee-houghton">Lee Houghton</a> for pushing this fix immediately. This is why I love open source :)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9e0862800694" width="1" height="1" alt=""><hr><p><a href="https://medium.com/yohan-liyanage/dustjs-getting-duster-to-work-on-os-x-linux-9e0862800694">DustJS — Getting ‘Duster’ to work on OS X / Linux</a> was originally published in <a href="https://medium.com/yohan-liyanage">Yohan Liyanage</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Integration Testing with MongoDB & Spring Data]]></title>
            <link>https://medium.com/yohan-liyanage/integration-testing-with-mongodb-spring-data-4a82c5345239?source=rss-af30b9623712------2</link>
            <guid isPermaLink="false">https://medium.com/p/4a82c5345239</guid>
            <category><![CDATA[spring]]></category>
            <category><![CDATA[mongodb]]></category>
            <category><![CDATA[spring-data]]></category>
            <category><![CDATA[java]]></category>
            <dc:creator><![CDATA[Yohan Liyanage]]></dc:creator>
            <pubDate>Sun, 04 Nov 2012 10:46:19 GMT</pubDate>
            <atom:updated>2018-01-29T11:13:11.373Z</atom:updated>
            <content:encoded><![CDATA[<p>Integration Testing is an often overlooked area in enterprise development. This is primarily due to the associated complexities in setting up the necessary infrastructure for an integration test. For applications backed by databases, it’s fairly complicated and time-consuming to setup databases for integration tests, and also to clean those up once test is complete (ex. data files, schemas etc.), to ensure repeatability of tests. While there have been many tools (ex. DBUnit) and mechanisms (ex. rollback after test) to assist in this, the inherent complexity and issues have been there always.</p><p>But if you are working with MongoDB, there’s a cool and easy way to do your unit tests, with almost the simplicity of writing a unit test with mocks. With ‘EmbedMongo’, we can easily setup an embedded MongoDB instance for testing, with in-built clean up support once tests are complete. In this article, we will walkthrough an example where EmbedMongo is used with JUnit for integration testing a Repository Implementation. <br><br>Here’s the technology stack that we will be using.</p><ul><li>MongoDB 2.2.0</li><li>EmbedMongo 1.26</li><li>Spring Data — Mongo 1.0.3</li><li>Spring Framework 3.1</li></ul><p>The Maven POM for the above setup looks like this.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b1bff96f395ad6aa0407d4376579f725/href">https://medium.com/media/b1bff96f395ad6aa0407d4376579f725/href</a></iframe><p>Or if you prefer Gradle (by the way, Gradle is an awesome build tool which you should check out if you haven’t done so already).</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/930a5bc0660a5ef676559675df54bcec/href">https://medium.com/media/930a5bc0660a5ef676559675df54bcec/href</a></iframe><p>To begin with, here’s the document that we will be storing in Mongo.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/207d1333326762ea6f35dfd27bbe08ef/href">https://medium.com/media/207d1333326762ea6f35dfd27bbe08ef/href</a></iframe><p>To assist with storing and managing this document, let’s write up a simple Repository implementation. The Repository Interface is as follows.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5337fab61fb9d8c8210a2d5450fb8447/href">https://medium.com/media/5337fab61fb9d8c8210a2d5450fb8447/href</a></iframe><p>And the implementation…</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/af9d2b53f9c163603e7aab73e2089bd2/href">https://medium.com/media/af9d2b53f9c163603e7aab73e2089bd2/href</a></iframe><p>To wire this up, we need a Spring Bean Configuration. Note that we do not need this for testing. But for the sake of completion, I have included this. The XML configuration is as follows.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e37c352ca4358bb64c76763b5d2c37b8/href">https://medium.com/media/e37c352ca4358bb64c76763b5d2c37b8/href</a></iframe><p>And now we are ready to write the Integration Test for our Repository Implementation using Embed Mongo.</p><p>Ideally, the integration tests should be placed in a separate source directory, just like we place our unit tests (ex. src/test/java =&gt; src/integration-test/java). However, neither Maven nor Gradle supports this out of the box (yet — v1.2. For Gradle, there’s an <a href="http://forums.gradle.org/gradle/topics/add_an_integration_test_plugin">on going discussion</a> for this facility).</p><p>Nevertheless, both Maven and Gradle are flexible, so you can configure the POM / build.gradle to handle this. However, to keep this discussion simple and focused, I will be placing the Integration Tests in the ‘src/test/java’, but I do not recommend this for a real application.</p><p>Let’s start writing up the Integration Test. First, let’s begin with a simple JUnit based Test for the methods.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/97de7e82d516a2276c56b7c9e719ba5a/href">https://medium.com/media/97de7e82d516a2276c56b7c9e719ba5a/href</a></iframe><p>When this JUnit Test Case initializes, we need to fire up EmbedMongo to start an embedded Mongo server. Also, when the Test Case ends, we need to cleanup the DB. The below code snippet does this.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/df766efabd15c4f21fb5edf8f3f15cbc/href">https://medium.com/media/df766efabd15c4f21fb5edf8f3f15cbc/href</a></iframe><p>The initializeDB() method is annotated with @BeforeClass to start this before test case beings. This method fires up an embedded MongoDB instance which is bound to the given port, and exposes a Mongo object which is set to use the given database. Internally, EmbedMongo creates the necessary data files in temporary directories.</p><p>When this method executes for the first time, EmbedMongo will download the necessary Mongo implementation (denoted by Version.V2_2_0 in above code) if it does not exist already. This is a nice facility specially when it comes to Continuous Integration servers. You don’t have to manually setup Mongo in each of the CI servers. That’s one less external dependency for the tests.</p><p>In the shutdownDB() method, which is annotated with @AfterClass, we stop the EmbedMongo process. This triggers the necessary cleanups in EmbedMongo to remove the temporary data files, restoring the state to where it was before Test Case was executed.</p><p>We have now updated setUp() method to build a Spring MongoTemplate object which is backed by the Mongo instance exposed by EmbedMongo, and to setup our RepoImpl with that template. The tearDown() method is updated to drop the ‘Sample’ collection to ensure that each of our test methods start with a clean state.<br> <br>Now it’s just a matter of writing the actual test methods.</p><p>Let’s start with the save method test.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5663d7bc62438af4c48c9b171fb88e8d/href">https://medium.com/media/5663d7bc62438af4c48c9b171fb88e8d/href</a></iframe><p>We create a Sample object, pass it to repoImpl.save(), and assert to make sure that there’s only one Sample in the Sample collection. Simple, straight-forward stuff.</p><p>And here’s the test method for findByKey method.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b7da91c09039f28525be83da178bd0ab/href">https://medium.com/media/b7da91c09039f28525be83da178bd0ab/href</a></iframe><p>Initially, we setup the data by adding a set of Sample objects into the data store. It’s important that we directly use template.save() here, because repoImpl.save() is a method under-test. We are not testing that here, so we use the underlying “trusted” template.save() during data setup. This is a basic concept in Unit / Integration testing. Then we execute the method under test ‘findByKey’, and assert to ensure that only two Samples matched our query.</p><p>Likewise, we can continue to write more tests for each of the repository methods, including negative tests. And here’s the final Integration Test file.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/caa3735008981b3cd0d149559316ceab/href">https://medium.com/media/caa3735008981b3cd0d149559316ceab/href</a></iframe><p>On a side note, one of the key concerns with Integration Tests is the execution time. We all want to keep our test execution times as low as possible, ideally a couple of seconds to make sure that we can run all the tests during CI, with minimal build and verification times. However, since Integration Tests rely on underlying infrastructure, usually Integration Tests take time to run. But with EmbedMongo, this is not the case. In my machine, above test suite runs in 1.8 seconds, and each test method takes only .166 seconds max. See the screenshot below.</p><figure><img alt="MongoDB Integration Tests" src="https://cdn-images-1.medium.com/max/287/0*LzOXT2YTP2W46f_G.png" /></figure><p>I have uploaded the code for above project into GitHub. You can download / clone it from here: <a href="https://github.com/yohanliyanage/blog-mongo-integration-tests">https://github.com/yohanliyanage/blog-mongo-integration-tests</a>. For more information regarding EmbedMongo, refer to their site at GitHub <a href="https://github.com/flapdoodle-oss/embedmongo.flapdoodle.de">https://github.com/flapdoodle-oss/embedmongo.flapdoodle.de</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4a82c5345239" width="1" height="1" alt=""><hr><p><a href="https://medium.com/yohan-liyanage/integration-testing-with-mongodb-spring-data-4a82c5345239">Integration Testing with MongoDB &amp; Spring Data</a> was originally published in <a href="https://medium.com/yohan-liyanage">Yohan Liyanage</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[STS in OS X — Where’s the sts.ini?]]></title>
            <link>https://medium.com/yohan-liyanage/sts-in-os-x-wheres-the-sts-ini-c2e2949beb14?source=rss-af30b9623712------2</link>
            <guid isPermaLink="false">https://medium.com/p/c2e2949beb14</guid>
            <category><![CDATA[spring]]></category>
            <category><![CDATA[eclipse]]></category>
            <category><![CDATA[t]]></category>
            <category><![CDATA[os-x]]></category>
            <dc:creator><![CDATA[Yohan Liyanage]]></dc:creator>
            <pubDate>Sat, 03 Nov 2012 14:48:00 GMT</pubDate>
            <atom:updated>2018-01-29T11:13:16.522Z</atom:updated>
            <content:encoded><![CDATA[<p>I’ve been using STS (SpringSource Tool Suite) on OS X Lion for sometime now, and today I realized that it’s getting a bit slow. I thought of tuning the heap size a bit to give it more memory, so I went into the installation to look for eclipse.ini, and there was none. So I googled, and found that STS bundles a ‘sts.ini’ file instead. Unfortunately for me, I wasn’t able to find this one in the folder either! Startled, I tried searching on the directory, etc, but it was not there. After googling around a bit more, I found in Spring Forums that the actual sts.ini file for Mac OS comes inside the application bundle. So if you are using Mac OS, and want to edit the sts.ini file, here are the steps.</p><ol><li>Go to your STS installation, and right-click on STS Application</li><li>Select ‘Show Package Contents’</li><li>A new Finder window will open and show the content of the application bundle. Go to ‘Contents’ directory in this window.</li><li>Inside ‘Contents’, go to ‘MacOS’ directory</li></ol><p>In this folder (Contents/MacOS) you will find the actual executable (STS) and the STS.ini file. Do your changes to STS.ini file (ex.changing Xmx) and save it just like you would under Linux / Windows with eclipse.ini / sts.ini.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c2e2949beb14" width="1" height="1" alt=""><hr><p><a href="https://medium.com/yohan-liyanage/sts-in-os-x-wheres-the-sts-ini-c2e2949beb14">STS in OS X — Where’s the sts.ini?</a> was originally published in <a href="https://medium.com/yohan-liyanage">Yohan Liyanage</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Eventing with Spring Framework]]></title>
            <link>https://medium.com/yohan-liyanage/eventing-with-spring-framework-137216467c40?source=rss-af30b9623712------2</link>
            <guid isPermaLink="false">https://medium.com/p/137216467c40</guid>
            <category><![CDATA[eventing]]></category>
            <category><![CDATA[spring]]></category>
            <category><![CDATA[java]]></category>
            <dc:creator><![CDATA[Yohan Liyanage]]></dc:creator>
            <pubDate>Sun, 30 Sep 2012 16:34:56 GMT</pubDate>
            <atom:updated>2018-01-29T11:13:09.380Z</atom:updated>
            <content:encoded><![CDATA[<p>Spring Framework, since it’s inception, included an eventing mechanism which can be used for application-wide eventing. This eventing mechanism was developed to be used internally by Spring Framework for eventing, such as notification of context being refreshed, etc, but it can be used for application specific custom events as well. This eventing API is based on an interface named {java}org.springframework.context.ApplicationListener{/java}, which defined one method named {java}onApplicationEvent{/java}. Below code snippet shows a simple events listener which just logs the event information.</p><pre>package com.yohanliyanage.blog.springevents;</pre><pre>import org.apache.commons.logging.Log;<br>import org.apache.commons.logging.LogFactory;<br>import org.springframework.context.ApplicationEvent;<br>import org.springframework.context.ApplicationListener;</pre><pre>public class MyEventListener implements ApplicationListener {</pre><pre>private static final Log LOG = LogFactory.getLog(MyEventListener.class);<br>	<br>	public void onApplicationEvent(ApplicationEvent event) {<br>		LOG.info(&quot;Event Occurred : &quot; + event);<br>	}<br>}</pre><pre>To register this event listener, all that we have to do is to add it as a Spring managed bean. If we just add it as a bean in Spring Bean Configuration XML, or if we have annotation scanning enabled, adding an annotation such as @Component, would ensure that our listener will receive events via Spring. Below XML block shows the simple bean definition in XML for registering this listener.</pre><pre>&lt; ?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;<br>	xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;<br>	xmlns:context=&quot;http://www.springframework.org/schema/context&quot;<br>	xsi:schemaLocation=&quot;http://www.springframework.org/schema/beans <a href="http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">http://www.springframework.org/schema/beans/spring-beans-3.0.xsd</a><br>		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd&quot;&gt;</pre><pre>Now, to test this code, let&#39;s write up a main method which creates the Spring Application Context. In this code, it&#39;s assumed that the Spring bean definition file is located at {java}META-INF/spring/application-context.xml{/java}, which is in class path. You can download the sample code <a href="http://blog.yohanliyanage.com/?attachment_id=320">here</a>.</pre><pre>public class Main {</pre><pre>public static void main(String[] args) throws InterruptedException {<br>		ApplicationContext context = <br>			new ClassPathXmlApplicationContext(&quot;classpath:META-INF/spring/application-context.xml&quot;);<br>	}<br>}</pre><pre>When we run this, we get the following output.</pre><pre>18:45:00 INFO Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@7a982589: startup date [Sun Sep 30 18:45:00 IST 2012]; root of context hierarchy<br>18:45:00 INFO Loading XML bean definitions from class path resource [META-INF/spring/application-context.xml]<br>18:45:00 INFO Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@33e228bc: defining beans [com.yohanliyanage.blog.springevents.MyEventListener#0]; root of factory hierarchy<br>18:45:00 INFO Event Occurred : org.springframework.context.event.ContextRefreshedEvent[source=org.springframework.context.support.ClassPathXmlApplicationContext@7a982589: startup date [Sun Sep 30 18:45:00 IST 2012]; root of context hierarchy]</pre><pre>As highlighted above, our listener gets notified by the framework when Spring Context is refreshed during initialization. This is a framework event, and of course, there&#39;s no magic to it. But how can we generate application specific, custom events? As you will see in the blow code block, this is also very simple and straight-forward.</pre><pre>First, let&#39;s create our own event implementation. For this, we just have to write a class that extends from {java}ApplicationEvent{/java}.</pre><pre>package com.yohanliyanage.blog.springevents;</pre><pre>import org.springframework.context.ApplicationEvent;</pre><pre>public class MyCustomEvent extends ApplicationEvent {</pre><pre>private static final long serialVersionUID = -5308299518665062983L;</pre><pre>public MyCustomEvent(Object source) {<br>		super(source);<br>	}<br>}</pre><pre>Next, we have to write a class which does the event publishing. In order to publish an event, we need to get a reference to {java}ApplicationEventPublisher{/java}. This can be easily done by implementing the {java}ApplicationEventPublisherAware{/java}, as shown in the code block below.</pre><pre>package com.yohanliyanage.blog.springevents;</pre><pre>import org.springframework.context.ApplicationEventPublisher;<br>import org.springframework.context.ApplicationEventPublisherAware;</pre><pre>public class MyEventPublisher implements ApplicationEventPublisherAware {</pre><pre>private ApplicationEventPublisher publisher;<br>	<br>	public void setApplicationEventPublisher(ApplicationEventPublisher publisher) {<br>		this.publisher = publisher;<br>	}</pre><pre>public void publish() {<br>		this.publisher.publishEvent(new MyCustomEvent(this));<br>	}<br>}</pre><pre>Now, this bean also should be added to the Spring bean configuration, as follows. Note that you do not have to write a separate publisher class in your application. Your existing Spring beans can easily to this by just implementing the {java}ApplicationEventPublisherAware{/java} interface.</pre><pre>Now, let&#39;s slightly modify the Main class to invoke the publish() method in our event publisher.</pre><pre>public static void main(String[] args) throws InterruptedException {<br>		ApplicationContext context = new ClassPathXmlApplicationContext(&quot;classpath:META-INF/spring/application-context.xml&quot;);<br>		<br>		MyEventPublisher publisher = context.getBean(MyEventPublisher.class);<br>		publisher.publish();<br>	}</pre><pre>When we run the application now, we get the following output.</pre><pre>19:21:18 INFO Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@7a982589: startup date [Sun Sep 30 19:21:18 IST 2012]; root of context hierarchy<br>19:21:18 INFO Loading XML bean definitions from class path resource [META-INF/spring/application-context.xml]<br>19:21:19 INFO Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@2565a3c2: defining beans [com.yohanliyanage.blog.springevents.MyEventListener#0,com.yohanliyanage.blog.springevents.MyEventPublisher#0]; root of factory hierarchy<br>19:21:19 INFO Event Occurred : org.springframework.context.event.ContextRefreshedEvent[source=org.springframework.context.support.ClassPathXmlApplicationContext@7a982589: startup date [Sun Sep 30 19:21:18 IST 2012]; root of context hierarchy]<br>19:21:19 INFO Event Occurred : com.yohanliyanage.blog.springevents.MyCustomEvent[source=com.yohanliyanage.blog.springevents.MyEventPublisher@5a676437]</pre><pre>As highlighted above, our custom event has been triggered, and our listener was able to handle that event.</pre><pre>So far, so good. But if you have noticed, our listener gets invoked for all of the events that occurs in the application, including framework events. But in most of the cases, this is not desirable. The listener will be interested in one or more specific events. Up until Spring 3.0, this eventing mechanism did not had support for filtering events. That is, if you implement an {java}ApplicationListener{/java}, you would end up receiving all events that occurs in the application, and you had to manually look into the ApplicationEvent object that gets passed in to your listener to identify and discard events that you are not interested in. This of course, was a hassle, and probably due to this, Spring Eventing did not get attention of most of the developers.</pre><pre>With Spring 3.0, this API was enhanced with Generics support, to provide filtering of events. Now, the <a href="http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/context/ApplicationListener.html">ApplicationListener</a> interface is parameterized as follows.</pre><pre>public interface ApplicationListener &lt; E extends ApplicationEvent &gt; extends EventListener</pre><pre>The {java}onApplicationEvent{/java} method parameter uses generic type E. With this, we can implement our listener as follows.</pre><pre>[java]<br>package com.yohanliyanage.blog.springevents;</pre><pre>import org.apache.commons.logging.Log;<br>import org.apache.commons.logging.LogFactory;<br>import org.springframework.context.ApplicationListener;</pre><pre>public class MyEventListener implements ApplicationListener &lt; MyCustomEvent &gt; {</pre><pre>private static final Log LOG = LogFactory.getLog(MyEventListener.class);<br>	<br>	public void onApplicationEvent(MyCustomEvent event) {<br>		LOG.info(&quot;Event Occurred : &quot; + event);<br>	}</pre><pre>}<br>[/java]</pre><pre>This listener implementation&#39;s {java}onApplicationEvent{/java} method will be called only for {java}MyCustomEvent{/java} based events. This in turn provides the necessary event filtering, where we don&#39;t have to write boilerplate code to discard unnecessary events. The log output is as follows.</pre><pre>19:29:31 INFO Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@7a982589: startup date [Sun Sep 30 19:29:31 IST 2012]; root of context hierarchy<br>19:29:31 INFO Loading XML bean definitions from class path resource [META-INF/spring/application-context.xml]<br>19:29:31 INFO Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@2565a3c2: defining beans [com.yohanliyanage.blog.springevents.MyEventListener#0,com.yohanliyanage.blog.springevents.MyEventPublisher#0]; root of factory hierarchy<br>19:29:31 INFO Event Occurred : com.yohanliyanage.blog.springevents.MyCustomEvent[source=com.yohanliyanage.blog.springevents.MyEventPublisher@5a676437]</pre><pre>As seen above in the output, we no longer receive the unwanted framework specific events, or any other events that we are not interested in.</pre><pre>So in conclusion, Spring does provide a decent eventing mechanism which is quite useful for implementing eventing support in applications. While this has been around since the early days of Spring, it did not see wide adoption primarily due to it&#39;s incapability of filtering out specific events for a listener. But with Spring 3.0, things are improved, and now it has reached a state where we can leverage it to broadcast events in our applications with ease. One thing to note is that by default, Spring Eventing is synchronous. But this can be made asynchronous by providing a custom {java}ApplicationEventMulticaster{/java} implementation that would make use of a {java}TaskExecutor{/java}.</pre><ul><li>Download Source Code — <a href="http://blog.yohanliyanage.com/?attachment_id=320">Spring Eventing Example Project</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=137216467c40" width="1" height="1" alt=""><hr><p><a href="https://medium.com/yohan-liyanage/eventing-with-spring-framework-137216467c40">Eventing with Spring Framework</a> was originally published in <a href="https://medium.com/yohan-liyanage">Yohan Liyanage</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[JAX-WS: Working with .NET Web Services]]></title>
            <link>https://medium.com/yohan-liyanage/jax-ws-working-with-net-web-services-846f68591041?source=rss-af30b9623712------2</link>
            <guid isPermaLink="false">https://medium.com/p/846f68591041</guid>
            <category><![CDATA[jax-ws]]></category>
            <category><![CDATA[java]]></category>
            <dc:creator><![CDATA[Yohan Liyanage]]></dc:creator>
            <pubDate>Thu, 21 Jun 2012 17:14:05 GMT</pubDate>
            <atom:updated>2018-01-29T11:13:10.533Z</atom:updated>
            <content:encoded><![CDATA[<p>If you happen to write a JAX-WS Web Services client for a service which is written using .NET Platform, you might come across the below error message when you execute <strong>wsimport</strong> command.</p><blockquote>A class/interface with the same name “?????” is already in use. Use a class customization to resolve this conflict.</blockquote><p>This happens because .NET generated WSDL documents may contain multiple elements with same name, which leads to a naming conflict when JAXB attempts to generate bindings. If you ever come across this situation, the solution is very simple. You just have to instruct the JAXB generator to automatically resolve any naming conflicts that might occur during the code generation. This can be done by providing -B-XautoNameResolution argument to wsimport tool. Note that the ‘-B-XautoNameResolution’ has no spaces. -B is used to pass instructions to JAXB Schema Compiler.</p><p>An example would be:</p><pre>wsimport -d gen-src -verbose -B-XautoNameResolution  <a href="https://sample.net/service.asmx?WSDL">https://sample.net/service.asmx?WSDL</a></pre><p>Note that generated code will refer to duplicate names with a numeric suffix. For example, if there are two elements with name ‘XYZ’, one class will be ‘XYZ’, and the other occurrence will be named as ‘XYZ2’.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=846f68591041" width="1" height="1" alt=""><hr><p><a href="https://medium.com/yohan-liyanage/jax-ws-working-with-net-web-services-846f68591041">JAX-WS: Working with .NET Web Services</a> was originally published in <a href="https://medium.com/yohan-liyanage">Yohan Liyanage</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>