Errai: The browser as a platform

Sunday, May 30, 2010

GWT, CDI and Errai at Jazoon

If you happen to be at Jazoon this year and you are interested in GWT, CDI and Errai then these sessions may be interesting to you:

Patterns and Best Practices for building large GWT applications
Tuesday, 1 June 2010, 14:00-14:50, Arena 3

In this presentation we’ll see how to organize a nontrivial GWT application. We’ll go through the lessons learned in a real world project and take a look the complete development lifecycle and best practices that go beyond what GWT has to offer out-of-the-box. This talk does focus on modularity of GWT applications and how to overcome the burdens of compile-time linking. We’ll talk about client side patterns and server side implementation options and explore different approaches that allow for quick turn around times without sacrificing maintainability.

GWT, CDI and JAX-RS: A match made in heaven
Tuesday, 1 June 2010, 15:00-15:50, Arena 3

Every non-trivial GWT application requires integration with the server side. While GWT itself ships with the integration capabilities (i.e GWT RPC) it doesn't go beyond that. Developers have to decide how to build the backend to their GWT applications. While freedom of choice is a good thing, it doesn’t always lead to a good decision. In this session we’ll look at two options, JSR-299 [1] and JSR-311 [2], both part of the EE6 specification and see how they interplay with GWT. We'll discuss the use cases and justifications for each technology see how they are applied in practice by looking at some code examples.

[1] JSR-299: Java Contexts and Dependency Injection for the Java EE platform (CDI) is the new Java standard for dependency injection and contextual lifecycle management.

[2] JSR-311: A that specification defines a set of Java APIs for the development of Web services built according to the Representational State Transfer[1] (REST) architectural style.



The complete schedule can be found here.

Wednesday, May 12, 2010

Hello, 1.1 Milestone 1!

Today, we’re pleased to announce our first step towards version 1.1, which sets us solidly on a course with 1.1 destiny.

This milestone release is a big step towards honing the concepts that we introduced in 1.0, driving someone them towards their logical conclusions, and smoothing out the rough edges. Community feedback was important to us, and we’ve worked diligently to respond to it.

This release brings a range of new features, including (but not limited to):

* Support for more servlet containers.
* New Async Task API
* A new bus monitor to make troubleshooting easier
* A new RPC API, that leverages the bus architecture, and provides an alternative to GWT-RPC.
* Better error handling.
* Better documentation.
* Bug fixes galore!

We think you should take a look. And we hope you have as much fun using it as we did building it.

Remember, Errai is always looking for community contributions. So if you’re interested in becoming a contributor, drop us a line. Download it before it gets cold.

Wednesday, May 5, 2010

Best Practices

Fellow JBoss core developer, and lead engineer for JBoss's security initiatives, Anil Saldhana took a deep dive into Errai in recent weeks, and he's documented some best practices here.

He offers some valuable advice worth checking out.

Sunday, April 25, 2010

Introducing new asynchronous task APIs

One of the things that we've come to realize while developing Errai is that when you work with a framework such as Errai, where push messaging is basically free, easy and awesome -- you end up wanting to do things like stream live data across the wire. Stuff like stock quotes, news feeds, twitter feeds, clocks, weather patterns, and the migration activity of pigeons.

A lot of our demos have involved creating threads and pushing data across completely asynchronously. And it's always messy code. You need to worry about managing those threads, making sure they die when the session dies, or when the subject is unsubscribed, etc. Having to worry about all this creates security problems, resource management issues, and it makes your code messy.

Well, worry no longer! The latest commit into trunk introduces a new comprehensive (and simple) way of creating asynchronously running tasks -- as part of the the standard MessageBuilder API.

Take our TimeDisplay demo, where we stream a bunch of updates from the server to the client containing System.currentTimeMillis() results.

Up until now, the demo consisted of a thread that looped around and around and built new messages to send. Through the addition of a new API extensions, this demo is greatly simplified.

The first addition is some helper classes that help you create managed contexts to store stuff in the session. One is called SessionContext, and the other is called LocalContext. SessionContext allows you to create session scoped attributes, and LocalContext lets you create locally-scoped, as in, page-scoped. So if a user has multiple browser windows open, or multiple tabs, each window or tab is it's own LocalContext. This is a pretty powerful little tool.

The second addition is the implementation of what I'm calling provided message parts. Unlike regular message parts, these parts are resolved via providers at the time of transmission. This is a key aspect of what we're about to show below, as it creates message re-usability.

The third addition is the implementation of a repeating and delayed message transmission calls as part of the standard messaging API.

Let's take a look at the example:


AsyncTask task = MessageBuilder.createConversation(message)
    .toSubject("TimeChannel").signalling()
    .withProvided(TimeServerParts.TimeString, new <>ResourceProvider() {
        public String get() {
            return String.valueOf(System.currentTimeMillis());
        }
    }).noErrorHandling().replyRepeating(TimeUnit.MILLISECONDS, 100);



In this example, we create a conversational message which replies not just once, but replies continuously. Once every 100 milliseconds as it would turn out. The replyRepeating() and replyDelayed(), sendRepeating() and sendDelayed() methods all return an instance of AsyncTask, which is a handle on the task being performed. You can use this object to cancel the task.

Doing so is pretty easy.

    task.cancel(true)

Knowing all this, which isn't very much -- and that's the cool part -- let's put it all together:


@Service("TimeServer")
@RequireAuthentication
public class TimeDisplay implements MessageCallback {
    private MessageBus bus;

    @Inject
    public TimeDisplay(MessageBus bus) {
        this.bus = bus;
    }

    public void callback(final Message message) {
        if (message.getCommandType() == null) return;

        /**
         * Create a local context to store state that is unique to this client instance. (not session wide).
         */

        final LocalContext context = LocalContext.get(message);

        /**
         * Switch on the TimeServerCommand type provided
         */

        switch (TimeServerCommands.valueOf(message.getCommandType())) {
            case Start:
                /**
                 * We want to start streaming.
                 */

                AsyncTask task = MessageBuilder.createConversation(message)
                        .toSubject("TimeChannel").signalling()
                        .withProvided(TimeServerParts.TimeString, new <>ResourceProvider() {
                            public String get() {
                                return String.valueOf(System.currentTimeMillis());
                            }
                        }).noErrorHandling().replyRepeating(TimeUnit.MILLISECONDS, 100);

                /**
                 * Store the task as an attribute uniquely identified by it's class type.
                 */

                context.setAttribute(AsyncTask.class, task);

                /**
                 * Create a listener that will kill the task gracefully if the subject is unsubscribed.  This
                 * isn't 100% necessary, as the task will be auto-killed ungracefully.  But this provides
                 * and opportunity to clean up after ourselves.
                 */

                bus.addUnsubscribeListener(new UnsubscribeListener() {
                    public void onUnsubscribe(SubscriptionEvent event) {
                        if ("TimeChannel".equals(event.getSubject())) {
                            /**
                             * Delete this listener after this execution.
                             */

                            event.setDisposeListener(true);

                            /**
                             * Stop the task from running.
                             */

                            context.getAttribute(AsyncTask.class).cancel(true);

                            /**
                             * Destroy the local context.  Sort of unnecessary, but helps reduce memory usage.
                             */

                            context.destroy();
                        }
                    }
                });
                break;

            case Stop:
                /**
                 * Access our stored AsyncTask from this instance and cancel it.
                 */

                context.getAttribute(AsyncTask.class).cancel(true);

                /**
                 * Destroy the local context.  Sort of unnecessary, but helps reduce memory usage.
                 */

                context.destroy();
                break;
        }
    }
}



That's all there is to it. It's pretty sweet. This API works on both the client and the server side. All the thread scheduling is all transparently managed by an executor service on the server, and by a simple Timer based implementation in the client. I'm hoping people will find this a welcome addition to Errai-land.

There will be more details coming as I iron out the bugs. This code is all new, so proceed at your own risk and all that stuff.

Tuesday, April 20, 2010