Kontakt

Archive for September, 2011

Publishing Tapestry 5 in Action Book On My Own

Freitag, September 23rd, 2011

Some weeks ago I have been informed by Manning Publications that the Tapestry 5 in Action book is canceled. Based on MEAP sales Manning did a sales forecast. It looks like the book is financially not attractive enough.

As you can imagine, this news made me upset. I was working on the book for about 8 month now. The half of the book (almost 250 pages) is already finished and I was looking forward to see the book in a final shape. After the MEAP has been started, I’ve got a lot of positive feedback regarding the book. It looks like the readers like the book’s content very much.

Manning’s decision was more than a surprise for me. After being upset for a couple of days I changed my opinion about Manning’s decision. Now I believe that this cancelation is the best thing that could happen to the book. I decided to publish the book on my own (probably on lulu.com). First of all, it is much more financially attractive for me. Secondly, I’ll be able to publish as many editions as I wish. I can update the book’s content every time a new Tapestry version is released. The only disadvantage is that lulu.com doesn’t provide something like an “early access program” so that the readers will need to wait until the book is finished.

If you was one of the MEAP customers who bought the Tapestry 5 in Action book, there is no reason to be sad. The book is going to be published. I promise you.

Stay tuned!

Scheduling Jobs With Tapestry

Sonntag, September 18th, 2011

Frequently web applications have a requirement to execute some business logic which is not bound to any HTTP request. For example, you might want to execute some periodic maintenance job to cleanup the database or a job sending messages to your users. In this article I’ll show you how to register jobs to be executed by Tapestry without blocking any incoming HTTP request.

Interval Jobs

Imagine you have a CleanupService service to be executed for database cleanup. The service’s interface is shown in the following example. The service implementation details are not interesting for this article as they are very specific to your application.

public interface CleanupService {
    void clean();
}

Now let’s schedule a periodic execution of the cleanup at application’s startup. This is accomplished by contributing to the RegistryStartup service’s configuration, as shown in the following example.

public class AppModule {

    @Startup
    public static void scheduleJobs(
                   PeriodicExecutor executor,
                   final CleanupService cleanupService) {

        executor.addJob(
            new IntervalSchedule(300000L),
            "Cleanup Job",
            new Runnable() {
                public void run() {
                    cleanupService.clean();
                }
            });
    }
}

As of Tapestry 5.3 the PeriodicExecutor service can be used to schedule jobs. In the example above, this service is injected into a startup method. The service’s addJob method is used to register jobs to be executed.This method takes three parameters:

  1. Instance of Schedule: defines when to execute the next job
  2. Name used in debugging output related to the job
  3. Instance of Runnable that represents the work to be done

In the example above, a IntervalSchedule instance is used to schedule a job to be executed every 300000 milliseconds (5 minutes). Note that the Runnable is just a wrapper around the CleanupService.

Using CRON expressions

Instead of defining intervals for job executions, Tapestry also allows you to use CRON expressions, such as described here. Note that Tapestry uses the CRON parser from the Quartz Scheduler without depending on it.

The following example demonstrates how to use CRON expressions.

public class AppModule {

    @Startup
    public static void scheduleJobs(
                   PeriodicExecutor executor,
                   final CleanupService cleanupService) {

        executor.addJob(
            new CronSchedule("0 0/5 14 * * ?"),
            "Cleanup Job",
            new Runnable() {
                public void run() {
                    cleanupService.clean();
                }
            });
    }
}

In the example above, a job is registered to be executed every 5 minutes starting at 2pm and ending at 2:55pm, every day.

Happy scheduling.

Tapestry 5 Blog - Copyright © 2009 - Eclectic Theme by Your Inspiration Web - Powered by WordPress