Friday, August 24, 2012

Get bean instance in CDI

In some cases it's required to get the bean instance instead of the reference.
You can simply do it with the following method:

public static <T> T getBeanInstance(final Class<T> type, final Class<? extends Annotation> scope, final BeanManager beanManager)
{
   final Context context = beanManager.getContext(scope);
   final Set<<Bean<?>> beans = beanManager.getBeans(type);
   final Bean<T> bean = (Bean<T>) beanManager.resolve(beans);
   final CreationalContext<T> creationalContext = beanManager.createCreationalContext(bean);

   return context.get(bean, creationalContext);
}

Tuesday, August 21, 2012

Increase your JSF application performance (Part 1 - Environment & Configuration)

  • Always use the newest MyFaces version (As you can read here: Blog Entry)
  • If you just need a plain servlet container, use Tomcat instead of Jetty
  • Use JUEL as EL implementation 
    • Add the newest JUEL API + implementation as depedency
    • Configure MyFaces to use JUEL:
    • <context-param>
              <param-name>org.apache.myfaces.EXPRESSION_FACTORY</param-name>
              <param-value>de.odysseus.el.ExpressionFactoryImpl</param-value>
      </context-param>
  • Increase expression cache in JUEL
    • Create src/main/resources/el.properties
    • Add property javax.el.cacheSize with a custom size. The default size is 1000. In my application i use a size of 3000.
  • If you use CDI, consider to use OpenWebBeans as implementation and configure this in your web.xml:
  • <context-param>
        <param-name>org.apache.myfaces.EL_RESOLVER_COMPARATOR</param-name>
        <param-value>org.apache.myfaces.el.unified.OpenWebBeansELResolverComparator</param-value>
    </context-param>
  • Enable MyFaces EL caching as described in MyFaces Wiki
  • Disable JSP support in MyFaces:
  • <context-param>
        <param-name>org.apache.myfaces.SUPPORT_JSP_AND_FACES_EL</param-name>
        <param-value>false</param-value>
    </context-param>
    Attention: If you set this, you need to provide the "org.apache.myfaces.EXPRESSION_FACTORY" parameter.
  • Other params to increase performance:
  • <context-param>
        <param-name>javax.faces.PROJECT_STAGE</param-name>
        <param-value>Production</param-value>
    </context-param>
    <context-param>
        <param-name>javax.faces.FACELETS_REFRESH_PERIOD</param-name>
        <param-value>-1</param-value>
    </context-param>
    <context-param>
        <param-name>org.apache.myfaces.CHECK_ID_PRODUCTION_MODE</param-name>
        <param-value>false</param-value>
    </context-param>
    <context-param>
        <param-name>org.apache.myfaces.VIEW_UNIQUE_IDS_CACHE_ENABLED</param-name>
        <param-value>true</param-value>
    </context-param>
    <context-param>
        <param-name>org.apache.myfaces.SAVE_STATE_WITH_VISIT_TREE_ON_PSS</param-name>
        <param-value>false</param-value>
    </context-param>
    
  • Configure state management as described in MyFaces Wiki
  • Use a custom ServletFilter to set the correct expires/cache headers of your resources (images, stylesheets, javascripts)
  • Compress and optimize your Javascripts in your build process. If you use maven, try primefaces-extensions' closure compiler maven plugin

Monday, August 20, 2012

Enable failover with OpenWebBeans (> 1.1.3)

With Tomcat 7.0.22, some changes has be done when

    ServletRequestListener
           #requestDestroyed(javax.servlet.ServletRequestEvent)

will be called. This caused that session replication was done before OpenWebBeans prepared the beans for the failover.

Therefore a new ServletFilter, which prepares the bean failover before session replication, was added in OpenWebBeans 1.1.4.

Just add it as FIRST filter in your web.xml:

org.apache.webbeans.web.failover.FailOverFilter

and map it for example to the FacesServlet.

Also don't forget the src/main/resources/META-INF/openwebbeans/openwebbeans.properties with following entries:

    configuration.ordinal=100
    org.apache.webbeans.web.failover.issupportfailover=true
    org.apache.webbeans.web.failover.issupportpassivation=true


Update for OWB 1.2.0 -> Click