Archive for the ‘Programming’ Category.

Writing a jboss - webservices client using python suds

I was experimenting with writing a secured web services client in python and I was surprised how easy it was using python suds.

First write your webservice in java:

@Remote
public interface TestService {
public String echo(String s);

public int add(int a, int b);
}

and the implementation:

@WebService
@Stateless
public class TestServiceImpl implements TestService {
public String echo(String s) {
return s;
}

public int add(int a, int b) {
return a+b;
}
}

Expose it using a webapp and configure security using web.xml:

<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<display-name>project-name-war</display-name>
<description></description>
<servlet>
<servlet-name>WS</servlet-name>
<servlet-class>net.ahlawat.TestServiceImpl</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>WS</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<security-constraint>
<web-resource-collection>
<web-resource-name>web service</web-resource-name>
<url-pattern>/*</url-pattern>
</web-resource-collection>
<auth-constraint>
<role-name>user</role-name>
</auth-constraint>
</security-constraint>
<login-config>
<auth-method>BASIC</auth-method>
</login-config>
<security-role>
<description>Known users of the Foobar service</description>
<role-name>user</role-name>
</security-role>
</web-app>

and configure it to a jboss login module using jboss-web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<jboss-web>
<security-domain>java:/jaas/tws</security-domain>
</jboss-web>

Basically that is it - you can then code up your python clinet like this:

from suds.client import Client

url = "https://127.0.0.1:8080/webServiceTest-war?wsdl"

client = Client(url, username="user", password="userPassword2010!")

#print client

result = client.service.add(1,2)

print result

result = client.service.echo("Hello world")

print result

And not surprisingly the result:

C:\cygwin\home\deepti\python-suds-0.4>python client.py
3
Hello world

C:\cygwin\home\deepti\python-suds-0.4>

Its amazing how easy this was. I think this is where you think programming in java sucks - putting out a simple client using basic auth would have taken 5 pages of code. Suds is an excellent library to write a quick web service client. Great tool to have in your programmers arsenal.

JBoss JMX extensions to EJB3

I don’t particularly like developing on JBoss - I think its bloated and slow. But with EJB3 the platform has become more viable. The turn around time to get a relatively complex application with web services etc. done is great.

One thing I like about JBoss 5.x are the JMX extensions to EJB3. Its really amazing how simple installing a JMX MBean is. Let’s say I have to write  a CalculatorService represented by this interface:

package net.ahlawat.cricket;

import javax.ejb.Remote;

import org.jboss.ejb3.annotation.Management;

@Management
@Remote
public interface CalculatorService {
	public double add(double a, double b);
	public double subtract(double a, double b);
	public double performOperation(double a, double b);
	public String getDefaultOperation();
	public void setDefaultOperation(String operation);
}

The interesting annotations here are @Management which tells JBoss deployer that the interface is to be bound as a JMX MBean. The implementation of this bean can be simple pojo - listed here.

package net.ahlawat.cricket;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.math.BigDecimal;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jboss.ejb3.annotation.Service;

@Service(objectName = "ahlawat:service=CalculatorService")
public class CalculatorServiceImpl implements CalculatorService {
	static Log logger = LogFactory.getLog(CalculatorServiceImpl.class);

	Method defaultOperation;

	@Override
	public double add(double a, double b) {
		return a+b;
	}

	@Override
	public String getDefaultOperation() {
		return defaultOperation.getName();
	}

	@Override
	public double performOperation(double a, double b) {
		try {
			BigDecimal bd = (BigDecimal) defaultOperation.invoke(new BigDecimal(a), new BigDecimal(b));
			return bd.doubleValue();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}

		return Double.NaN;
	}

	@Override
	public void setDefaultOperation(String operation) {
		try {
			Method m = BigDecimal.class.getMethod(operation, BigDecimal.class);
			logger.info("Setting default operation to " + operation);
			defaultOperation = m;
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		}

	}

	@Override
	public double subtract(double a, double b) {
		return a-b;
	}

	//lifecycle methods
	public void create() throws Exception {
		logger.info("Creating service");
	}

	public void start() throws Exception {
		logger.info("Starting service");
		setDefaultOperation("add");
	}
}

The only interesting thing here is @Service - which tells Jboss that this is a singleton bean. And the fact that MBean lifecycle methods like create() and start() are available to be implemented here.

The complete list of life cycle methods are:

   void create() throws Exception;
   void start() throws Exception;
   void stop();
   void destroy();

Once this is jar’d up and deployed to Jboss - you can go to jmx-console and see that the service is visible:

jmx-console

jmx-console

You can also look up the bean using the default JNDI name CalculatorServiceImpl/remote

public class Main {
	public static void main(String[] args) throws Exception {
		InitialContext ctx = new InitialContext();

		CalculatorService cs = (CalculatorService)ctx.lookup("CalculatorServiceImpl/remote");

		System.out.println(cs.add(2.1, 3.2));
	}
}

Here is the jndi.properties:

#jboss JNDI properties
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.provider.url=jnp://localhost:1099
java.naming.factory.url.pkgs=org.jnp.interfaces

And you can also inject the referece to another EJB using the @EJB annotation like this:

@Stateless
public class PlayerServiceImpl implements PlayerService {
	static Log logger = LogFactory.getLog(PlayerServiceImpl.class);

	@PersistenceContext(unitName = "default")
	EntityManager entityManager;

	@EJB
	CalculatorService calculatorService;

	@PostConstruct
	public void gotCalculator() {
		System.out.println(">>>>>>>>>>>>>>>>>> " + calculatorService.add(1.0, 1.0));
	}

...
}

Really small amount of code and guess what - no XML. I really like it.

A simple animated collapsible panel in JQuery

How often does one have to write a collapsible panel? If you are a UI developer quite a lot - and how often have you wanted an animated one, if you are a UI developer who wants pizazz, like myself, you want it often.

But then comes the cross browser headaches and the pain of making it work consistently. I have been learning JQuery and it amazes me how painless JQuery can make this stuff.

Here is a simple example:

I start by writing a simple stylesheet (style.css):

body {
	font-family: Arial, "MS Trebuchet", sans-serif;
}

.bd {
	border: 1px solid #999;
}

Next here is my html page:

<html>
	<head>
		<title>jquery test</title>
		<LINK href="style.css" rel="stylesheet" type="text/css"/>
		<script type="text/javascript" src="jquery.js"></script>
		<script type="text/javascript">
			//<![CDATA[
			$(function() {
				$("div.slider > div").addClass("bd").hide();

				$("div.slider > a").click(function() {
					$(this.nextElementSibling).slideToggle('fast');
				});
			});
			//]]>
		</script>

	</head>
	<body>
		<h1>Test page for jquery</h1>
		<hr />
		<div>
			This is some content
		</div>
		<div id="firstDiv" class="slider">
			<a href="#">Click here to show this stuff</a>
			<div>
				This content will be revealed
			</div>
		</div>
	</body>
</html>

Thats it - I express my collapsible panel with a marker style called “slider”. I next use a css selector “div.slider > div” to add a border to the content I wish to show and another selector element “div.slider > a” to add click handlers to my links. Since I really want to hide/show the content in the div next to the anchor element I use “this.nextElementSibling” to get to that div and simply call the slideToggle() method. The result is something like this:

Anytime I want a collapsible panel I just have to write a markup with a marker style like this:

<div class="slider">
<a href="#">Click here to show/hide</a>
<div>
Your content here
</div>
</div>

And the rest is taken care of by jquery. Simple, effective, pretty … love JQuery.

Spring DM deployment problems with schema declaration

OK - so I am trying to learn spring DM and finally got vanilla, non spring OSGI bundles deployed on Spring DM server. Things were finally looking good. Next I tried a basic Spring DM configuration like this:

project

project

Pretty straight forward project setup in STS.

I have a HelloService and a HelloImpl (which is an implementation of this service). My manifest.mf file exposes the net.ahlawat.hello package and looked like this (I am not using my bundle activator for anything):

Manifest-Version: 1.0
Bundle-Version: 1.0.0
Bundle-Name: HelloWorld
Bundle-ManifestVersion: 2
Bundle-SymbolicName: HelloWorld

The spring beans file exposes the osgi service using the osgi namespace and looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:osgi="http://www.springframework.org/schema/osgi"
	xsi:schemaLocation="http://www.springframework.org/schema/osgi http://www.springframework.org/schema/osgi/spring-osgi-2.0-m1.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

	<osgi:service interface="net.ahlawat.hello.HelloService" ref="helloService" />

	<bean id="helloService" class="net.ahlawat.hello.impl.HelloImpl" />

</beans>

Nothing can possible go wrong. I downloaded spring DM server 2.x and setup the project for running - and got the following exception:

[2010-03-17 18:19:35.207] Thread-5                      Application context creation failure for bundle 'HelloWorld' version '1.0.0'. org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.osgi.service.exporter.support.OsgiServiceFactoryBean#0': Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'cacheTarget' of bean class [org.springframework.osgi.service.exporter.support.OsgiServiceFactoryBean]: Bean property 'cacheTarget' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1341)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1067)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:511)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:450)
	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:290)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:287)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:189)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:540)
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:842)
	at org.springframework.osgi.context.support.AbstractDelegatedExecutionApplicationContext.access$1600(AbstractDelegatedExecutionApplicationContext.java:69)
	at org.springframework.osgi.context.support.AbstractDelegatedExecutionApplicationContext$4.run(AbstractDelegatedExecutionApplicationContext.java:355)
	at org.springframework.osgi.util.internal.PrivilegedUtils.executeWithCustomTCCL(PrivilegedUtils.java:85)
	at org.springframework.osgi.context.support.AbstractDelegatedExecutionApplicationContext.completeRefresh(AbstractDelegatedExecutionApplicationContext.java:320)
	at org.springframework.osgi.extender.internal.dependencies.startup.DependencyWaiterApplicationContextExecutor$CompleteRefreshTask.run(DependencyWaiterApplicationContextExecutor.java:132)
	at com.springsource.kernel.agent.dm.ContextPropagatingTaskExecutor$2.run(ContextPropagatingTaskExecutor.java:106)
	at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
	at java.lang.Thread.run(Thread.java:637)
Caused by: org.springframework.beans.NotWritablePropertyException: Invalid property 'cacheTarget' of bean class [org.springframework.osgi.service.exporter.support.OsgiServiceFactoryBean]: Bean property 'cacheTarget' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
	at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:1012)
	at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:857)
	at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:76)
	at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:58)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1338)
	... 18 common frames omitted

It really was baffling - everything was done inside STS and till I introduced the spring DM config file everything worked well.

Some research pointed out what the problem was - the import declaration needed to be changed to 1.2.xsd instead of 2.0.m1.xsd in the xsi:schemaLocation attribute. Making the change leads to a clean deployment:

[2010-03-17 18:21:36.413] TCP Connection(13)-127.0.0.1  Installing bundle 'HelloWorld' version '1.0.0'.
[2010-03-17 18:21:36.461] TCP Connection(13)-127.0.0.1  Installed bundle 'HelloWorld' version '1.0.0'.
[2010-03-17 18:21:36.476] TCP Connection(13)-127.0.0.1  Starting bundle 'HelloWorld' version '1.0.0'.
[2010-03-17 18:21:36.596] start-signalling-3            Started bundle 'HelloWorld' version '1.0.0'.

I think spring needs better error messages.

A groovy one liner to recursively calculate the size of a directory

A groovy one liner to recursively calculate the size of a directory.

C:\Users\deepti>groovy -e "s=0; new File('.').eachFileRecurse {s += it.length()}; println s/1024/1024"
41921.6436758041
C:\Users\deepti>

Pretty useful on windows - which unfortunately does not have a command to achieve this.

Reverting a file in ‘git’

I have just begun to learn ‘git’ and understand the motivation behind distributed SCMs. One of the great powers of git, in addition to the fact that it is super fast is its excellent merging and branching capabilities. I use perforce at work and SVN at home and I have used CVS as well so to tune your head around a distributed SCM is challenging and requires some reorientation.

We use perforce at work and created merged 104 branches in the last year (all experimentation and execution is done in branches with the trunk or mainline being stable) - I think the merging in perforce is quite good but too slow, plus it costs money. SVN and CVS are different stories however - merging is downright painful and requires endless hours on manual merges and testing. So I was interested in seeing what GIT has to offer.

Installation and creation of a repository was quite straight forward. I checked in a project and was functional.

Next I tried to edit a file and tried to revert it.

pranay@pranaydesktop:~/dev/workspace/grails/testApp/web-app/js/prototype$ vim prototype.js
pranay@pranaydesktop:~/dev/workspace/grails/testApp/web-app/js/prototype$ git status
# On branch master
# Changed but not updated:
#   (use "git add ..." to update what will be committed)
#   (use "git checkout -- ..." to discard changes in working directory)
#
#	modified:   prototype.js
#
no changes added to commit (use "git add" and/or "git commit -a")
pranay@pranaydesktop:~/dev/workspace/grails/testApp/web-app/js/prototype$ git revert prototype.js
fatal: Cannot find 'prototype.js'
pranay@pranaydesktop:~/dev/workspace/grails/testApp/web-app/js/prototype$ 

Not what I really expected. A little research made me learn is “revert” in git is actually a “rollback” of a checked in change. This was a little counter intuitive - I would have preferred it being called “rollback” instead.

The real way to actually revert a file is check it out again.

pranay@pranaydesktop:~/dev/workspace/grails/testApp/web-app/js/prototype$ git checkout prototype.js
pranay@pranaydesktop:~/dev/workspace/grails/testApp/web-app/js/prototype$ git status
# On branch master
nothing to commit (working directory clean)
pranay@pranaydesktop:~/dev/workspace/grails/testApp/web-app/js/prototype$
pranay@pranaydesktop:~/dev/workspace/grails/testApp/web-app/js/prototype$ 

Hopefully I will never forget this.

Spring Roo - first impressions

Ok so another productivity tool hits the market - this time its from the big boys at Spring. Spring Roo is an open source software tool that uses convention-over-configuration principles to provide rapid application development of Java-based enterprise software. The resulting applications use common Java technologies such as Spring Framework, Java Persistence API, Java Server Pages, Apache Maven and AspectJ. Spring Roo is a member of the Spring portfolio of projects.

I was able to install and create an application in under 5 minutes with this thing. Which is great for demo but then I took a look at the generated source code and was not too happy. For a framework that claims to pure java RAD framework there is a bit too much AspectJ in the source folder. In fact its silly the amount of files the generator spits out. The documentation tells you that there is “very little” blackbox and that “you should never be editing” the IDT generated files directly. But for a groovy/java developer who has a lot of web application development experience I am just a little nervous with aspectJ polluting my class files the way i don’t want it too. We use dynamic proxies in more frameworks and places than we can care to think about but there is something about compile time weaving that just makes me a little uncomfortable. Of course this could just be my inexperience with acj and aspectJ talking here.

capture

capture

So for one domain there are a total of 5 source files - 4 of which I should not be touching?

The next thing that was a little bit restrictive was the whole domain modelling - its absoloutely great for simple one-one relationships or many to one relationships but it’s not suitable for slightly more complex types. Also I don’t know how the framework will behave with slightly more advanced but common features of Hibernate (if you are using a Hibernate provider) - like custom hibernate types, hibernate collection of items etc.There is little in the documentation about this - so its hit and try or hit the source code of the project to figure it out.

The scaffold views are not bad but too busy .. from a js/css point - frameworks should ship with minimal style sheets.

I am a little disapointed really I was really excited about this. If I have to choose a RAD framework that will work on a standard JVM I will stick to tomcat + grails.

Here are the advantages of grails as I see it:

1. Cleaner MVC - convention is great /controller/view/id type bindings work great

2. Extremely extensible using plugins, this is a huge advantage and there are some pretty good ones out there from charting to security

3. GORM is great and for the most part works OK

4. Scriptable environment - great little tools like grails console

5. Groovy is great and the framework does not prevent you from using pure java when needed

If you use roo productively - let me know and I will give it a shot again.

Circumventing creating objects via Reflection vs Direct object manipulation

Of course its no secret that reflection is a lot slower than that direct object manipulation but with java 5 and 6 the speed has dramatically improved I wanted to find out just how much of a penalty hit was it to use reflection rather than direct object manipulation to create objects (from the database end for eg.).

The answer is - substantial.

Here is a code that basically populates an object with random data - first completely via reflection and then by using actual java code:

package net.ahlawat.Main;

import java.lang.reflect.Method;
import java.util.*;

public class Main {
    static class TestClass {
        private String firstname;
        private String lastname;
        private String username;
        private String address;
        private String email;

        public String getFirstname() {
            return firstname;
        }

        public void setFirstname(String firstname) {
            this.firstname = firstname;
        }

        public String getLastname() {
            return lastname;
        }

        public void setLastname(String lastname) {
            this.lastname = lastname;
        }

        public String getUsername() {
            return username;
        }

        public void setUsername(String username) {
            this.username = username;
        }

        public String getAddress() {
            return address;
        }

        public void setAddress(String address) {
            this.address = address;
        }

        public String getEmail() {
            return email;
        }

        public void setEmail(String email) {
            this.email = email;
        }
    }

    public static void main(String[] args) {
        System.out.println("Reflection performance");

        try {
            long start = System.currentTimeMillis();
            Method[] methods = TestClass.class.getMethods();
            List<Method> objectMethods = Arrays.asList(Object.class.getMethods()); 

            List<Method> setters = new ArrayList<Method>();

            for (Method method : methods) {
                String name = method.getName();
                if (objectMethods.contains(method)) {
                    continue;
                } else if (name.startsWith("set") && method.getParameterTypes().length == 1) {
                    setters.add(method);
                }
            }

            System.out.println(String.format("Resolving all methods in %s ms", (System.currentTimeMillis()-start)));

            int objectCount = 10000;
            Random random = new Random();
            start = System.currentTimeMillis();
            for (int x=0; x<objectCount; x++) {
                Object o = TestClass.class.newInstance();
                for (Method getter : setters) {
                    getter.invoke(o, random.nextDouble() + "");
                }
            }
            long refTime = System.currentTimeMillis() - start;
            System.out.println(String.format(Locale.getDefault(),"Creating %d objects via reflection: %d ms",
                    objectCount, refTime));

            start = System.currentTimeMillis();
            for(int x = 0; x<objectCount; x++) {
                TestClass ob = new TestClass();
                ob.setAddress(random.nextDouble() + "");
                ob.setFirstname(random.nextDouble() + "");
                ob.setLastname(random.nextDouble() + "");
                ob.setUsername(random.nextDouble() + "");
                ob.setEmail(random.nextDouble() + "");
            }

            long directTime = System.currentTimeMillis() - start;
            System.out.println(String.format(Locale.getDefault(),"Creating %d objects via direct object manipulation: %d ms",
                    objectCount, directTime));

            System.out.println(String.format("Slowness: %.2f%%", (double)(refTime-directTime)*100/directTime));
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}

Quite straight forward. Here are the results:

Reflection performance
Resolving all methods in 2 ms
Creating 10000 objects via reflection: 218 ms
Creating 10000 objects via direct object manipulation: 129 ms
Slowness: 68.99%

Oddly internally the compiler optimized the reflective transfer if you do it many times over - I don’t know what exactly causes it but increasing the number to 100,000 instead the slowness is 40%.

Reflection performance
Resolving all methods in 2 ms
Creating 100000 objects via reflection: 1714 ms
Creating 100000 objects via direct object manipulation: 1213 ms
Slowness: 41.30%

If you take a look at just milliseconds it might not seem like a lot - but if you use reflection excessively it will kill the performance of you app. The good thing is you don’t have to live with it.

If you must use reflection due to the dynamic nature of the framework you are putting in place you are much better off writing a one time code generator. Using this generator you can overcome the 50% penalty of using reflection and yet keep the application dynamic.

So use reflection to produce the code that does direct object transfer to a .java file - compile it and use it. I plan to use the same for a data access service object which reads stuff from a table and populates an object using annotations on the class (a hibernate wannabe if you will). The milliseconds will add up pretty fast. I can share the groovy code generator if people are interested.

A flawless Publisher-Subscriber using BlockingQueue

java.util.concurrent simply rocks. I cant believe how simple it has made every day programming tasks.

What is the first thing your learn when you do multi-threading - a producer consumer. It’s a great example to learn notify, wait and understanding the locking semantics of java threading. Its a pity I started earlier because those of us starting out with java 1.5/6 will have their lives too easy. The BlockingQueue is a fantastic addition to the language and using it one can implement a synchronized multi publisher-multi subscriber system using semantics and constructs no different from java collections.

Here is an example with 5 publishers and 2 subscribers:

package net.ahlawat;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.Date;

/**
 * @author Pranay Ahlawat
 */
public class PubSubTest {
    static class Publisher implements Runnable {
        BlockingQueue<String> queue;
        String name;
        public Publisher(BlockingQueue<String> queue, String name) {
            this.queue = queue;
            this.name = name;
        }

        public void run() {
            while(true) {
                queue.add(String.format("Msg from %s: %s [on %s]", name, Math.random(), new Date()));
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    break;
                }
            }
        }
    }

    static class Subscriber implements Runnable {
       BlockingQueue<String> queue;
        String name;
        public Subscriber(BlockingQueue<String> queue, String name) {
            this.queue = queue;
            this.name = name;
        }

        public void run() {
            while(true) {
                try {
                    String in = queue.take();
                    System.out.println(String.format("[%s GOT MESSAGE] %s",name,in));
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    break;
                }

            }
        }
    }

    public static void main(String[] args) {
        final int numberOfPublishers = 5;
        BlockingQueue<String> blockingQueue = new ArrayBlockingQueue<String>(10);
        for (int x=1; x<=numberOfPublishers; x++) {
            Publisher publisher = new Publisher(blockingQueue, x+"");
            new Thread(publisher).start();
        }

        Subscriber subscriber1 = new Subscriber(blockingQueue, "Subscriber 1");
        new Thread(subscriber1).start();
        Subscriber subscriber2 = new Subscriber(blockingQueue, "Subscriber 2");
        new Thread(subscriber2).start();
    }
}

It’s quite straight forward - there are a total of 7 threads interacting with the queue. 5 publishers are putting stuff on the queue and 2 subscribers are picking up stuff from it and creatively printing it out the standard out. What I want you to see is the number of times I have used ’synchronized’ in the code - 0.

The output is not surprising:

[Subscriber 1 GOT MESSAGE] Msg from 4: 0.6466875854315378 [on Fri Dec 11 02:11:27 EST 2009]
[Subscriber 1 GOT MESSAGE] Msg from 1: 0.33362845358296433 [on Fri Dec 11 02:11:27 EST 2009]
[Subscriber 1 GOT MESSAGE] Msg from 2: 0.11207796566244055 [on Fri Dec 11 02:11:27 EST 2009]
[Subscriber 1 GOT MESSAGE] Msg from 3: 0.6810655758824113 [on Fri Dec 11 02:11:27 EST 2009]
[Subscriber 1 GOT MESSAGE] Msg from 5: 0.5679631128460616 [on Fri Dec 11 02:11:27 EST 2009]
[Subscriber 1 GOT MESSAGE] Msg from 1: 0.6304440131162121 [on Fri Dec 11 02:11:28 EST 2009]
[Subscriber 1 GOT MESSAGE] Msg from 2: 0.021117766277559014 [on Fri Dec 11 02:11:28 EST 2009]
[Subscriber 2 GOT MESSAGE] Msg from 3: 0.1955294791717468 [on Fri Dec 11 02:11:28 EST 2009]
[Subscriber 2 GOT MESSAGE] Msg from 4: 0.884529348835637 [on Fri Dec 11 02:11:28 EST 2009]
[Subscriber 1 GOT MESSAGE] Msg from 5: 0.034690283475101946 [on Fri Dec 11 02:11:28 EST 2009]
[Subscriber 2 GOT MESSAGE] Msg from 1: 0.5764439934861816 [on Fri Dec 11 02:11:29 EST 2009]
[Subscriber 1 GOT MESSAGE] Msg from 2: 0.3629499102212388 [on Fri Dec 11 02:11:29 EST 2009]
[Subscriber 1 GOT MESSAGE] Msg from 4: 0.3770428828123388 [on Fri Dec 11 02:11:29 EST 2009]
[Subscriber 2 GOT MESSAGE] Msg from 3: 0.9450938944637225 [on Fri Dec 11 02:11:29 EST 2009]
[Subscriber 1 GOT MESSAGE] Msg from 5: 0.8910317407643176 [on Fri Dec 11 02:11:29 EST 2009]
[Subscriber 2 GOT MESSAGE] Msg from 1: 0.5785955008786261 [on Fri Dec 11 02:11:30 EST 2009]
[Subscriber 2 GOT MESSAGE] Msg from 4: 0.9442550853581151 [on Fri Dec 11 02:11:30 EST 2009]
[Subscriber 2 GOT MESSAGE] Msg from 3: 0.3308239883343358 [on Fri Dec 11 02:11:30 EST 2009]
[Subscriber 2 GOT MESSAGE] Msg from 5: 0.5450057593023042 [on Fri Dec 11 02:11:30 EST 2009]
[Subscriber 1 GOT MESSAGE] Msg from 2: 0.13504231409694423 [on Fri Dec 11 02:11:30 EST 2009]
[Subscriber 2 GOT MESSAGE] Msg from 1: 0.1018850869879191 [on Fri Dec 11 02:11:31 EST 2009]
[Subscriber 1 GOT MESSAGE] Msg from 2: 0.7325884278324815 [on Fri Dec 11 02:11:31 EST 2009]
[Subscriber 2 GOT MESSAGE] Msg from 4: 0.8804538983093999 [on Fri Dec 11 02:11:31 EST 2009]
...

Such an elegant solution to a classic problem - the wonderful BlockingQueue …

FTP made easy with Python

I am falling in love with python every day - its amazing how simple it makes tasks. The indented syntax and the functional aspects of the languages are growing on me every day.

Python comes with a bunch of libraries to make your life easy and one of them is ftplib. Here is a little script to list, chage directories and ultimately download a binary and a text file:

from ftplib import FTP
import getpass
serverName = "ahlawat.net"
ftp = FTP(serverName, raw_input("Username: "), getpass.getpass())
#change working directory
ftp.cwd("ftp")

#list the files
print ftp.dir()

baseDir = "c:/dev/python/pytest/pkg/"

#transfer a text file - some script
f = open(baseDir + "testUrl.groovy", "w")
ftp.retrlines("RETR testUrl.groovy", lambda x : f.write(x))
f.close()

#trasnfer a binary file - a png image
f = open(baseDir + "cube_logo.png", "wb")
ftp.retrbinary("RETR cube_logo.png", lambda x : f.write(x))
f.close()

More to come ..