Package de.javakaffee.web.msm.MemcachedSessionService

Examples of de.javakaffee.web.msm.MemcachedSessionService.SessionManager


        tomcat.addUser(USER_NAME,PASSWORD);
        tomcat.addRole(USER_NAME,ROLE_NAME);

        // brings logging.properties
        tomcat.setBaseDir(docBase);
        final SessionManager sessionManager = createSessionManager();


        Context context;
        try {
            context = tomcat.addWebapp(CONTEXT_PATH, docBase + fileSeparator + "webapp");
        } catch (final ServletException e) {
            throw new IllegalStateException(e);
        }

        context.setManager( sessionManager );
        context.setBackgroundProcessorDelay( 1 );
        context.setCookies(cookies);

        if ( loginType != null ) {
            context.addConstraint( createSecurityConstraint( "/*", ROLE_NAME ) );
            context.addSecurityRole( ROLE_NAME );
            final LoginConfig loginConfig = loginType == LoginType.FORM
                    ? new LoginConfig( "FORM", null, "/login", "/error" )
                    : new LoginConfig( "BASIC", null, null, null );
            context.setLoginConfig( loginConfig );
        }

        /* we must set the maxInactiveInterval after the context,
         * as setContainer(context) uses the session timeout set on the context
         */
        sessionManager.getMemcachedSessionService().setMemcachedNodes( memcachedNodes );
        sessionManager.getMemcachedSessionService().setFailoverNodes( failoverNodes );
        sessionManager.getMemcachedSessionService().setEnabled(enabled);
        sessionManager.getMemcachedSessionService().setSticky(sticky);
        if(lockingMode != null) {
            sessionManager.getMemcachedSessionService().setLockingMode(lockingMode.name());
        }
        sessionManager.getMemcachedSessionService().setMemcachedProtocol(memcachedProtocol);
        sessionManager.getMemcachedSessionService().setUsername(username);
        sessionManager.setMaxInactiveInterval( sessionTimeout ); // 1 second
        sessionManager.getMemcachedSessionService().setSessionBackupAsync( false );
        sessionManager.getMemcachedSessionService().setSessionBackupTimeout( 100 );
        sessionManager.setProcessExpiresFrequency( 1 ); // 1 second (factor for context.setBackgroundProcessorDelay)
        sessionManager.getMemcachedSessionService().setTranscoderFactoryClass( transcoderFactoryClassName != null ? transcoderFactoryClassName : DEFAULT_TRANSCODER_FACTORY );
        sessionManager.getMemcachedSessionService().setRequestUriIgnorePattern(".*\\.(png|gif|jpg|css|js|ico)$");
        sessionManager.getMemcachedSessionService().setStorageKeyPrefix(storageKeyPrefix);

        return tomcat;
    }
View Full Code Here


    }

    @Test( enabled = true )
    public void testDeserializeHibernateCollection() {

        final SessionManager manager = createSessionManager();
        manager.setContainer( createContext() );

        final Set<Animal> animals = new HashSet<Animal>( Arrays.asList( new Animal( "cat" ) ) );
        final Person person = new Person( "foo bar", animals );

        final Long personId = createPerson( person );
View Full Code Here

        }
    }

    @Nonnull
    protected SessionManager createSessionManager() {
        final SessionManager manager = mock( SessionManager.class );
        when( manager.getContainer() ).thenReturn( new StandardContext() ); // needed for createSession
        when( manager.getMemcachedSessionService() ).thenReturn(newMemcachedSessionService(manager));
        when( manager.newMemcachedBackupSession() ).thenReturn( new MemcachedBackupSession( manager ) );
        return manager;
    }
View Full Code Here

    private void setStickyness(final SessionAffinityMode sessionAffinity) {
        if(!sessionAffinity.isSticky()) {
            _tomcat1.getEngine().setJvmRoute(null);
        }
        final SessionManager manager = _tomcat1.getManager();
        manager.setSticky( sessionAffinity.isSticky() );

        try {
            waitForReconnect(manager.getMemcachedSessionService().getMemcached(), 1, 500);
        } catch (final InterruptedException e) {
            Thread.currentThread().interrupt();
            throw new RuntimeException(e);
        }
    }
View Full Code Here

     * @throws Exception if something goes wrong with the http communication with tomcat
     */
    @Test( enabled = true, dataProviderClass = TestUtils.class, dataProvider = STICKYNESS_PROVIDER )
    public void testExpirationOfSessionsInMemcachedIfBackupWasSkippedSimple( final SessionAffinityMode stickyness ) throws Exception {

        final SessionManager manager = _tomcat1.getManager();
        setStickyness(stickyness);

        // set to 1 sec above (in setup), default is 10 seconds
        final int delay = manager.getContainer().getBackgroundProcessorDelay();
        manager.setMaxInactiveInterval( delay * 4 );

        final String sessionId1 = makeRequest( _httpClient, _portTomcat1, null );
        assertNotNull( sessionId1, "No session created." );
        assertNotNull( _memcached.get( sessionId1 ), "Session not available in memcached." );

View Full Code Here

     * @throws Exception if something goes wrong with the http communication with tomcat
     */
    @Test( enabled = true, dataProviderClass = TestUtils.class, dataProvider = STICKYNESS_PROVIDER )
    public void testExpirationOfSessionsInMemcachedIfBackupWasSkippedManyReadonlyRequests( final SessionAffinityMode stickyness ) throws Exception {

        final SessionManager manager = _tomcat1.getManager();
        setStickyness(stickyness);

        // set to 1 sec above (in setup), default is 10 seconds
        final int delay = manager.getContainer().getBackgroundProcessorDelay();
        manager.setMaxInactiveInterval( delay * 4 );

        final String sessionId1 = makeRequest( _httpClient, _portTomcat1, null );
        assertNotNull( sessionId1, "No session created." );
        assertWaitingWithProxy(Predicates.<MemcachedClientIF> notNull(), 200l, _memcached).get( sessionId1 );

        /* after 3 seconds make another request without changing the session, so that
         * it's not sent to memcached
         */
        Thread.sleep( TimeUnit.SECONDS.toMillis( delay * 3 ) );
        assertEquals( makeRequest( _httpClient, _portTomcat1, sessionId1 ), sessionId1, "SessionId should be the same" );
        assertNotNull( _memcached.get( sessionId1 ), "Session should still exist in memcached." );

        /* after another 3 seconds make another request without changing the session
         */
        Thread.sleep( TimeUnit.SECONDS.toMillis( delay * 3 ) );
        assertEquals( makeRequest( _httpClient, _portTomcat1, sessionId1 ), sessionId1, "SessionId should be the same" );
        assertNotNull( _memcached.get( sessionId1 ), "Session should still exist in memcached." );

        /* after another nearly 4 seconds (maxInactiveInterval) check that the session is still alive in memcached,
         * this would have been expired without an updated expiration
         */
        Thread.sleep( TimeUnit.SECONDS.toMillis( manager.getMaxInactiveInterval() ) - 500 );
        assertNotNull( _memcached.get( sessionId1 ), "Session should still exist in memcached." );

        /* after another second in sticky mode (more than 4 seconds since the last request), or an two times the
         * maxInactiveInterval in non-sticky mode (we must keep sessions in memcached with double expirationtime)
         * the session must be expired in memcached
View Full Code Here

     */
    @Test( enabled = true )
    public void testNotAssociatedSessionGetsAssociatedIssue49() throws InterruptedException, IOException, ExecutionException, TimeoutException {
        _daemon.stop();

        final SessionManager manager = _tomcat1.getManager();
        manager.setMaxInactiveInterval( 5 );
        manager.setSticky( true );
        final SessionIdFormat sessionIdFormat = new SessionIdFormat();

        final Session session = manager.createSession( null );
        assertNull( sessionIdFormat.extractMemcachedId( session.getId() ) );

        _daemon.start();

        // Wait so that the daemon will be available and the client can reconnect (async get didn't do the trick)
        waitForReconnect(manager.getMemcachedSessionService().getMemcached(), 1, 4000);

        final String newSessionId = manager.getMemcachedSessionService().changeSessionIdOnMemcachedFailover( session.getId() );
        assertNotNull( newSessionId );
        assertEquals( newSessionId, session.getId() );
        assertEquals( sessionIdFormat.extractMemcachedId( newSessionId ), _memcachedNodeId );

    }
View Full Code Here

    /**
     * Test for issue #60 (Add possibility to disable msm at runtime): disable msm
     */
    @Test( enabled = true )
    public void testDisableMsmAtRuntime() throws InterruptedException, IOException, ExecutionException, TimeoutException, LifecycleException, HttpException {
        final SessionManager manager = _tomcat1.getManager();
        manager.setSticky( true );
        // disable msm, shutdown our server and our client
        manager.setEnabled( false );
        _memcached.shutdown();
        _daemon.stop();

        checkSessionFunctionalityWithMsmDisabled();
    }
View Full Code Here

        final String sessionId2 = makeRequest( _httpClient, _portTomcat1, sessionId1 );
        assertNotSame( sessionId2, sessionId1, "SessionId not changed." );
    }

    private void waitForSessionExpiration(final boolean sticky) throws InterruptedException {
        final SessionManager manager = _tomcat1.getManager();
        assertEquals( manager.getMemcachedSessionService().isSticky(), sticky );
        final Container container = manager.getContainer();
        final long timeout = TimeUnit.SECONDS.toMillis(
                sticky ? container.getBackgroundProcessorDelay() + manager.getMaxInactiveInterval()
                       : 2 * manager.getMaxInactiveInterval() ) + 1000;
        Thread.sleep( timeout );
    }
View Full Code Here

        _daemon3 = createDaemon( address3 );
        _daemon3.start();

        final String memcachedNodes = MEMCACHED_NODES + "," + NODE_ID_3 + ":localhost:" + MEMCACHED_PORT_3;

        final SessionManager manager = _tomcat1.getManager();
        manager.setMaxInactiveInterval( 5 );
        manager.setMemcachedNodes(memcachedNodes);
        manager.getMemcachedSessionService().setSessionBackupAsync(false);

        waitForReconnect(manager.getMemcachedSessionService().getMemcached(), 3, 1000);

        final NodeIdList nodeIdList = NodeIdList.create(NODE_ID_1, NODE_ID_2, NODE_ID_3);
        final Map<String, MemCacheDaemon<?>> memcachedsByNodeId = new HashMap<String, MemCacheDaemon<?>>();
        memcachedsByNodeId.put(NODE_ID_1, _daemon1);
        memcachedsByNodeId.put(NODE_ID_2, _daemon2);
View Full Code Here

TOP

Related Classes of de.javakaffee.web.msm.MemcachedSessionService.SessionManager

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.