JQuery…my new favorite Ajax framework

After a several stints with Dojo, Ext and Prototype, I decided to see if JQuery could provide anything better. I figure it’s a good thing to be versed in several Ajax frameworks. You never know which one you might use next really. I am definitely weary of Dojo and it’s lack of support and constant revamping to remove things that worked previously. I love Ext, but turning around and charging for it was really a bad idea on their part. I think they have the best forums and support and their API is really easy to use.
After looking into JQuery, I was pleasantly surprised to find out how lightweight it was. It is a tiny fraction of the size of the Dojo library and the syntax of use is more concise and direct. Of course, this lack of size is mainly because all of the widgeting control is included in other separate plugins that you can add on, but I do like that. It is simple to download a plugin or write your own. I really avoid writing something custom unless a last resort. There is always one out there ready for use.
So, I put JQuery into my project and I already had a Spring Controller using Content negotiation serving up my JSON and I gave it a whirl. It just a few minutes I was able to get data from the JSON object and then populate anything or do anything I wanted to do with it. I accomplished it in just a few lines of code.

$(document).ready(function(){

$.getJSON(“rest/DataService/loanTypes.json”,{id: $(this).val(), ajax: ‘true’}, function(j){

var options = ”;
var loanTypes = j.loanTypeList;

var currOptions = $(“select#loanTypes”).html(options);

for (var i = 0; i < loanTypes.length; i++) { options += '’ + loanTypes[i].loanType + ”;
}

$(“select#loanTypes”).html(options);

});

});

At first the $ notation takes a little getting used to, but just think of it as a dojo.byId on steroids. It’s a really powerful notation. This is just a snippet from the example, but I just get the JSON from the server and populate a select box manually creating option elements. Ordinarily I would frown upon doing this, but it’s just an example. A proper way would be to use a JQuery UI component that would just take the JSON automatically.

I found several plugins that were really useful on the JQuery site. I even used JQuery UI and another plugin to replace alert messages with Ajax Alerts.

Most of the other libraries out there are all very self contained, but JQuery just allows you to download plugins at will. This is very powerful but it has it’s drawbacks. You have to make sure that the plugin you download and all it’s dependencies all match with the supported versions of each. This can make upgrading JQuery difficult if you use a lot of plugins. I recommend using them cautiously.

You can find a list of all the JQuery plugins here.

http://plugins.jquery.com/

You can download and install JQuery into your project as simply as downloading the js file and including in your HTML page.

Go get it and try it out.

JQuery http://www.jquery.com

If you want support or see other examples of how other users are leveraging the framework, check out the forum.

http://forum.jquery.com/

Make your Web Services simple with Apache CXF and Spring

Have you ever just wanted to make Java Web Services so easy that you can whip one out in a few minutes, well here is your shot. Apache CXF and Spring make creating a Web Service almost stupid simple. I’ll get right to it.

First you need to configure CXF in your web application by adding a few lines to the web.xml.

CXFServlet

org.apache.cxf.transport.servlet.CXFServlet

CXFServlet
/ws/*

All this is doing is setting it up so that all requests for /ws/* to get routed to CXF. Now we need to include a new xml file for Spring to load.

<beans xmlns="http://www.springframework.org/schema/beans&quot;
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance&#8221;
xmlns:context=”http://www.springframework.org/schema/context&#8221;
xmlns:cxf=”http://cxf.apache.org/core&#8221;
xmlns:jaxws=”http://cxf.apache.org/jaxws&#8221;
xsi:schemaLocation=”http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://cxf.apache.org/core
http://cxf.apache.org/schemas/core.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd&#8221;
default-autowire=”byName”>


<bean id="aegisBean"
class=”org.apache.cxf.aegis.databinding.AegisDatabinding”
scope=”prototype”>

<bean id="jaxws-and-aegis-service-factory"
class=”org.apache.cxf.jaxws.support.JaxWsServiceFactoryBean”
scope=”prototype”>

<jaxws:endpoint id="applicationWebService"
implementorClass=”com.domain.services.impl.ApplicationServiceImpl”
implementor=”#applicationService”
address=”/applicationService”>

Now, we are ready to write our service class. I typically create service classes to interact with my other backend services, databases, web services, etc. Here is an example.

@WebService(endpointInterface = “com.domain.services.IApplicationService”, name = “applicationService”, serviceName = “applicationService”, portName = “applicationPort”, targetNamespace = “http://services.domain.com/&#8221;)
@Service(name=”applicationService”)
public class ApplicationServiceImpl extends ServiceImpl implements
IApplicationService {

public String sayHello(){

return “Hello”;
}

And that is pretty much it. Start up your Web server and hit /ws/applicationService?wsdl and you should get a WSDL for the service automatically generated. You can use SoapUI as a testing tool to test out the services. You can also use SoapUI to generate any type of client that you want for the services, JAX-WS, JAX-RPC, Axis, you name it.

It doesn’t get much easier than that does it? I’ve only scratched the surface of CXF as well. It has many more features.

Spring and CXF make a truly versatile way to create an SOA architecture or an Enterprise Service Bus all on your own as well.

Caching Java methods with Spring 3

A while back I wrote about caching Java methods using an earlier version of Spring, but now I am pleased to announce that you can cache your methods using annotations with Spring 3 using Ehcache annotations for Spring.

Similar to my other post you can cache a Java method in a Spring bean by simply adding the annotation.

@Cacheable(cacheName=”dogCache”)
public Collection getDogs() throws Exception {

I really put this annotation to work in my latest project because I called many Web Services in my code that return very static data that is managed by our Enterprise Service Bus. There isn’t any need to go get the data every time since it hardly ever changes.

Using the annotation, I specify that the cache region I want to use is dogCache from the ehcache.xml file and I have it configured specific to have it handle caching dogs exactly like I want it to, even to overflow to disk if necessary. Now let’s look at the configuration necessary.

You will need to drip the jar file for ehcache-annotations in your WEB-INF/lib and then make the following modifications to your applicationContext.xml.

classpath:ehcache-spring.xml

And that’s it, you are now caching your results. Be very cautious with using this, each and every method that gets cached must be thoroughly thought through. For example, I accidentally placed this annotation on a method that returned domain objects that were already configured to be cached via Hibernate, thus doubling the amount of memory that the server was using for each object loaded.

While a very powerful tool that can increase performance, caching can also decrease it.