Screaming Coder


July 23, 2009

Scheduling Portable Extension for Weld / Seam 3

Filed under: frameworks, general, it, open source, seam, software development — Pete @ 3:24 pm

Introducing Weld  Scheduling – the best way to run scheduled events in JBoss Weld, Seam 3 and possibly any JSR-299 implementation. It makes use of CDI’s typesafe event model for tying business logic to schedules. That is, you define your schedules using the provided qualifiers, which you apply to observer methods containing the business logic that you wish to be run at those times. In other words:

    public void onSchedule(@Observes @Scheduled("20 */2 * ? * *") Event e) {
        // do something every 2 minutes, at 20 seconds past the minute.
    }

The CDI container will fire the @Scheduled(“20 */2 * ? * *”) Event at 20 seconds past every second minute, causing the onSchedule method to be executed each time. When CDI starts up with this module on the classpath, all observers of the Event class are detected by the module using standard CDI APIs. The module then inspects each associated @Scheduled binding annotation and sets up a schedule to fire an Event with that binding at the schedule found. Currently Quartz is used as the underlying scheduler.

One obvious shortcoming of this is that we’ve managed to hard-code scheduling information in our Java code. The answer to this is to define the schedule as a property in the scheduler.properties file at the root of your classpath, for example:

# This schedule is named "test.one" and runs every 2 minutes
test.one=20 */2 * ? * *
# This schedule is named "after.hours" and runs in the wee hours every day
after.hours=0 0 2 ? * *

You can then observe that schedule like this:

    public void onNamedSchedule(@Observes @Scheduled("test.one") Event event) {
        // the schedule is defined in scheduler.properties
    }

This is getting better, but that “test.one” String is still setting off some refactoring alarm bells. No worries, we can deal with this pretty easily using meta-annotations. We just create a custom qualifier like so:

@Scheduled("after.hours")
@Qualifier
@Retention(RetentionPolicy.RUNTIME)
@Target( { ElementType.PARAMETER, ElementType.METHOD, ElementType.FIELD, ElementType.TYPE })
public @interface AfterHours {}

And now we can observe the event in a typesafe manner, in as many places as we want throughout our codebase with all the benefits of code-completion and none of the refactoring headaches:

    public void onTypesafeSchedule(@Observes @AfterHours Event e) {
        // do something after hours
    }

There are also some built-in convenience events for regular schedules:

    public void everySecond(@Observes @Every Second second) {
        // this gets executed every second
    }

    public void everyMinute(@Observes @Every Minute minute) {
        // this gets executed every minute
    }

    public void everyHour(@Observes @Every Hour hour) {
        // this gets executed every hour
    }

Note though that none of these built-in events will be scheduled, let alone fired, unless the module finds an observer for them on startup.

This project has been submitted to the Seam 3 sandbox (find it in seam/sandbox/modules). An early release of the Weld Scheduling module and Memory Grapher example app can be downloaded from here: WeldScheduling.tgz. They’re both built with Maven 2.0.10+. To run the example app, ‘mvn clean install‘ both projects (’scheduling’ first, then ‘MemoryGrapher’) and then run ‘mvn -Drun install’ from inside MemoryGrapher. It uses the Weld SE extension to run it without an app server (it’s a Swing app).

MNA542C29VE6

March 9, 2008

I Know Shoes

Filed under: Ruby, general, it, open source, software development — Pete @ 8:18 pm

Finally! My copy of Nobody Knows Shoes arrived in the mail this week. As an adoring fan of Why’s Poignant Guide I had perhaps unfairly high expectations of NKS. As can be seen in the downloadable PDF version it’s not as long nor as entertaining as the Guide, seems targeted at a slightly younger crowd and is decidedly less poignant. Unlike the Guide, the comic strips in NKS make no sense whatsoever and have abs(zero) relevance to the actual subject matter. There’s not even any mention of Chunky Bacon. But this is not to say that NKS is somehow inferior compared to WPG, more that I really had no business comparing the two in the first place. After all, Shoes is just a tiny toolkit by it’s own admission, designed with new programmers in mind. For those with a passing interest I recommend simply downloading the on-line PDF version (it is printed at cost anyway so you’ll just be saving _why the effort). But I would recommend it to any teacher-types with a class full of wanna-be programmers. Just hand them each a copy and let _why’s deranged cosmo-babble do the rest.

May 15, 2006

Senorita wont you take me to the drug store?

Filed under: Published, general, it, music — Pete @ 7:52 pm

Went out last night to see Gerling play at the Zoo. It was unbelievably awesome! They played a good mix of new and old stuff, the absolute highlight being the encore – Ghost Patrol!! Anyone who’s never seen them or doesn’t know who they are should get out and see them next time they’re in town. They’re a mad live show, doesn’t realy matter what you’re into. They were supported by Dappled Cities Fly whose insane dynamics and accents earned them instant respect in my books.

We met up with a couple from the Goldcoast. They were realy friendly and we ended up all staying out till the 3am lockout, just talking and experiencing the wierdness of other people in general. We swapped numbers and stuff so hopefully we’ll see them again.

The only regret of the night was that I left without getting one of those wicked Gerling benies.

Also discovered a new band a couple of weeks ago. A friend of a friend described them as “cooky pop”. That got me interested, and as soon as I hit their website I fell in love with their artwork. One of their album covers features one robot handing love to another on floppy disk! And the track listing takes me back to the good old days of BASICA on the TRaSh-80.

Musically they sound a bit like Grandaddy but with one major difference: Whereas Grandaddy will throw in shameless pop-candy to relieve the otherwise brooding nature of their songs, Sekiden tend to feature it more as a main ingredient. This makes for some pretty catchy songs, you just have to be in the right head-space to appreciate them.

They were supported by the Zoobombs, a nutzoid Japanese “funk hardcore” band. Nuff said.

January 7, 2006

A Great Day for Programming

Filed under: general, it — Pete @ 10:34 am

It’s a great day for programming (and such). Little bit of sun, little bit of cloud cover, a cool breeze blowing in over my shoulder (in a cruel twist of fate, good programming weather is almost identical to good beach weather).

So I finaly got motivated to work on something I’ve been meaning to do for a long time. That is, create an address online where we can gather all our thoughts and plans regarding our home church. We’ve sometimes talked about stuff (eg: that list) and thought it would be good to have somewhere online where we could record what we came up with. Also we’ve agreed it would be nice to be able to let everyone in on what someone is planning on talking about ahead of time so we can all come a bit more prepared to contribute to the various discussions.

Being a programmer I immediately thought “wiki” (ala Wikipedia). Being an avid live-journaler, elPoncho suggested something of a similar nature to livejournal.com (ie: some kind of blog that we can all contribute to). Both have two of the most important ingredients I can think of – user contributed content plus RSS/email notification of updates – though with slightly different focuses. A wiki is more about creating structured content, with the *ability* for use as a communication medium (via user “talk” pages). A blog on the other hand is more about communicating ideas in real time, with the *ability* to create kind-of structured content in the form of “pages”. I do think though that blogs tend to do the structured content thing a bit better than wikis can do communication.

Given the choice I think that, for our purposes, timely communication would be more important than the ability to create structured content – at least initialy, if not ultimately. So I think we’ll probably prefer the communal church journal with the odd page of content at first, and maybe “grow into” the wiki if we start doing more with content. In any case I’ve set them both up so we can see how it goes, experiment with both for a while maybe.

The wiki is powered by MediaWiki, and can be found here. Feel free to sign up and poke around and have a go adding content or whatever. The blog is powered by Wordpress 2.0, and can be found here. Again, join up, add entries, go nuts!

Note that both have RSS feeds (the MediaWiki one is in the “Recent Changes” section).

With any luck, this just might inspire the return of something a bit like the much missed “freedom project” with a wider community. Vee shall see.