Search in sources :

Example 6 with Conversation

use of javax.enterprise.context.Conversation in project core by weld.

the class NaiveClusterTest method testConversationReplication.

@Test(description = "A simple test to check conversation replication")
public void testConversationReplication() throws Exception {
    TestContainer container1 = bootstrapContainer(1, Collections.singletonList(Baz.class));
    BeanManagerImpl beanManager1 = getBeanManager(container1);
    TestContainer container2 = bootstrapContainer(2, Collections.singletonList(Baz.class));
    BeanManagerImpl beanManager2 = getBeanManager(container2);
    use(1);
    // Set up the conversation context
    BoundRequest request1 = new BoundRequestImpl(container1.getSessionStore());
    BoundConversationContext conversationContext1 = Utils.getReference(beanManager1, BoundConversationContext.class);
    conversationContext1.associate(request1);
    conversationContext1.activate();
    // Set a value into Baz1
    Baz baz1 = Utils.getReference(beanManager1, Baz.class);
    baz1.setName("pete");
    // Begin the conversation
    Conversation conversation1 = Utils.getReference(beanManager1, Conversation.class);
    conversation1.begin();
    // refetch the test bean and check it has the right value
    baz1 = Utils.getReference(beanManager1, Baz.class);
    assert baz1.getName().equals("pete");
    // Simulate ending the request (from the POV of the conversation only!)
    assert !conversation1.isTransient();
    String cid = conversation1.getId();
    conversationContext1.invalidate();
    conversationContext1.deactivate();
    conversationContext1.dissociate(request1);
    // and start another, propagating the conversation
    request1 = new BoundRequestImpl(container1.getSessionStore());
    conversationContext1.associate(request1);
    conversationContext1.activate(cid);
    // refetch the test bean and check it has the right value
    baz1 = Utils.getReference(beanManager1, Baz.class);
    assert baz1.getName().equals("pete");
    assert !conversation1.isTransient();
    replicateSession(1, container1, 2, container2);
    use(2);
    // Set up the conversation context
    BoundRequest request2 = new BoundRequestImpl(container2.getSessionStore());
    BoundConversationContext conversationContext2 = Utils.getReference(beanManager2, BoundConversationContext.class);
    conversationContext2.associate(request2);
    conversationContext2.activate(cid);
    Baz baz2 = Utils.getReference(beanManager2, Baz.class);
    assert baz2.getName().equals("pete");
    Conversation conversation2 = Utils.getReference(beanManager2, Conversation.class);
    assert !conversation2.isTransient();
    use(2);
    container2.stopContainer();
    use(1);
    container1.stopContainer();
}
Also used : TestContainer(org.jboss.arquillian.container.weld.embedded.mock.TestContainer) BeanManagerImpl(org.jboss.weld.manager.BeanManagerImpl) BoundConversationContext(org.jboss.weld.context.bound.BoundConversationContext) BoundRequest(org.jboss.weld.context.bound.BoundRequest) Conversation(javax.enterprise.context.Conversation) Test(org.testng.annotations.Test) AbstractClusterTest(org.jboss.weld.mock.cluster.AbstractClusterTest)

Example 7 with Conversation

use of javax.enterprise.context.Conversation in project wicket by apache.

the class ConversationPropagator method onRequestHandlerResolved.

@Override
public void onRequestHandlerResolved(RequestCycle cycle, IRequestHandler handler) {
    String cid = cycle.getRequest().getRequestParameters().getParameterValue(CID).toString();
    Page page = getPage(handler);
    if (cid == null && page != null) {
        cid = page.getMetaData(CID_KEY);
    }
    Conversation current = getConversation(cycle);
    if (current != null && !Objects.isEqual(current.getId(), cid)) {
        logger.info("Conversation {} has expired for {}", cid, page);
        throw new ConversationExpiredException(null, cid, page, handler);
    }
    activateConversationIfNeeded(cycle, handler, cid);
}
Also used : Page(org.apache.wicket.Page) IRequestablePage(org.apache.wicket.request.component.IRequestablePage) Conversation(javax.enterprise.context.Conversation)

Example 8 with Conversation

use of javax.enterprise.context.Conversation in project wicket by apache.

the class ConversationPropagator method activateConversationIfNeeded.

private void activateConversationIfNeeded(RequestCycle cycle, IRequestHandler handler, String cid) {
    Conversation current = getConversation(cycle);
    if (current != null || !activateForHandler(handler)) {
        return;
    }
    logger.debug("Activating conversation {}", cid);
    try {
        container.activateConversationalContext(cycle, cid);
        fireOnAfterConversationStarted(cycle);
    } catch (NonexistentConversationException e) {
        logger.info("Unable to restore conversation with id {}", cid, e.getMessage());
        logger.debug("Unable to restore conversation", e);
        fireOnAfterConversationStarted(cycle);
        throw new ConversationExpiredException(e, cid, getPage(handler), handler);
    }
    cycle.setMetaData(CONVERSATION_STARTED_KEY, true);
}
Also used : NonexistentConversationException(javax.enterprise.context.NonexistentConversationException) Conversation(javax.enterprise.context.Conversation)

Example 9 with Conversation

use of javax.enterprise.context.Conversation in project wicket by apache.

the class ConversationPropagator method onRequestHandlerExecuted.

@Override
public void onRequestHandlerExecuted(RequestCycle cycle, IRequestHandler handler) {
    Conversation conversation = getConversation(cycle);
    if (conversation == null) {
        return;
    }
    Page page = getPage(handler);
    if (page == null) {
        return;
    }
    // apply auto semantics
    autoEndIfNecessary(page, handler, conversation);
    autoBeginIfNecessary(page, handler, conversation);
    if (propagation.propagatesViaPage(page, handler)) {
        // propagate a conversation across non-bookmarkable page instances
        setConversationOnPage(conversation, page);
    }
}
Also used : Conversation(javax.enterprise.context.Conversation) Page(org.apache.wicket.Page) IRequestablePage(org.apache.wicket.request.component.IRequestablePage)

Aggregations

Conversation (javax.enterprise.context.Conversation)9 Page (org.apache.wicket.Page)3 IRequestablePage (org.apache.wicket.request.component.IRequestablePage)3 BeanManagerImpl (org.jboss.weld.manager.BeanManagerImpl)2 ContextNotActiveException (javax.enterprise.context.ContextNotActiveException)1 NonexistentConversationException (javax.enterprise.context.NonexistentConversationException)1 BeanManager (javax.enterprise.inject.spi.BeanManager)1 FacesContext (javax.faces.context.FacesContext)1 InitialContext (javax.naming.InitialContext)1 NamingException (javax.naming.NamingException)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 HttpServletResponseWrapper (javax.servlet.http.HttpServletResponseWrapper)1 IRequestCycleListener (org.apache.wicket.request.cycle.IRequestCycleListener)1 ResourceReferenceRequestHandler (org.apache.wicket.request.handler.resource.ResourceReferenceRequestHandler)1 PageParameters (org.apache.wicket.request.mapper.parameter.PageParameters)1 PackageResourceReference (org.apache.wicket.request.resource.PackageResourceReference)1 TestContainer (org.jboss.arquillian.container.weld.embedded.mock.TestContainer)1 ConversationContext (org.jboss.weld.context.ConversationContext)1 BoundConversationContext (org.jboss.weld.context.bound.BoundConversationContext)1 BoundRequest (org.jboss.weld.context.bound.BoundRequest)1