org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer Java Examples
The following examples show how to use
org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.
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: SpringAmqpStubMessages.java From spring-cloud-contract with Apache License 2.0 | 6 votes |
@Override public void send(Message message, String destination) { final String routingKey = message.getMessageProperties().getReceivedRoutingKey(); List<SimpleMessageListenerContainer> listenerContainers = this.messageListenerAccessor .getListenerContainersForDestination(destination, routingKey); if (listenerContainers.isEmpty()) { throw new IllegalStateException( "no listeners found for destination " + destination); } for (SimpleMessageListenerContainer listenerContainer : listenerContainers) { Object messageListener = listenerContainer.getMessageListener(); if (isChannelAwareListener(listenerContainer, messageListener)) { try { ((ChannelAwareMessageListener) messageListener).onMessage(message, createChannel(listenerContainer, transactionalChannel())); } catch (Exception e) { throw new RuntimeException(e); } } else { ((MessageListener) messageListener).onMessage(message); } } }
Example #2
Source File: RabbitConfigurer.java From bird-java with MIT License | 6 votes |
@Bean @ConditionalOnProperty(value = EventbusConstant.Rabbit.LISTENER_PACKAGES) public SimpleMessageListenerContainer messageContainer(Queue queue, EventDispatcher eventDispatcher) { for (String topic : eventDispatcher.getAllTopics()) { this.initExchange(topic,queue); } SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory()); container.setQueues(queue); container.setMessageConverter(messageConverter()); container.setExposeListenerChannel(true); container.setMaxConcurrentConsumers(1); container.setConcurrentConsumers(1); container.setAcknowledgeMode(AcknowledgeMode.MANUAL); //设置确认模式手工确认 RabbitEventArgListener listener = new RabbitEventArgListener(eventDispatcher); container.setMessageListener(listener); return container; }
Example #3
Source File: RabbitMQConfig.java From jim-framework with Apache License 2.0 | 6 votes |
@Bean public SimpleMessageListenerContainer messageContainer() { SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory()); container.setQueues(queue()); container.setExposeListenerChannel(true); container.setMaxConcurrentConsumers(1); container.setConcurrentConsumers(1); container.setAcknowledgeMode(AcknowledgeMode.MANUAL); container.setMessageListener(new ChannelAwareMessageListener() { public void onMessage(Message message, com.rabbitmq.client.Channel channel) throws Exception { byte[] body = message.getBody(); logger.info("消费端接收到消息 : " + new String(body)); channel.basicAck(message.getMessageProperties().getDeliveryTag(), false); } }); return container; }
Example #4
Source File: AmqpConfig.java From myth with Apache License 2.0 | 6 votes |
/** * Message container simple message listener container. * * @return the simple message listener container */ @Bean @ConditionalOnProperty(prefix = "spring.rabbitmq", name = "host") public SimpleMessageListenerContainer messageContainer() { SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory); container.setQueues(queue()); container.setExposeListenerChannel(true); container.setMaxConcurrentConsumers(3); container.setConcurrentConsumers(1); //设置确认模式手工确认 container.setAcknowledgeMode(AcknowledgeMode.MANUAL); container.setMessageListener((ChannelAwareMessageListener) (message, channel) -> { byte[] messageBody = message.getBody(); LogUtil.debug(LOGGER, () -> "springcloud account服务 amqp接收消息"); //确认消息成功消费 final Boolean success = mythMqReceiveService.processMessage(messageBody); if (success) { channel.basicAck(message.getMessageProperties().getDeliveryTag(), false); } }); return container; }
Example #5
Source File: AmqpConfig.java From myth with Apache License 2.0 | 6 votes |
@Bean @ConditionalOnProperty(prefix = "spring.rabbitmq", name = "host") public SimpleMessageListenerContainer messageContainer() { SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory); container.setQueues(queue()); container.setExposeListenerChannel(true); container.setMaxConcurrentConsumers(1); container.setConcurrentConsumers(1); //设置确认模式手工确认 container.setAcknowledgeMode(AcknowledgeMode.MANUAL); container.setMessageListener((ChannelAwareMessageListener) (message, channel) -> { byte[] messageBody = message.getBody(); //确认消息成功消费 final Boolean success = mythMqReceiveService.processMessage(messageBody); if (success) { try { channel.basicAck(message.getMessageProperties().getDeliveryTag(), false); } catch (IOException e) { e.printStackTrace(); } } }); return container; }
Example #6
Source File: AmqpConfig.java From myth with Apache License 2.0 | 6 votes |
/** * Message container simple message listener container. * * @return the simple message listener container */ @Bean @ConditionalOnProperty(prefix = "spring.rabbitmq", name = "host") public SimpleMessageListenerContainer messageContainer() { SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory); container.setQueues(queue()); container.setExposeListenerChannel(true); container.setMaxConcurrentConsumers(2); container.setConcurrentConsumers(1); //设置确认模式手工确认 container.setAcknowledgeMode(AcknowledgeMode.MANUAL); container.setMessageListener((ChannelAwareMessageListener) (message, channel) -> { byte[] messageBody = message.getBody(); LOGGER.debug("motan 框架接收到的消息"); //确认消息成功消费 final Boolean success = mythMqReceiveService.processMessage(messageBody); if (success) { channel.basicAck(message.getMessageProperties().getDeliveryTag(), false); } }); return container; }
Example #7
Source File: RabbitBinderTests.java From spring-cloud-stream-binder-rabbit with Apache License 2.0 | 6 votes |
@Test public void testAnonWithBuiltInExchangeCustomPrefix() throws Exception { RabbitTestBinder binder = getBinder(); ExtendedConsumerProperties<RabbitConsumerProperties> properties = createConsumerProperties(); properties.getExtension().setDeclareExchange(false); properties.getExtension().setQueueNameGroupOnly(true); properties.getExtension().setAnonymousGroupPrefix("customPrefix."); Binding<MessageChannel> consumerBinding = binder.bindConsumer("amq.topic", null, createBindableChannel("input", new BindingProperties()), properties); Lifecycle endpoint = extractEndpoint(consumerBinding); SimpleMessageListenerContainer container = TestUtils.getPropertyValue(endpoint, "messageListenerContainer", SimpleMessageListenerContainer.class); String queueName = container.getQueueNames()[0]; assertThat(queueName).startsWith("customPrefix."); assertThat(container.isRunning()).isTrue(); consumerBinding.unbind(); assertThat(container.isRunning()).isFalse(); }
Example #8
Source File: RabbitBinderTests.java From spring-cloud-stream-binder-rabbit with Apache License 2.0 | 6 votes |
@Test public void testAnonWithBuiltInExchange() throws Exception { RabbitTestBinder binder = getBinder(); ExtendedConsumerProperties<RabbitConsumerProperties> properties = createConsumerProperties(); properties.getExtension().setDeclareExchange(false); properties.getExtension().setQueueNameGroupOnly(true); Binding<MessageChannel> consumerBinding = binder.bindConsumer("amq.topic", null, createBindableChannel("input", new BindingProperties()), properties); Lifecycle endpoint = extractEndpoint(consumerBinding); SimpleMessageListenerContainer container = TestUtils.getPropertyValue(endpoint, "messageListenerContainer", SimpleMessageListenerContainer.class); String queueName = container.getQueueNames()[0]; assertThat(queueName).startsWith("anonymous."); assertThat(container.isRunning()).isTrue(); consumerBinding.unbind(); assertThat(container.isRunning()).isFalse(); }
Example #9
Source File: MessageListenerAccessor.java From spring-cloud-contract with Apache License 2.0 | 6 votes |
private List<SimpleMessageListenerContainer> getListenersByBoundQueues( List<SimpleMessageListenerContainer> listenerContainers, Set<String> queueNames) { List<SimpleMessageListenerContainer> matchingContainers = new ArrayList<>(); for (SimpleMessageListenerContainer listenerContainer : listenerContainers) { if (listenerContainer.getQueueNames() != null) { for (String queueName : listenerContainer.getQueueNames()) { if (queueNames.contains(queueName)) { matchingContainers.add(listenerContainer); break; } } } } return matchingContainers; }
Example #10
Source File: RabbitMessageChannelBinder.java From spring-cloud-stream-binder-rabbit with Apache License 2.0 | 6 votes |
private void setSMLCProperties( ExtendedConsumerProperties<RabbitConsumerProperties> properties, SimpleMessageListenerContainer listenerContainer, int concurrency) { listenerContainer.setConcurrentConsumers(concurrency); int maxConcurrency = properties.getExtension().getMaxConcurrency(); if (maxConcurrency > concurrency) { listenerContainer.setMaxConcurrentConsumers(maxConcurrency); } listenerContainer.setDeBatchingEnabled(!properties.isBatchMode()); listenerContainer.setBatchSize(properties.getExtension().getBatchSize()); if (properties.getExtension().getQueueDeclarationRetries() != null) { listenerContainer.setDeclarationRetries( properties.getExtension().getQueueDeclarationRetries()); } }
Example #11
Source File: MessageListenerAccessor.java From spring-cloud-contract with Apache License 2.0 | 6 votes |
private List<SimpleMessageListenerContainer> collectListenerContainers() { List<SimpleMessageListenerContainer> listenerContainers = new ArrayList<>(); if (this.simpleMessageListenerContainers != null) { listenerContainers.addAll(this.simpleMessageListenerContainers); } if (this.rabbitListenerEndpointRegistry != null) { for (MessageListenerContainer listenerContainer : this.rabbitListenerEndpointRegistry .getListenerContainers()) { if (listenerContainer instanceof SimpleMessageListenerContainer) { listenerContainers .add((SimpleMessageListenerContainer) listenerContainer); } } } return listenerContainers; }
Example #12
Source File: RabbitBinderTests.java From spring-cloud-stream-binder-rabbit with Apache License 2.0 | 5 votes |
@Test public void testConsumerPropertiesWithUserInfrastructureNoBind() throws Exception { RabbitAdmin admin = new RabbitAdmin(this.rabbitAvailableRule.getResource()); Queue queue = new Queue("propsUser1.infra"); admin.declareQueue(queue); DirectExchange exchange = new DirectExchange("propsUser1"); admin.declareExchange(exchange); admin.declareBinding(BindingBuilder.bind(queue).to(exchange).with("foo")); RabbitTestBinder binder = getBinder(); ExtendedConsumerProperties<RabbitConsumerProperties> properties = createConsumerProperties(); properties.getExtension().setDeclareExchange(false); properties.getExtension().setBindQueue(false); Binding<MessageChannel> consumerBinding = binder.bindConsumer("propsUser1", "infra", createBindableChannel("input", new BindingProperties()), properties); Lifecycle endpoint = extractEndpoint(consumerBinding); SimpleMessageListenerContainer container = TestUtils.getPropertyValue(endpoint, "messageListenerContainer", SimpleMessageListenerContainer.class); assertThat(TestUtils.getPropertyValue(container, "missingQueuesFatal", Boolean.class)).isFalse(); assertThat(container.isRunning()).isTrue(); consumerBinding.unbind(); assertThat(container.isRunning()).isFalse(); Client client = new Client("http://guest:guest@localhost:15672/api/"); List<?> bindings = client.getBindingsBySource("/", exchange.getName()); assertThat(bindings.size()).isEqualTo(1); }
Example #13
Source File: Issue178ListenerConfiguration.java From spring-cloud-contract with Apache License 2.0 | 5 votes |
@Bean SimpleMessageListenerContainer messageListenerContainer( ConnectionFactory connectionFactory, RabbitTemplate rabbitTemplate) { SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(); container.setConnectionFactory(connectionFactory); container.addQueueNames("rated-item-service.rated-item-event.exchange"); container.setMessageListener(exampleListener(rabbitTemplate)); return container; }
Example #14
Source File: AmqpMessagingApplication.java From spring-cloud-contract with Apache License 2.0 | 5 votes |
@Bean public SimpleMessageListenerContainer simpleMessageListenerContainer( ConnectionFactory connectionFactory, MessageListenerAdapter listenerAdapter) { SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(); container.setConnectionFactory(connectionFactory); container.setQueueNames("test.queue"); container.setMessageListener(listenerAdapter); return container; }
Example #15
Source File: MessageListenerAccessor.java From spring-cloud-contract with Apache License 2.0 | 5 votes |
MessageListenerAccessor(RabbitListenerEndpointRegistry rabbitListenerEndpointRegistry, List<SimpleMessageListenerContainer> simpleMessageListenerContainers, List<Binding> bindings) { this.rabbitListenerEndpointRegistry = rabbitListenerEndpointRegistry; this.simpleMessageListenerContainers = simpleMessageListenerContainers; this.bindings = bindings; }
Example #16
Source File: MessageListenerAccessor.java From spring-cloud-contract with Apache License 2.0 | 5 votes |
List<SimpleMessageListenerContainer> getListenerContainersForDestination( String destination, String routingKey) { List<SimpleMessageListenerContainer> listenerContainers = collectListenerContainers(); // we interpret the destination as exchange name and collect all the queues bound // to this exchange Set<String> queueNames = collectQueuesBoundToDestination(destination, routingKey); return getListenersByBoundQueues(listenerContainers, queueNames); }
Example #17
Source File: SzzRestApplication.java From OpenSZZ-Cloud-Native with GNU General Public License v3.0 | 5 votes |
@Bean SimpleMessageListenerContainer containerAnaylsis(ConnectionFactory connectionFactory) { SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(); container.setConnectionFactory(connectionFactory()); container.setQueueNames(queueNameSzz); container.setMessageListener(new MessageReceivedComponent(rabbitTemplate(connectionFactory()), dbEntryDao)); return container; }
Example #18
Source File: RabbitBinderTests.java From spring-cloud-stream-binder-rabbit with Apache License 2.0 | 5 votes |
private SimpleMessageListenerContainer verifyContainer(Lifecycle endpoint) { SimpleMessageListenerContainer container; RetryTemplate retry; container = TestUtils.getPropertyValue(endpoint, "messageListenerContainer", SimpleMessageListenerContainer.class); assertThat(container.getAcknowledgeMode()).isEqualTo(AcknowledgeMode.NONE); assertThat(container.getQueueNames()[0]).startsWith("foo.props.0"); assertThat(TestUtils.getPropertyValue(container, "transactional", Boolean.class)) .isFalse(); assertThat(TestUtils.getPropertyValue(container, "concurrentConsumers")) .isEqualTo(2); assertThat(TestUtils.getPropertyValue(container, "maxConcurrentConsumers")) .isEqualTo(3); assertThat(TestUtils.getPropertyValue(container, "defaultRequeueRejected", Boolean.class)).isFalse(); assertThat(TestUtils.getPropertyValue(container, "prefetchCount")).isEqualTo(20); assertThat(TestUtils.getPropertyValue(container, "batchSize")).isEqualTo(10); retry = TestUtils.getPropertyValue(endpoint, "retryTemplate", RetryTemplate.class); assertThat(TestUtils.getPropertyValue(retry, "retryPolicy.maxAttempts")) .isEqualTo(23); assertThat(TestUtils.getPropertyValue(retry, "backOffPolicy.initialInterval")) .isEqualTo(2000L); assertThat(TestUtils.getPropertyValue(retry, "backOffPolicy.maxInterval")) .isEqualTo(20000L); assertThat(TestUtils.getPropertyValue(retry, "backOffPolicy.multiplier")) .isEqualTo(5.0); List<?> requestMatchers = TestUtils.getPropertyValue(endpoint, "headerMapper.requestHeaderMatcher.matchers", List.class); assertThat(requestMatchers).hasSize(1); assertThat(TestUtils.getPropertyValue(requestMatchers.get(0), "pattern")) .isEqualTo("foo"); return container; }
Example #19
Source File: AmqpConfig.java From demo_springboot_rabbitmq with Apache License 2.0 | 5 votes |
@Bean public SimpleMessageListenerContainer messageListenerContainer(MessageListenerAdapter listenerAdapter) { SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(); container.setConnectionFactory(connectionFactory()); container.setQueueNames(AmqpConfig.QUEUE_NAME); container.setExposeListenerChannel(true); container.setMaxConcurrentConsumers(1); container.setConcurrentConsumers(1); container.setAcknowledgeMode(AcknowledgeMode.MANUAL); //设置确认模式手工确认 container.setMessageListener(listenerAdapter); return container; }
Example #20
Source File: EMSConfiguration.java From generic-vnfm with Apache License 2.0 | 5 votes |
@Bean SimpleMessageListenerContainer container_emsRegistrator() { if (listenerAdapter_emsRegistrator != null) { SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(); container.setConnectionFactory(emsConnectionFactory); container.setQueueNames(queueName_emsRegistrator); container.setMessageListener(listenerAdapter_emsRegistrator); return container; } else return null; }
Example #21
Source File: ConfigurableRabbitListenerContainerFactory.java From hawkbit with Eclipse Public License 1.0 | 5 votes |
@Override // Exception squid:UnusedProtectedMethod - called by // AbstractRabbitListenerContainerFactory @SuppressWarnings("squid:UnusedProtectedMethod") protected void initializeContainer(final SimpleMessageListenerContainer instance, final RabbitListenerEndpoint endpoint) { super.initializeContainer(instance, endpoint); instance.setDeclarationRetries(declarationRetries); }
Example #22
Source File: AmqpConfiguration.java From hawkbit with Eclipse Public License 1.0 | 5 votes |
/** * Create RabbitListenerContainerFactory bean if no listenerContainerFactory * bean found * * @return RabbitListenerContainerFactory bean */ @Bean @ConditionalOnMissingBean(name = "listenerContainerFactory") public RabbitListenerContainerFactory<SimpleMessageListenerContainer> listenerContainerFactory( final SimpleRabbitListenerContainerFactoryConfigurer configurer, final ErrorHandler errorHandler) { final ConfigurableRabbitListenerContainerFactory factory = new ConfigurableRabbitListenerContainerFactory( amqpProperties.isMissingQueuesFatal(), amqpProperties.getDeclarationRetries(), errorHandler); configurer.configure(factory, rabbitConnectionFactory); return factory; }
Example #23
Source File: ScrapingResultConsumerConfiguration.java From scraping-microservice-java-python-rabbitmq with Apache License 2.0 | 5 votes |
@Bean public SimpleMessageListenerContainer listenerContainer() { SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(); container.setConnectionFactory(connectionFactory()); container.setQueueNames(this.scrapingResultQueue); container.setMessageListener(messageListenerAdapter()); return container; }
Example #24
Source File: ExceptionQueueContextConfig.java From sinavi-jfw with Apache License 2.0 | 5 votes |
/** * {@link SimpleMessageListenerContainer}のインスタンスを生成します。 * このインスタンスを生成後にRabbitMQへのコネクション{@link #factory()}の設定や * メッセージ受信側での例外ハンドラ{@link #errorHandler()}の設定、 * リトライ処理{@link #advice()}の設定を行います。 * @return {@link SimpleMessageListenerContainer}のインスタンス */ protected SimpleMessageListenerContainer messageListenerContainer() { SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(); container.setConnectionFactory(factory()); container.setErrorHandler(errorHandler()); container.setAdviceChain(advice()); return container; }
Example #25
Source File: MessageListenerRetryTest.java From sinavi-jfw with Apache License 2.0 | 5 votes |
private SimpleMessageListenerContainer container() { SimpleMessageListenerContainer c = new SimpleMessageListenerContainer(ctx.getBean(ConnectionFactory.class)); c.setConnectionFactory(ctx.getBean(ConnectionFactory.class)); c.setErrorHandler(ctx.getBean(ErrorHandler.class)); c.setAdviceChain(ctx.getBean(Advice[].class)); return c; }
Example #26
Source File: MessageListenerConfig_Pre_1_4_0.java From pinpoint with Apache License 2.0 | 5 votes |
@Bean public SimpleMessageListenerContainer listenerContainer(ConnectionFactory connectionFactory, TestMessageHandler testMessageHandler) { SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(); container.setConnectionFactory(connectionFactory); container.setQueueNames(RabbitMQTestConstants.QUEUE_PUSH); container.setMessageListener(new MessageListenerAdapter(testMessageHandler)); return container; }
Example #27
Source File: SpringIntegrationConfig.java From bearchoke with Apache License 2.0 | 5 votes |
@Bean(destroyMethod = "destroy") public SimpleMessageListenerContainer simpleMessageListenerContainer() { SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory); container.setQueues(queue()); container.setMessageListener(loggingMessageListenerAdapter()); container.setAcknowledgeMode(AcknowledgeMode.AUTO); return container; }
Example #28
Source File: AmqpConfiguration.java From SkyEye with GNU General Public License v3.0 | 5 votes |
@Bean public SimpleMessageListenerContainer messageContainer(ConnectionFactory connectionFactory, MessageListenerAdapter messageListenerAdapter) { SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory); container.setQueues(this.queue); container.setExposeListenerChannel(true); container.setMaxConcurrentConsumers(this.maxConcurrentConsumers); container.setConcurrentConsumers(this.concurrentConsumers); container.setAcknowledgeMode(AcknowledgeMode.AUTO); container.setMessageListener(messageListenerAdapter); return container; }
Example #29
Source File: RabbitAutoConfiguration.java From easy-rabbitmq with Apache License 2.0 | 5 votes |
@Bean public SimpleMessageListenerContainer simpleMessageListenerContainer(MessageConverter converter, ConnectionFactory connectionFactory, @Autowired(required = false) List<Consumer<?>> list) { SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory); init(list); Receiver receiver = new Receiver(converter); container.setMessageListener(new MessageListenerAdapter(receiver, converter)); container.setQueueNames(queueNames.toArray(new String[0])); container.setAcknowledgeMode(AcknowledgeMode.MANUAL); container.setPrefetchCount(prefetchCount); container.setTxSize(txSize); return container; }
Example #30
Source File: SzzRestApplication.java From OpenSZZ-Cloud-Native with GNU General Public License v3.0 | 5 votes |
@Bean SimpleMessageListenerContainer containerAnaylsis(ConnectionFactory connectionFactory) { SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(); container.setConnectionFactory(connectionFactory()); container.setQueueNames(queueNameSzz); container.setMessageListener(new MessageReceivedComponent(rabbitTemplate(connectionFactory()))); return container; }