Skip navigation

Tag Archives: GWT

Because of the new file structure of GWT 1.6 the Cypal Eclipse plugin doesn’t work properly any more. Since you want at least a bit of comfort while developing, you have to use Googles Eclipse plugin. But that plugin is everything else than a stroke of genius…

If you want to create a new project, you get a complete GWT example project that demonstrates how to use GWT. This might be fine for the first look at GWT, but its annoying to clean that up every time you create a new project.

While Cypal had some functionality to create RPC services with just one click  – including interface, async-handler and the actual implementation – the Google plugin doesn’t offer any help at all. You have to write everything by your one, even the changes in web.xml.

We can only hope, that Google will add these functionalities soon, or Cypal will getcompatible to GWT 1.6.

Marc Englund has developed a GWT widget that allows you to receive user-input via gestures.

For more information about this cool stuff take a look at his blog.

Yesterday I’ve discovered GWTEventService. Today i started to play around with it, after some research in the example-svn and reading the docs, I was able to write my own “Hello World”.

This Hello World is a small chatting application. You are only able to send messages but not set a nickname or something similar.

Here you can see what it will look like:gwteventservice-helloworld1

In this short overview I’ll just show you the parts that are associated with the GWTEventSerice. The whole regular GWT UI/RPC stuff is left out. If you want to see the complete example, you can download it here.

First of all you need an Event:

public class MyEvent implements Event {
	public String message;

	public MyEvent(){}

	public MyEvent(String message) {
		this.setMessage(message);
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

}

This Event gets fired when the server gets a new message from a client.

The next thing you need is a EventListener that gets notified when an Event occures:

public class MyListener implements RemoteEventListener {

	/**
	 * This function gets called by EventService
	 */
	public void apply(Event anEvent) {
		/**
		 * Check if the incoming Event is from the type MyEvent and if so, call the corresponding function
		 */
		if(anEvent instanceof MyEvent)
			onMyEvent((MyEvent)anEvent);
	}

	/**
	 * This function gets called when the incomming Event is from the Type MyEvent
	 *
	 * @param event
	 */
	public void onMyEvent(MyEvent event){}

}

The Listener receives the Events from the server an processes them.

Now on the client-side you need to register your Listener to the server:


	/**
	 * The domain to which we want to register our listener to
	 */
	private static final Domain DOMAIN = DomainFactory.getDomain("my_domain");

	public void onModuleLoad() {

		/**
		 * Create an EventService
		 */
		RemoteEventServiceFactory theEventServiceFactory = RemoteEventServiceFactory.getInstance();
		RemoteEventService theEventService = theEventServiceFactory.getRemoteEventService();

		/**
		 * Register our listener to the domain
		 */
		theEventService.addListener(DOMAIN, new MyListener(){
			public void onMyEvent(MyEvent event){
				/**
				 * Get the message from the event and append it to the allready received messages
				 */
				getIncommingMessages().setText( getIncommingMessages().getText() + event.getMessage() + "n" );
			}
		});

                [.....]
	}

On the client-side we just register our Listener, and tell him what to do when an Event occurs.

The last thing that we need is the Server:

public class HelloWorldServiceImpl extends RemoteEventServiceServlet implements HelloWorldService {

	/**
	 * The Domain that this Service sends Events to.
	 */
	private static final Domain DOMAIN = DomainFactory.getDomain("my_domain");

	/**
	 * A normal function that gets called when a client sends a message to the server.
	 */
	public void sendMessage(String message) {
		/**
		 * We dont save the incomming messages on the server. We just send an MyEvent to all
		 * registered Listeners on the Domain.
		 */
		this.addEvent(DOMAIN, new MyEvent(message));
	}

}

Instead of the default RemoteServiceServlet we extend this one with RemoteEventServiceServlet from the GWTEventService lib. This gives you the ability to send Events to the clients.

Some nasty thing with GWT was, that if you wanted your client to get notified about changes on the server, you had to use polling. Like a Timer that makes  a RPC every 500ms. That isnt very handy or elegant not just because it causes a lot of traffic but also because its long-winded. More clever would be some kind of Events and the corresponding Listeners.

To get rid of that you can use GWTEventService.

GWTEventService is an event-based client-server communication framework. It uses GWT-RPC and the Comet / server-push technique. The client side offers a high-level API with opportunities to register listeners to the server like to a GUI component. Events can be added to a domain on the server side and the listeners on the client side get informed about the incoming events. The server side is completely independent of the client implementation and is highly configurable. Domains can be defined to decide which events are important for the different contexts.

Advantages

  • Encapsulation of the client-server communication
  • High-level API with listeners and events
  • Only one open connection for event listening
  • Reduction of server calls
  • Reduction of connection peaks
  • Events are returned directly when the event has occurred (instead of polling)
  • Events are bundled to reduce server calls
  • Based on the GWT-RPC mechanism
  • Automatic timeout recognition and handling
  • Extensible architecture