Monday, March 05, 2007

RAD, Spring, and thoomp!

Ok, so only the first two of the title trio have anything to do with each other. The latter's just the sound an air cannon makes and I thought it went well here. Moving on:


One of the best things about the Spring Framework for Java is that thanks to the fact that it's an Inversion of Control (IoC) container, you can very easily add to projects and update them with new code. The latest thing I did with one of my Spring-based projects was to add notifiers when certain batches are uploaded to the system. The first notifier I did was an email notifier, so nothing special. But immediately after I finished implementing it, my "Wouldn't it be cool if ..." sense kicked in and I considered implementing a notifier for MSN Messenger so that certain people in our organization could receive instant notification for batch uploads. After two minutes of google searching, I came upon the JML (Java MSN Messenger Library) posted on SourceForge.net . After a few minutes of looking at the example code posted with the project (still fairly sparse), I came up with the following to inject an ApplicationListener into my Spring context:

<bean id="msnMessenger" class="net.sf.jml.impl.MsnMessengerFactory" factory-method="createMsnMessenger" init-method="login" destroy-method="logout">
<constructor-arg value="my-email@hotmail.com"/>
<constructor-arg value="mypass"/>

<property name="logIncoming" value="true"/>
<property name="logOutgoing" value="true"/>
</bean>

<bean id="msnMessengerNotifier" class="my.package.ApplicationListenerImpl">
<constructor-arg ref="msnMessenger"/>

<property name="recipients">
<map>
<entry key="email_of_our_batch_processing_guy@ourcompany.com" value="His Name"/>
</map>
</property>
</bean>

...and the relevant implementation code :

this.msnMessenger.sendText(Email.parseStr(emailAddress), messageString);

...and in under 20 minutes I had an instant messaging notifier implemented thanks to the ease of Spring and the JML library. Please go see that site if you need instant messaging in Java with MSN Messenger, it's actually a great little project and they deserve to be supported. It seems however, that the one little caveat of this (and it's not even JML's fault) is that the notifier and the person receiving the notification must have each other on their contact lists.

1 comment:

Unknown said...

Hi!

I try to used this library, but I have some problems. You can maybe help me :


I try to compile the BasicMessenger example of the JML library but I found some strange things :

in the main they are



public static void main(String[] args) throws Exception {
if (args.length != 3) {
System.out.println("Usage: java messengerClassName email password");
return;
}

BasicMessenger messenger = (BasicMessenger) Class.forName(args[0]).newInstance();

messenger.setEmail(args[1]);
messenger.setPassword(args[2]);
messenger.start();
}




They say : Usage: java messengerClassName email password

they are 2 arguments and not 3!
if I print arg[1] this is the password, and they set the password in the email!



This is not possible : Arg[0] should be the name of the class. why doing that?

why not :

BasicMessenger messenger = new BasicMessenger();

???

So to solve this bug I try to set the password and the email in the code because the argument is shifted forward...

If I initialise the class like that :

BasicMessenger messenger = (BasicMessenger) Class.forName("net.sf.jml.example.BasicMessenger").newInstance

it's working better but I have got this error after :

$ java net.sf.jml.example.BasicMessenger
Exception in thread "main" java.lang.NoClassDefFoundError: net/sf/jml/impl/MsnMessengerFactory
at net.sf.jml.example.BasicMessenger.start(BasicMessenger.java:50)
at net.sf.jml.example.BasicMessenger.main(BasicMessenger.java:87)



How can i do to compile and execute a basic example with javac then with java please?

Have you some working example to show me please?

I know that my english is not very good, so if we can talk by the gmail tchat, msn, icq or other this will be easier and very nice!

Thanks for your Attention.