org.messaginghub.pooled.jms.JmsPoolConnectionFactory Java Examples

The following examples show how to use org.messaginghub.pooled.jms.JmsPoolConnectionFactory. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MQConnectionFactoryConfiguration.java    From mq-jms-spring with Apache License 2.0 6 votes vote down vote up
private JmsPoolConnectionFactory create(ConnectionFactory connectionFactory, JmsPoolConnectionFactoryProperties poolProperties) {

      JmsPoolConnectionFactory pooledConnectionFactory = new JmsPoolConnectionFactory();
      pooledConnectionFactory.setConnectionFactory(connectionFactory);

      pooledConnectionFactory.setBlockIfSessionPoolIsFull(poolProperties.isBlockIfFull());

      if (poolProperties.getBlockIfFullTimeout() != null) {
        pooledConnectionFactory.setBlockIfSessionPoolIsFullTimeout(poolProperties.getBlockIfFullTimeout().toMillis());
      }

      if (poolProperties.getIdleTimeout() != null) {
        pooledConnectionFactory.setConnectionIdleTimeout((int) poolProperties.getIdleTimeout().toMillis());
      }

      pooledConnectionFactory.setMaxConnections(poolProperties.getMaxConnections());
      pooledConnectionFactory.setMaxSessionsPerConnection(poolProperties.getMaxSessionsPerConnection());

      if (poolProperties.getTimeBetweenExpirationCheck() != null) {
        pooledConnectionFactory.setConnectionCheckInterval(poolProperties.getTimeBetweenExpirationCheck().toMillis());
      }

      pooledConnectionFactory.setUseAnonymousProducers(poolProperties.isUseAnonymousProducers());
      return pooledConnectionFactory;
    }
 
Example #2
Source File: AMQP10JMSProperties.java    From amqp-10-jms-spring-boot with Apache License 2.0 6 votes vote down vote up
public void configurePooledFactory(JmsPoolConnectionFactory factory) {
    factory.setBlockIfSessionPoolIsFull(isBlockIfSessionPoolIsFull());
    if (getBlockIfSessionPoolIsFullTimeout() != null) {
        factory.setBlockIfSessionPoolIsFullTimeout(getBlockIfSessionPoolIsFullTimeout().toMillis());
    }
    if (getConnectionCheckInterval() != null) {
        factory.setConnectionCheckInterval(getConnectionCheckInterval().toMillis());
    }
    if (getConnectionIdleTimeout() != null) {
        factory.setConnectionIdleTimeout((int) getConnectionIdleTimeout().toMillis());
    }
    factory.setExplicitProducerCacheSize(getExplicitProducerCacheSize());
    factory.setMaxConnections(getMaxConnections());
    factory.setMaxSessionsPerConnection(getMaxSessionsPerConnection());
    factory.setUseAnonymousProducers(isUseAnonymousProducers());
    factory.setUseProviderJMSContext(isUseProviderJMSContext());
}
 
Example #3
Source File: AMQP10JMSAutoConfiguration.java    From amqp-10-jms-spring-boot with Apache License 2.0 6 votes vote down vote up
@Bean(destroyMethod = "stop")
@ConditionalOnProperty(prefix = "amqphub.amqp10jms.pool", name = "enabled", havingValue = "true", matchIfMissing = false)
public JmsPoolConnectionFactory pooledJmsConnectionFactory(
        AMQP10JMSProperties properties,
        ObjectProvider<List<AMQP10JMSConnectionFactoryCustomizer>> factoryCustomizers) {

    JmsPoolConnectionFactory pooledConnectionFactory = new JmsPoolConnectionFactory();
    pooledConnectionFactory.setConnectionFactory(
        new AMQP10JMSConnectionFactoryFactory(properties, factoryCustomizers.getIfAvailable())
            .createConnectionFactory(JmsConnectionFactory.class));

    AMQP10JMSProperties.Pool pool = properties.getPool();

    pool.configurePooledFactory(pooledConnectionFactory);

    return pooledConnectionFactory;
}
 
Example #4
Source File: PooledConnectionTest.java    From pooled-jms with Apache License 2.0 6 votes vote down vote up
@Test(timeout = 60000)
public void testSetClientIDAfterConnectionStart() throws Exception {
    LOG.debug("running testRepeatedSetClientIDCalls()");

    ConnectionFactory cf = createPooledConnectionFactory();
    Connection conn = cf.createConnection();

    // test: try to call setClientID() after start()
    // should result in an exception
    try {
        conn.start();
        conn.setClientID("newID3");
        fail("Calling setClientID() after start() must raise a JMSException.");
    } catch (IllegalStateException ise) {
        LOG.debug("Correctly received " + ise);
    } finally {
        conn.close();
        ((JmsPoolConnectionFactory) cf).stop();
    }

    LOG.debug("Test finished.");
}
 
Example #5
Source File: PooledConnectionTest.java    From pooled-jms with Apache License 2.0 6 votes vote down vote up
@Test(timeout = 60000)
public void testSetClientIDTwiceWithDifferentID() throws Exception {
    LOG.debug("running testRepeatedSetClientIDCalls()");

    ConnectionFactory cf = createPooledConnectionFactory();
    Connection conn = cf.createConnection();

    // test: call setClientID() twice with different IDs
    // this should result in an IllegalStateException
    conn.setClientID("newID1");
    try {
        conn.setClientID("newID2");
        fail("calling Connection.setClientID() twice with different clientID must raise an IllegalStateException");
    } catch (IllegalStateException ise) {
        LOG.debug("Correctly received " + ise);
    } finally {
        conn.close();
        ((JmsPoolConnectionFactory) cf).stop();
    }

    LOG.debug("Test finished.");
}
 
Example #6
Source File: PooledConnectionTest.java    From pooled-jms with Apache License 2.0 6 votes vote down vote up
@Test(timeout = 60000)
public void testSetClientIDTwiceWithSameID() throws Exception {
    LOG.debug("running testRepeatedSetClientIDCalls()");

    // test: call setClientID("newID") twice
    // this should be tolerated and not result in an exception
    ConnectionFactory cf = createPooledConnectionFactory();
    Connection conn = cf.createConnection();
    conn.setClientID("newID");

    try {
        conn.setClientID("newID");
        conn.start();
        conn.close();
    } catch (IllegalStateException ise) {
        LOG.error("Repeated calls to newID2.setClientID(\"newID\") caused " + ise.getMessage());
        fail("Repeated calls to newID2.setClientID(\"newID\") caused " + ise.getMessage());
    } finally {
        ((JmsPoolConnectionFactory) cf).stop();
    }

    LOG.debug("Test finished.");
}
 
Example #7
Source File: PooledConnectionFactoryTest.java    From pooled-jms with Apache License 2.0 6 votes vote down vote up
@Test(timeout = 60000)
public void testConnectionsArePooled() throws Exception {
    JmsPoolConnectionFactory cf = createPooledConnectionFactory();
    cf.setMaxConnections(1);

    JmsPoolConnection conn1 = (JmsPoolConnection) cf.createConnection();
    JmsPoolConnection conn2 = (JmsPoolConnection) cf.createConnection();
    JmsPoolConnection conn3 = (JmsPoolConnection) cf.createConnection();

    assertSame(conn1.getConnection(), conn2.getConnection());
    assertSame(conn1.getConnection(), conn3.getConnection());
    assertSame(conn2.getConnection(), conn3.getConnection());

    assertEquals(1, cf.getNumConnections());

    cf.stop();
}
 
Example #8
Source File: PooledConnectionFactoryTest.java    From pooled-jms with Apache License 2.0 6 votes vote down vote up
@Test(timeout = 60000)
public void testConnectionsAreRotated() throws Exception {
    JmsPoolConnectionFactory cf = createPooledConnectionFactory();
    cf.setMaxConnections(10);

    Connection previous = null;

    // Front load the pool.
    for (int i = 0; i < 10; ++i) {
        cf.createConnection();
    }

    for (int i = 0; i < 100; ++i) {
        Connection current = ((JmsPoolConnection) cf.createConnection()).getConnection();
        assertNotSame(previous, current);
        previous = current;
    }

    cf.stop();
}
 
Example #9
Source File: PooledConnectionFactoryTest.java    From pooled-jms with Apache License 2.0 6 votes vote down vote up
@Test(timeout = 60000)
public void testMaxConnectionsAreCreated() throws Exception {
    JmsPoolConnectionFactory cf = createPooledConnectionFactory();
    cf.setMaxConnections(3);

    JmsPoolConnection conn1 = (JmsPoolConnection) cf.createConnection();
    JmsPoolConnection conn2 = (JmsPoolConnection) cf.createConnection();
    JmsPoolConnection conn3 = (JmsPoolConnection) cf.createConnection();

    assertNotSame(conn1.getConnection(), conn2.getConnection());
    assertNotSame(conn1.getConnection(), conn3.getConnection());
    assertNotSame(conn2.getConnection(), conn3.getConnection());

    assertEquals(3, cf.getNumConnections());

    cf.stop();
}
 
Example #10
Source File: PooledConnectionFactoryTest.java    From pooled-jms with Apache License 2.0 5 votes vote down vote up
@Test(timeout = 60000)
public void testClearAllConnections() throws Exception {
    JmsPoolConnectionFactory cf = createPooledConnectionFactory();
    cf.setMaxConnections(3);

    JmsPoolConnection conn1 = (JmsPoolConnection) cf.createConnection();
    JmsPoolConnection conn2 = (JmsPoolConnection) cf.createConnection();
    JmsPoolConnection conn3 = (JmsPoolConnection) cf.createConnection();

    assertNotSame(conn1.getConnection(), conn2.getConnection());
    assertNotSame(conn1.getConnection(), conn3.getConnection());
    assertNotSame(conn2.getConnection(), conn3.getConnection());

    assertEquals(3, cf.getNumConnections());

    cf.clear();

    assertEquals(0, cf.getNumConnections());

    conn1 = (JmsPoolConnection) cf.createConnection();
    conn2 = (JmsPoolConnection) cf.createConnection();
    conn3 = (JmsPoolConnection) cf.createConnection();

    assertNotSame(conn1.getConnection(), conn2.getConnection());
    assertNotSame(conn1.getConnection(), conn3.getConnection());
    assertNotSame(conn2.getConnection(), conn3.getConnection());

    cf.stop();
}
 
Example #11
Source File: QpidJmsPoolTestSupport.java    From pooled-jms with Apache License 2.0 5 votes vote down vote up
protected JmsPoolConnectionFactory createPooledConnectionFactory() {
    JmsPoolConnectionFactory cf = new JmsPoolConnectionFactory();
    cf.setConnectionFactory(qpidJmsConnectionFactory);
    cf.setMaxConnections(1);
    LOG.debug("ConnectionFactory initialized.");
    return cf;
}
 
Example #12
Source File: PooledConnectionFactoryTest.java    From pooled-jms with Apache License 2.0 5 votes vote down vote up
@Test(timeout = 60000)
public void testInstanceOf() throws  Exception {
    JmsPoolConnectionFactory pcf = new JmsPoolConnectionFactory();
    assertTrue(pcf instanceof QueueConnectionFactory);
    assertTrue(pcf instanceof TopicConnectionFactory);
    pcf.stop();
}
 
Example #13
Source File: JmsPoolTestSupport.java    From pooled-jms with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() throws Exception {
    LOG.info("========== start test: " + getTestName() + " ==========");

    factory = new MockJMSConnectionFactory();
    cf = new JmsPoolConnectionFactory();
    cf.setConnectionFactory(factory);
    cf.setMaxConnections(1);
}
 
Example #14
Source File: JmsPoolConnectionTemporaryDestinationTest.java    From pooled-jms with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() throws Exception {
    mock = new MockJMSConnectionFactory();

    // Ensure only one connection for that tests validate that shared connections
    // only destroy their own created temporary destinations.
    cf = new JmsPoolConnectionFactory();
    cf.setConnectionFactory(mock);
    cf.setMaxConnections(1);
}
 
Example #15
Source File: PooledConnectionFactoryTest.java    From pooled-jms with Apache License 2.0 5 votes vote down vote up
@Test(timeout = 60000)
public void testInstanceOf() throws  Exception {
    JmsPoolConnectionFactory pcf = new JmsPoolConnectionFactory();
    assertTrue(pcf instanceof QueueConnectionFactory);
    assertTrue(pcf instanceof TopicConnectionFactory);
    pcf.stop();
}
 
Example #16
Source File: JmsPoolSessionExhaustionTest.java    From pooled-jms with Apache License 2.0 5 votes vote down vote up
@Override
@Before
   public void setUp() throws Exception {
       factory = new MockJMSConnectionFactory();
       cf = new JmsPoolConnectionFactory();
       cf.setConnectionFactory(factory);
       cf.setMaxConnections(1);
       cf.setBlockIfSessionPoolIsFull(false);
       cf.setMaxSessionsPerConnection(1);
   }
 
Example #17
Source File: MQConnectionFactoryConfiguration.java    From mq-jms-spring with Apache License 2.0 5 votes vote down vote up
@Bean(destroyMethod = "stop")
@ConditionalOnProperty(prefix = "ibm.mq.pool", name = "enabled", havingValue = "true", matchIfMissing = false)
public JmsPoolConnectionFactory pooledJmsConnectionFactory(MQConfigurationProperties properties,
    ObjectProvider<List<MQConnectionFactoryCustomizer>> factoryCustomizers) {

  MQConnectionFactory connectionFactory = createConnectionFactory(properties, factoryCustomizers);

  return create(connectionFactory, properties.getPool());
}
 
Example #18
Source File: AMQP10JMSPooledAutoConfigurationTest.java    From amqp-10-jms-spring-boot with Apache License 2.0 5 votes vote down vote up
@Test
public void testDefaultsToPoolDefaults() {
    load(EmptyConfiguration.class,
        "amqphub.amqp10jms.pool.enabled=true");

    JmsTemplate jmsTemplate = this.context.getBean(JmsTemplate.class);
    ConnectionFactory connectionFactory =
        this.context.getBean(ConnectionFactory.class);

    assertTrue(connectionFactory instanceof JmsPoolConnectionFactory);
    assertEquals(jmsTemplate.getConnectionFactory(), connectionFactory);

    JmsPoolConnectionFactory pooledFactory = (JmsPoolConnectionFactory) connectionFactory;

    assertTrue(pooledFactory.isBlockIfSessionPoolIsFull());
    assertTrue(pooledFactory.isUseAnonymousProducers());
    assertFalse(pooledFactory.isUseProviderJMSContext());

    assertEquals(1, pooledFactory.getMaxConnections());
    assertEquals(500, pooledFactory.getMaxSessionsPerConnection());
    assertEquals(0, pooledFactory.getExplicitProducerCacheSize());

    assertEquals(30000, pooledFactory.getConnectionIdleTimeout());
    assertEquals(-1, pooledFactory.getConnectionCheckInterval());
    assertEquals(-1, pooledFactory.getBlockIfSessionPoolIsFullTimeout());

    assertTrue(pooledFactory.getConnectionFactory() instanceof JmsConnectionFactory);

    JmsConnectionFactory qpidJmsFactory = (JmsConnectionFactory) pooledFactory.getConnectionFactory();

    assertEquals("amqp://localhost:5672", qpidJmsFactory.getRemoteURI());
    assertNull(qpidJmsFactory.getUsername());
    assertNull(qpidJmsFactory.getPassword());
}
 
Example #19
Source File: AMQP10JMSPooledAutoConfigurationTest.java    From amqp-10-jms-spring-boot with Apache License 2.0 5 votes vote down vote up
@Test
public void testConfiguredJmsPoolConnectionFactory() {
    load(EmptyConfiguration.class,
        "amqphub.amqp10jms.pool.blockIfSessionPoolIsFull=false",
        "amqphub.amqp10jms.pool.useAnonymousProducers=false",
        "amqphub.amqp10jms.pool.useProviderJMSContext=true",
        "amqphub.amqp10jms.pool.maxConnections=2",
        "amqphub.amqp10jms.pool.maxSessionsPerConnection=100",
        "amqphub.amqp10jms.pool.explicitProducerCacheSize=5",
        "amqphub.amqp10jms.pool.connectionIdleTimeout=100",
        "amqphub.amqp10jms.pool.connectionCheckInterval=50",
        "amqphub.amqp10jms.pool.blockIfSessionPoolIsFullTimeout=3000",
        "amqphub.amqp10jms.pool.enabled=true");

    ConnectionFactory connectionFactory =
        this.context.getBean(ConnectionFactory.class);

    assertTrue(connectionFactory instanceof JmsPoolConnectionFactory);

    JmsPoolConnectionFactory pooledFactory = (JmsPoolConnectionFactory) connectionFactory;

    assertFalse(pooledFactory.isBlockIfSessionPoolIsFull());
    assertFalse(pooledFactory.isUseAnonymousProducers());
    assertTrue(pooledFactory.isUseProviderJMSContext());

    assertEquals(2, pooledFactory.getMaxConnections());
    assertEquals(100, pooledFactory.getMaxSessionsPerConnection());
    assertEquals(5, pooledFactory.getExplicitProducerCacheSize());

    assertEquals(100, pooledFactory.getConnectionIdleTimeout());
    assertEquals(50, pooledFactory.getConnectionCheckInterval());
    assertEquals(3000, pooledFactory.getBlockIfSessionPoolIsFullTimeout());

    assertTrue(pooledFactory.getConnectionFactory() instanceof JmsConnectionFactory);
}
 
Example #20
Source File: PooledConnectionFactoryTest.java    From pooled-jms with Apache License 2.0 4 votes vote down vote up
@Test(timeout = 60000)
public void testFactoryStopStart() throws Exception {
    JmsPoolConnectionFactory cf = createPooledConnectionFactory();
    cf.setMaxConnections(1);

    JmsPoolConnection conn1 = (JmsPoolConnection) cf.createConnection();

    cf.stop();

    assertNull(cf.createConnection());

    cf.start();

    JmsPoolConnection conn2 = (JmsPoolConnection) cf.createConnection();

    assertNotSame(conn1.getConnection(), conn2.getConnection());

    assertEquals(1, cf.getNumConnections());

    cf.stop();
}