Data Push with GDS Gravity and Tomcat

I’ve been working on a project that uses the very nice application stack of mysql-hibernate-graniteds-flex and recently decided I needed to push data from the server to the client. Since I was already using GraniteDS for AMF remoting, I thought I’d take advantage of the Gravity package which does a Comet-like data push. The GDS documentation has some of the information I needed. Since I was running on OS X, I also needed these instructions for getting APR installed with tomcat (which gravity requires for native I/O). BTW, from what I read, APR is a must for any production use of tomcat since it increases I/O performance through native calls to the OS.

After getting this all set up, I was able to get my channel configured and subscribe to it from the flex client. In my tomcat log, I could see how the comet request timeouts happened at regular intervals (as you’d expect). I wasn’t, however, getting messages sent to the client. Since that is the whole purpose of this exercise, getting this working was very important!

After digging through the gravity code, it seems that the thing that was missing is that the sub_topic header needs to be set on the message. Once I set that to be the same as the client was expecting, it worked great! Here is the code for a servlet I extend for some REST web services which need to send a message to my flex app.

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

import flex.messaging.messages.AsyncMessage;
import flex.messaging.messages.Message;
import org.granite.gravity.AbstractChannel;
import org.granite.gravity.Gravity;
import org.granite.gravity.tomcat.TomcatChannelFactory;

public class GravityServlet extends HttpServlet {
	private Gravity gravity;
	private AbstractChannel pubChannel;

	public void init(ServletConfig config) throws ServletException {
		gravity = (Gravity)config.getServletContext().getAttribute("org.granite.gravity.Gravity");
		pubChannel = new AbstractChannel(gravity) {
				@Override protected void clearQueue() { }
				@Override public void deliver(
						AbstractChannel from,
						Message message,
						String subscriptionId
					) { }

	protected void sendMessage(String msg) {
		AsyncMessage message = new AsyncMessage();
		message.setHeader(AsyncMessage.SUBTOPIC_HEADER, "discussion");
		gravity.publishMessage(pubChannel, message);

Here is the code that registers to receive the messages;

	// this code sets up listener for server push of EDL update status
	consumer = new Consumer();
	consumer.destination = "etlprocess";
	consumer.topic = "discussion";
	consumer.addEventListener(MessageEvent.MESSAGE, handleETL);

The web.xml looks like this;

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "
    <!-- read services-config.xml file at web application startup -->

    <!-- handle AMF requests ([de]serialization) -->

    <!-- handle AMF requests (execution) -->

One more piece that I seem to have left out. In the services-config.xml which configures graniteDS, I had to add a service def that defined my destination (in this case, “etlprocess”).

        <service id="messaging-service"

            <destination id="etlprocess">
                    <channel ref="my-gravityamf"/>

7 thoughts on “Data Push with GDS Gravity and Tomcat

  1. That was odd. I had posted it, but somehow, it was rolled back. I found the version with the web.xml in autosave… I see it now, so I hope you can also.

  2. Do you have the full code for this?

    When I try to set the topic on the connection object it gives this error:

    1119: Access of possibly undefined property topic through a reference with static type.

    Also when I run the Flex app, it complains:

    Destination ‘etlprocess’ either does not exist or the destination has no channels defined (and the application does not define any default channels.

    Is the WEB-INF/flex/services-config.xml and those other XMLS still needed? I’d like to see the full project….

  3. Hi,

    I’m having the same problem as DavidT:-

    Destination ‘etlprocess’ either does not exist or the destination has no channels defined (and the application does not define any default channels.

    Great if you could send full code or comment on likely issue.

    Thanks in advance.

    1. I’m sorry it took me so long with this. I didn’t see DavidT’s comment till you pointed it out. What I forgot to include in the blog post (and just added) was the addition to the services-config.xml which defines etlprocess as a destination.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s