Live from TSSJS – Lightweight J2EE with Adam

For my final TheServerSide Java Symposium breakout session for the day, I am attending “Lightweight Application Development with Java EE 6” presented by Adam Bien.  Adam’s talk is in part an attempt to bring former J2EE developers (myself included) back to the table who may have been driven off by previous, heavy-weight J2EE implementations, such as J2EE 1.4.

Adam spends most of his presentation actually building a simple Java EE 6 application on the fly, using Casino/Police/Mafia metaphor (since this conference is in Las Vegas).  I’ll try to capture the highlights in this blog, but the code itself says volumes about how simple to use Java EE 6 has become.  He develops in NetBeans IDe 6.9.1 with Glassfish as a web server, deploying everything as a WAR.

1.  Simplifying Code with Annotations
Java EE 6, like Java EE 5 before it, solves a lot of complexity and deployment issues through Java 1.5’s creation of annotations.  Adam shows a code example of a Stateless session bean is essentially just a POJO with the @Stateless annotation.  The only required import is “javax.ejb.Stateless” for the annotation to reference.

2.  Interceptors
Java EE 6 supports @Interceptors that redirect business methods for a better separation of POJOs and business logic.  Serves as a filter for business methods.

3.  Dependency Injection
The @Inject tag allows you create instances of related objects that live as long as the parent module.  Easy to create events and inject them into classes.

4.  Qualifiers
Adapted from Spring, helps control event ordering as far as which observer gets the message.  The annotation @Qualifier is used on the class level with a @Retention and @Target parameters.  The listener can then be updated to only listen to events of particular types or values.  Furthermore, they can be use for filtering on events which is useful for batch processing.

5.  Messaging
Java EE 6 supports @Asynchronous annotation for creating messaging handlers.

6.  Interfaces
Similar to previous versions of J2EE, you can define an interface for a session bean and use references to the interface instead of the concrete class.  Compiler throws an error if you try to inject an interface with no concrete implementation.

7.  J2EE and Spring
Adam recommends not mixing the two, and building entirely in Spring xor J2EE 5 or 6.  The features are very similar between the two and mixing them can be complicated.  One of the major new features of Java EE 6 is REST integration.

Conclusion
Adam did not present any slides, but built a quite impressive Java EE 6 application on the fly with very little code.  Everything he built deployed instanteously on his MacBook, and was accessible from the web browser right away.  Adam pointed out not all computers or operating systems build as quickly.

I asked Adam if he would be posting his code online since it was quite educational.  He said he will once he is back in Germany.

Live from TSSJS – Spring Messaging with Mark

I’m live blogging from “Message Driven Architecture with Spring” by Mark Fisher at TheServerSide Java Symposium.  Mark talks about how Spring messaging involved with the idea simple POJOs, similar to JMS.  Mark’s examples involve RabbitMQ, as one of the more recent and powerful messaging providers.  Below are some notes for this talk.

1.  AMQP
Mark introduces Advanced Message Queuing Protocol, AMQP for short, protocol as an alternative to standard JMS messaging but with a richer feature set including an advancing system of binding for filtering/routing of messages.  Also, AMQP templates are very similar to JMS templates.  Mark presented a number of slides with code examples of AMQP using the templates.

2.  MailSender
Mark presents MailSender, which can be used to send simple mail massages. 

3.  Lifecycle Management
The Spring SmartLifeCycle has been created in Spring 3.0 to help facilitate auto-start and graceful shutdown of listeners with the rest of the application.  This feature includes an optionaly isAutoStartup() attribute which can be used, for backwards compatibility and maintenance, to disable this feature.  By graceful shutdown, the components are stopped in the reverse order of startup, as needed.

4.  Task Execution
Create managed and pooled tasks that don’t need to be started manually (a big no-no)

5.  @Async
Async was added in Spring 3.0 that uses the @Async annotation for executor framework.

6.  Task Schedule
Built-in framework in Spring 3.0, no longer requires Quartz scheduling plugins and similar structures.  Supports recurring and cancelable tasks.  The @Scheduled(“*/5 * 9-17 * * ?”) annotation allows developers to build cron-like schedule tasks.  There is also an alternative solution to externalize the schedule information in XML so it’s tied to the code and the code does not require import statements for the annotations.  Finally, it supports meta-annotations that uses the @Scheduled annotation but points to XML reference in another location.

7.  Message Publishing Interceptor
Send message a by-product of another event.

Conclusion
Mark covered a large variety of material in a very short time, so I wasn’t able to write all the important points from his slides.  He did demonstrate a number of really useful features available in Spring 3.0 and showed how messaging in Spring has really grown to a more mature development platform.  He presentation was especially good because he managed to include demonstrations of the code alongside the spec information.

Message driven architecture with spring – the server side java symposium

Mark Fisher has a cool twitter handle @m_f_
Pardon the lack of formatting, there was just too much information to learn plus watch live demos and type lists on an ipad. I will clean it up over the weekend. [cleaned up] Too much content is a good “problem” for a presentation to have.

libraries:

  1. Spring Integration
  2. Spring AMQP in release candidate mode.  Similar to Spring JMS but doesn’t hide it behind JMS. AMQP is an open wireless protocol.  Also use you set up a routing exchange and set up a routing key for clients to bind to.  For example news.star.us so can subscribe to different levels on broker. works with non Java clients

Events

  • Has been in Spring since the very beginning
  • interface ApplicationEventPublisherAware allows you to make a callback
  • Interface ApplicationListener is called when event occurs, can use generics to say what type of events you care about
  • Can do without forcing to extend spring interface as well

Messaging

  • JmsTemplate – prefer CachingConnectionFactory to ensure sessions are reused
  • Jms:listener-container defined in xml on a pojo
  • Or use AmqpTemplate instead of JmsTemplate to use amqp, works the same as jmstemplate, for example you can call receive/send methods
  • Amqp namespace released this week to support listeners
  • Showed demo with rabbitmq including using web ui to see how many messages on queue
  • RestTemplate for http messaging, can use HttpMessageConverters if need to be more specific

Mail

  • SimpleMailMessage to create a message and inject MailSender to send it, good for simple messages, there is a mime one for more complex messsages
  • Core Spring cannot receive e-mail. Spring Integration can

Polling

  • Better to use events/messaging and only poll when you have to
  • Lifecycle interface – start(), stop() and isRunning()
  • SmartLifecycle does those three plus autostartup, phases and adds a callback on stop

Scheduling

  • taskScheduler interface lets you schedule one time tasks, tasks at a fixed rate and tasks at a fixed delay
  • Trigger interface lets you control the next execution time along with the Trigger Context that gives information about the last scheduled/actual time
  • This means you don’t need quartz for simple operations
  • The @Scheduled annotation lets you specify cron or fixed time/delay expressions instead of interfaces
  • Can also create your own annotations to externalize the cron expression so using your custom annotation without duplicating the configuration of the cron expression

Pipes and filters

  • Message can be any object as the payload with headers in a map, the message is immutable unless your payload itself is mutable
  • MessageChannel can do point to point or publish subscribe, set channel, types in xml using  and can also set whether async
  • A gateway can be one way or bidirectional
  • Can use @publisher to put the result of a method or argument to go on a channel as a side effect of calling the method
  • Filters return booleans
  • Router directs to proper channel name
  • Can route on payload type, header type etc
  • Can split/aggregate channels
  • Spring Integration 2.0 lets you use el to specify bindings, etc
  • Can chain when don’t need explicit channel names