Java Code Examples for javax.jms.TopicConnection#createSession()

The following examples show how to use javax.jms.TopicConnection#createSession() . 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: SingleConnectionFactoryTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testCachingConnectionFactoryWithTopicConnectionFactoryAndJms102Usage() throws JMSException {
	TopicConnectionFactory cf = mock(TopicConnectionFactory.class);
	TopicConnection con = mock(TopicConnection.class);
	TopicSession txSession = mock(TopicSession.class);
	TopicSession nonTxSession = mock(TopicSession.class);

	given(cf.createTopicConnection()).willReturn(con);
	given(con.createTopicSession(true, Session.AUTO_ACKNOWLEDGE)).willReturn(txSession);
	given(txSession.getTransacted()).willReturn(true);
	given(con.createTopicSession(false, Session.CLIENT_ACKNOWLEDGE)).willReturn(nonTxSession);

	CachingConnectionFactory scf = new CachingConnectionFactory(cf);
	scf.setReconnectOnException(false);
	Connection con1 = scf.createTopicConnection();
	Session session1 = con1.createSession(true, Session.AUTO_ACKNOWLEDGE);
	session1.getTransacted();
	session1.close();  // should lead to rollback
	session1 = con1.createSession(false, Session.CLIENT_ACKNOWLEDGE);
	session1.close();
	con1.start();
	TopicConnection con2 = scf.createTopicConnection();
	Session session2 = con2.createTopicSession(false, Session.CLIENT_ACKNOWLEDGE);
	session2.close();
	session2 = con2.createSession(true, Session.AUTO_ACKNOWLEDGE);
	session2.getTransacted();
	session2.close();
	con2.start();
	con1.close();
	con2.close();
	scf.destroy();  // should trigger actual close

	verify(txSession).close();
	verify(nonTxSession).close();
	verify(con).start();
	verify(con).stop();
	verify(con).close();
}
 
Example 2
Source File: SingleConnectionFactoryTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testCachingConnectionFactoryWithTopicConnectionFactoryAndJms102Usage() throws JMSException {
	TopicConnectionFactory cf = mock(TopicConnectionFactory.class);
	TopicConnection con = mock(TopicConnection.class);
	TopicSession txSession = mock(TopicSession.class);
	TopicSession nonTxSession = mock(TopicSession.class);

	given(cf.createTopicConnection()).willReturn(con);
	given(con.createTopicSession(true, Session.AUTO_ACKNOWLEDGE)).willReturn(txSession);
	given(txSession.getTransacted()).willReturn(true);
	given(con.createTopicSession(false, Session.CLIENT_ACKNOWLEDGE)).willReturn(nonTxSession);

	CachingConnectionFactory scf = new CachingConnectionFactory(cf);
	scf.setReconnectOnException(false);
	Connection con1 = scf.createTopicConnection();
	Session session1 = con1.createSession(true, Session.AUTO_ACKNOWLEDGE);
	session1.getTransacted();
	session1.close();  // should lead to rollback
	session1 = con1.createSession(false, Session.CLIENT_ACKNOWLEDGE);
	session1.close();
	con1.start();
	TopicConnection con2 = scf.createTopicConnection();
	Session session2 = con2.createTopicSession(false, Session.CLIENT_ACKNOWLEDGE);
	session2.close();
	session2 = con2.createSession(true, Session.AUTO_ACKNOWLEDGE);
	session2.getTransacted();
	session2.close();
	con2.start();
	con1.close();
	con2.close();
	scf.destroy();  // should trigger actual close

	verify(txSession).close();
	verify(nonTxSession).close();
	verify(con).start();
	verify(con).stop();
	verify(con).close();
}
 
Example 3
Source File: TimeToLiveTest.java    From qpid-broker-j with Apache License 2.0 5 votes vote down vote up
@Test
public void testPassiveTTLWithDurableSubscription() throws Exception
{
    long timeToLiveMillis = getReceiveTimeout() * 2;
    String subscriptionName = getTestName() + "_sub";
    Topic topic = createTopic(getTestName());
    TopicConnection connection = getTopicConnection();
    try
    {
        Session session = connection.createSession(true, Session.SESSION_TRANSACTED);
        TopicSubscriber durableSubscriber = session.createDurableSubscriber(topic, subscriptionName);
        MessageProducer producer = session.createProducer(topic);
        producer.setTimeToLive(timeToLiveMillis);
        producer.send(session.createTextMessage("A"));
        producer.setTimeToLive(0);
        producer.send(session.createTextMessage("B"));
        session.commit();

        connection.start();
        Message message = durableSubscriber.receive(getReceiveTimeout());

        assertTrue("TextMessage should be received", message instanceof TextMessage);
        assertEquals("Unexpected message received", "A", ((TextMessage)message).getText());

        Thread.sleep(timeToLiveMillis);

        session.rollback();
        message = durableSubscriber.receive(getReceiveTimeout());

        assertTrue("TextMessage should be received after waiting for TTL", message instanceof TextMessage);
        assertEquals("Unexpected message received after waiting for TTL", "B", ((TextMessage) message).getText());
    }
    finally
    {
        connection.close();
    }
}
 
Example 4
Source File: TimeToLiveTest.java    From qpid-broker-j with Apache License 2.0 5 votes vote down vote up
@Test
public void testActiveTTLWithDurableSubscription() throws Exception
{
    long timeToLiveMillis = getReceiveTimeout();
    String subscriptionName = getTestName() + "_sub";
    Topic topic = createTopic(getTestName());
    TopicConnection connection = getTopicConnection();
    try
    {
        Session session = connection.createSession(true, Session.SESSION_TRANSACTED);
        TopicSubscriber durableSubscriber = session.createDurableSubscriber(topic, subscriptionName);
        MessageProducer producer = session.createProducer(topic);
        producer.setTimeToLive(timeToLiveMillis);
        producer.send(session.createTextMessage("A"));
        producer.setTimeToLive(0);
        producer.send(session.createTextMessage("B"));
        session.commit();

        Thread.sleep(timeToLiveMillis);

        connection.start();
        Message message = durableSubscriber.receive(getReceiveTimeout());

        assertTrue("TextMessage should be received", message instanceof TextMessage);
        assertEquals("Unexpected message received", "B", ((TextMessage)message).getText());
    }
    finally
    {
        connection.close();
    }
}
 
Example 5
Source File: SingleConnectionFactoryTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testCachingConnectionFactoryWithTopicConnectionFactoryAndJms102Usage() throws JMSException {
	TopicConnectionFactory cf = mock(TopicConnectionFactory.class);
	TopicConnection con = mock(TopicConnection.class);
	TopicSession txSession = mock(TopicSession.class);
	TopicSession nonTxSession = mock(TopicSession.class);

	given(cf.createTopicConnection()).willReturn(con);
	given(con.createTopicSession(true, Session.AUTO_ACKNOWLEDGE)).willReturn(txSession);
	given(txSession.getTransacted()).willReturn(true);
	given(con.createTopicSession(false, Session.CLIENT_ACKNOWLEDGE)).willReturn(nonTxSession);

	CachingConnectionFactory scf = new CachingConnectionFactory(cf);
	scf.setReconnectOnException(false);
	Connection con1 = scf.createTopicConnection();
	Session session1 = con1.createSession(true, Session.AUTO_ACKNOWLEDGE);
	session1.getTransacted();
	session1.close();  // should lead to rollback
	session1 = con1.createSession(false, Session.CLIENT_ACKNOWLEDGE);
	session1.close();
	con1.start();
	TopicConnection con2 = scf.createTopicConnection();
	Session session2 = con2.createTopicSession(false, Session.CLIENT_ACKNOWLEDGE);
	session2.close();
	session2 = con2.createSession(true, Session.AUTO_ACKNOWLEDGE);
	session2.getTransacted();
	session2.close();
	con2.start();
	con1.close();
	con2.close();
	scf.destroy();  // should trigger actual close

	verify(txSession).close();
	verify(nonTxSession).close();
	verify(con).start();
	verify(con).stop();
	verify(con).close();
}