Archive for the ‘Spring’ Category

Flex, BlazeDS, ActiveMQ, Spring application context gotchas.

Thursday, April 15th, 2010

In one of my current projects I have been fighting a lot getting BlazeDS, ActiveMQ and Spring to dance together.

We are using the Spring BlazeDS integration project to connect our Flex frontend with our Java backend.

The project uses remoting AMF and messaging in the communication and the backend consists of three independent Java applications.

One of the toughest things is configuring ActiveMQ to work with BlazeDS.

Lately I had to build a session listener, that detects when users are logging in and logging out. This proved extra hard, as the the session listener is configured in the web.xml and doesn’t have access to the beans in the application context directly.

Inside the session listener I need to get the application context, to get the beans from the Spring context.

Unfortunately with ActiveMQ and BlazeDS, we need a separate context file, than the default applicationContext.xml, else there will be problems with the Flex message broker and ActiveMQ.

I solved this by getting the messaging context, instead of the application context inside the session listener. Here is an example:

public void attributeAdded(HttpSessionBindingEvent e)
{
  logger.info("Attribute added to session: " + e.getSession().getId());
e.getSession();
ServletContext servletContext = session.getServletContext();
WebApplicationContext appContext = (WebApplicationContext) servletContext.getAttribute(WEB_APPLICATION_CONTEXT_MESSAGING);
UserBrokerAccountManager userBrokerAccountManager = (UserBrokerAccountManager) appContext.getBean(USER_BROKER_ACCOUNT_MANAGER_BEAN_ID);
userBrokerAccountManager.doSomething();
}

Basically this shows how to get access to specific beans inside a specific application context.

Another useful method is getting all contexts from the servlet context. Here is a method to do exactly that:

/**
* Show servlet attributes.
*
* @param servletContext the servlet context
*/
@SuppressWarnings({ "unused", "unchecked" })
private void showServletAttributes(ServletContext servletContext)
{
  Enumeration<String> en = servletContext.getAttributeNames();
while (en.hasMoreElements())
  {
    logger.info("Servlet attribute - Enum: " + en.nextElement());
  }
}

Post to Twitter