SingleConnectionFactory subclass that adds Session, MessageProducer, and MessageConsumer caching. This ConnectionFactory also switches the ReconnectOnException property to true by default, allowing for automatic recovery of the underlying Connection.
By default, only one single Session will be cached, with further requested Sessions being created and disposed on demand. Consider raising the SessionCacheSize property in case of a high-concurrency environment.

NOTE: This ConnectionFactory requires explicit closing of all Sessions obtained from its shared Connection. This is the usual recommendation for native NMS access code anyway. However, with this ConnectionFactory, its use is mandatory in order to actually allow for Session reuse.

Note also that MessageConsumers obtained from a cached Session won't get closed until the Session will eventually be removed from the pool. This may lead to semantic side effects in some cases. For a durable subscriber, the logical Session.Close() call will also close the subscription. Re-registering a durable consumer for the same subscription on the same Session handle is not supported; close and reobtain a cached Session first.

Inheritance: Spring.Messaging.Nms.Connections.SingleConnectionFactory
コード例 #1
0
        public void CachedMessageProducerTwoRequests()
        {
            IConnectionFactory connectionFactory = CreateConnectionFactory();

            CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory();

            cachingConnectionFactory.TargetConnectionFactory = connectionFactory;
            IConnection con1 = cachingConnectionFactory.CreateConnection();

            ISession            sessionA  = con1.CreateSession(AcknowledgementMode.Transactional);
            IMessageProducer    producerA = sessionA.CreateProducer();
            TestMessageProducer tmpA      = GetTestMessageProducer(producerA);


            ISession            sessionB  = con1.CreateSession(AcknowledgementMode.Transactional);
            IMessageProducer    producerB = sessionB.CreateProducer();
            TestMessageProducer tmpB      = GetTestMessageProducer(producerB);

            Assert.AreNotSame(tmpA, tmpB);

            sessionA.Close();

            ISession            sessionC  = con1.CreateSession(AcknowledgementMode.Transactional);
            IMessageProducer    producerC = sessionC.CreateProducer();
            TestMessageProducer tmpC      = GetTestMessageProducer(producerC);

            Assert.AreSame(tmpA, tmpC);
        }
コード例 #2
0
        public void CachedSession()
        {
            IConnectionFactory connectionFactory = CreateConnectionFactory();

            mocks.ReplayAll();

            CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory();
            cachingConnectionFactory.TargetConnectionFactory = connectionFactory;

            IConnection con1 = cachingConnectionFactory.CreateConnection();

            ISession session1 = con1.CreateSession(AcknowledgementMode.Transactional);        
            TestSession testSession = GetTestSession(session1);
            Assert.AreEqual(1, testSession.CreatedCount);
            Assert.AreEqual(0, testSession.CloseCount);


            session1.Close();  // won't close, will put in session cache.
            Assert.AreEqual(0, testSession.CloseCount);
            
            ISession session2 = con1.CreateSession(AcknowledgementMode.Transactional);


            TestSession testSession2 = GetTestSession(session2);
            

            Assert.AreSame(testSession, testSession2);

            Assert.AreEqual(1, testSession.CreatedCount);
            Assert.AreEqual(0, testSession.CloseCount);
           
            mocks.VerifyAll();

            //don't explicitly call close on 
        }
コード例 #3
0
        public void CachedMessageConsumer()
        {
            IConnectionFactory connectionFactory = (IConnectionFactory)mocks.CreateMock(typeof(IConnectionFactory));
            IConnection connection = new TestConnection();

            Expect.Call(connectionFactory.CreateConnection()).Return(connection).Repeat.Once();

            mocks.ReplayAll();

            CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory();
            cachingConnectionFactory.TargetConnectionFactory = connectionFactory;
            IConnection con1 = cachingConnectionFactory.CreateConnection();

            ISession sessionA = con1.CreateSession(AcknowledgementMode.Transactional);
            IDestination destination = new ActiveMQQueue("test.dest");
            IMessageConsumer consumerA = sessionA.CreateConsumer(destination);
            TestMessageConsumer tmpA = GetTestMessageConsumer(consumerA);

            sessionA.Close();

            ISession sessionB = con1.CreateSession(AcknowledgementMode.Transactional);
            IMessageConsumer consumerB = sessionB.CreateConsumer(destination);
            TestMessageConsumer tmpB = GetTestMessageConsumer(consumerB);

            Assert.AreSame(tmpA, tmpB);
            mocks.VerifyAll();
        }
コード例 #4
0
        public void CachedSession()
        {
            IConnectionFactory connectionFactory = CreateConnectionFactory();

            CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory();

            cachingConnectionFactory.TargetConnectionFactory = connectionFactory;

            IConnection con1 = cachingConnectionFactory.CreateConnection();

            ISession    session1    = con1.CreateSession(AcknowledgementMode.Transactional);
            TestSession testSession = GetTestSession(session1);

            Assert.AreEqual(1, testSession.CreatedCount);
            Assert.AreEqual(0, testSession.CloseCount);


            session1.Close();  // won't close, will put in session cache.
            Assert.AreEqual(0, testSession.CloseCount);

            ISession session2 = con1.CreateSession(AcknowledgementMode.Transactional);


            TestSession testSession2 = GetTestSession(session2);


            Assert.AreSame(testSession, testSession2);

            Assert.AreEqual(1, testSession.CreatedCount);
            Assert.AreEqual(0, testSession.CloseCount);

            //don't explicitly call close on
        }
コード例 #5
0
        public void CachedMessageProducer()
        {
            IConnectionFactory connectionFactory = (IConnectionFactory)mocks.CreateMock(typeof(IConnectionFactory));
            IConnection        connection        = new TestConnection();

            Expect.Call(connectionFactory.CreateConnection()).Return(connection).Repeat.Once();

            mocks.ReplayAll();


            CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory();

            cachingConnectionFactory.TargetConnectionFactory = connectionFactory;
            IConnection con1 = cachingConnectionFactory.CreateConnection();

            ISession            sessionA  = con1.CreateSession(AcknowledgementMode.Transactional);
            IMessageProducer    producerA = sessionA.CreateProducer();
            TestMessageProducer tmpA      = GetTestMessageProducer(producerA);

            sessionA.Close();

            ISession            sessionB  = con1.CreateSession(AcknowledgementMode.Transactional);
            IMessageProducer    producerB = sessionB.CreateProducer();
            TestMessageProducer tmpB      = GetTestMessageProducer(producerB);

            Assert.AreSame(tmpA, tmpB);

            mocks.VerifyAll();
        }
コード例 #6
0
        public void CachedMessageConsumer()
        {
            IConnectionFactory connectionFactory = CreateConnectionFactory();

            mocks.ReplayAll();


            CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory();

            cachingConnectionFactory.TargetConnectionFactory = connectionFactory;
            IConnection con1 = cachingConnectionFactory.CreateConnection();

            ISession            sessionA    = con1.CreateSession(AcknowledgementMode.Transactional);
            IDestination        destination = new ActiveMQQueue("test.dest");
            IMessageConsumer    consumerA   = sessionA.CreateConsumer(destination);
            TestMessageConsumer tmpA        = GetTestMessageConsumer(consumerA);

            sessionA.Close();

            ISession            sessionB  = con1.CreateSession(AcknowledgementMode.Transactional);
            IMessageConsumer    consumerB = sessionB.CreateConsumer(destination);
            TestMessageConsumer tmpB      = GetTestMessageConsumer(consumerB);

            Assert.AreSame(tmpA, tmpB);
            mocks.VerifyAll();
        }
コード例 #7
0
        public void CachingConnectionFactory()
        {
            IConnectionFactory connectionFactory = mocks.StrictMock <IConnectionFactory>();
            IConnection        connection        = mocks.StrictMock <IConnection>();
            ISession           txSession         = mocks.StrictMock <ISession>();
            ISession           nonTxSession      = mocks.StrictMock <ISession>();

            Expect.Call(connectionFactory.CreateConnection()).Return(connection).Repeat.Once();

            Expect.Call(connection.CreateSession(AcknowledgementMode.Transactional)).Return(txSession).Repeat.Once();
            Expect.Call(txSession.Transacted).Return(true).Repeat.Twice();
            txSession.Rollback();
            LastCall.Repeat.Once();
            txSession.Commit();
            LastCall.Repeat.Once();
            txSession.Close();
            LastCall.Repeat.Once();

            Expect.Call(connection.CreateSession(AcknowledgementMode.ClientAcknowledge)).Return(nonTxSession).Repeat.Once();
            nonTxSession.Close();
            LastCall.Repeat.Once();
            connection.Start();
            LastCall.Repeat.Twice();
            connection.Stop();
            LastCall.Repeat.Once();
            connection.Close();
            LastCall.Repeat.Once();

            mocks.ReplayAll();

            CachingConnectionFactory scf = new CachingConnectionFactory(connectionFactory);

            scf.ReconnectOnException = false;

            IConnection con1     = scf.CreateConnection();
            ISession    session1 = con1.CreateSession(AcknowledgementMode.Transactional);
            bool        b        = session1.Transacted;

            session1.Close(); // should be ignored
            session1 = con1.CreateSession(AcknowledgementMode.ClientAcknowledge);
            session1.Close(); // should be ignored
            con1.Start();
            con1.Close();     // should be ignored
            IConnection con2     = scf.CreateConnection();
            ISession    session2 = con2.CreateSession(AcknowledgementMode.ClientAcknowledge);

            session2.Close(); // should be ignored
            session2 = con2.CreateSession(AcknowledgementMode.Transactional);
            session2.Commit();
            session2.Close(); // should be ignored
            con2.Start();
            con2.Close();
            scf.Dispose();

            mocks.Verify(connectionFactory);
            mocks.Verify(connection);
            mocks.Verify(txSession);
            mocks.Verify(nonTxSession);
        }
コード例 #8
0
ファイル: CachedSession.cs プロジェクト: fgq841103/spring-net
 /// <summary>
 /// Initializes a new instance of the <see cref="CachedSession"/> class.
 /// </summary>
 /// <param name="targetSession">The target session.</param>
 /// <param name="sessionList">The session list.</param>
 /// <param name="ccf">The CachingConnectionFactory.</param>
 public CachedSession(ISession targetSession, LinkedList sessionList, CachingConnectionFactory ccf)
 {
     target = targetSession;
     this.sessionList = sessionList;
     sessionCacheSize = ccf.SessionCacheSize;
     shouldCacheProducers = ccf.CacheProducers;
     shouldCacheConsumers = ccf.CacheConsumers;
     this.ccf = ccf;
 }
コード例 #9
0
 /// <summary>
 /// Initializes a new instance of the <see cref="CachedSession"/> class.
 /// </summary>
 /// <param name="targetSession">The target session.</param>
 /// <param name="sessionList">The session list.</param>
 /// <param name="ccf">The CachingConnectionFactory.</param>
 public CachedSession(ISession targetSession, LinkedList sessionList, CachingConnectionFactory ccf)
 {
     target                = targetSession;
     this.sessionList      = sessionList;
     this.sessionCacheSize = ccf.SessionCacheSize;
     shouldCacheProducers  = ccf.CacheProducers;
     shouldCacheConsumers  = ccf.CacheConsumers;
     this.ccf              = ccf;
 }
コード例 #10
0
        public void CachingConnectionFactory()
        {
            IConnectionFactory connectionFactory = (IConnectionFactory)mocks.CreateMock(typeof(IConnectionFactory));
            IConnection connection = (IConnection)mocks.CreateMock(typeof(IConnection));
            ISession txSession = (ISession)mocks.CreateMock(typeof(ISession));
            ISession nonTxSession = (ISession)mocks.CreateMock(typeof(ISession));
            Expect.Call(connectionFactory.CreateConnection()).Return(connection).Repeat.Once();

            Expect.Call(connection.CreateSession(AcknowledgementMode.Transactional)).Return(txSession).Repeat.Once();
            Expect.Call(txSession.Transacted).Return(true).Repeat.Twice();
            txSession.Rollback();
            LastCall.Repeat.Once();
            txSession.Commit();
            LastCall.Repeat.Once();
            txSession.Close();
            LastCall.Repeat.Once();

            Expect.Call(connection.CreateSession(AcknowledgementMode.ClientAcknowledge)).Return(nonTxSession).Repeat.Once();
            nonTxSession.Close();
            LastCall.Repeat.Once();
            connection.Start();
            LastCall.Repeat.Twice();
            connection.Stop();
            LastCall.Repeat.Once();
            connection.Close();
            LastCall.Repeat.Once();

            mocks.ReplayAll();

            CachingConnectionFactory scf = new CachingConnectionFactory(connectionFactory);
            scf.ReconnectOnException = false;

            IConnection con1 = scf.CreateConnection();
            ISession session1 = con1.CreateSession(AcknowledgementMode.Transactional);
            bool b = session1.Transacted;
            session1.Close();  // should be ignored
            session1 = con1.CreateSession(AcknowledgementMode.ClientAcknowledge);
            session1.Close();  // should be ignored
            con1.Start();
            con1.Close(); // should be ignored
            IConnection con2 = scf.CreateConnection();
            ISession session2 = con2.CreateSession(AcknowledgementMode.ClientAcknowledge);
            session2.Close(); // should be ignored
            session2 = con2.CreateSession(AcknowledgementMode.Transactional);
            session2.Commit();
            session2.Close(); // should be ignored
            con2.Start();
            con2.Close();
            scf.Dispose();

            mocks.Verify(connectionFactory);
            mocks.Verify(connection);
            mocks.Verify(txSession);
            mocks.Verify(nonTxSession);
        }
コード例 #11
0
        public void CachingConnectionFactory()
        {
            IConnectionFactory connectionFactory = A.Fake <IConnectionFactory>();
            IConnection        connection        = A.Fake <IConnection>();
            ISession           txSession         = A.Fake <ISession>();
            ISession           nonTxSession      = A.Fake <ISession>();

            A.CallTo(() => connectionFactory.CreateConnection()).Returns(connection).Once();

            A.CallTo(() => connection.CreateSession(AcknowledgementMode.Transactional)).Returns(txSession).Once();
            A.CallTo(() => txSession.Transacted).Returns(true).Twice();

            A.CallTo(() => connection.CreateSession(AcknowledgementMode.ClientAcknowledge)).Returns(nonTxSession).Once();

            CachingConnectionFactory scf = new CachingConnectionFactory(connectionFactory);

            scf.ReconnectOnException = false;

            IConnection con1     = scf.CreateConnection();
            ISession    session1 = con1.CreateSession(AcknowledgementMode.Transactional);
            bool        b        = session1.Transacted;

            session1.Close(); // should be ignored
            session1 = con1.CreateSession(AcknowledgementMode.ClientAcknowledge);
            session1.Close(); // should be ignored
            con1.Start();
            con1.Close();     // should be ignored
            IConnection con2     = scf.CreateConnection();
            ISession    session2 = con2.CreateSession(AcknowledgementMode.ClientAcknowledge);

            session2.Close(); // should be ignored
            session2 = con2.CreateSession(AcknowledgementMode.Transactional);
            session2.Commit();
            session2.Close(); // should be ignored
            con2.Start();
            con2.Close();
            scf.Dispose();

            A.CallTo(() => txSession.Rollback()).MustHaveHappenedOnceExactly();
            A.CallTo(() => txSession.Commit()).MustHaveHappenedOnceExactly();
            A.CallTo(() => txSession.Close()).MustHaveHappenedOnceExactly();

            A.CallTo(() => nonTxSession.Close()).MustHaveHappenedOnceExactly();
            A.CallTo(() => connection.Start()).MustHaveHappenedTwiceExactly();
            A.CallTo(() => connection.Stop()).MustHaveHappenedOnceExactly();
            A.CallTo(() => connection.Close()).MustHaveHappenedOnceExactly();
        }
コード例 #12
0
        public void CachedSessionTwoRequests()
        {
            IConnectionFactory connectionFactory = (IConnectionFactory)mocks.CreateMock(typeof(IConnectionFactory));
            IConnection        connection        = new TestConnection();

            Expect.Call(connectionFactory.CreateConnection()).Return(connection).Repeat.Once();

            mocks.ReplayAll();

            CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory();

            cachingConnectionFactory.TargetConnectionFactory = connectionFactory;
            IConnection con1 = cachingConnectionFactory.CreateConnection();

            ISession    session1     = con1.CreateSession(AcknowledgementMode.Transactional);
            TestSession testSession1 = GetTestSession(session1);

            Assert.AreEqual(1, testSession1.CreatedCount);
            Assert.AreEqual(0, testSession1.CloseCount);


            //will create a new one, not in the cache.
            ISession    session2     = con1.CreateSession(AcknowledgementMode.Transactional);
            TestSession testSession2 = GetTestSession(session2);

            Assert.AreEqual(1, testSession2.CreatedCount);
            Assert.AreEqual(0, testSession2.CloseCount);

            Assert.AreNotSame(testSession1, testSession2);

            Assert.AreNotSame(session1, session2);

            session1.Close();  // will be put in the cache

            ISession    session3     = con1.CreateSession(AcknowledgementMode.Transactional);
            TestSession testSession3 = GetTestSession(session3);

            Assert.AreSame(testSession1, testSession3);
            Assert.AreSame(session1, session3);
            Assert.AreEqual(1, testSession1.CreatedCount);
            Assert.AreEqual(0, testSession1.CloseCount);

            mocks.VerifyAll();
        }
コード例 #13
0
        public void CachedSession()
        {
            IConnectionFactory connectionFactory = (IConnectionFactory)mocks.CreateMock(typeof(IConnectionFactory));
            IConnection        connection        = new TestConnection();

            Expect.Call(connectionFactory.CreateConnection()).Return(connection).Repeat.Once();

            mocks.ReplayAll();

            CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory();

            cachingConnectionFactory.TargetConnectionFactory = connectionFactory;

            IConnection con1 = cachingConnectionFactory.CreateConnection();

            ISession    session1    = con1.CreateSession(AcknowledgementMode.Transactional);
            TestSession testSession = GetTestSession(session1);

            Assert.AreEqual(1, testSession.CreatedCount);
            Assert.AreEqual(0, testSession.CloseCount);


            session1.Close();  // won't close, will put in session cache.
            Assert.AreEqual(0, testSession.CloseCount);

            ISession session2 = con1.CreateSession(AcknowledgementMode.Transactional);


            TestSession testSession2 = GetTestSession(session2);


            Assert.AreSame(testSession, testSession2);

            Assert.AreEqual(1, testSession.CreatedCount);
            Assert.AreEqual(0, testSession.CloseCount);

            mocks.VerifyAll();

            //don't explicitly call close on
        }
コード例 #14
0
        public void CachedSessionTwoRequests()
        {
            IConnectionFactory connectionFactory = CreateConnectionFactory();

            CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory();

            cachingConnectionFactory.TargetConnectionFactory = connectionFactory;
            IConnection con1 = cachingConnectionFactory.CreateConnection();

            ISession    session1     = con1.CreateSession(AcknowledgementMode.Transactional);
            TestSession testSession1 = GetTestSession(session1);

            Assert.AreEqual(1, testSession1.CreatedCount);
            Assert.AreEqual(0, testSession1.CloseCount);


            //will create a new one, not in the cache.
            ISession    session2     = con1.CreateSession(AcknowledgementMode.Transactional);
            TestSession testSession2 = GetTestSession(session2);

            Assert.AreEqual(1, testSession2.CreatedCount);
            Assert.AreEqual(0, testSession2.CloseCount);

            Assert.AreNotSame(testSession1, testSession2);

            Assert.AreNotSame(session1, session2);

            session1.Close();  // will be put in the cache

            ISession    session3     = con1.CreateSession(AcknowledgementMode.Transactional);
            TestSession testSession3 = GetTestSession(session3);

            Assert.AreSame(testSession1, testSession3);
            Assert.AreSame(session1, session3);
            Assert.AreEqual(1, testSession1.CreatedCount);
            Assert.AreEqual(0, testSession1.CloseCount);
        }
コード例 #15
0
        public void CachedSessionTwoRequests()
        {
            IConnectionFactory connectionFactory = CreateConnectionFactory();

            mocks.ReplayAll();

            CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory();
            cachingConnectionFactory.TargetConnectionFactory = connectionFactory;
            IConnection con1 = cachingConnectionFactory.CreateConnection();

            ISession session1 = con1.CreateSession(AcknowledgementMode.Transactional);
            TestSession testSession1 = GetTestSession(session1);
            Assert.AreEqual(1, testSession1.CreatedCount);
            Assert.AreEqual(0, testSession1.CloseCount);


            //will create a new one, not in the cache.
            ISession session2 = con1.CreateSession(AcknowledgementMode.Transactional);
            TestSession testSession2 = GetTestSession(session2);
            Assert.AreEqual(1, testSession2.CreatedCount);
            Assert.AreEqual(0, testSession2.CloseCount);

            Assert.AreNotSame(testSession1, testSession2);

            Assert.AreNotSame(session1, session2);

            session1.Close();  // will be put in the cache

            ISession session3 = con1.CreateSession(AcknowledgementMode.Transactional);
            TestSession testSession3 = GetTestSession(session3);
            Assert.AreSame(testSession1, testSession3);
            Assert.AreSame(session1, session3);
            Assert.AreEqual(1, testSession1.CreatedCount);
            Assert.AreEqual(0, testSession1.CloseCount);

            mocks.VerifyAll();


        }
コード例 #16
0
        public void CachedMessageProducerTwoRequests()
        {
            IConnectionFactory connectionFactory = CreateConnectionFactory();

            mocks.ReplayAll();


            CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory();
            cachingConnectionFactory.TargetConnectionFactory = connectionFactory;
            IConnection con1 = cachingConnectionFactory.CreateConnection();

            ISession sessionA = con1.CreateSession(AcknowledgementMode.Transactional);
            IMessageProducer producerA = sessionA.CreateProducer();
            TestMessageProducer tmpA = GetTestMessageProducer(producerA);

           
            ISession sessionB = con1.CreateSession(AcknowledgementMode.Transactional);
            IMessageProducer producerB = sessionB.CreateProducer();
            TestMessageProducer tmpB = GetTestMessageProducer(producerB);
            
            Assert.AreNotSame(tmpA, tmpB);

            sessionA.Close();

            ISession sessionC = con1.CreateSession(AcknowledgementMode.Transactional);
            IMessageProducer producerC = sessionC.CreateProducer();
            TestMessageProducer tmpC = GetTestMessageProducer(producerC);

            Assert.AreSame(tmpA, tmpC);

            mocks.VerifyAll();
        }