Search in sources :

Example 1 with MessageListener

use of org.springframework.amqp.core.MessageListener in project spring-integration by spring-projects.

the class ChannelTests method messageConversionTests.

@Test
public void messageConversionTests() throws Exception {
    RabbitTemplate amqpTemplate = new RabbitTemplate(this.connectionFactory);
    MessageConverter messageConverter = mock(MessageConverter.class);
    amqpTemplate.setMessageConverter(messageConverter);
    PointToPointSubscribableAmqpChannel channel = new PointToPointSubscribableAmqpChannel("testConvertFail", new SimpleMessageListenerContainer(this.connectionFactory), amqpTemplate);
    channel.afterPropertiesSet();
    MessageListener listener = TestUtils.getPropertyValue(channel, "container.messageListener", MessageListener.class);
    willThrow(new MessageConversionException("foo", new IllegalStateException("bar"))).given(messageConverter).fromMessage(any(org.springframework.amqp.core.Message.class));
    this.exception.expect(MessageConversionException.class);
    this.exception.expectCause(instanceOf(IllegalStateException.class));
    listener.onMessage(mock(org.springframework.amqp.core.Message.class));
}
Also used : RabbitTemplate(org.springframework.amqp.rabbit.core.RabbitTemplate) MessageConversionException(org.springframework.amqp.support.converter.MessageConversionException) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) SimpleMessageListenerContainer(org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer) MessageListener(org.springframework.amqp.core.MessageListener) MessageConverter(org.springframework.amqp.support.converter.MessageConverter) Test(org.junit.Test)

Example 2 with MessageListener

use of org.springframework.amqp.core.MessageListener in project spring-integration by spring-projects.

the class AbstractSubscribableAmqpChannel method onInit.

@Override
public void onInit() throws Exception {
    super.onInit();
    this.dispatcher = this.createDispatcher();
    if (this.maxSubscribers == null) {
        this.maxSubscribers = this.getIntegrationProperty(this.isPubSub ? IntegrationProperties.CHANNELS_MAX_BROADCAST_SUBSCRIBERS : IntegrationProperties.CHANNELS_MAX_UNICAST_SUBSCRIBERS, Integer.class);
    }
    setMaxSubscribers(this.maxSubscribers);
    String queue = obtainQueueName(this.channelName);
    this.container.setQueueNames(queue);
    MessageConverter converter = (this.getAmqpTemplate() instanceof RabbitTemplate) ? ((RabbitTemplate) this.getAmqpTemplate()).getMessageConverter() : new SimpleMessageConverter();
    MessageListener listener = new DispatchingMessageListener(converter, this.dispatcher, this, this.isPubSub, getMessageBuilderFactory(), getInboundHeaderMapper());
    this.container.setMessageListener(listener);
    if (!this.container.isActive()) {
        this.container.afterPropertiesSet();
    }
}
Also used : RabbitTemplate(org.springframework.amqp.rabbit.core.RabbitTemplate) MessageListener(org.springframework.amqp.core.MessageListener) SimpleMessageConverter(org.springframework.amqp.support.converter.SimpleMessageConverter) SimpleMessageConverter(org.springframework.amqp.support.converter.SimpleMessageConverter) MessageConverter(org.springframework.amqp.support.converter.MessageConverter)

Example 3 with MessageListener

use of org.springframework.amqp.core.MessageListener in project spring-integration by spring-projects.

the class DispatcherHasNoSubscribersTests method testPubSub.

@Test
public void testPubSub() {
    final Channel channel = mock(Channel.class);
    Connection connection = mock(Connection.class);
    doAnswer(invocation -> channel).when(connection).createChannel(anyBoolean());
    ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
    when(connectionFactory.createConnection()).thenReturn(connection);
    SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
    container.setConnectionFactory(connectionFactory);
    AmqpTemplate amqpTemplate = mock(AmqpTemplate.class);
    PublishSubscribeAmqpChannel amqpChannel = new PublishSubscribeAmqpChannel("noSubscribersChannel", container, amqpTemplate);
    amqpChannel.setBeanName("noSubscribersChannel");
    amqpChannel.setBeanFactory(mock(BeanFactory.class));
    amqpChannel.afterPropertiesSet();
    List<String> logList = insertMockLoggerInListener(amqpChannel);
    MessageListener listener = (MessageListener) container.getMessageListener();
    listener.onMessage(new Message("Hello world!".getBytes(), null));
    verifyLogReceived(logList);
}
Also used : ConnectionFactory(org.springframework.amqp.rabbit.connection.ConnectionFactory) Message(org.springframework.amqp.core.Message) Channel(com.rabbitmq.client.Channel) Connection(org.springframework.amqp.rabbit.connection.Connection) SimpleMessageListenerContainer(org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer) BeanFactory(org.springframework.beans.factory.BeanFactory) MessageListener(org.springframework.amqp.core.MessageListener) Matchers.containsString(org.hamcrest.Matchers.containsString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) AmqpTemplate(org.springframework.amqp.core.AmqpTemplate) Test(org.junit.Test)

Example 4 with MessageListener

use of org.springframework.amqp.core.MessageListener in project spring-integration by spring-projects.

the class DispatcherHasNoSubscribersTests method testPtP.

@SuppressWarnings("unchecked")
@Test
public void testPtP() throws Exception {
    final Channel channel = mock(Channel.class);
    DeclareOk declareOk = mock(DeclareOk.class);
    when(declareOk.getQueue()).thenReturn("noSubscribersChannel");
    when(channel.queueDeclare(anyString(), anyBoolean(), anyBoolean(), anyBoolean(), isNull())).thenReturn(declareOk);
    Connection connection = mock(Connection.class);
    doAnswer(invocation -> channel).when(connection).createChannel(anyBoolean());
    ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
    when(connectionFactory.createConnection()).thenReturn(connection);
    SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
    container.setConnectionFactory(connectionFactory);
    AmqpTemplate amqpTemplate = mock(AmqpTemplate.class);
    PointToPointSubscribableAmqpChannel amqpChannel = new PointToPointSubscribableAmqpChannel("noSubscribersChannel", container, amqpTemplate);
    amqpChannel.setBeanName("noSubscribersChannel");
    amqpChannel.setBeanFactory(mock(BeanFactory.class));
    amqpChannel.afterPropertiesSet();
    MessageListener listener = (MessageListener) container.getMessageListener();
    try {
        listener.onMessage(new Message("Hello world!".getBytes(), null));
        fail("Exception expected");
    } catch (MessageDeliveryException e) {
        assertThat(e.getMessage(), containsString("Dispatcher has no subscribers for amqp-channel 'noSubscribersChannel'."));
    }
}
Also used : DeclareOk(com.rabbitmq.client.AMQP.Queue.DeclareOk) ConnectionFactory(org.springframework.amqp.rabbit.connection.ConnectionFactory) Message(org.springframework.amqp.core.Message) Channel(com.rabbitmq.client.Channel) Connection(org.springframework.amqp.rabbit.connection.Connection) SimpleMessageListenerContainer(org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer) BeanFactory(org.springframework.beans.factory.BeanFactory) MessageListener(org.springframework.amqp.core.MessageListener) MessageDeliveryException(org.springframework.messaging.MessageDeliveryException) AmqpTemplate(org.springframework.amqp.core.AmqpTemplate) Test(org.junit.Test)

Aggregations

MessageListener (org.springframework.amqp.core.MessageListener)4 Test (org.junit.Test)3 SimpleMessageListenerContainer (org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer)3 Channel (com.rabbitmq.client.Channel)2 AmqpTemplate (org.springframework.amqp.core.AmqpTemplate)2 Message (org.springframework.amqp.core.Message)2 Connection (org.springframework.amqp.rabbit.connection.Connection)2 ConnectionFactory (org.springframework.amqp.rabbit.connection.ConnectionFactory)2 RabbitTemplate (org.springframework.amqp.rabbit.core.RabbitTemplate)2 MessageConverter (org.springframework.amqp.support.converter.MessageConverter)2 BeanFactory (org.springframework.beans.factory.BeanFactory)2 DeclareOk (com.rabbitmq.client.AMQP.Queue.DeclareOk)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)1 MessageConversionException (org.springframework.amqp.support.converter.MessageConversionException)1 SimpleMessageConverter (org.springframework.amqp.support.converter.SimpleMessageConverter)1 Message (org.springframework.messaging.Message)1 MessageDeliveryException (org.springframework.messaging.MessageDeliveryException)1 GenericMessage (org.springframework.messaging.support.GenericMessage)1