Search in sources :

Example 11 with SimpleMessageListenerContainer

use of org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer in project spring-integration by spring-projects.

the class Amqp method inboundAdapter.

/**
 * Create an initial AmqpInboundChannelAdapterSpec using a
 * {@link SimpleMessageListenerContainer}.
 * @param connectionFactory the connectionFactory.
 * @param queueNames the queueNames.
 * @return the AmqpInboundChannelAdapterSpec.
 */
public static AmqpInboundChannelAdapterSMLCSpec inboundAdapter(ConnectionFactory connectionFactory, String... queueNames) {
    SimpleMessageListenerContainer listenerContainer = new SimpleMessageListenerContainer(connectionFactory);
    listenerContainer.setQueueNames(queueNames);
    return new AmqpInboundChannelAdapterSMLCSpec(listenerContainer);
}
Also used : SimpleMessageListenerContainer(org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer)

Example 12 with SimpleMessageListenerContainer

use of org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer in project fru-paqx-parent by dellemc-symphony.

the class ConsumerConfig method fruRequestListenerContainer.

@Bean
SimpleMessageListenerContainer fruRequestListenerContainer() {
    SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
    container.setConnectionFactory(rabbitConnectionFactory);
    container.setAcknowledgeMode(AcknowledgeMode.AUTO);
    container.setQueues(responseQueue);
    container.setAdviceChain(new Advice[] { retryPolicyAdvice() });
    container.setMessageListener(fruMessageListener());
    container.setErrorHandler(new DefaultContainerErrorHandler("fruRequestListenerContainer"));
    return container;
}
Also used : SimpleMessageListenerContainer(org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer) DefaultContainerErrorHandler(com.dell.cpsd.common.rabbitmq.context.builder.DefaultContainerErrorHandler) Bean(org.springframework.context.annotation.Bean)

Example 13 with SimpleMessageListenerContainer

use of org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer in project microservices by pwillhan.

the class ContainerListenerExample method main.

public static void main(String[] args) {
    CachingConnectionFactory factory = new CachingConnectionFactory("localhost");
    SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(factory);
    Object listener = new Object() {

        public void handleMessage(String message) {
            System.out.println("Message received: " + message);
        }
    };
    MessageListenerAdapter adapter = new MessageListenerAdapter(listener);
    container.setMessageListener(adapter);
    container.setQueueNames("sample-queue");
    container.start();
}
Also used : MessageListenerAdapter(org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter) CachingConnectionFactory(org.springframework.amqp.rabbit.connection.CachingConnectionFactory) SimpleMessageListenerContainer(org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer)

Example 14 with SimpleMessageListenerContainer

use of org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer 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 15 with SimpleMessageListenerContainer

use of org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer 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

SimpleMessageListenerContainer (org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer)27 Test (org.junit.Test)13 ConnectionFactory (org.springframework.amqp.rabbit.connection.ConnectionFactory)10 BeanFactory (org.springframework.beans.factory.BeanFactory)8 DirectChannel (org.springframework.integration.channel.DirectChannel)8 QueueChannel (org.springframework.integration.channel.QueueChannel)8 Channel (com.rabbitmq.client.Channel)7 MessageProperties (org.springframework.amqp.core.MessageProperties)7 Connection (org.springframework.amqp.rabbit.connection.Connection)7 ChannelAwareMessageListener (org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener)7 Jackson2JsonMessageConverter (org.springframework.amqp.support.converter.Jackson2JsonMessageConverter)5 Bean (org.springframework.context.annotation.Bean)5 Message (org.springframework.messaging.Message)5 PollableChannel (org.springframework.messaging.PollableChannel)5 GenericMessage (org.springframework.messaging.support.GenericMessage)5 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)4 RabbitTemplate (org.springframework.amqp.rabbit.core.RabbitTemplate)4 AbstractMessageListenerContainer (org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer)4 MessageConversionException (org.springframework.amqp.support.converter.MessageConversionException)4 MessageConverter (org.springframework.amqp.support.converter.MessageConverter)4