Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3

Intro/Motivation

MyFaces CODI provides powerful scopes for JSF applications. The inspiration for most parts came from MyFaces Orchestra. If you already know MyFaces Orchestra you will be able to use the scopes provided by MyFaces CODI easily. Also the migration of existing applications shouldn't be that hard.

For users who aren't familiar with the conversation concepts provided by MyFaces Orchestra:
Orchestra allows to group beans within a (named) conversation. Furthermore, it's possible to use beans with different (conversation-) lifecycles in the same page. (Usually there isn't one big conversation - like the std. CDI conversation which is more like a bit smarter session for a window/tab.) Conversations are bound to a window/tab of a browser. That means you automatically get new conversations, if you open the application in a new tab/window. Conversations are started automatically as terminates as a conversation scoped bean is used. A conversation terminates based on a timeout (in case of the access scope - a conversation terminates as soon as it wasn't used by a page). Furthermore, it's possible to close (or restart) conversations manually. That means via an API call it's possible to terminate a conversation immediately. The next access leads to a new conversation. If the whole session gets destroyed, also all conversations will be destroyed automatically.

MyFaces CODI provides all these principles of MyFaces Orchestra in combination with typesafe improvements as well as the support of CDI mechanisms like qualifiers.

And why do we need new scopes? CDI already provides conversations. That's right and CDI is a great spec. and the EG did a nice job, however, (compared to the concepts of MyFaces Orchestra) CDI std. conversations are not that nice. So we saw the need to port a lot of the concepts provided by MyFaces Orchestra to CDI.

MyFaces CODI provides the following types of conversations:

  • @ConversationScoped (!= the std. CDI scope)
  • @WindowScoped
  • @ViewAccessScoped

Simple Usage

Conversation Scoped

Code Block
titleUsage of @ConversationScoped
borderStylesolid
@ConversationScoped
public class DemoBean1 implements Serializable
{
  public String getWelcomeText()
  {
    return "Hello MyFaces CODI!";
  }
}
Tip
titleThat's it!

As soon as you access the bean, the conversation gets started!

Note
titleHint

If you would like to use the bean within your JSF pages, you have to add @Named (javax.inject.Named ).

Code Block
titleInjecting conversation scoped beans
borderStylesolid
@Named
@ConversationScoped
public class DemoBean2 implements Serializable
{
  @Inject
  private DemoBean1 bean1;

  private String text;

  @PostConstruct
  protected void init()
  {
    text = this.bean1.getWelcomeText();
  }

  /*generated*/
  public String getText()
  {
    return text;
  }
}
Code Block
titleUsage of conversation scoped beans in JSF pages
borderStylesolid
#{demoBean2.text}
//e.g.: <h:outputText value="#{demoBean2.text}"/>
Note
titleBe Careful

If you are using MyFaces CODI you don't need std. CDI conversations any more. So it might be a good idea to exclude javax.enterprise.context.ConversationScoped and javax.enterprise.context.Conversation via IDE settings. If you are using those classes instead of the CODI classes, you will see different results.

@ConversationScoped means you are using a bean with a timeout (default: 30 minutes). As soon as a bean gets accessed, the bean is touched which leads to a reset of the timeout.

Tip
titleHint

You can see the default values in: org.apache.myfaces.extensions.cdi.javaee.jsf.api.ConfigParameter

Code Block
titleInjecting the current Conversation
borderStylesolid
@ConversationScoped
public class DemoBean3 implements Serializable
{
  @Inject
  private Conversation conversation;

  public void save()
  {
    //your custom logic
    //...

    this.conversation.close();

    //or use:
    //conversation.restart();
    //... if you know that you will need new instances of the same beans e.g. in the next view
    //#restart it's just a possible performance improvement
  }
}

If you don't like to close a conversation immediately, you will be able to terminate a conversation after the rendering process of the next view. This feature will be implemented soon. Currently you can use phase-observer-methods in combination with @View to do it manually.

Window Scope

The usage is exactly the same like @ConversationScoped . The only difference is that there is no timeout.
You can compare it to a session per window. However, as we will see later on it's possible to reset the window-context manually.

Code Block
titleUsage of @WindowScoped
borderStylesolid
@WindowScoped
public class DemoBean4 implements Serializable
{
}

View-Access Scope

The usage is exactly the same like @ConversationScoped . The only difference is that there is no timeout. If you don't use @ViewAccessScoped beans within a view, they won't be available any longer. That means: They get cleaned up automatically, as soon as you don't use them.

Code Block
titleUsage of @ViewAccessScoped
borderStylesolid
@ViewAccessScoped
public class DemoBean5 implements Serializable
{
}

Conversation Groups

MyFaces Orchestra introduced conversation names to group conversation scoped beans. Instead of names MyFaces CODI provides a typesafe alternative: conversation groups. A conversation group is a marker class or interface. Behind the scenes @ConversationGroup is a CDI qualifier which is treated in a special way. If you don't define a special group the class of the bean is used as conversation group. That means: every conversation scoped bean belongs to an explicit or implicit conversation group.

Code Block
titleUsage of @ConversationGroup
borderStylesolid
@ConversationScoped
@ConversationGroup(ConversationGroup1.class)
public class DemoBean6 implements Serializable
{
    @Inject
    private Conversation conversation;

    public void save()
    {
      //...

      this.conversation.close();
    }
}
Code Block
titleImplementation of ConversationGroup1
borderStylesolid
public interface ConversationGroup1
{
}
Code Block
titleA bean for different conversation groups
borderStylesolid
public class DemoBean7 implements Serializable
{
    @Produces
    @ConversationScoped
    @ConversationGroup(ConversationGroup1.class)
    public DemoBean7 createBeanForGroup1()
    {
        return new DemoBean7();
    }

    @Produces
    @ConversationScoped
    @ConversationGroup(ConversationGroup2.class)
    public DemoBean7 createBeanForGroup2()
    {
        return new DemoBean7();
    }
}
Note
titleBe Careful

In case of this.conversation.close(); the whole conversation of ConversationGroup1 will be destroyed (in this case 2 beans).

If you would like to close all conversations of an other conversation-group, you can use the WindowContext.

Code Block
titleClose a whole conversation group
borderStylesolid
public class DemoBean8
{
    @Inject
    private WindowContext windowContext;

    public void save()
    {
      //...

      this.windowContext.closeConversationGroup(ConversationGroup2.class);
    }
}