Showing posts with label Cometd. Show all posts
Showing posts with label Cometd. Show all posts

Tuesday, April 5, 2011

Wicket: wicket-ajax Channel busy postponing

I got above error when using Wicket + wiQuery + wicket-push(cometd). I struggled for a day and finally, solved it.

In my case, the issue appeared on Firefox, but not happened on Chrome. Also When the page was first loaded, it worked fine. However, after reload the page, it happened. Any wicket's ajax request was blocked. And in the Wicket-Ajax-Debug window, "INFO: Channel busy postponing" message appeared.

The problem is caused by, **I guess**, wicket-ajax.js + jquery. When some jquery javascript start earlier than something, it will harm wicket-ajax.js. In my case, by cometd javascript, (see this) $.cometd.configure() and $.cometd.handshake() starts without waiting loading whole contents in the page. So I fix as follows.

$(window).load(function(){
    $.cometd.configure();
    $.cometd.handshake();
});

This will delay the two functions to start. And problem was gone.

I'm not sure this is a right way to do. Please leave comment if you find the better solution for the same problem.

Wednesday, February 16, 2011

Wicket push reload support

When I look the cometd's instruction page here, it seems the the based source code do not support reload. As shown in the example, it requires a client side javascript code. But it was not written anywhere, as far as I saw.

I start supporting reload extension on my custom wicketstuff-push (see the post). Let's see the code.

public abstract class CometdAbstractBehavior extends AbstractDefaultAjaxBehavior {
    private static final long serialVersionUID = 1L;
 
    // FIXME: put this in application scope, we may have several webapp using
    // CometdBehavior in the same web container!
    private final static String cometdServletPath = getCometdServletPath();
  
    private static final ResourceReference COMETD =
        new CompressedResourceReference(CometdAbstractBehavior.class, "org/cometd.js");  
    private static final ResourceReference JQ_JSON2 =
        new CompressedResourceReference(CometdAbstractBehavior.class, "jquery/json2.js");
    private static final ResourceReference JQ_COMETD =
        new CompressedResourceReference(CometdAbstractBehavior.class, "jquery/jquery.cometd.js");

    private static final ResourceReference COMETD_RELOAD =
        new CompressedResourceReference(CometdAbstractBehavior.class, "org/cometd/ReloadExtension.js");
    private static final ResourceReference JQ_COOKIE =
        new CompressedResourceReference(CometdAbstractBehavior.class, "jquery/jquery.cookie.js");
    private static final ResourceReference JQ_COMETD_RELOAD =
        new CompressedResourceReference(CometdAbstractBehavior.class, "jquery/jquery.cometd-reload.js");

    @Override
    public void renderHead(final IHeaderResponse response) {
        super.renderHead(response);
        if (channelId == null) {
            throw new IllegalArgumentException("ChannelId in a CometdBehavior can not be null");
        }
        response.renderJavascriptReference(COMETD);
        response.renderJavascriptReference(JQ_JSON2);
        response.renderJavascriptReference(JQ_COMETD);

        response.renderJavascriptReference(COMETD_RELOAD);
        response.renderJavascriptReference(JQ_COOKIE);
        response.renderJavascriptReference(JQ_COMETD_RELOAD);
 
        response.renderJavascript(getInitCometdScript(), "initCometd");
        final String cometdInterceptorScript = getCometdInterceptorScript();
        if (cometdInterceptorScript != null) {
            response.renderJavascript(cometdInterceptorScript, "Interceptor"
                    + getBehaviorMarkupId());
        }
        response.renderJavascript(getSubscriberScript(), "Subscribe"
                + getBehaviorMarkupId());
    }

    protected final CharSequence getInitCometdScript() {
        return new PackagedTextTemplate(CometdBehavior.class, "CometdReloadInit.js").getString() +
            getConfigureCometdScript() + getHandshakeCometdScript(); 
    }

I add a javascript file. This code is written based on the reload example for jQuery (here).
/* handshake listener to report client IDs */
$.cometd.addListener("/meta/handshake", function(message)
{
    if (message.successful)
    {
        $('#previous').html(org.cometd.COOKIE.get('demoLastCometdID'));
        $('#current').html(message.clientId);
        org.cometd.COOKIE.set('demoLastCometdID', message.clientId, {
            'max-age': 300,
            path : '/',
            expires: new Date(new Date().getTime() + 300 * 1000)
        });
    }
    else
    {
        $('#previous').html('Handshake Failed');
        $('#current').html('Handshake Failed');
    }
});

/* Setup reload extension */
$(window).unload(function()
{
    $.cometd.reload();
});


Be care that this works on not only browser reloads, but also loading the same page with different parameters. It means, subscribing to the different channel also happens without handshake.

Saturday, February 12, 2011

Wicket push

Wicketstuff push has dramatically changed at 1.4.13. But the API changes so much at that version, and I cannot make it work. Since the 1.4.12 uses very OLD dojo libraries, I want to migrate to the new version.

While looking the release documents of Cometd 2.1.0, I find that it supports jQuery bindings and Dojo bindings. So, I try to change the bindings to jQuery based on 1.4.12.

The files I have changed are two, CometdAbstractBehavior.java, CometdBehavior.java. And import javascripts from cometd-javascript-jquery-2.1.0.war.

public abstract class CometdAbstractBehavior extends AbstractDefaultAjaxBehavior {
    private static final long serialVersionUID = 1L;

    // FIXME: put this in application scope, we may have several webapp using
    // CometdBehavior in the same web container!
    private final static String cometdServletPath = getCometdServletPath();
 
    private static final ResourceReference COMETD =
        new CompressedResourceReference(CometdAbstractBehavior.class, "org/cometd.js");  
    private static final ResourceReference JQ_JSON2 =
        new CompressedResourceReference(CometdAbstractBehavior.class, "jquery/json2.js");
    private static final ResourceReference JQ_COMETD =
        new CompressedResourceReference(CometdAbstractBehavior.class, "jquery/jquery.cometd.js");

    @Override
    public void renderHead(final IHeaderResponse response) {
        super.renderHead(response);
        if (channelId == null) {
            throw new IllegalArgumentException("ChannelId in a CometdBehavior can not be null");
        }
        response.renderJavascriptReference(COMETD);
        response.renderJavascriptReference(JQ_JSON2);
        response.renderJavascriptReference(JQ_COMETD);

        response.renderJavascript(getInitCometdScript(), "initCometd");
        final String cometdInterceptorScript = getCometdInterceptorScript();
        if (cometdInterceptorScript != null) {
            response.renderJavascript(cometdInterceptorScript, "Interceptor"
                    + getBehaviorMarkupId());
        }
        response.renderJavascript(getSubscriberScript(), "Subscribe"
                + getBehaviorMarkupId());
    }

    protected final CharSequence getInitCometdScript() {
        return getConfigureCometdScript() + getHandshakeCometdScript(); 
    }

    protected final String getConfigureCometdScript() {
        return "$.cometd.configure('" + cometdServletPath + "')\n";
    }
 
    protected String getHandshakeCometdScript() {
        return "$.cometd.handshake()\n";
    }
 
    public final CharSequence getSubscriberScript() {
        return "$.cometd.subscribe('/" + getChannelId() + "', "
                + getPartialSubscriber() + ");\n";
    }

public class CometdBehavior extends CometdAbstractBehavior {

    @Override
    public final String getCometdInterceptorScript() {
  
        final Map map = new HashMap();
        map.put("behaviorMarkupId", getBehaviorMarkupId());
        map.put("url", getCallbackUrl().toString());
  
        return new PackagedTextTemplate(CometdBehavior.class, "CometdDefaultBehaviorTemplate.js").asString(map);
    }

    @Override
    public final CharSequence getPartialSubscriber() {
        return "onEventFor" + getBehaviorMarkupId();
    }

Yes, it works. Thanks for chrome's developer tool. It helps me lot! And I also refer this.