Search in sources :

Example 6 with MessageListener

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

the class JmsListenerEndpointTests method setupConcurrencySimpleContainer.

@Test
public void setupConcurrencySimpleContainer() {
    SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
    MessageListener messageListener = new MessageListenerAdapter();
    SimpleJmsListenerEndpoint endpoint = new SimpleJmsListenerEndpoint();
    // simple implementation only support max value
    endpoint.setConcurrency("5-10");
    endpoint.setMessageListener(messageListener);
    endpoint.setupListenerContainer(container);
    assertThat(new DirectFieldAccessor(container).getPropertyValue("concurrentConsumers")).isEqualTo(10);
}
Also used : MessageListenerAdapter(org.springframework.jms.listener.adapter.MessageListenerAdapter) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) SimpleMessageListenerContainer(org.springframework.jms.listener.SimpleMessageListenerContainer) MessageListener(jakarta.jms.MessageListener) Test(org.junit.jupiter.api.Test)

Example 7 with MessageListener

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

the class SimpleJmsListenerEndpointTests method createListener.

@Test
public void createListener() {
    SimpleJmsListenerEndpoint endpoint = new SimpleJmsListenerEndpoint();
    MessageListener messageListener = new MessageListenerAdapter();
    endpoint.setMessageListener(messageListener);
    assertThat(endpoint.createMessageListener(container)).isSameAs(messageListener);
}
Also used : MessageListenerAdapter(org.springframework.jms.listener.adapter.MessageListenerAdapter) MessageListener(jakarta.jms.MessageListener) Test(org.junit.jupiter.api.Test)

Example 8 with MessageListener

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

the class SimpleMessageListenerContainerTests method testTransactedSessionsGetRollbackLogicAppliedAndThatExceptionsStillDo_NOT_Propagate.

@Test
public void testTransactedSessionsGetRollbackLogicAppliedAndThatExceptionsStillDo_NOT_Propagate() throws Exception {
    this.container.setSessionTransacted(true);
    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(true);
    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);
    this.container.setConnectionFactory(connectionFactory);
    this.container.setDestinationName(DESTINATION_NAME);
    this.container.setMessageListener((MessageListener) message -> {
        throw new UnsupportedOperationException();
    });
    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);
    // Session is rolled back 'cos it is transacted...
    verify(session).rollback();
    verify(connection).setExceptionListener(this.container);
    verify(connection).start();
}
Also used : Message(jakarta.jms.Message) StubQueue(org.springframework.jms.StubQueue) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Set(java.util.Set) JMSException(jakarta.jms.JMSException) MessageListener(jakarta.jms.MessageListener) ExceptionListener(jakarta.jms.ExceptionListener) Session(jakarta.jms.Session) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) Mockito.verify(org.mockito.Mockito.verify) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test) ErrorHandler(org.springframework.util.ErrorHandler) Assertions.fail(org.assertj.core.api.Assertions.fail) MessageConsumer(jakarta.jms.MessageConsumer) BDDMockito.given(org.mockito.BDDMockito.given) Assertions.assertThatIllegalArgumentException(org.assertj.core.api.Assertions.assertThatIllegalArgumentException) Nullable(org.springframework.lang.Nullable) ConnectionFactory(jakarta.jms.ConnectionFactory) Connection(jakarta.jms.Connection) Mockito.mock(org.mockito.Mockito.mock) ConnectionFactory(jakarta.jms.ConnectionFactory) Message(jakarta.jms.Message) Connection(jakarta.jms.Connection) Session(jakarta.jms.Session) Test(org.junit.jupiter.api.Test)

Example 9 with MessageListener

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

the class SimpleMessageListenerContainerTests method testNoRollbackOccursIfSessionIsNotTransactedAndThatExceptionsDo_NOT_Propagate.

@Test
public void testNoRollbackOccursIfSessionIsNotTransactedAndThatExceptionsDo_NOT_Propagate() 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);
    this.container.setConnectionFactory(connectionFactory);
    this.container.setDestinationName(DESTINATION_NAME);
    this.container.setMessageListener((MessageListener) message -> {
        throw new UnsupportedOperationException();
    });
    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();
}
Also used : Message(jakarta.jms.Message) StubQueue(org.springframework.jms.StubQueue) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Set(java.util.Set) JMSException(jakarta.jms.JMSException) MessageListener(jakarta.jms.MessageListener) ExceptionListener(jakarta.jms.ExceptionListener) Session(jakarta.jms.Session) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) Mockito.verify(org.mockito.Mockito.verify) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test) ErrorHandler(org.springframework.util.ErrorHandler) Assertions.fail(org.assertj.core.api.Assertions.fail) MessageConsumer(jakarta.jms.MessageConsumer) BDDMockito.given(org.mockito.BDDMockito.given) Assertions.assertThatIllegalArgumentException(org.assertj.core.api.Assertions.assertThatIllegalArgumentException) Nullable(org.springframework.lang.Nullable) ConnectionFactory(jakarta.jms.ConnectionFactory) Connection(jakarta.jms.Connection) Mockito.mock(org.mockito.Mockito.mock) ConnectionFactory(jakarta.jms.ConnectionFactory) Message(jakarta.jms.Message) Connection(jakarta.jms.Connection) Session(jakarta.jms.Session) Test(org.junit.jupiter.api.Test)

Example 10 with MessageListener

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

the class JmsListenerContainerFactoryTests method backOffOverridesRecoveryInterval.

@Test
public void backOffOverridesRecoveryInterval() {
    DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
    BackOff backOff = new FixedBackOff();
    factory.setBackOff(backOff);
    factory.setRecoveryInterval(2000L);
    SimpleJmsListenerEndpoint endpoint = new SimpleJmsListenerEndpoint();
    MessageListener messageListener = new MessageListenerAdapter();
    endpoint.setMessageListener(messageListener);
    endpoint.setDestination("myQueue");
    DefaultMessageListenerContainer container = factory.createListenerContainer(endpoint);
    assertThat(new DirectFieldAccessor(container).getPropertyValue("backOff")).isSameAs(backOff);
}
Also used : MessageListenerAdapter(org.springframework.jms.listener.adapter.MessageListenerAdapter) DefaultMessageListenerContainer(org.springframework.jms.listener.DefaultMessageListenerContainer) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) MessageListener(jakarta.jms.MessageListener) FixedBackOff(org.springframework.util.backoff.FixedBackOff) BackOff(org.springframework.util.backoff.BackOff) FixedBackOff(org.springframework.util.backoff.FixedBackOff) Test(org.junit.jupiter.api.Test)

Aggregations

MessageListener (jakarta.jms.MessageListener)14 Test (org.junit.jupiter.api.Test)12 MessageListenerAdapter (org.springframework.jms.listener.adapter.MessageListenerAdapter)9 DefaultMessageListenerContainer (org.springframework.jms.listener.DefaultMessageListenerContainer)5 Session (jakarta.jms.Session)3 Connection (jakarta.jms.Connection)2 ConnectionFactory (jakarta.jms.ConnectionFactory)2 ExceptionListener (jakarta.jms.ExceptionListener)2 JMSException (jakarta.jms.JMSException)2 Message (jakarta.jms.Message)2 MessageConsumer (jakarta.jms.MessageConsumer)2 HashSet (java.util.HashSet)2 Set (java.util.Set)2 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)2 Assertions.assertThatIllegalArgumentException (org.assertj.core.api.Assertions.assertThatIllegalArgumentException)2 Assertions.fail (org.assertj.core.api.Assertions.fail)2 BDDMockito.given (org.mockito.BDDMockito.given)2 Mockito.mock (org.mockito.Mockito.mock)2 Mockito.verify (org.mockito.Mockito.verify)2 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)2