Search in sources :

Example 21 with JMSException

use of jakarta.jms.JMSException in project spring-framework by spring-projects.

the class DefaultMessageListenerContainerTests method createSuccessfulConnectionFactory.

private ConnectionFactory createSuccessfulConnectionFactory() {
    try {
        ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
        given(connectionFactory.createConnection()).willReturn(mock(Connection.class));
        return connectionFactory;
    } catch (JMSException ex) {
        // never happen
        throw new IllegalStateException(ex);
    }
}
Also used : ConnectionFactory(jakarta.jms.ConnectionFactory) Connection(jakarta.jms.Connection) JMSException(jakarta.jms.JMSException)

Example 22 with JMSException

use of jakarta.jms.JMSException in project spring-framework by spring-projects.

the class SimpleMessageListenerContainerTests method testRegisteredExceptionListenerIsInvokedOnException.

@Test
public void testRegisteredExceptionListenerIsInvokedOnException() throws Exception {
    final SimpleMessageConsumer messageConsumer = new SimpleMessageConsumer();
    Session session = mock(Session.class);
    // Queue gets created in order to create MessageConsumer for that Destination...
    given(session.createQueue(DESTINATION_NAME)).willReturn(QUEUE_DESTINATION);
    // and then the MessageConsumer gets created...
    // no MessageSelector...
    given(session.createConsumer(QUEUE_DESTINATION, null)).willReturn(messageConsumer);
    // an exception is thrown, so the rollback logic is being applied here...
    given(session.getTransacted()).willReturn(false);
    Connection connection = mock(Connection.class);
    // session gets created in order to register MessageListener...
    given(connection.createSession(this.container.isSessionTransacted(), this.container.getSessionAcknowledgeMode())).willReturn(session);
    // and the connection is start()ed after the listener is registered...
    ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
    given(connectionFactory.createConnection()).willReturn(connection);
    final JMSException theException = new JMSException(EXCEPTION_MESSAGE);
    this.container.setConnectionFactory(connectionFactory);
    this.container.setDestinationName(DESTINATION_NAME);
    this.container.setMessageListener((SessionAwareMessageListener<Message>) (Message message, @Nullable Session session1) -> {
        throw theException;
    });
    ExceptionListener exceptionListener = mock(ExceptionListener.class);
    this.container.setExceptionListener(exceptionListener);
    this.container.afterPropertiesSet();
    this.container.start();
    // manually trigger an Exception with the above bad MessageListener...
    final Message message = mock(Message.class);
    // a Throwable from a MessageListener MUST simply be swallowed...
    messageConsumer.sendMessage(message);
    verify(connection).setExceptionListener(this.container);
    verify(connection).start();
    verify(exceptionListener).onException(theException);
}
Also used : ConnectionFactory(jakarta.jms.ConnectionFactory) Message(jakarta.jms.Message) Connection(jakarta.jms.Connection) JMSException(jakarta.jms.JMSException) ExceptionListener(jakarta.jms.ExceptionListener) Session(jakarta.jms.Session) Test(org.junit.jupiter.api.Test)

Example 23 with JMSException

use of jakarta.jms.JMSException in project spring-framework by spring-projects.

the class SimpleJmsHeaderMapperTests method attemptToWriteDisallowedReplyToPropertyIsNotFatal.

@Test
public void attemptToWriteDisallowedReplyToPropertyIsNotFatal() throws JMSException {
    Message<String> message = initBuilder().setHeader(JmsHeaders.REPLY_TO, new Destination() {
    }).setHeader("foo", "bar").build();
    jakarta.jms.Message jmsMessage = new StubTextMessage() {

        @Override
        public void setJMSReplyTo(Destination replyTo) throws JMSException {
            throw new JMSException("illegal property");
        }
    };
    mapper.fromHeaders(message.getHeaders(), jmsMessage);
    assertThat(jmsMessage.getJMSReplyTo()).isNull();
    assertThat(jmsMessage.getStringProperty("foo")).isNotNull();
    assertThat(jmsMessage.getStringProperty("foo")).isEqualTo("bar");
}
Also used : Destination(jakarta.jms.Destination) StubTextMessage(org.springframework.jms.StubTextMessage) JMSException(jakarta.jms.JMSException) Test(org.junit.jupiter.api.Test)

Example 24 with JMSException

use of jakarta.jms.JMSException in project spring-boot by spring-projects.

the class JmsHealthIndicatorTests method jmsBrokerUsesFailover.

@Test
void jmsBrokerUsesFailover() throws JMSException {
    ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
    ConnectionMetaData connectionMetaData = mock(ConnectionMetaData.class);
    given(connectionMetaData.getJMSProviderName()).willReturn("JMS test provider");
    Connection connection = mock(Connection.class);
    given(connection.getMetaData()).willReturn(connectionMetaData);
    willThrow(new JMSException("Could not start", "123")).given(connection).start();
    given(connectionFactory.createConnection()).willReturn(connection);
    JmsHealthIndicator indicator = new JmsHealthIndicator(connectionFactory);
    Health health = indicator.health();
    assertThat(health.getStatus()).isEqualTo(Status.DOWN);
    assertThat(health.getDetails().get("provider")).isNull();
}
Also used : ConnectionFactory(jakarta.jms.ConnectionFactory) Health(org.springframework.boot.actuate.health.Health) ConnectionMetaData(jakarta.jms.ConnectionMetaData) Connection(jakarta.jms.Connection) JMSException(jakarta.jms.JMSException) Test(org.junit.jupiter.api.Test)

Example 25 with JMSException

use of jakarta.jms.JMSException in project spring-boot by spring-projects.

the class JmsHealthIndicatorTests method jmsBrokerCouldNotRetrieveProviderMetadata.

@Test
void jmsBrokerCouldNotRetrieveProviderMetadata() throws JMSException {
    ConnectionMetaData connectionMetaData = mock(ConnectionMetaData.class);
    given(connectionMetaData.getJMSProviderName()).willThrow(new JMSException("test", "123"));
    Connection connection = mock(Connection.class);
    given(connection.getMetaData()).willReturn(connectionMetaData);
    ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
    given(connectionFactory.createConnection()).willReturn(connection);
    JmsHealthIndicator indicator = new JmsHealthIndicator(connectionFactory);
    Health health = indicator.health();
    assertThat(health.getStatus()).isEqualTo(Status.DOWN);
    assertThat(health.getDetails().get("provider")).isNull();
    then(connection).should().close();
}
Also used : ConnectionFactory(jakarta.jms.ConnectionFactory) Health(org.springframework.boot.actuate.health.Health) ConnectionMetaData(jakarta.jms.ConnectionMetaData) Connection(jakarta.jms.Connection) JMSException(jakarta.jms.JMSException) Test(org.junit.jupiter.api.Test)

Aggregations

JMSException (jakarta.jms.JMSException)38 Connection (jakarta.jms.Connection)21 Test (org.junit.jupiter.api.Test)20 ConnectionFactory (jakarta.jms.ConnectionFactory)18 Session (jakarta.jms.Session)16 Destination (jakarta.jms.Destination)10 Message (jakarta.jms.Message)10 MessageProducer (jakarta.jms.MessageProducer)8 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)8 Assertions.assertThatExceptionOfType (org.assertj.core.api.Assertions.assertThatExceptionOfType)8 BDDMockito.given (org.mockito.BDDMockito.given)8 Mockito.mock (org.mockito.Mockito.mock)8 Mockito.times (org.mockito.Mockito.times)8 Mockito.verify (org.mockito.Mockito.verify)8 TransactionStatus (org.springframework.transaction.TransactionStatus)8 UnexpectedRollbackException (org.springframework.transaction.UnexpectedRollbackException)8 AfterEach (org.junit.jupiter.api.AfterEach)7 StubQueue (org.springframework.jms.StubQueue)7 JmsTemplate (org.springframework.jms.core.JmsTemplate)7 SessionCallback (org.springframework.jms.core.SessionCallback)7