How to add programmatically RichFaces a4j:attachQueue
<p>I tried so hard but I cannot get a simple Richfaces a4j:attachQueue programmatically inserted in my page. I generate the RichFaces components from Java 
code, not xhtml.</p>

<p>I have some problems with the output of the data. Every request should be queued and fired after 2 seconds.If there are more than one request, only the 
last request should be fired, the other request should be queued and destroyed 
</p>

<p>I want that, if the User selects an item of the DropDownMenu 1 ( see method  getSelectOneMenu1() ) and than select an item of the DropDownMenu 2 
( see method  getSelectOneMenu2() ), the first request should be queued and destroyed, RichFace should show only the data of the last request. 
For example , the user select Quinie of the DropDownMenu 1  and then selects Pierrette of the DropDownMenu 2, only Pierrette should be showed , 
not Quinie and then after 2 seconds Pierrette. At the moment i receive two names from the same queue  Quinie and after 2 seconds Pierrette.
That is not what i,ve expected, because i,ve implemented UIAttachQueue and uiAttachQueue.setRequestGroupingId("mygroupingId"); 
My big problem ,i don,t know how to attach programmatically a4j:attachQueue to a4j:ajax as child .I want to generate programmatically something
like this, with java not with xhml.</p>

<pre><code>&lt;h:selectOneMenu value="#{queueBean.username}"&gt;
                &lt;f:selectItem itemValue="Quinie" itemLabel="Quinie" /&gt;
                &lt;f:selectItem itemValue="William" itemLabel="William" /&gt;
                &lt;f:selectItem itemValue="Aron" itemLabel="Aron" /&gt;
                &lt;f:selectItem itemValue="Faith" itemLabel="Faith" /&gt;
                &lt;f:selectItem itemValue="Divine" itemLabel="Divine" /&gt;                                      
                &lt;a4j:ajax event="change" execute="@this"    
                listener="#{queueBean.selectComponents}" render="rep"&gt;
                  &lt;a4j:attachQueue requestGroupingId="group" name="myQueue"/&gt;
                &lt;/a4j:ajax&gt;                                                          
            &lt;/h:selectOneMenu&gt; 
</code></pre>

<p>Please thake a look of my Java code .</p>

<p>Any help will be appreciated!</p>

<p>cheers,</p>

<p>Marco</p>

<p>That's what I do:</p>

<p>queuedemo.xhtml</p>

<pre><code>&lt;h2&gt;JSF and RichFaces Queue&lt;/h2&gt;

&lt;a4j:queue id="myQueue" name="myQueue" requestDelay="2000" ignoreDupResponses="true"&gt;&lt;/a4j:queue&gt;

&lt;h:form id="form_id"&gt;       

    &lt;rich:panel&gt;

      &lt;f:facet name="header"&gt;

          Programmatic RichFaces components

        &lt;/f:facet&gt;

        &lt;h:panelGrid binding="#{queueBean.panelGrid}" &gt;&lt;/h:panelGrid&gt;

        &lt;rich:panel&gt;         

            &lt;a4j:outputPanel id="outputPanel_id" ajaxRendered="false" layout="block"&gt;

            &lt;h:outputText binding="#{queueBean.outputText}" /&gt;

        &lt;/a4j:outputPanel&gt;           

        &lt;/rich:panel&gt;

    &lt;/rich:panel&gt;      
</code></pre>

<p></p>

<p>Here is my ManagedBean</p>

<p>@ManagedBean(name = "queueBean")
@RequestScoped</p>

<pre><code>public class MyQueueBean {
private String          username;
private String          firstname;
private String          lastname;
private List&lt;String&gt;    renderComponents;
private HtmlPanelGrid   panelGrid   = null;
private HtmlOutputText  outputText;

@PostConstruct
public void init() {
    this.renderComponents = new ArrayList&lt;String&gt;();
}

private Application application = null;

private Application getApplication() {
    if (null == this.application) {
        this.application = FacesContext.getCurrentInstance().getApplication();
    }
    return this.application;
}

@SuppressWarnings("unchecked")
private &lt;T&gt; T createComponent(String componentType) {
    T component = (T) getApplication().createComponent(componentType);
    return component;
}

private ELContext getELContext() {
    return FacesContext.getCurrentInstance().getELContext();
}

private ExpressionFactory getExpressionFactory() {
    return getApplication().getExpressionFactory();
}

private ValueExpression createValueExpression(String binding, Class&lt;?&gt; expectedType) {
    return getExpressionFactory().createValueExpression(getELContext(), binding, expectedType);
}

public AjaxBehavior createAjaxBehavior(String expression, List&lt;String&gt; rederList, List&lt;String&gt; executeList) {
    AjaxBehavior ajaxBehavior = (AjaxBehavior) getApplication().createBehavior(AjaxBehavior.BEHAVIOR_ID);
    if (StringUtils.isNotEmpty(expression)) {
        ajaxBehavior.addAjaxBehaviorListener(createAjaxBehaviorListener(expression));
    }
    if (CollectionUtils.isNotEmpty(executeList)) {
        ajaxBehavior.setExecute(executeList);
    }
    if (CollectionUtils.isNotEmpty(rederList)) {
        ajaxBehavior.setRender(rederList);
    }
    return ajaxBehavior;
}

private MethodExpression createMethodExpression(String expression, Class&lt;?&gt; expectedReturnType,
        Class&lt;?&gt;[] expectedParamTypes) {
    return getExpressionFactory().createMethodExpression(getELContext(), expression, expectedReturnType,
            expectedParamTypes);
}

private MethodExpressionAjaxBehaviorListener createAjaxBehaviorListener(String expression) {
    return new MethodExpressionAjaxBehaviorListener(createMethodExpression(expression, Void.class,
            new Class[] { AjaxBehaviorEvent.class }));
}

public HtmlPanelGrid getPanelGrid() {
    if (null == this.panelGrid) {
        this.panelGrid = createComponent(HtmlPanelGrid.COMPONENT_TYPE);
        this.panelGrid.setId("panel_id");
        this.panelGrid.setColumns(3);
        this.panelGrid.getChildren().add(getSelectOneMenu1());
        this.panelGrid.getChildren().add(getSelectOneMenu2());
        this.panelGrid.getChildren().add(getSelectOneMenu3());
    }
    return this.panelGrid;
}

public void setPanelGrid(HtmlPanelGrid panelGrid) {
    this.panelGrid = panelGrid;
}

private UIAttachQueue createUIAttachQueue() {
    UIAttachQueue uiAttachQueue = createComponent(UIAttachQueue.COMPONENT_TYPE);
    uiAttachQueue.setName("myQueue");
    uiAttachQueue.setRequestGroupingId("mygroupingId");
    return uiAttachQueue;
}

private HtmlSelectOneMenu getSelectOneMenu1() {
    HtmlSelectOneMenu selectOneMenu1 = createComponent(HtmlSelectOneMenu.COMPONENT_TYPE);
    selectOneMenu1.setId("select_1");
    UISelectItems selectItems = createUISelectItemsComponent(createFirstItems());
    selectOneMenu1.getChildren().add(selectItems);
    selectOneMenu1.setValueExpression("value", createValueExpression("#{queueBean.username}", String.class));
    UIAttachQueue uiAttachQueue = createUIAttachQueue();
    uiAttachQueue.setId("uiqueueId1");
    List&lt;String&gt; idsToBeRendered = new ArrayList&lt;String&gt;();
    idsToBeRendered.add("outputPanel_id");
    AjaxBehavior ajaxBehavior = createAjaxBehavior("#{queueBean.selectComponents}", idsToBeRendered,
            Arrays.asList("@this"));
    ajaxBehavior.setQueueId(uiAttachQueue.getName());
    uiAttachQueue.associateWith(ajaxBehavior);
    selectOneMenu1.addClientBehavior("change", ajaxBehavior);
    uiAttachQueue.associateWith(selectOneMenu1);
    return selectOneMenu1;
}

private HtmlSelectOneMenu getSelectOneMenu2() {
    HtmlSelectOneMenu selectOneMenu2 = createComponent(HtmlSelectOneMenu.COMPONENT_TYPE);
    selectOneMenu2.setId("select_2");
    UISelectItems selectItems = createUISelectItemsComponent(createSecondItems());
    selectOneMenu2.getChildren().add(selectItems);
    selectOneMenu2.setValueExpression("value", createValueExpression("#{queueBean.username}", String.class));
    UIAttachQueue uiAttachQueue = createUIAttachQueue();
    uiAttachQueue.setId("uiqueueId2");
    List&lt;String&gt; idsToBeRendered = new ArrayList&lt;String&gt;();
    idsToBeRendered.add("outputPanel_id");
    AjaxBehavior ajaxBehavior = createAjaxBehavior("#{queueBean.selectComponents}", idsToBeRendered,
            Arrays.asList("@this"));
    ajaxBehavior.setQueueId(uiAttachQueue.getName());
    uiAttachQueue.associateWith(ajaxBehavior);
    selectOneMenu2.addClientBehavior("change", ajaxBehavior);
    uiAttachQueue.associateWith(selectOneMenu2);
    return selectOneMenu2;
}

private HtmlSelectOneMenu getSelectOneMenu3() {
    HtmlSelectOneMenu selectOneMenu3 = createComponent(HtmlSelectOneMenu.COMPONENT_TYPE);
    selectOneMenu3.setId("select_3");
    UISelectItems selectItems = createUISelectItemsComponent(createThirdItems());
    selectOneMenu3.getChildren().add(selectItems);
    selectOneMenu3.setValueExpression("value", createValueExpression("#{queueBean.username}", String.class));
    UIAttachQueue uiAttachQueue = createUIAttachQueue();
    uiAttachQueue.setId("uiqueueId3");
    List&lt;String&gt; idsToBeRendered = new ArrayList&lt;String&gt;();
    idsToBeRendered.add("outputPanel_id");
    AjaxBehavior ajaxBehavior = createAjaxBehavior("#{queueBean.selectComponents}", idsToBeRendered,
            Arrays.asList("@this"));
    ajaxBehavior.setQueueId(uiAttachQueue.getName());
    selectOneMenu3.addClientBehavior("change", ajaxBehavior);
    uiAttachQueue.associateWith(ajaxBehavior);
    uiAttachQueue.associateWith(selectOneMenu3);
    return selectOneMenu3;
}

public HtmlOutputText getOutputText() {
    if (this.outputText == null) {
        this.outputText = createComponent(HtmlOutputText.COMPONENT_TYPE);
        this.outputText.setId("output_id");
        this.outputText.setValueExpression("value",
                createValueExpression("Selected Name : #{queueBean.username}", String.class));
    }
    return this.outputText;
}

public void setOutputText(HtmlOutputText outputText) {
    this.outputText = outputText;
}

private List&lt;SelectItem&gt; createFirstItems() {
    FacesContext.getCurrentInstance();
    List&lt;SelectItem&gt; asList = Arrays.asList(new SelectItem("Quinie", "Quinie"),
            new SelectItem("William", "William"), new SelectItem("Aron", "Aron"));
    return asList;
}

private List&lt;SelectItem&gt; createSecondItems() {
    List&lt;SelectItem&gt; asList = Arrays.asList(new SelectItem("Pierrette", "Pierrette"), new SelectItem("Biblias",
            "Biblias"), new SelectItem("Pauline", "Pauline"));
    return asList;
}

private List&lt;SelectItem&gt; createThirdItems() {
    List&lt;SelectItem&gt; asList = Arrays.asList(new SelectItem("Durand", "Durand"), new SelectItem("Igor", "Igor"),
            new SelectItem("Nathalie", "Nathalie"));
    return asList;
}

private &lt;U&gt; UISelectItems createUISelectItemsComponent(List&lt;U&gt; list) {
    UISelectItems items = new UISelectItems();
    items.setId("id_" + String.valueOf(new Random().nextInt(1000)));
    items.setValue(list);
    return items;
}

public void selectComponents(AjaxBehaviorEvent event) {
    this.renderComponents.add(this.username);
}

public String getUsername() {
    return this.username;
}

public void setUsername(String username) {
    this.username = username;
}

public List&lt;String&gt; getRenderComponents() {
    return this.renderComponents;
}

public void setRenderComponents(List&lt;String&gt; renderComponents) {
    this.renderComponents = renderComponents;
}

public String getFirstname() {
    return this.firstname;
}

public void setFirstname(String firstname) {
    this.firstname = firstname;
}

public String getLastname() {
    return this.lastname;
}

public void setLastname(String lastname) {
    this.lastname = lastname;
}
</code></pre>

<p>}</p>
