Improving XPages Application Performance with Managed Beans (Russell Maher, The View Advanced Xpages 2012)

Introduction

This post is part of my road to more Java in my XPages projects. The next presentation was held at The View – Advanced XPages workshop early 2012 in Amsterdam. I am a bit reserved what I can and cannot share. The presentation is not on-line available so I will re-create all screenshots. Hopefully this abstract won’t trigger someone from their legal department to contact me =)

Main topics:

  • Java Design element.
  • Managed beans.

Java design element

This design element is new in 8.5.3 and makes using custom Java much easier in XPage application:

  • Your class will be added automatically to the project build path.
  • It allows you to work with Java without going via the Package Explorer view in DDE.

What allows this Java design element? You can create:

  • A class or package of classes.
  • Interface; a set of required methods in order to “implement”.
  • Enum; set of constants in Java.

Java class

  • A POJO (plain old java object) is created.
  • Representing a single Java class.
  • Multiple Java classes can be packaged together.

Some rules:

  • Package name and class name need to be defined during creation.
    • Package name typically are domain names in reverse:
      • e.g. com.wordpress.bildr
        • Will be displayed in DDE as com/wordpress/bildr/classname.java

The result in DDE (Code\Java view):

The result in the Java editor (DDE):

The result in the Package Explorer Eclipse view:

How do you call the Java class?

  • From any server-side JavaScript.
  • From other Java classes within your application.

Example calling one class from another

SecondClass that returns a string:

package com.wordpress.abc;

public class SecondClass {

public String printMe(){
return “hello world”;
}
}

FirstClass that creates a SecondClass object and calls the printMe() method in it:

package com.wordpress.abc;

public class FirstClass {

public String callMe(){
SecondClass sc = new SecondClass();
return sc.printMe();

}
}

Server-side JavaScript that creates and uses a FirstClass object:

Output

You can create output to log.nsf via:

  • print(“”)
  • System.out.println(“”)

Java Interface

An interface defines a set of methods that can be “implemented” by Java classes. You can structure your code more via an interface.

Java Enum

An Enum defines one or more constants that you can use in your Java code.

  • declared public
  • defines constants
  • declares a private constructor
  • declares any public methods

Java Bean

A standalone piece of Java code constructed in a way that it can easily be consumed by other Java code.

Rules

  • A bean is a Java class with a no-argument constructor:
    • The constructor is the method with the same name as the class.
  • A bean usually has some properties for the class itself.
  • “getters/setters” are used to access bean properties.
  • Getters/setters are named to match their associated properties
    • These methods must match.
      • Example: If the property called myAge, the method must be called getMyAge.

Managed Beans

A managed bean is a Java bean but is not associated with a visual control.

A managed bean follows the standard bean requirements:

  • No-argument constructor.
  • Properties exposed via public getter / setters.

A managed bean needs to be on the build path of your application. The Java design element takes care of this. Alternatively go via the Package Explorer and create a source
folder in the project to add code there.

You need to register a managed bean to make it well-known within your application. You do this in the faces-config.xml file.

Configuration settings

  • Name:
    • Reference to the bean from other code.
  • Class:
    • Java class of the bean.
  • Scope:
    • Application, session, view, request or none.

<managed-bean>
<managed-bean-name>carBean</managed-bean-name>
<managed-bean-class>com.wordpress.myCar</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>

When the bean has been registered you can use it from different places.

From other Java code

myCar mc=new carBean();

From server-side JavaScript

carBean.buildFromField(currentDocument.getDocument())

Via Expression Language

#{carBean.isSporty}

Serialization

The ability to store the state of a Java object for later use. Serialization can improve the performance of Java. It is therefore recommended that beans implement Serialization.

Screenshots

sessionMap

The sessionMap object can be used to pass data between managed beans.  You can put data into and read out the sessionMap object.

Typical scenario:

  • Bean X puts data into the map.
  • Bean Y reads the data from the map.

Methods:

Bean injection

When one managed bean is a managed property value of another managed bean, the managed bean instantiates itself.

<managed-bean>
<managed-bean-name>carBean</managed-bean-name>
<managed-bean-class>com.wordpress.myCar</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
<managed-property>
<property-name>imagesDB</property-name>
<value>#configBean</value>
</managed-property>
</managed-bean>

Scope

You need to make sure the injected bean lasts at least as long as the dependent bean so they accommodate each other!

Summary

This post is just a small abstract of the presentation so if you want a clear understanding of the topic I recommend everyone to participate in an XPages bootcamp organized by The View. I am sure that in 2013 they will visit the old continent another time.

The presentation brings me a step further into using Java in XPages. Especially the managed beans section is well explained and some items are beyond the first steps I will take implementing Java.

You can start using Per Hendrik Lausten’s blog post as a starter to get more into managed beans. Jeremy Hodge also covers the topic here.

So far the presentations learned me about Java fundamentals, the Java design element, the Package Explorer view in DDE, working with Managed Beans and importing third party libraries.

The next step for me would be either to look more into the technology or to start looking at some real world code examples. I found one example I like: Log data changes using beans and the DataObject interface.

Advertisements

2 thoughts on “Improving XPages Application Performance with Managed Beans (Russell Maher, The View Advanced Xpages 2012)

  1. dif1754 2012-October-31 / 4:36 pm

    Thanks for sharing.

Comments are closed.